client-group-chat-room.cpp 16.6 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 105
	const string &subject,
	list<shared_ptr<Participant>> &&participants
Ronan's avatar
Ronan committed
106 107
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(peerAddress, me->getAddress())),
RemoteConference(core, me->getAddress(), nullptr) {
108 109
	L_D();
	L_D_T(RemoteConference, dConference);
110

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

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

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

	dConference->eventHandler->subscribe(getChatRoomId());
121 122
}

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

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

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

135
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
136
	return RemoteConference::getConferenceAddress();
137
}
138

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

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

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

157 158 159 160
	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;
	}
161

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

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

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

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

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

196
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
197 198 199 200 201 202 203 204 205 206 207
	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
208
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
209 210 211
	return RemoteConference::getParticipants();
}

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

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

221
	LinphoneCore *cCore = getCore()->getCCore();
222 223

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

234 235 236 237
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

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

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

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

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

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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();
278

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

287
	d->setState(ChatRoom::State::TerminationPending);
288 289
}

290 291
// -----------------------------------------------------------------------------

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

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

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

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

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

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

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

	if (isFullState)
		return;

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

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

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

345 346
	L_D_T(RemoteConference, dConference);

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

354 355
	dConference->participants.remove(participant);

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

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

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

377 378 379 380 381
	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);

382 383 384
	if (isFullState)
		return;

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

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

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

399 400 401
	if (isFullState)
		return;

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

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

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

	if (isFullState)
		return;

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

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

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

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

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

459 460
// -----------------------------------------------------------------------------

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

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

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

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

492
LINPHONE_END_NAMESPACE