client-group-chat-room.cpp 18.4 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, callSessionListener);
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
void ClientGroupChatRoomPrivate::setCallSessionListener (CallSessionListener *listener) {
	L_Q();
	L_Q_T(RemoteConference, qConference);
	callSessionListener = listener;
	shared_ptr<CallSession> session = qConference->getPrivate()->focus->getPrivate()->getSession();
	if (session)
		session->getPrivate()->setCallSessionListener(listener);
	for (const auto &participant : q->getParticipants()) {
		session = participant->getPrivate()->getSession();
		if (session)
			session->getPrivate()->setCallSessionListener(listener);
	}
}

// -----------------------------------------------------------------------------

102 103 104 105 106 107 108 109 110 111 112 113
void ClientGroupChatRoomPrivate::onChatRoomInsertRequested (const shared_ptr<AbstractChatRoom> &chatRoom) {
	L_Q();
	q->getCore()->getPrivate()->insertChatRoom(chatRoom);
}

void ClientGroupChatRoomPrivate::onChatRoomInsertInDatabaseRequested (const shared_ptr<AbstractChatRoom> &chatRoom) {
	L_Q();
	q->getCore()->getPrivate()->insertChatRoomWithDb(chatRoom);
}

// -----------------------------------------------------------------------------

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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) {
131
		if (q->getState() == ChatRoom::State::CreationPending) {
132 133 134 135
			IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly());
			q->onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
				qConference->getPrivate()->eventHandler->subscribe(q->getChatRoomId());
136
		} else if (q->getState() == ChatRoom::State::TerminationPending)
137
			qConference->getPrivate()->focus->getPrivate()->getSession()->terminate();
138
	} else if ((newState == LinphoneCallReleased) && (q->getState() == ChatRoom::State::TerminationPending)) {
139
		q->onConferenceTerminated(q->getConferenceAddress());
140
	} else if ((newState == LinphoneCallError) && (q->getState() == ChatRoom::State::CreationPending)) {
141 142 143 144
		setState(ChatRoom::State::CreationFailed);
	}
}

145 146
// =============================================================================

147
ClientGroupChatRoom::ClientGroupChatRoom (
148
	const shared_ptr<Core> &core,
149
	const string &uri,
150
	const IdentityAddress &me,
151
	const string &subject
152
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(IdentityAddress(), me)),
153
RemoteConference(core, me, nullptr) {
154
	L_D_T(RemoteConference, dConference);
155 156 157
	IdentityAddress focusAddr(uri);
	dConference->focus = make_shared<Participant>(focusAddr);
	dConference->focus->getPrivate()->addDevice(focusAddr);
158
	RemoteConference::setSubject(subject);
159 160
}

161 162
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
Benjamin REIS's avatar
Benjamin REIS committed
163
	const ChatRoomId &chatRoomId,
Ronan's avatar
Ronan committed
164
	shared_ptr<Participant> &me,
165
	const string &subject,
166
	list<shared_ptr<Participant>> &&participants,
167
	unsigned int lastNotifyId
Benjamin REIS's avatar
Benjamin REIS committed
168
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, chatRoomId),
Ronan's avatar
Ronan committed
169
RemoteConference(core, me->getAddress(), nullptr) {
170
	L_D_T(RemoteConference, dConference);
171

Benjamin REIS's avatar
Benjamin REIS committed
172
	const IdentityAddress &peerAddress = chatRoomId.getPeerAddress();
173
	dConference->focus = make_shared<Participant>(peerAddress);
174
	dConference->focus->getPrivate()->addDevice(peerAddress);
175 176
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
177
	dConference->participants = move(participants);
178

Ronan's avatar
Ronan committed
179
	getMe()->getPrivate()->setAdmin(me->isAdmin());
180

181
	dConference->eventHandler->setLastNotify(lastNotifyId);
182
	dConference->eventHandler->subscribe(getChatRoomId());
183 184
}

185 186 187 188
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

189
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
190
	return Capabilities::Conference;
191 192
}

193
bool ClientGroupChatRoom::hasBeenLeft () const {
194
	return getState() != State::Created;
195 196
}

197 198 199 200
bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

201
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
202
	return RemoteConference::getConferenceAddress();
203
}
204

205 206
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
207 208
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
209 210
}

211
void ClientGroupChatRoom::addParticipants (
212
	const list<IdentityAddress> &addresses,
213 214 215
	const CallSessionParams *params,
	bool hasMedia
) {
216
	L_D();
217
	L_D_T(RemoteConference, dConference);
218

219
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
220
	if (addressesList.empty())
221
		return;
222

223
	if ((getState() != ChatRoom::State::Instantiated) && (getState() != ChatRoom::State::Created)) {
224 225 226
		lError() << "Cannot add participants to the ClientGroupChatRoom in a state other than Instantiated or Created";
		return;
	}
227

228
	Content content;
229
	content.setBody(getResourceLists(addressesList));
230 231
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
232

233
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
234
	if (session)
235
		session->update(nullptr, getSubject(), &content);
236 237
	else {
		session = d->createSession();
238
		session->startInvite(nullptr, getSubject(), &content);
239
		if (getState() == ChatRoom::State::Instantiated)
240
			d->setState(ChatRoom::State::CreationPending);
241
	}
242 243
}

244
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
245
	LinphoneCore *cCore = getCore()->getCCore();
246 247

	SalReferOp *referOp = new SalReferOp(cCore->sal);
248
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
249
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
250 251 252 253 254 255
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
256 257
}

258
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
259
	RemoteConference::removeParticipants(participants);
