client-group-chat-room.cpp 16.7 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 66
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, q);
	const Address &myAddress = q->getMe()->getAddress();
67 68 69
	Address myCleanedAddress(myAddress);
	myCleanedAddress.setUriParam("gr"); // Remove gr parameter for INVITE
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myCleanedAddress, focus->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
void ClientGroupChatRoomPrivate::onChatMessageReceived (const shared_ptr<ChatMessage> &) {}

86 87
// =============================================================================

88
ClientGroupChatRoom::ClientGroupChatRoom (
89 90
	const shared_ptr<Core> &core,
	const string &factoryUri,
91
	const IdentityAddress &me,
92
	const string &subject
93
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(IdentityAddress(), me)),
94
RemoteConference(core, me, nullptr) {
95
	L_D_T(RemoteConference, dConference);
Ghislain MARY's avatar
Ghislain MARY committed
96
	dConference->focus = make_shared<Participant>(Address(factoryUri));
97
	RemoteConference::setSubject(subject);
98 99
}

100 101
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
Ronan's avatar
Ronan committed
102 103
	const IdentityAddress &peerAddress,
	shared_ptr<Participant> &me,
104
	const string &subject,
105 106
	list<shared_ptr<Participant>> &&participants,
	unsigned int lastNotifyId
Ronan's avatar
Ronan committed
107 108
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(peerAddress, me->getAddress())),
RemoteConference(core, me->getAddress(), nullptr) {
109 110
	L_D();
	L_D_T(RemoteConference, dConference);
111

112 113 114
	dConference->focus = make_shared<Participant>(peerAddress);
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
115
	dConference->participants = move(participants);
116

117
	d->state = ChatRoom::State::Created;
118

Ronan's avatar
Ronan committed
119
	getMe()->getPrivate()->setAdmin(me->isAdmin());
120

121
	dConference->eventHandler->setLastNotify(lastNotifyId);
122
	dConference->eventHandler->subscribe(getChatRoomId());
123 124
}

125 126 127 128
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

129 130 131 132 133 134 135 136
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

137
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
138
	return RemoteConference::getConferenceAddress();
139
}
140

141 142
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
143 144
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
145 146
}

147
void ClientGroupChatRoom::addParticipants (
148
	const list<IdentityAddress> &addresses,
149 150 151
	const CallSessionParams *params,
	bool hasMedia
) {
152
	L_D();
153
	L_D_T(RemoteConference, dConference);
154

155
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
156
	if (addressesList.empty())
157
		return;
158

159 160 161 162
	if ((d->state != ChatRoom::State::Instantiated) && (d->state != ChatRoom::State::Created)) {
		lError() << "Cannot add participants to the ClientGroupChatRoom in a state other than Instantiated or Created";
		return;
	}
163

164
	Content content;
165
	content.setBody(getResourceLists(addressesList));
166 167
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
168

169
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
170
	if (session)
171
		session->update(nullptr, getSubject(), &content);
172 173
	else {
		session = d->createSession();
174 175
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
176
			d->setState(ChatRoom::State::CreationPending);
177
	}
178 179
}

180
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
181
	LinphoneCore *cCore = getCore()->getCCore();
182 183

	SalReferOp *referOp = new SalReferOp(cCore->sal);
184
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
185
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
186 187 188 189 190 191
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
192 193
}

194
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
195
	RemoteConference::removeParticipants(participants);
196
}
197

198
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
199 200 201 202 203 204 205 206 207 208 209
	return RemoteConference::findParticipant(addr);
}

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

int ClientGroupChatRoom::getNbParticipants () const {
	return RemoteConference::getNbParticipants();
}

Ghislain MARY's avatar
Ghislain MARY committed
210
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
211 212 213
	return RemoteConference::getParticipants();
}

214 215 216
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
217 218

	if (!getMe()->isAdmin()) {
219 220 221
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
222

223
	LinphoneCore *cCore = getCore()->getCCore();
224 225

	SalReferOp *referOp = new SalReferOp(cCore->sal);
226
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
227
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
228 229 230 231 232 233 234 235
	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();
}

236 237 238 239
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

240 241
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
242
	L_D_T(RemoteConference, dConference);
243

244 245 246 247
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
248 249

	if (!getMe()->isAdmin()) {
250 251 252
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
253

254
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
255 256 257 258 259 260
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
261 262
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
void ClientGroupChatRoom::join () {
	L_D();
	L_D_T(RemoteConference, dConference);

	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (!session && ((d->state == ChatRoom::State::Instantiated) || (d->state == ChatRoom::State::Terminated))) {
		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();
280

281 282 283 284 285 286 287
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
288

289
	d->setState(ChatRoom::State::TerminationPending);
290 291
}

292 293
// -----------------------------------------------------------------------------

294
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
295
	L_D();
296 297
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
298
	dConference->focus->getPrivate()->setAddress(addr);
299
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
300
	getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
301 302
}

303
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
304
	L_D();
305 306
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
307
	d->setState(ChatRoom::State::Terminated);
308 309
}

