client-group-chat-room.cpp 17.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
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, this);
66
	const Address &myAddress = q->getMe()->getAddress();
67 68
	Address myCleanedAddress(myAddress);
	myCleanedAddress.setUriParam("gr"); // Remove gr parameter for INVITE
69
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myCleanedAddress, focus->getPrivate()->getDevices().front()->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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
// -----------------------------------------------------------------------------

void ClientGroupChatRoomPrivate::onCallSessionSetReleased (const shared_ptr<const CallSession> &session) {
	L_Q_T(RemoteConference, qConference);

	ParticipantPrivate *participantPrivate = qConference->getPrivate()->focus->getPrivate();
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
}

void ClientGroupChatRoomPrivate::onCallSessionStateChanged (
	const shared_ptr<const CallSession> &session,
	LinphoneCallState newState,
	const string &message
) {
	L_Q();
	L_Q_T(RemoteConference, qConference);

	if (newState == LinphoneCallConnected) {
		if (state == ChatRoom::State::CreationPending) {
			IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly());
			q->onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
				qConference->getPrivate()->eventHandler->subscribe(q->getChatRoomId());
		} else if (state == ChatRoom::State::TerminationPending)
			qConference->getPrivate()->focus->getPrivate()->getSession()->terminate();
	} else if ((newState == LinphoneCallReleased) && (state == ChatRoom::State::TerminationPending)) {
		q->onConferenceTerminated(q->getConferenceAddress());
	} else if ((newState == LinphoneCallError) && (state == ChatRoom::State::CreationPending)) {
		setState(ChatRoom::State::CreationFailed);
	}
}

// -----------------------------------------------------------------------------

119 120
void ClientGroupChatRoomPrivate::onChatMessageReceived (const shared_ptr<ChatMessage> &) {}

121 122
// =============================================================================

123
ClientGroupChatRoom::ClientGroupChatRoom (
124
	const shared_ptr<Core> &core,
125
	const string &uri,
126
	const IdentityAddress &me,
127
	const string &subject
128
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(IdentityAddress(), me)),
129
RemoteConference(core, me, nullptr) {
130
	L_D_T(RemoteConference, dConference);
131 132 133
	IdentityAddress focusAddr(uri);
	dConference->focus = make_shared<Participant>(focusAddr);
	dConference->focus->getPrivate()->addDevice(focusAddr);
134
	RemoteConference::setSubject(subject);
135 136
}

137 138
ClientGroupChatRoom::ClientGroupChatRoom (
	const shared_ptr<Core> &core,
Benjamin REIS's avatar
Benjamin REIS committed
139
	const ChatRoomId &chatRoomId,
Ronan's avatar
Ronan committed
140
	shared_ptr<Participant> &me,
141
	const string &subject,
142
	list<shared_ptr<Participant>> &&participants,
143
	unsigned int lastNotifyId
Benjamin REIS's avatar
Benjamin REIS committed
144
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, chatRoomId),
Ronan's avatar
Ronan committed
145
RemoteConference(core, me->getAddress(), nullptr) {
146
	L_D_T(RemoteConference, dConference);
147

Benjamin REIS's avatar
Benjamin REIS committed
148
	const IdentityAddress &peerAddress = chatRoomId.getPeerAddress();
149 150 151
	dConference->focus = make_shared<Participant>(peerAddress);
	dConference->conferenceAddress = peerAddress;
	dConference->subject = subject;
152
	dConference->participants = move(participants);
153

Ronan's avatar
Ronan committed
154
	getMe()->getPrivate()->setAdmin(me->isAdmin());
155

156
	dConference->eventHandler->setLastNotify(lastNotifyId);
157
	dConference->eventHandler->subscribe(getChatRoomId());
158 159
}

160 161 162 163
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

164 165 166 167
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

168
bool ClientGroupChatRoom::hasBeenLeft () const {
169
	return getState() != State::Created;
170 171
}

172 173 174 175
bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

176
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
177
	return RemoteConference::getConferenceAddress();
178
}
179

180 181
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
182 183
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
184 185
}

186
void ClientGroupChatRoom::addParticipants (
187
	const list<IdentityAddress> &addresses,
188 189 190
	const CallSessionParams *params,
	bool hasMedia
) {
191
	L_D();
192
	L_D_T(RemoteConference, dConference);
193

194
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
195
	if (addressesList.empty())
196
		return;
197

198 199 200 201
	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;
	}
202

203
	Content content;
204
	content.setBody(getResourceLists(addressesList));
205 206
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
207

208
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
209
	if (session)
210
		session->update(nullptr, getSubject(), &content);
211 212
	else {
		session = d->createSession();
213 214
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
215
			d->setState(ChatRoom::State::CreationPending);
216
	}
217 218
}

219
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
220
	LinphoneCore *cCore = getCore()->getCCore();
221 222

	SalReferOp *referOp = new SalReferOp(cCore->sal);
223
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
224
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
225 226 227 228 229 230
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
231 232
}

233
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
234
	RemoteConference::removeParticipants(participants);
235
}
236

237
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
238 239 240 241 242 243 244
	return RemoteConference::findParticipant(addr);
}

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