260
}
261

262
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
263 264 265 266 267 268 269
	return RemoteConference::findParticipant(addr);
}

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

270 271
int ClientGroupChatRoom::getParticipantCount () const {
	return RemoteConference::getParticipantCount();
272 273
}

Ghislain MARY's avatar
Ghislain MARY committed
274
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
275 276 277
	return RemoteConference::getParticipants();
}

278
void ClientGroupChatRoom::setParticipantAdminStatus (const shared_ptr<Participant> &participant, bool isAdmin) {
279 280
	if (isAdmin == participant->isAdmin())
		return;
281 282

	if (!getMe()->isAdmin()) {
283 284 285
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
286

287
	LinphoneCore *cCore = getCore()->getCCore();
288 289

	SalReferOp *referOp = new SalReferOp(cCore->sal);
290
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
291
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
292 293 294 295 296 297 298 299
	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();
}

300 301 302 303
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

304 305
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
306
	L_D_T(RemoteConference, dConference);
307

308
	if (getState() != ChatRoom::State::Created) {
309 310 311
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
312 313

	if (!getMe()->isAdmin()) {
314 315 316
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
317

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

327 328 329 330 331
void ClientGroupChatRoom::join () {
	L_D();
	L_D_T(RemoteConference, dConference);

	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
332
	if (!session && ((getState() == ChatRoom::State::Instantiated) || (getState() == ChatRoom::State::Terminated))) {
333 334 335 336 337 338 339 340 341 342 343
		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();
344

345 346 347 348 349 350 351
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
352

353
	d->setState(ChatRoom::State::TerminationPending);
354 355
}

356 357
// -----------------------------------------------------------------------------

358
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
359
	L_D();
360 361
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
362
	dConference->focus->getPrivate()->setAddress(addr);
363 364
	dConference->focus->getPrivate()->clearDevices();
	dConference->focus->getPrivate()->addDevice(addr);
365
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
366
	d->chatRoomListener->onChatRoomInsertRequested(getSharedFromThis());
367 368
}

369
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
370
	L_D();
371 372
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
373
	d->setState(ChatRoom::State::Terminated);
374 375 376 377 378
	getCore()->getPrivate()->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceTerminated,
		time(nullptr),
		d->chatRoomId
	));
379 380
}

381
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
382 383
	L_D();
	d->setState(ChatRoom::State::Created);
384

385 386
	d->chatRoomListener->onChatRoomInsertInDatabaseRequested(getSharedFromThis());
	getCore()->getPrivate()->mainDb->addEvent(make_shared<ConferenceEvent>(
387 388 389 390
		EventLog::Type::ConferenceCreated,
		time(nullptr),
		d->chatRoomId
	));
391 392
}

393
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
394 395
	L_D_T(RemoteConference, dConference);

396
	const IdentityAddress &addr = event->getParticipantAddress();
397 398
	if (isMe(addr))
		return;
399

400 401 402 403 404
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
405

Ronan's avatar
Ronan committed
406
	participant = make_shared<Participant>(addr);
407
	dConference->participants.push_back(participant);
408 409 410 411

	if (isFullState)
		return;

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

414
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
415 416 417
	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
418
		cb(cr, L_GET_C_BACK_PTR(event));
419 420
}

421
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
422
	(void)isFullState;
423

424 425
	L_D_T(RemoteConference, dConference);

426
	const IdentityAddress &addr = event->getParticipantAddress();
427 428
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
429
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
430 431
		return;
	}
432

433
	dConference->participants.remove(participant);
Benjamin REIS's avatar
Benjamin REIS committed
434
	getCore()->getPrivate()->mainDb->addEvent(event);
435

436
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
437 438 439
	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
440
		cb(cr, L_GET_C_BACK_PTR(event));
441 442
}

443
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
444
	const IdentityAddress &addr = event->getParticipantAddress();
445
	shared_ptr<Participant> participant;
446
	if (isMe(addr))
447
		participant = getMe();
448 449
	else
		participant = findParticipant(addr);
450 451 452 453
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
454

455 456 457 458 459
	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);

460 461 462
	if (isFullState)
		return;

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

465
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
466 467 468
	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
469
		cb(cr, L_GET_C_BACK_PTR(event));
470 471
}

472
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
473 474
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
475
	RemoteConference::setSubject(event->getSubject());
476

477 478 479
	if (isFullState)
		return;

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

482 483 484 485
	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
486
		cb(cr, L_GET_C_BACK_PTR(event));
487 488
}

489
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
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 << " added a device but is not in the list of participants!";
		return;
	}
500
	participant->getPrivate()->addDevice(event->getDeviceAddress());
501 502 503 504

	if (isFullState)
		return;

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

Benjamin REIS's avatar
Benjamin REIS committed
507 508 509 510
	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
511
		cb(cr, L_GET_C_BACK_PTR(event));
512 513
}

514
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
515
	(void)isFullState;
516

517
	const IdentityAddress &addr = event->getParticipantAddress();
518
	shared_ptr<Participant> participant;
519
	if (isMe(addr))
520
		participant = getMe();
521 522 523 524 525 526
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
527
	participant->getPrivate()->removeDevice(event->getDeviceAddress());
Benjamin REIS's avatar
Benjamin REIS committed
528 529
	getCore()->getPrivate()->mainDb->addEvent(event);

Benjamin REIS's avatar
Benjamin REIS committed
530 531 532 533
	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
534
		cb(cr, L_GET_C_BACK_PTR(event));
535 536
}

537
LINPHONE_END_NAMESPACE