client-group-chat-room.cpp 16.9 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
	unsigned int lastNotifyId
Ronan's avatar
Ronan committed
107 108
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(peerAddress, me->getAddress())),
RemoteConference(core, me->getAddress(), nullptr) {
109
	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

Ronan's avatar
Ronan committed
116
	getMe()->getPrivate()->setAdmin(me->isAdmin());
117

118
	dConference->eventHandler->setLastNotify(lastNotifyId);
119
	dConference->eventHandler->subscribe(getChatRoomId());
120 121
}

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

126 127 128 129
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

130
bool ClientGroupChatRoom::hasBeenLeft () const {
131
	return getState() != State::Created;
132 133
}

134 135 136 137
bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

138
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
139
	return RemoteConference::getConferenceAddress();
140
}
141

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

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

156
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
157
	if (addressesList.empty())
158
		return;
159

160 161 162 163
	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;
	}
164

165
	Content content;
166
	content.setBody(getResourceLists(addressesList));
167 168
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
169

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

181
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
182
	LinphoneCore *cCore = getCore()->getCCore();
183 184

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

195
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
196
	RemoteConference::removeParticipants(participants);
197
}
198

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

215 216 217
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
218 219

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

224
	LinphoneCore *cCore = getCore()->getCCore();
225 226

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

237 238 239 240
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

241 242
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
243
	L_D_T(RemoteConference, dConference);
244

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

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

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

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

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

290
	d->setState(ChatRoom::State::TerminationPending);
291 292
}

293 294
// -----------------------------------------------------------------------------

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

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

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

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

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

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

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

	if (isFullState)
		return;

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

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

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

353 354
	L_D_T(RemoteConference, dConference);

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

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

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

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

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

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

390 391 392
	if (isFullState)
		return;

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

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

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

407 408 409
	if (isFullState)
		return;

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

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

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

	if (isFullState)
		return;

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

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

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

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

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

467 468
// -----------------------------------------------------------------------------

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

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

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

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

500
LINPHONE_END_NAMESPACE