client-group-chat-room.cpp 17.3 KB
Newer Older
1 2
/*
 * client-group-chat-room.cpp
Ghislain MARY's avatar
Ghislain MARY committed
3
 * Copyright (C) 2010-2017 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 */

20 21
#include "linphone/utils/utils.h"

22
#include "address/address-p.h"
23
#include "c-wrapper/c-wrapper.h"
24
#include "client-group-chat-room-p.h"
25
#include "conference/handlers/remote-conference-event-handler.h"
26
#include "conference/participant-p.h"
27 28
#include "conference/remote-conference-p.h"
#include "conference/session/call-session-p.h"
29
#include "core/core-p.h"
30
#include "event-log/events.h"
31
#include "logger/logger.h"
32
#include "sal/refer-op.h"
33

Ronan's avatar
Ronan committed
34
// =============================================================================
35

36 37 38 39
using namespace std;

LINPHONE_BEGIN_NAMESPACE

40 41
// -----------------------------------------------------------------------------

42
list<IdentityAddress> ClientGroupChatRoomPrivate::cleanAddressesList (const list<IdentityAddress> &addresses) const {
43
	L_Q();
44
	list<IdentityAddress> cleanedList(addresses);
45 46 47
	cleanedList.sort();
	cleanedList.unique();
	for (auto it = cleanedList.begin(); it != cleanedList.end();) {
48
		if (q->findParticipant(*it) || (q->getMe()->getAddress() == *it))
49 50 51 52 53 54 55
			it = cleanedList.erase(it);
		else
			it++;
	}
	return cleanedList;
}

56 57
shared_ptr<CallSession> ClientGroupChatRoomPrivate::createSession () {
	L_Q();
58
	L_Q_T(RemoteConference, qConference);
59

60 61
	CallSessionParams csp;
	csp.addCustomHeader("Require", "recipient-list-invite");
62
	csp.addCustomContactParameter("text");
63

64
	shared_ptr<Participant> focus = qConference->getPrivate()->focus;
65
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, this);
66
	const Address &myAddress = q->getMe()->getAddress();
67 68
	Address myCleanedAddress(myAddress);
	myCleanedAddress.setUriParam("gr"); // Remove gr parameter for INVITE
69
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myCleanedAddress, focus->getPrivate()->getDevices().front()->getAddress());
70 71 72 73
	session->initiateOutgoing();
	return session;
}

Ghislain MARY's avatar
Ghislain MARY committed
74
void ClientGroupChatRoomPrivate::notifyReceived (const string &body) {
75 76
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->notifyReceived(body);
77 78
}

79 80 81 82 83
void ClientGroupChatRoomPrivate::multipartNotifyReceived (const string &body) {
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->multipartNotifyReceived(body);
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
// -----------------------------------------------------------------------------

void ClientGroupChatRoomPrivate::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
	L_Q_T(RemoteConference, qConference);

	ParticipantPrivate *participantPrivate = qConference->getPrivate()->focus->getPrivate();
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
}

void ClientGroupChatRoomPrivate::onCallSessionStateChanged (
	const shared_ptr<const CallSession> &session,
	LinphoneCallState newState,
	const string &message
) {
	L_Q();
	L_Q_T(RemoteConference, qConference);

	if (newState == LinphoneCallConnected) {
103
		if (q->getState() == ChatRoom::State::CreationPending) {
104 105 106 107
			IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly());
			q->onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
				qConference->getPrivate()->eventHandler->subscribe(q->getChatRoomId());
108
		} else if (q->getState() == ChatRoom::State::TerminationPending)
109
			qConference->getPrivate()->focus->getPrivate()->getSession()->terminate();
110
	} else if ((newState == LinphoneCallReleased) && (q->getState() == ChatRoom::State::TerminationPending)) {
111
		q->onConferenceTerminated(q->getConferenceAddress());
112
	} else if ((newState == LinphoneCallError) && (q->getState() == ChatRoom::State::CreationPending)) {
113 114 115 116
		setState(ChatRoom::State::CreationFailed);
	}
}

117 118
// =============================================================================

119
ClientGroupChatRoom::ClientGroupChatRoom (
120
	const shared_ptr<Core> &core,
121
	const string &uri,
122
	const IdentityAddress &me,
123
	const string &subject
124
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(IdentityAddress(), me)),
125
RemoteConference(core, me, nullptr) {
126
	L_D_T(RemoteConference, dConference);
127 128 129
	IdentityAddress focusAddr(uri);
	dConference->focus = make_shared<Participant>(focusAddr);
	dConference->focus->getPrivate()->addDevice(focusAddr);
130
	RemoteConference::setSubject(subject);
131 132
}

133 134
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
Benjamin REIS's avatar
Benjamin REIS committed
135
	const ChatRoomId &chatRoomId,
Ronan's avatar
Ronan committed
136
	shared_ptr<Participant> &me,
