client-group-chat-room.cpp 17 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,
107
	bool hasBeenLeft
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 = hasBeenLeft ? 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::hasBeenLeft () 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 316 317
	getCore()->getPrivate()->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceTerminated,
		time(nullptr),
		d->chatRoomId
	));
318 319
}

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

326
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
327 328
	L_D_T(RemoteConference, dConference);

329
	const IdentityAddress &addr = event->getParticipantAddress();
330 331
	if (isMe(addr))
		return;
332

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

Ronan's avatar
Ronan committed
339
	participant = make_shared<Participant>(addr);
340
	dConference->participants.push_back(participant);
341 342 343 344

	if (isFullState)
		return;

345
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
346 347
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
348
	getCore()->getPrivate()->mainDb->addEvent(event);
349

350
	if (cb)
Ronan's avatar
Ronan committed
351
		cb(cr, L_GET_C_BACK_PTR(event));
352 353
}

354
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
355
	(void)isFullState;
356

357 358
	L_D_T(RemoteConference, dConference);

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

366 367
	dConference->participants.remove(participant);

368
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
369 370
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
371
	getCore()->getPrivate()->mainDb->addEvent(event);
372

373
	if (cb)
Ronan's avatar
Ronan committed
374
		cb(cr, L_GET_C_BACK_PTR(event));
375 376
}

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

389 390 391 392 393
	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);

394 395 396
	if (isFullState)
		return;

397
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
398 399
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
400
	getCore()->getPrivate()->mainDb->addEvent(event);
401

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

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

411 412 413
	if (isFullState)
		return;

414 415 416
	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);
417
	getCore()->getPrivate()->mainDb->addEvent(event);
418

419
	if (cb)
Ronan's avatar
Ronan committed
420
		cb(cr, L_GET_C_BACK_PTR(event));
421 422
}

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

	if (isFullState)
		return;

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

	if (cb)
Ronan's avatar
Ronan committed
445
		cb(cr, L_GET_C_BACK_PTR(event));
446 447
}

448
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
449
	(void)isFullState;
450

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

	if (cb)
Ronan's avatar
Ronan committed
468
		cb(cr, L_GET_C_BACK_PTR(event));
469 470
}

471 472
// -----------------------------------------------------------------------------

473
void ClientGroupChatRoom::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
474
	L_D_T(RemoteConference, dConference);
475

476
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
477 478
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
479 480
}

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

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

504
LINPHONE_END_NAMESPACE