client-group-chat-room.cpp 15.7 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
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myAddress, focus->getAddress());
68 69 70 71
	session->initiateOutgoing();
	return session;
}

Ghislain MARY's avatar
Ghislain MARY committed
72
void ClientGroupChatRoomPrivate::notifyReceived (const string &body) {
73 74
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->notifyReceived(body);
75 76
}

77 78 79 80 81
void ClientGroupChatRoomPrivate::multipartNotifyReceived (const string &body) {
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->multipartNotifyReceived(body);
}

82 83
void ClientGroupChatRoomPrivate::onChatMessageReceived (const shared_ptr<ChatMessage> &) {}

84 85
// =============================================================================

86
ClientGroupChatRoom::ClientGroupChatRoom (
87
	const std::shared_ptr<Core> &core,
Ghislain MARY's avatar
Ghislain MARY committed
88
	const std::string &factoryUri,
89
	const IdentityAddress &me,
90
	const std::string &subject
91
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(IdentityAddress(), me)),
92
RemoteConference(core, me, nullptr) {
93
	L_D_T(RemoteConference, dConference);
Ghislain MARY's avatar
Ghislain MARY committed
94
	dConference->focus = make_shared<Participant>(Address(factoryUri));
95
	RemoteConference::setSubject(subject);
96 97
}

98 99 100 101
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

102 103 104 105 106 107 108 109
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

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

110
const IdentityAddress &ClientGroupChatRoom::getConferenceAddress () const {
111
	return RemoteConference::getConferenceAddress();
112
}
113

114 115
void ClientGroupChatRoom::addParticipant (const IdentityAddress &addr, const CallSessionParams *params, bool hasMedia) {
	list<IdentityAddress> addresses;
116 117
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
118 119
}

120
void ClientGroupChatRoom::addParticipants (
121
	const list<IdentityAddress> &addresses,
122 123 124
	const CallSessionParams *params,
	bool hasMedia
) {
125
	L_D();
126
	L_D_T(RemoteConference, dConference);
127

128
	list<IdentityAddress> addressesList = d->cleanAddressesList(addresses);
129
	if (addressesList.empty())
130
		return;
131

132 133 134 135
	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;
	}
136

137
	Content content;
138
	content.setBody(getResourceLists(addressesList));
139 140
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
141

142
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
143
	if (session)
144
		session->update(nullptr, getSubject(), &content);
145 146
	else {
		session = d->createSession();
147 148
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
149
			d->setState(ChatRoom::State::CreationPending);
150
	}
151 152
}

153
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
154
	LinphoneCore *cCore = getCore()->getCCore();
155 156

	SalReferOp *referOp = new SalReferOp(cCore->sal);
157
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
158
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
159 160 161 162 163 164
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
165 166
}

167
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
168
	RemoteConference::removeParticipants(participants);
169
}
170

171
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const IdentityAddress &addr) const {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	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();
}

187 188 189
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
190 191

	if (!getMe()->isAdmin()) {
192 193 194
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
195

196
	LinphoneCore *cCore = getCore()->getCCore();
197 198

	SalReferOp *referOp = new SalReferOp(cCore->sal);
199
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
200
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
201 202 203 204 205 206 207 208
	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();
}

209 210 211 212
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

213 214
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
215
	L_D_T(RemoteConference, dConference);
216

217 218 219 220
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
221 222

	if (!getMe()->isAdmin()) {
223 224 225
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
226

227
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
228 229 230 231 232 233
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
234 235
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
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();
253

254 255 256 257 258 259 260
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
261

262
	d->setState(ChatRoom::State::TerminationPending);
263 264
}

265 266
// -----------------------------------------------------------------------------

267
void ClientGroupChatRoom::onConferenceCreated (const IdentityAddress &addr) {
268
	L_D();
269 270
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
271
	d->chatRoomId = ChatRoomId(addr, d->chatRoomId.getLocalAddress());
272
	getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
273 274
}

275
void ClientGroupChatRoom::onConferenceTerminated (const IdentityAddress &addr) {
276
	L_D();
277 278
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
279
	d->setState(ChatRoom::State::Terminated);
280 281
}