137
	const string &subject,
138
	list<shared_ptr<Participant>> &&participants,
139
	unsigned int lastNotifyId
Benjamin REIS's avatar
Benjamin REIS committed
140
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, chatRoomId),
Ronan's avatar
Ronan committed
141
RemoteConference(core, me->getAddress(), nullptr) {
142
	L_D_T(RemoteConference, dConference);
143

Benjamin REIS's avatar
Benjamin REIS committed
144
	const IdentityAddress &peerAddress = chatRoomId.getPeerAddress();
145
	dConference->focus = make_shared<Participant>(peerAddress);
146
	dConference->focus->getPrivate()->addDevice(peerAddress);
147 148
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
149
	dConference->participants = move(participants);
150

Ronan's avatar
Ronan committed
151
	getMe()->getPrivate()->setAdmin(me->isAdmin());
152

153
	dConference->eventHandler->setLastNotify(lastNotifyId);
154
	dConference->eventHandler->subscribe(getChatRoomId());
155 156
}

157 158 159 160
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

161
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
162
	return Capabilities::Conference;
163 164
}

165
bool ClientGroupChatRoom::hasBeenLeft () const {
166
	return getState() != State::Created;
167 168
}

169 170 171 172
bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

173
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
174
	return RemoteConference::getConferenceAddress();
175
}
176

177 178
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
179 180
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
181 182
}

183
void ClientGroupChatRoom::addParticipants (
184
	const list<IdentityAddress> &addresses,
185 186 187
	const CallSessionParams *params,
	bool hasMedia
) {
188
	L_D();
189
	L_D_T(RemoteConference, dConference);
190

191
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
192
	if (addressesList.empty())
193
		return;
194

195
	if ((getState() != ChatRoom::State::Instantiated) && (getState() != ChatRoom::State::Created)) {
196 197 198
		lError() << "Cannot add participants to the ClientGroupChatRoom in a state other than Instantiated or Created";
		return;
	}
199

200
	Content content;
201
	content.setBody(getResourceLists(addressesList));
202 203
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
204

205
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
206
	if (session)
207
		session->update(nullptr, getSubject(), &content);
208 209
	else {
		session = d->createSession();
210
		session->startInvite(nullptr, getSubject(), &content);
211
		if (getState() == ChatRoom::State::Instantiated)
212
			d->setState(ChatRoom::State::CreationPending);
213
	}
214 215
}

216
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
217
	LinphoneCore *cCore = getCore()->getCCore();
218 219

	SalReferOp *referOp = new SalReferOp(cCore->sal);
220
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
221
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
222 223 224 225 226 227
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
228 229
}

230
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
231
	RemoteConference::removeParticipants(participants);
232
}
233

234
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
235 236 237 238 239 240 241
	return RemoteConference::findParticipant(addr);
}

shared_ptr<Participant> ClientGroupChatRoom::getMe () const {
	return RemoteConference::getMe();
}

242 243
int ClientGroupChatRoom::getParticipantCount () const {
	return RemoteConference::getParticipantCount();
244 245
}

Ghislain MARY's avatar
Ghislain MARY committed
246
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
247 248 249
	return RemoteConference::getParticipants();
}

250
void ClientGroupChatRoom::setParticipantAdminStatus (const shared_ptr<Participant> &participant, bool isAdmin) {
251 252
	if (isAdmin == participant->isAdmin())
		return;
253 254

	if (!getMe()->isAdmin()) {
255 256 257
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
258

259
	LinphoneCore *cCore = getCore()->getCCore();
260 261

	SalReferOp *referOp = new SalReferOp(cCore->sal);
262
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
263
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
264 265 266 267 268 269 270 271
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setParam("admin", Utils::toString(isAdmin));
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
}

272 273 274 275
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

276 277
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
278
	L_D_T(RemoteConference, dConference);
279

280
	if (getState() != ChatRoom::State::Created) {
281 282 283
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
284 285

	if (!getMe()->isAdmin()) {
286 287 288
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
289

290
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
291 292 293 294 295 296
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
297 298
}

299 300 301 302 303
void ClientGroupChatRoom::join () {
	L_D();
	L_D_T(RemoteConference, dConference);

	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
304
	if (!session && ((getState() == ChatRoom::State::Instantiated) || (getState() == ChatRoom::State::Terminated))) {
305 306 307 308 309 310 311 312 313 314 315
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
		d->setState(ChatRoom::State::CreationPending);
	}
}

void ClientGroupChatRoom::leave () {
	L_D();
	L_D_T(RemoteConference, dConference);

	dConference->eventHandler->unsubscribe();
316

317 318 319 320 321 322 323
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
324

325
	d->setState(ChatRoom::State::TerminationPending);
326 327
}

328 329
// -----------------------------------------------------------------------------

330
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
331
	L_D();
332 333
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
334
	dConference->focus->getPrivate()->setAddress(addr);
335 336
	dConference->focus->getPrivate()->clearDevices();
	dConference->focus->getPrivate()->addDevice(addr);
337
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
338
	getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
339 340
}

341
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
342
	L_D();
343 344
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
345
	d->setState(ChatRoom::State::Terminated);
346 347 348 349 350
	getCore()->getPrivate()->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceTerminated,
		time(nullptr),
		d->chatRoomId
	));
