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
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
Ronan's avatar
Ronan committed
102 103
	const IdentityAddress &peerAddress,
	shared_ptr<Participant> &me,
104 105
	const string &subject,
	list<shared_ptr<Participant>> &&participants
Ronan's avatar
Ronan committed
106 107
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(peerAddress, me->getAddress())),
RemoteConference(core, me->getAddress(), nullptr) {
108 109 110 111 112
	L_D();
	L_D_T(RemoteConference, dConference);
	dConference->focus = make_shared<Participant>(peerAddress);
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
113
	dConference->participants = move(participants);
114
	d->state = ChatRoom::State::Created;
Ronan's avatar
Ronan committed
115
	getMe()->getPrivate()->setAdmin(me->isAdmin());
116 117
}

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

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

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

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

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

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

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

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

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

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

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

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

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

191
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	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();
}

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

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

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

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

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

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

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

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

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

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

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

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

285 286
// -----------------------------------------------------------------------------

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

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

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

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

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

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

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

	if (isFullState)
		return;

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

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

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

340 341
	L_D_T(RemoteConference, dConference);

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

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

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

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

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

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

377 378 379
	if (isFullState)
		return;

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

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

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

394 395 396
	if (isFullState)
		return;

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

	if (isFullState)
		return;

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

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

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

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

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

454 455
// -----------------------------------------------------------------------------

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

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

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

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

487
LINPHONE_END_NAMESPACE