client-group-chat-room.cpp 16.4 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 103 104 105 106 107 108 109 110 111 112 113 114
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
	const ChatRoomId &chatRoomId,
	const string &subject
) : 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;
	d->state = ChatRoom::State::Created;
}

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

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

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

127
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
128
	return RemoteConference::getConferenceAddress();
129
}
130

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

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

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

149 150 151 152
	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;
	}
153

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

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

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

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

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

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

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

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

213
	LinphoneCore *cCore = getCore()->getCCore();
214 215

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

226 227 228 229
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

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

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

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

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

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

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

279
	d->setState(ChatRoom::State::TerminationPending);
280 281
}

282 283
// -----------------------------------------------------------------------------

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

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

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

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

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

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

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

	if (isFullState)
		return;

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

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

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

337 338
	L_D_T(RemoteConference, dConference);

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

346 347
	dConference->participants.remove(participant);

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

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

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

369 370 371 372 373
	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);

374 375 376
	if (isFullState)
		return;

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

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

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

391 392 393
	if (isFullState)
		return;

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

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

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

	if (isFullState)
		return;

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

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

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

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

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

451 452
// -----------------------------------------------------------------------------

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

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

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

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

484
LINPHONE_END_NAMESPACE