client-group-chat-room.cpp 16.5 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 102
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
	const ChatRoomId &chatRoomId,
103 104
	const string &subject,
	list<shared_ptr<Participant>> &&participants
105 106 107 108 109 110 111 112
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, chatRoomId),
RemoteConference(core, chatRoomId.getLocalAddress(), nullptr) {
	L_D();
	L_D_T(RemoteConference, dConference);
	const IdentityAddress &peerAddress = chatRoomId.getPeerAddress();
	dConference->focus = make_shared<Participant>(peerAddress);
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
113
	dConference->participants = move(participants);
114 115 116
	d->state = ChatRoom::State::Created;
}

117 118 119 120
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

121 122 123 124 125 126 127 128
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

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

129
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
130
	return RemoteConference::getConferenceAddress();
131
}
132

133 134
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
135 136
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
137 138
}

139
void ClientGroupChatRoom::addParticipants (
140
	const list<IdentityAddress> &addresses,
141 142 143
	const CallSessionParams *params,
	bool hasMedia
) {
144
	L_D();
145
	L_D_T(RemoteConference, dConference);
146

147
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
148
	if (addressesList.empty())
149
		return;
150

151 152 153 154
	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;
	}
155

156
	Content content;
157
	content.setBody(getResourceLists(addressesList));
158 159
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
160

161
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
162
	if (session)
163
		session->update(nullptr, getSubject(), &content);
164 165
	else {
		session = d->createSession();
166 167
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
168
			d->setState(ChatRoom::State::CreationPending);
169
	}
170 171
}

172
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
173
	LinphoneCore *cCore = getCore()->getCCore();
174 175

	SalReferOp *referOp = new SalReferOp(cCore->sal);
176
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
177
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
178 179 180 181 182 183
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
184 185
}

186
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
187
	RemoteConference::removeParticipants(participants);
188
}
189

190
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	return RemoteConference::findParticipant(addr);
}

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

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

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

206 207 208
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
209 210

	if (!getMe()->isAdmin()) {
211 212 213
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
214

215
	LinphoneCore *cCore = getCore()->getCCore();
216 217

	SalReferOp *referOp = new SalReferOp(cCore->sal);
218
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
219
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
220 221 222 223 224 225 226 227
	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();
}

228 229 230 231
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

232 233
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
234
	L_D_T(RemoteConference, dConference);
235

236 237 238 239
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
240 241

	if (!getMe()->isAdmin()) {
242 243 244
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
245

246
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
247 248 249 250 251 252
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
253 254
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
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();
272

273 274 275 276 277 278 279
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
280

281
	d->setState(ChatRoom::State::TerminationPending);
282 283
}

284 285
// -----------------------------------------------------------------------------

286
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
287
	L_D();
288 289
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
290
	dConference->focus->getPrivate()->setAddress(addr);
291
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
292
	getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
293 294
}

295
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
296
	L_D();
297 298
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
299
	d->setState(ChatRoom::State::Terminated);
300 301
}

302
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
303 304
	L_D();
	d->setState(ChatRoom::State::Created);
305
	getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
306 307
}

308
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
309 310
	L_D_T(RemoteConference, dConference);

311
	const IdentityAddress &addr = event->getParticipantAddress();
312 313
	if (isMe(addr))
		return;
314

315 316 317 318 319
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
320

Ronan's avatar
Ronan committed
321
	participant = make_shared<Participant>(addr);
322
	dConference->participants.push_back(participant);
323 324 325 326

	if (isFullState)
		return;

327
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
328 329
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
330
	getCore()->getPrivate()->mainDb->addEvent(event);
331

332
	if (cb)
Ronan's avatar
Ronan committed
333
		cb(cr, L_GET_C_BACK_PTR(event));
334 335
}

336
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
337
	(void)isFullState;
338

339 340
	L_D_T(RemoteConference, dConference);

341
	const IdentityAddress &addr = event->getParticipantAddress();
342 343
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
344
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
345 346
		return;
	}
347

348 349
	dConference->participants.remove(participant);

350
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
351 352
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
353
	getCore()->getPrivate()->mainDb->addEvent(event);
354

355
	if (cb)
Ronan's avatar
Ronan committed
356
		cb(cr, L_GET_C_BACK_PTR(event));
357 358
}

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

371 372 373 374 375
	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);

376 377 378
	if (isFullState)
		return;

379
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
380 381
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
382
	getCore()->getPrivate()->mainDb->addEvent(event);
383

384
	if (cb)
Ronan's avatar
Ronan committed
385
		cb(cr, L_GET_C_BACK_PTR(event));
386 387
}

388
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
389 390
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
391
	RemoteConference::setSubject(event->getSubject());
392

393 394 395
	if (isFullState)
		return;

396 397 398
	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);
399
	getCore()->getPrivate()->mainDb->addEvent(event);
400

401
	if (cb)
Ronan's avatar
Ronan committed
402
		cb(cr, L_GET_C_BACK_PTR(event));
403 404
}

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

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
421 422 423
	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);
424
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
425 426

	if (cb)
Ronan's avatar
Ronan committed
427
		cb(cr, L_GET_C_BACK_PTR(event));
428 429
}

430
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
431
	(void)isFullState;
432

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

	if (cb)
Ronan's avatar
Ronan committed
450
		cb(cr, L_GET_C_BACK_PTR(event));
451 452
}

453 454
// -----------------------------------------------------------------------------

455
void ClientGroupChatRoom::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
456
	L_D_T(RemoteConference, dConference);
457

458
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
459 460
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
461 462
}

463
void ClientGroupChatRoom::onCallSessionStateChanged (
464
	const shared_ptr<const CallSession> &session,
465 466 467
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
468
	L_D();
469 470
	L_D_T(RemoteConference, dConference);

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

486
LINPHONE_END_NAMESPACE