245 246
int ClientGroupChatRoom::getParticipantCount () const {
	return RemoteConference::getParticipantCount();
247 248
}

Ghislain MARY's avatar
Ghislain MARY committed
249
const list<shared_ptr<Participant>> &ClientGroupChatRoom::getParticipants () const {
250 251 252
	return RemoteConference::getParticipants();
}

253 254 255
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
256 257

	if (!getMe()->isAdmin()) {
258 259 260
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
261

262
	LinphoneCore *cCore = getCore()->getCCore();
263 264

	SalReferOp *referOp = new SalReferOp(cCore->sal);
265
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
266
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
267 268 269 270 271 272 273 274
	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();
}

275 276 277 278
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

279 280
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
281
	L_D_T(RemoteConference, dConference);
282

283 284 285 286
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
287 288

	if (!getMe()->isAdmin()) {
289 290 291
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
292

293
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
294 295 296 297 298 299
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
300 301
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
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();
319

320 321 322 323 324 325 326
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
327

328
	d->setState(ChatRoom::State::TerminationPending);
329 330
}

331 332
// -----------------------------------------------------------------------------

333
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
334
	L_D();
335 336
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
337
	dConference->focus->getPrivate()->setAddress(addr);
338 339
	dConference->focus->getPrivate()->clearDevices();
	dConference->focus->getPrivate()->addDevice(addr);
340
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
341
	getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
342 343
}

344
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
345
	L_D();
346 347
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
348
	d->setState(ChatRoom::State::Terminated);
349 350 351 352 353
	getCore()->getPrivate()->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceTerminated,
		time(nullptr),
		d->chatRoomId
	));
354 355
}

356
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
357 358
	L_D();
	d->setState(ChatRoom::State::Created);
359 360 361 362 363 364 365 366

	CorePrivate *dCore = getCore()->getPrivate();
	dCore->insertChatRoomWithDb(getSharedFromThis());
	dCore->mainDb->addEvent(make_shared<ConferenceEvent>(
		EventLog::Type::ConferenceCreated,
		time(nullptr),
		d->chatRoomId
	));
367 368
}

369
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
370 371
	L_D_T(RemoteConference, dConference);

372
	const IdentityAddress &addr = event->getParticipantAddress();
373 374
	if (isMe(addr))
		return;
375

376 377 378 379 380
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
381

Ronan's avatar
Ronan committed
382
	participant = make_shared<Participant>(addr);
383
	dConference->participants.push_back(participant);
384 385 386 387

	if (isFullState)
		return;

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

390
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
391 392 393
	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
394
		cb(cr, L_GET_C_BACK_PTR(event));
395 396
}

397
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
398
	(void)isFullState;
399

400 401
	L_D_T(RemoteConference, dConference);

402
	const IdentityAddress &addr = event->getParticipantAddress();
403 404
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
405
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
406 407
		return;
	}
408

409
	dConference->participants.remove(participant);
Benjamin REIS's avatar
Benjamin REIS committed
410
	getCore()->getPrivate()->mainDb->addEvent(event);
411

412
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
413 414 415
	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
416
		cb(cr, L_GET_C_BACK_PTR(event));
417 418
}

419
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
420
	const IdentityAddress &addr = event->getParticipantAddress();
421
	shared_ptr<Participant> participant;
422
	if (isMe(addr))
423
		participant = getMe();
424 425
	else
		participant = findParticipant(addr);
426 427 428 429
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
430

431 432 433 434 435
	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);

436 437 438
	if (isFullState)
		return;

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

441
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
442 443 444
	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
445
		cb(cr, L_GET_C_BACK_PTR(event));
446 447
}

448
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
449 450
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
451
	RemoteConference::setSubject(event->getSubject());
452

453 454 455
	if (isFullState)
		return;

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

458 459 460 461
	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
462
		cb(cr, L_GET_C_BACK_PTR(event));
463 464
}

465
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
466
	const IdentityAddress &addr = event->getParticipantAddress();
467
	shared_ptr<Participant> participant;
468
	if (isMe(addr))
469
		participant = getMe();
470 471 472 473 474 475
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
476
	participant->getPrivate()->addDevice(event->getDeviceAddress());
477 478 479 480

	if (isFullState)
		return;

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

Benjamin REIS's avatar
Benjamin REIS committed
483 484 485 486
	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
487
		cb(cr, L_GET_C_BACK_PTR(event));
488 489
}

490
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
491
	(void)isFullState;
492

493
	const IdentityAddress &addr = event->getParticipantAddress();
494
	shared_ptr<Participant> participant;
495
	if (isMe(addr))
496
		participant = getMe();
497 498 499 500 501 502
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
503
	participant->getPrivate()->removeDevice(event->getDeviceAddress());
Benjamin REIS's avatar
Benjamin REIS committed
504 505
	getCore()->getPrivate()->mainDb->addEvent(event);

Benjamin REIS's avatar
Benjamin REIS committed
506 507 508 509
	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
510
		cb(cr, L_GET_C_BACK_PTR(event));
511 512
}

513
LINPHONE_END_NAMESPACE