310
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
311 312
	L_D();
	d->setState(ChatRoom::State::Created);
313
	getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
314 315
}

316
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
317 318
	L_D_T(RemoteConference, dConference);

319
	const IdentityAddress &addr = event->getParticipantAddress();
320 321
	if (isMe(addr))
		return;
322

323 324 325 326 327
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
328

Ronan's avatar
Ronan committed
329
	participant = make_shared<Participant>(addr);
330
	dConference->participants.push_back(participant);
331 332 333 334

	if (isFullState)
		return;

335
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
336 337
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
338
	getCore()->getPrivate()->mainDb->addEvent(event);
339

340
	if (cb)
Ronan's avatar
Ronan committed
341
		cb(cr, L_GET_C_BACK_PTR(event));
342 343
}

344
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
345
	(void)isFullState;
346

347 348
	L_D_T(RemoteConference, dConference);

349
	const IdentityAddress &addr = event->getParticipantAddress();
350 351
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
352
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
353 354
		return;
	}
355

356 357
	dConference->participants.remove(participant);

358
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
359 360
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
361
	getCore()->getPrivate()->mainDb->addEvent(event);
362

363
	if (cb)
Ronan's avatar
Ronan committed
364
		cb(cr, L_GET_C_BACK_PTR(event));
365 366
}

367
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
368
	const IdentityAddress &addr = event->getParticipantAddress();
369
	shared_ptr<Participant> participant;
370
	if (isMe(addr))
371
		participant = getMe();
372 373
	else
		participant = findParticipant(addr);
374 375 376 377
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
378

379 380 381 382 383
	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);

384 385 386
	if (isFullState)
		return;

387
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
388 389
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
390
	getCore()->getPrivate()->mainDb->addEvent(event);
391

392
	if (cb)
Ronan's avatar
Ronan committed
393
		cb(cr, L_GET_C_BACK_PTR(event));
394 395
}

396
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
397 398
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
399
	RemoteConference::setSubject(event->getSubject());
400

401 402 403
	if (isFullState)
		return;

404 405 406
	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);
407
	getCore()->getPrivate()->mainDb->addEvent(event);
408

409
	if (cb)
Ronan's avatar
Ronan committed
410
		cb(cr, L_GET_C_BACK_PTR(event));
411 412
}

413
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
414
	const IdentityAddress &addr = event->getParticipantAddress();
415
	shared_ptr<Participant> participant;
416
	if (isMe(addr))
417
		participant = getMe();
418 419 420 421 422 423
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
424
	participant->getPrivate()->addDevice(event->getDeviceAddress());
425 426 427 428

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
429 430 431
	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);
432
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
433 434

	if (cb)
Ronan's avatar
Ronan committed
435
		cb(cr, L_GET_C_BACK_PTR(event));
436 437
}

438
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
439
	(void)isFullState;
440

441
	const IdentityAddress &addr = event->getParticipantAddress();
442
	shared_ptr<Participant> participant;
443
	if (isMe(addr))
444
		participant = getMe();
445 446 447 448 449 450
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
451
	participant->getPrivate()->removeDevice(event->getDeviceAddress());
Benjamin REIS's avatar
Benjamin REIS committed
452 453 454
	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);
455
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
456 457

	if (cb)
Ronan's avatar
Ronan committed
458
		cb(cr, L_GET_C_BACK_PTR(event));
459 460
}

461 462
// -----------------------------------------------------------------------------

463
void ClientGroupChatRoom::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
464
	L_D_T(RemoteConference, dConference);
465

466
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
467 468
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
469 470
}

471
void ClientGroupChatRoom::onCallSessionStateChanged (
472
	const shared_ptr<const CallSession> &session,
473 474 475
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
476
	L_D();
477 478
	L_D_T(RemoteConference, dConference);

479
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
480
		if (d->state == ChatRoom::State::CreationPending) {
481
			IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly());
Ghislain MARY's avatar
Ghislain MARY committed
482 483
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
484
				dConference->eventHandler->subscribe(getChatRoomId());
485
		} else if (d->state == ChatRoom::State::TerminationPending)
486
			dConference->focus->getPrivate()->getSession()->terminate();
487
	} else if ((state == LinphoneCallReleased) && (d->state == ChatRoom::State::TerminationPending)) {
488
		onConferenceTerminated(getConferenceAddress());
489
	} else if ((state == LinphoneCallError) && (d->state == ChatRoom::State::CreationPending)) {
490 491
		d->setState(ChatRoom::State::CreationFailed);
	}
492 493
}

494
LINPHONE_END_NAMESPACE