client-group-chat-room.cpp 16.8 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
	list<shared_ptr<Participant>> &&participants,
106 107
	unsigned int lastNotifyId,
	bool isReadOnly
Ronan's avatar
Ronan committed
108 109
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(peerAddress, me->getAddress())),
RemoteConference(core, me->getAddress(), nullptr) {
110 111
	L_D();
	L_D_T(RemoteConference, dConference);
112

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

118
	d->state = isReadOnly ? ChatRoom::State::Terminated : ChatRoom::State::Created;
119

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

203
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
204 205 206 207 208 209 210 211 212 213 214
	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
215
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
216 217 218
	return RemoteConference::getParticipants();
}

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

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

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

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

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

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

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

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

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

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

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

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

297 298
// -----------------------------------------------------------------------------

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

308
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
309
	L_D();
310 311
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
312
	d->setState(ChatRoom::State::Terminated);
313 314
}

315
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
316 317
	L_D();
	d->setState(ChatRoom::State::Created);
318
	getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
319 320
}

321
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
322 323
	L_D_T(RemoteConference, dConference);

324
	const IdentityAddress &addr = event->getParticipantAddress();
325 326
	if (isMe(addr))
		return;
327

328 329 330 331 332
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
333

Ronan's avatar
Ronan committed
334
	participant = make_shared<Participant>(addr);
335
	dConference->participants.push_back(participant);
336 337 338 339

	if (isFullState)
		return;

340
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
341 342
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
343
	getCore()->getPrivate()->mainDb->addEvent(event);
344

345
	if (cb)
Ronan's avatar
Ronan committed
346
		cb(cr, L_GET_C_BACK_PTR(event));
347 348
}

349
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
350
	(void)isFullState;
351

352 353
	L_D_T(RemoteConference, dConference);

354
	const IdentityAddress &addr = event->getParticipantAddress();
355 356
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
357
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
358 359
		return;
	}
360

361 362
	dConference->participants.remove(participant);

363
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
364 365
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
366
	getCore()->getPrivate()->mainDb->addEvent(event);
367

368
	if (cb)
Ronan's avatar
Ronan committed
369
		cb(cr, L_GET_C_BACK_PTR(event));
370 371
}

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

384 385 386 387 388
	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);

389 390 391
	if (isFullState)
		return;

392
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
393 394
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
395
	getCore()->getPrivate()->mainDb->addEvent(event);
396

397
	if (cb)
Ronan's avatar
Ronan committed
398
		cb(cr, L_GET_C_BACK_PTR(event));
399 400
}

401
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
402 403
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
404
	RemoteConference::setSubject(event->getSubject());
405

406 407 408
	if (isFullState)
		return;

409 410 411
	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);
412
	getCore()->getPrivate()->mainDb->addEvent(event);
413

414
	if (cb)
Ronan's avatar
Ronan committed
415
		cb(cr, L_GET_C_BACK_PTR(event));
416 417
}

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

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
434 435 436
	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);
437
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
438 439

	if (cb)
Ronan's avatar
Ronan committed
440
		cb(cr, L_GET_C_BACK_PTR(event));
441 442
}

443
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
444
	(void)isFullState;
445

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

	if (cb)
Ronan's avatar
Ronan committed
463
		cb(cr, L_GET_C_BACK_PTR(event));
464 465
}

466 467
// -----------------------------------------------------------------------------

468
void ClientGroupChatRoom::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
469
	L_D_T(RemoteConference, dConference);
470

471
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
472 473
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
474 475
}

476
void ClientGroupChatRoom::onCallSessionStateChanged (
477
	const shared_ptr<const CallSession> &session,
478 479 480
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
481
	L_D();
482 483
	L_D_T(RemoteConference, dConference);

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

499
LINPHONE_END_NAMESPACE