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,
Benjamin REIS's avatar
Benjamin REIS committed
102
	const ChatRoomId &chatRoomId,
Ronan's avatar
Ronan committed
103
	shared_ptr<Participant> &me,
104
	const string &subject,
105
	list<shared_ptr<Participant>> &&participants,
106
	unsigned int lastNotifyId
Benjamin REIS's avatar
Benjamin REIS committed
107
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, chatRoomId),
Ronan's avatar
Ronan committed
108
RemoteConference(core, me->getAddress(), nullptr) {
109
	L_D_T(RemoteConference, dConference);
110

Benjamin REIS's avatar
Benjamin REIS committed
111
	const IdentityAddress &peerAddress = chatRoomId.getPeerAddress();
112 113 114
	dConference->focus = make_shared<Participant>(peerAddress);
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
115
	dConference->participants = move(participants);
116

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

200
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
201 202 203 204 205 206 207
	return RemoteConference::findParticipant(addr);
}

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

208 209
int ClientGroupChatRoom::getParticipantCount () const {
	return RemoteConference::getParticipantCount();
210 211
}

Ghislain MARY's avatar
Ghislain MARY committed
212
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
213 214 215
	return RemoteConference::getParticipants();
}

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

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

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

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

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

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

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

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

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

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

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

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

294 295
// -----------------------------------------------------------------------------

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

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

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

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

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

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

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

	if (isFullState)
		return;

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

344
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
345 346 347
	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
348
		cb(cr, L_GET_C_BACK_PTR(event));
349 350
}

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

354 355
	L_D_T(RemoteConference, dConference);

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

363
	dConference->participants.remove(participant);
Benjamin REIS's avatar
Benjamin REIS committed
364
	getCore()->getPrivate()->mainDb->addEvent(event);
365

366
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
367 368 369
	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
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;

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

395
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
396 397 398
	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
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;

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

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

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

Benjamin REIS's avatar
Benjamin REIS committed
460 461 462 463
	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
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