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 66
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, q);
	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
void ClientGroupChatRoomPrivate::onChatMessageReceived (const shared_ptr<ChatMessage> &) {}

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

88
ClientGroupChatRoom::ClientGroupChatRoom (
89
	const shared_ptr<Core> &core,
90
	const string &uri,
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);
96 97 98
	IdentityAddress focusAddr(uri);
	dConference->focus = make_shared<Participant>(focusAddr);
	dConference->focus->getPrivate()->addDevice(focusAddr);
99
	RemoteConference::setSubject(subject);
100 101
}

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

Benjamin REIS's avatar
Benjamin REIS committed
113
	const IdentityAddress &peerAddress = chatRoomId.getPeerAddress();
114 115 116
	dConference->focus = make_shared<Participant>(peerAddress);
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
117
	dConference->participants = move(participants);
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
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

133
bool ClientGroupChatRoom::hasBeenLeft () const {
134
	return getState() != State::Created;
135 136
}

137 138 139 140
bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

141
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
142
	return RemoteConference::getConferenceAddress();
143
}
144

145 146
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
147 148
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
149 150
}

151
void ClientGroupChatRoom::addParticipants (
152
	const list<IdentityAddress> &addresses,
153 154 155
	const CallSessionParams *params,
	bool hasMedia
) {
156
	L_D();
157
	L_D_T(RemoteConference, dConference);
158

159
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
160
	if (addressesList.empty())
161
		return;
162

163 164 165 166
	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;
	}
167

168
	Content content;
169
	content.setBody(getResourceLists(addressesList));
170 171
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
172

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

184
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
185
	LinphoneCore *cCore = getCore()->getCCore();
186 187

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

198
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
199
	RemoteConference::removeParticipants(participants);
200
}
201

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

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

210 211
int ClientGroupChatRoom::getParticipantCount () const {
	return RemoteConference::getParticipantCount();
212 213
}

Ghislain MARY's avatar
Ghislain MARY committed
214
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
215 216 217
	return RemoteConference::getParticipants();
}

218 219 220
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
221 222

	if (!getMe()->isAdmin()) {
223 224 225
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
226

227
	LinphoneCore *cCore = getCore()->getCCore();
228 229

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

240 241 242 243
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

244 245
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
246
	L_D_T(RemoteConference, dConference);
247

248 249 250 251
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
252 253

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

258
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
259 260 261 262 263 264
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
265 266
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
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();
284

285 286 287 288 289 290 291
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
292

293
	d->setState(ChatRoom::State::TerminationPending);
294 295
}

296 297
// -----------------------------------------------------------------------------

298
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
299
	L_D();
300 301
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
302
	dConference->focus->getPrivate()->setAddress(addr);
303 304
	dConference->focus->getPrivate()->clearDevices();
	dConference->focus->getPrivate()->addDevice(addr);
305
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
306
	getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
307 308
}

309
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
310
	L_D();
311 312
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
313
	d->setState(ChatRoom::State::Terminated);
314 315 316 317 318
	getCore()->getPrivate()->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceTerminated,
		time(nullptr),
		d->chatRoomId
	));
319 320
}

321
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
322 323
	L_D();
	d->setState(ChatRoom::State::Created);
324 325 326 327 328 329 330 331

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

334
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
335 336
	L_D_T(RemoteConference, dConference);

337
	const IdentityAddress &addr = event->getParticipantAddress();
338 339
	if (isMe(addr))
		return;
340

341 342 343 344 345
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
346

Ronan's avatar
Ronan committed
347
	participant = make_shared<Participant>(addr);
348
	dConference->participants.push_back(participant);
349 350 351 352

	if (isFullState)
		return;

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

355
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
356 357 358
	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
359
		cb(cr, L_GET_C_BACK_PTR(event));
360 361
}

362
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
363
	(void)isFullState;
364

365 366
	L_D_T(RemoteConference, dConference);

367
	const IdentityAddress &addr = event->getParticipantAddress();
368 369
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
370
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
371 372
		return;
	}
373

374
	dConference->participants.remove(participant);
Benjamin REIS's avatar
Benjamin REIS committed
375
	getCore()->getPrivate()->mainDb->addEvent(event);
376

377
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
378 379 380
	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
381
		cb(cr, L_GET_C_BACK_PTR(event));
382 383
}

384
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
385
	const IdentityAddress &addr = event->getParticipantAddress();
386
	shared_ptr<Participant> participant;
387
	if (isMe(addr))
388
		participant = getMe();
389 390
	else
		participant = findParticipant(addr);
391 392 393 394
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
395

396 397 398 399 400
	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);

401 402 403
	if (isFullState)
		return;

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

406
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
407 408 409
	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
410
		cb(cr, L_GET_C_BACK_PTR(event));
411 412
}

413
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
414 415
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
416
	RemoteConference::setSubject(event->getSubject());
417

418 419 420
	if (isFullState)
		return;

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

423 424 425 426
	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
427
		cb(cr, L_GET_C_BACK_PTR(event));
428 429
}

430
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
431
	const IdentityAddress &addr = event->getParticipantAddress();
432
	shared_ptr<Participant> participant;
433
	if (isMe(addr))
434
		participant = getMe();
435 436 437 438 439 440
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
441
	participant->getPrivate()->addDevice(event->getDeviceAddress());
442 443 444 445

	if (isFullState)
		return;

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

Benjamin REIS's avatar
Benjamin REIS committed
448 449 450 451
	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
452
		cb(cr, L_GET_C_BACK_PTR(event));
453 454
}

455
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
456
	(void)isFullState;
457

458
	const IdentityAddress &addr = event->getParticipantAddress();
459
	shared_ptr<Participant> participant;
460
	if (isMe(addr))
461
		participant = getMe();
462 463 464 465 466 467
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
468
	participant->getPrivate()->removeDevice(event->getDeviceAddress());
Benjamin REIS's avatar
Benjamin REIS committed
469 470
	getCore()->getPrivate()->mainDb->addEvent(event);

Benjamin REIS's avatar
Benjamin REIS committed
471 472 473 474
	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
475
		cb(cr, L_GET_C_BACK_PTR(event));
476 477
}

478 479
// -----------------------------------------------------------------------------

480
void ClientGroupChatRoom::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
481
	L_D_T(RemoteConference, dConference);
482

483
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
484 485
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
486 487
}

488
void ClientGroupChatRoom::onCallSessionStateChanged (
489
	const shared_ptr<const CallSession> &session,
490 491 492
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
493
	L_D();
494 495
	L_D_T(RemoteConference, dConference);

496
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
497
		if (d->state == ChatRoom::State::CreationPending) {
498
			IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly());
Ghislain MARY's avatar
Ghislain MARY committed
499 500
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
501
				dConference->eventHandler->subscribe(getChatRoomId());
502
		} else if (d->state == ChatRoom::State::TerminationPending)
503
			dConference->focus->getPrivate()->getSession()->terminate();
504
	} else if ((state == LinphoneCallReleased) && (d->state == ChatRoom::State::TerminationPending)) {
505
		onConferenceTerminated(getConferenceAddress());
506
	} else if ((state == LinphoneCallError) && (d->state == ChatRoom::State::CreationPending)) {
507 508
		d->setState(ChatRoom::State::CreationFailed);
	}
509 510
}

511
LINPHONE_END_NAMESPACE