282
void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) {
283 284
	L_D();
	d->setState(ChatRoom::State::Created);
285
	getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
286 287
}

288
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
289 290
	L_D_T(RemoteConference, dConference);

291
	const IdentityAddress &addr = event->getParticipantAddress();
292 293
	if (isMe(addr))
		return;
294

295 296 297 298 299
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
300

Ronan's avatar
Ronan committed
301
	participant = make_shared<Participant>(addr);
302
	dConference->participants.push_back(participant);
303 304 305 306

	if (isFullState)
		return;

307
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
308 309
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
310
	getCore()->getPrivate()->mainDb->addEvent(event);
311

312
	if (cb)
Ronan's avatar
Ronan committed
313
		cb(cr, L_GET_C_BACK_PTR(event));
314 315
}

316
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
317
	(void)isFullState;
318

319 320
	L_D_T(RemoteConference, dConference);

321
	const IdentityAddress &addr = event->getParticipantAddress();
322 323
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
324
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
325 326
		return;
	}
327

328 329
	dConference->participants.remove(participant);

330
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
331 332
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
333
	getCore()->getPrivate()->mainDb->addEvent(event);
334

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

339
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
340
	const IdentityAddress &addr = event->getParticipantAddress();
341
	shared_ptr<Participant> participant;
342
	if (isMe(addr))
343
		participant = getMe();
344 345
	else
		participant = findParticipant(addr);
346 347 348 349
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
350

351 352 353 354 355
	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);

356 357 358
	if (isFullState)
		return;

359
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
360 361
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
362
	getCore()->getPrivate()->mainDb->addEvent(event);
363

364
	if (cb)
Ronan's avatar
Ronan committed
365
		cb(cr, L_GET_C_BACK_PTR(event));
366 367
}

368
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
369 370
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
371
	RemoteConference::setSubject(event->getSubject());
372

373 374 375
	if (isFullState)
		return;

376 377 378
	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);
379
	getCore()->getPrivate()->mainDb->addEvent(event);
380

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

385
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
386
	const IdentityAddress &addr = event->getParticipantAddress();
387
	shared_ptr<Participant> participant;
388
	if (isMe(addr))
389
		participant = getMe();
390 391 392 393 394 395
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
396
	participant->getPrivate()->addDevice(event->getDeviceAddress());
397 398 399 400

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
401 402 403
	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);
404
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
405 406

	if (cb)
Ronan's avatar
Ronan committed
407
		cb(cr, L_GET_C_BACK_PTR(event));
408 409
}

410
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
411
	(void)isFullState;
412

413
	const IdentityAddress &addr = event->getParticipantAddress();
414
	shared_ptr<Participant> participant;
415
	if (isMe(addr))
416
		participant = getMe();
417 418 419 420 421 422
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
423
	participant->getPrivate()->removeDevice(event->getDeviceAddress());
Benjamin REIS's avatar
Benjamin REIS committed
424 425 426
	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);
427
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
428 429

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

433 434
// -----------------------------------------------------------------------------

435
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
436
	L_D_T(RemoteConference, dConference);
437

438
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
439 440
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
441 442
}

443 444 445 446 447
void ClientGroupChatRoom::onCallSessionStateChanged (
	const std::shared_ptr<const CallSession> &session,
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
448
	L_D();
449 450
	L_D_T(RemoteConference, dConference);

451
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
452
		if (d->state == ChatRoom::State::CreationPending) {
453
			IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly());
Ghislain MARY's avatar
Ghislain MARY committed
454 455
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
456
				dConference->eventHandler->subscribe(getChatRoomId());
457
		} else if (d->state == ChatRoom::State::TerminationPending)
458
			dConference->focus->getPrivate()->getSession()->terminate();
459
	} else if ((state == LinphoneCallReleased) && (d->state == ChatRoom::State::TerminationPending)) {
460
		onConferenceTerminated(getConferenceAddress());
461
	} else if ((state == LinphoneCallError) && (d->state == ChatRoom::State::CreationPending)) {
462 463
		d->setState(ChatRoom::State::CreationFailed);
	}
464 465
}

466
LINPHONE_END_NAMESPACE