351 352
}

353
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
354 355
	L_D();
	d->setState(ChatRoom::State::Created);
356 357 358 359 360 361 362 363

	CorePrivate *dCore = getCore()->getPrivate();
	dCore->insertChatRoomWithDb(getSharedFromThis());
	dCore->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceCreated,
		time(nullptr),
		d->chatRoomId
	));
364 365
}

366
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
367 368
	L_D_T(RemoteConference, dConference);

369
	const IdentityAddress &addr = event->getParticipantAddress();
370 371
	if (isMe(addr))
		return;
372

373 374 375 376 377
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
378

Ronan's avatar
Ronan committed
379
	participant = make_shared<Participant>(addr);
380
	dConference->participants.push_back(participant);
381 382 383 384

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
385 386
	getCore()->getPrivate()->mainDb->addEvent(event);

387
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
388 389 390
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
	if (cb)
Ronan's avatar
Ronan committed
391
		cb(cr, L_GET_C_BACK_PTR(event));
392 393
}

394
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
395
	(void)isFullState;
396

397 398
	L_D_T(RemoteConference, dConference);

399
	const IdentityAddress &addr = event->getParticipantAddress();
400 401
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
402
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
403 404
		return;
	}
405

406
	dConference->participants.remove(participant);
Benjamin REIS's avatar
Benjamin REIS committed
407
	getCore()->getPrivate()->mainDb->addEvent(event);
408

409
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
410 411 412
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
	if (cb)
Ronan's avatar
Ronan committed
413
		cb(cr, L_GET_C_BACK_PTR(event));
414 415
}

416
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
417
	const IdentityAddress &addr = event->getParticipantAddress();
418
	shared_ptr<Participant> participant;
419
	if (isMe(addr))
420
		participant = getMe();
421 422
	else
		participant = findParticipant(addr);
423 424 425 426
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
427

428 429 430 431 432
	bool isAdmin = event->getType() == EventLog::Type::ConferenceParticipantSetAdmin;
	if (participant->isAdmin() == isAdmin)
		return; // No change in the local admin status, do not notify
	participant->getPrivate()->setAdmin(isAdmin);

433 434 435
	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
436 437
	getCore()->getPrivate()->mainDb->addEvent(event);

438
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
439 440 441
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
	if (cb)
Ronan's avatar
Ronan committed
442
		cb(cr, L_GET_C_BACK_PTR(event));
443 444
}

445
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
446 447
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
448
	RemoteConference::setSubject(event->getSubject());
449

450 451 452
	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
453 454
	getCore()->getPrivate()->mainDb->addEvent(event);

455 456 457 458
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsSubjectChangedCb cb = linphone_chat_room_cbs_get_subject_changed(cbs);
	if (cb)
Ronan's avatar
Ronan committed
459
		cb(cr, L_GET_C_BACK_PTR(event));
460 461
}

462
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
463
	const IdentityAddress &addr = event->getParticipantAddress();
464
	shared_ptr<Participant> participant;
465
	if (isMe(addr))
466
		participant = getMe();
467 468 469 470 471 472
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
473
	participant->getPrivate()->addDevice(event->getDeviceAddress());
474 475 476 477

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
478 479
	getCore()->getPrivate()->mainDb->addEvent(event);

Benjamin REIS's avatar
Benjamin REIS committed
480 481 482 483
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantDeviceAddedCb cb = linphone_chat_room_cbs_get_participant_device_added(cbs);
	if (cb)
Ronan's avatar
Ronan committed
484
		cb(cr, L_GET_C_BACK_PTR(event));
485 486
}

487
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
488
	(void)isFullState;
489

490
	const IdentityAddress &addr = event->getParticipantAddress();
491
	shared_ptr<Participant> participant;
492
	if (isMe(addr))
493
		participant = getMe();
494 495 496 497 498 499
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
500
	participant->getPrivate()->removeDevice(event->getDeviceAddress());
Benjamin REIS's avatar
Benjamin REIS committed
501 502
	getCore()->getPrivate()->mainDb->addEvent(event);

Benjamin REIS's avatar
Benjamin REIS committed
503 504 505 506
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantDeviceRemovedCb cb = linphone_chat_room_cbs_get_participant_device_removed(cbs);
	if (cb)
Ronan's avatar
Ronan committed
507
		cb(cr, L_GET_C_BACK_PTR(event));
508 509
}

510
LINPHONE_END_NAMESPACE