callbacks.c 28.3 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
linphone
Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)

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.

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
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Simon Morlat's avatar
Simon Morlat committed
18 19 20
*/


21 22 23
#include "c-wrapper/internal/c-sal.h"
#include "sal/call-op.h"
#include "sal/message-op.h"
24
#include "sal/refer-op.h"
Simon Morlat's avatar
Simon Morlat committed
25

26
#include "linphone/core.h"
27
#include "linphone/utils/utils.h"
Simon Morlat's avatar
Simon Morlat committed
28
#include "private.h"
29
#include "mediastreamer2/mediastream.h"
30
#include "linphone/lpconfig.h"
31
#include <bctoolbox/defs.h>
32

33
// stat
34
#ifndef _WIN32
35 36 37 38 39
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

40
#include "c-wrapper/c-wrapper.h"
41
#include "call/call-p.h"
Ronan's avatar
Ronan committed
42
#include "chat/chat-room/chat-room.h"
43
#include "chat/chat-room/server-group-chat-room-p.h"
44
#include "conference/participant.h"
45
#include "conference/session/call-session-p.h"
46
#include "conference/session/call-session.h"
47
#include "conference/session/media-session-p.h"
48
#include "conference/session/media-session.h"
49
#include "core/core-p.h"
50

51 52
using namespace std;

53
using namespace LinphonePrivate;
54

55
static void register_failure(SalOp *op);
56

57
static void call_received(SalCallOp *h) {
58
	/* Look if this INVITE is for a call that has already been notified but broken because of network failure */
59
	LinphoneCore *lc = reinterpret_cast<LinphoneCore *>(h->get_sal()->get_user_pointer());
60
	if (L_GET_PRIVATE_FROM_C_OBJECT(lc)->inviteReplacesABrokenCall(h))
61
		return;
62

63
	LinphoneAddress *fromAddr = nullptr;
64
	const char *pAssertedId = sal_custom_header_find(h->get_recv_custom_header(), "P-Asserted-Identity");
65 66 67 68 69
	/* In some situation, better to trust the network rather than the UAC */
	if (lp_config_get_int(linphone_core_get_config(lc), "sip", "call_logs_use_asserted_id_instead_of_from", 0)) {
		if (pAssertedId) {
			LinphoneAddress *pAssertedIdAddr = linphone_address_new(pAssertedId);
			if (pAssertedIdAddr) {
70
				ms_message("Using P-Asserted-Identity [%s] instead of from [%s] for op [%p]", pAssertedId, h->get_from(), h);
71 72 73 74 75 76 77 78
				fromAddr = pAssertedIdAddr;
			} else
				ms_warning("Unsupported P-Asserted-Identity header for op [%p] ", h);
		} else
			ms_warning("No P-Asserted-Identity header found so cannot use it for op [%p] instead of from", h);
	}

	if (!fromAddr)
79 80
		fromAddr = linphone_address_new(h->get_from());
	LinphoneAddress *toAddr = linphone_address_new(h->get_to());
81

82 83 84 85 86 87 88 89
	if (_linphone_core_is_conference_creation(lc, toAddr)) {
		if (sal_address_has_param(h->get_remote_contact_address(), "text"))
			_linphone_core_create_server_group_chat_room(lc, h);
		// TODO: handle media conference creation if the "text" feature tag is not present
		linphone_address_unref(toAddr);
		linphone_address_unref(fromAddr);
		return;
	} else if (sal_address_has_param(h->get_remote_contact_address(), "text")) {
90
		shared_ptr<AbstractChatRoom> chatRoom = L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(
91
			ChatRoomId(IdentityAddress(h->get_to()), IdentityAddress(h->get_to()))
92 93 94
		);
		if (chatRoom) {
			L_GET_PRIVATE(static_pointer_cast<ServerGroupChatRoom>(chatRoom))->confirmJoining(h);
95 96
			linphone_address_unref(toAddr);
			linphone_address_unref(fromAddr);
97
			return;
98 99 100 101 102
		}
	} else {
		// TODO: handle media conference joining if the "text" feature tag is not present
	}

103 104 105
	/* First check if we can answer successfully to this invite */
	LinphonePresenceActivity *activity = nullptr;
	if ((linphone_presence_model_get_basic_status(lc->presence_model) == LinphonePresenceBasicStatusClosed)
106
		&& (activity = linphone_presence_model_get_activity(lc->presence_model))) {
107
		char *altContact = nullptr;
108 109
		switch (linphone_presence_activity_get_type(activity)) {
			case LinphonePresenceActivityPermanentAbsence:
110 111
				altContact = linphone_presence_model_get_contact(lc->presence_model);
				if (altContact) {
Ghislain MARY's avatar
Ghislain MARY committed
112 113
					SalErrorInfo sei;
					memset(&sei, 0, sizeof(sei));
114
					sal_error_info_set(&sei, SalReasonRedirect, "SIP", 0, nullptr, nullptr);
115 116
					SalAddress *altAddr = sal_address_new(altContact);
					h->decline_with_error_info(&sei, altAddr);
117
					ms_free(altContact);
118
					sal_address_unref(altAddr);
119 120 121
					LinphoneErrorInfo *ei = linphone_error_info_new();
					linphone_error_info_set(ei, nullptr, LinphoneReasonMovedPermanently, 302, "Moved permanently", nullptr);
					linphone_core_report_early_failed_call(lc, LinphoneCallIncoming, fromAddr, toAddr, ei);
122
					h->release();
123
					sal_error_info_reset(&sei);
124
					return;
125
				}
126 127
				break;
			default:
128
				/* Nothing special to be done */
129
				break;
130
		}
Simon Morlat's avatar
Simon Morlat committed
131
	}
132

133
	if (!L_GET_PRIVATE_FROM_C_OBJECT(lc)->canWeAddCall()) { /* Busy */
134
		h->decline(SalReasonBusy, nullptr);
135 136 137
		LinphoneErrorInfo *ei = linphone_error_info_new();
		linphone_error_info_set(ei, nullptr, LinphoneReasonBusy, 486, "Busy - too many calls", nullptr);
		linphone_core_report_early_failed_call(lc, LinphoneCallIncoming, fromAddr, toAddr, ei);
138
		h->release();
Simon Morlat's avatar
Simon Morlat committed
139 140
		return;
	}
141

142 143
	/* Check if I'm the caller */
	LinphoneAddress *fromAddressToSearchIfMe = nullptr;
144
	if (h->get_privacy() == SalPrivacyNone)
145 146 147 148 149
		fromAddressToSearchIfMe = linphone_address_clone(fromAddr);
	else if (pAssertedId)
		fromAddressToSearchIfMe = linphone_address_new(pAssertedId);
	else
		ms_warning("Hidden from identity, don't know if it's me");
150
	if (fromAddressToSearchIfMe && L_GET_PRIVATE_FROM_C_OBJECT(lc)->isAlreadyInCallWithAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(fromAddressToSearchIfMe))) {
151 152
		char *addr = linphone_address_as_string(fromAddr);
		ms_warning("Receiving a call while one with same address [%s] is initiated, refusing this one with busy message", addr);
153
		h->decline(SalReasonBusy, nullptr);
154 155 156
		LinphoneErrorInfo *ei = linphone_error_info_new();
		linphone_error_info_set(ei, nullptr, LinphoneReasonBusy, 486, "Busy - duplicated call", nullptr);
		linphone_core_report_early_failed_call(lc, LinphoneCallIncoming, fromAddr, toAddr, ei);
157
		h->release();
158
		linphone_address_unref(fromAddressToSearchIfMe);
159
		ms_free(addr);
Simon Morlat's avatar
Simon Morlat committed
160 161
		return;
	}
162 163
	if (fromAddressToSearchIfMe)
		linphone_address_unref(fromAddressToSearchIfMe);
164

165 166 167
	LinphoneCall *call = linphone_call_new_incoming(lc, fromAddr, toAddr, h);
	linphone_address_unref(fromAddr);
	linphone_address_unref(toAddr);
168
	L_GET_PRIVATE_FROM_C_OBJECT(call)->startIncomingNotification();
169 170
}

171 172
static void call_rejected(SalCallOp *h){
	LinphoneCore *lc=(LinphoneCore *)h->get_sal()->get_user_pointer();
173 174
	LinphoneErrorInfo *ei = linphone_error_info_new();
	linphone_error_info_from_sal_op(ei, h);
175
	linphone_core_report_early_failed_call(lc, LinphoneCallIncoming, linphone_address_new(h->get_from()), linphone_address_new(h->get_to()), ei);
176 177
}

178
static void call_ringing(SalOp *h) {
179
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(h->get_user_pointer());
180 181
	if (!session) return;
	L_GET_PRIVATE(session)->remoteRinging();
182 183
}

184 185 186 187 188
/*
 * could be reach :
 *  - when the call is accepted
 *  - when a request is accepted (pause, resume)
 */
189
static void call_accepted(SalOp *op) {
190
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
191 192 193
	if (!session) {
		ms_warning("call_accepted: CallSession no longer exists");
		return;
Simon Morlat's avatar
Simon Morlat committed
194
	}
195
	L_GET_PRIVATE(session)->accepted();
196 197
}

198
/* this callback is called when an incoming re-INVITE/ SIP UPDATE modifies the session*/
199
static void call_updating(SalOp *op, bool_t is_update) {
200
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
201 202 203
	if (!session) {
		ms_warning("call_updating: CallSession no longer exists");
		return;
204
	}
205
	L_GET_PRIVATE(session)->updating(!!is_update);
206 207 208
}


209
static void call_ack_received(SalOp *op, SalCustomHeader *ack) {
210
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
211 212
	if (!session) {
		ms_warning("call_ack_received(): no CallSession for which an ack is expected");
213 214
		return;
	}
215
	L_GET_PRIVATE(session)->ackReceived(reinterpret_cast<LinphoneHeaders *>(ack));
216 217
}

218

219
static void call_ack_being_sent(SalOp *op, SalCustomHeader *ack) {
220
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
221 222
	if (!session) {
		ms_warning("call_ack_being_sent(): no CallSession for which an ack is supposed to be sent");
223 224
		return;
	}
225
	L_GET_PRIVATE(session)->ackBeingSent(reinterpret_cast<LinphoneHeaders *>(ack));
226 227
}

228
static void call_terminated(SalOp *op, const char *from) {
229
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
230 231 232
	if (!session)
		return;
	L_GET_PRIVATE(session)->terminated();
233 234
}

235
static void call_failure(SalOp *op) {
236
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
237 238 239
	if (!session) {
		ms_warning("Failure reported on already terminated CallSession");
		return;
240
	}
241
	L_GET_PRIVATE(session)->failure();
242 243
}

244
static void call_released(SalOp *op) {
245
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
246 247 248 249
	if (!session) {
		/* We can get here when the core manages call at Sal level without creating a Call object. Typicially,
		 * when declining an incoming call with busy because maximum number of calls is reached. */
		return;
Simon Morlat's avatar
Simon Morlat committed
250
	}
251
	L_GET_PRIVATE(session)->setState(LinphoneCallReleased, "Call released");
252 253
}

254
static void call_cancel_done(SalOp *op) {
255 256 257 258
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
	if (!session) {
		ms_warning("Cancel done reported on already terminated CallSession");
		return;
259
	}
260
	L_GET_PRIVATE(session)->cancelDone();
261 262
}

jehan's avatar
jehan committed
263
static void auth_failure(SalOp *op, SalAuthInfo* info) {
264
	LinphoneCore *lc = reinterpret_cast<LinphoneCore *>(op->get_sal()->get_user_pointer());
265
	LinphoneAuthInfo *ai = NULL;
266

267 268
	if (info != NULL) {
		ai = (LinphoneAuthInfo*)_linphone_core_find_auth_info(lc, info->realm, info->username, info->domain, TRUE);
269
		if (ai){
270
			LinphoneAuthMethod method = info->mode == SalAuthModeHttpDigest ? LinphoneAuthHttpDigest : LinphoneAuthTls;
271
			LinphoneAuthInfo *auth_info = linphone_core_create_auth_info(lc, info->username, NULL, NULL, NULL, info->realm, info->domain);
272
			ms_message("%s/%s/%s/%s authentication fails.", info->realm, info->username, info->domain, info->mode == SalAuthModeHttpDigest ? "HttpDigest" : "Tls");
273
			/*ask again for password if auth info was already supplied but apparently not working*/
274
			linphone_core_notify_authentication_requested(lc, auth_info, method);
Ghislain MARY's avatar
Ghislain MARY committed
275
			linphone_auth_info_unref(auth_info);
276 277
			// Deprecated
			linphone_core_notify_auth_info_requested(lc, info->realm, info->username, info->domain);
278
		}
jehan's avatar
jehan committed
279
	}
280
}
281

282
static void register_success(SalOp *op, bool_t registered){
283
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig *)op->get_user_pointer();
284 285
	if (!cfg){
		ms_message("Registration success for deleted proxy config, ignored");
286 287
		return;
	}
288
	linphone_proxy_config_set_state(cfg, registered ? LinphoneRegistrationOk : LinphoneRegistrationCleared ,
289
									registered ? "Registration successful" : "Unregistration done");
290 291
}

292
static void register_failure(SalOp *op){
293 294
	LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)op->get_user_pointer();
	const SalErrorInfo *ei=op->get_error_info();
295
	const char *details=ei->full_string;
296 297 298 299 300 301

	if (cfg==NULL){
		ms_warning("Registration failed for unknown proxy config.");
		return ;
	}
	if (details==NULL)
302
		details="no response timeout";
303

304
	if ((ei->reason == SalReasonServiceUnavailable || ei->reason == SalReasonIOError)
305
			&& linphone_proxy_config_get_state(cfg) == LinphoneRegistrationOk) {
306
		linphone_proxy_config_set_state(cfg,LinphoneRegistrationProgress,"Service unavailable, retrying");
307 308 309
	} else {
		linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,details);
	}
310
	if (cfg->presence_publish_event){
311
		/*prevent publish to be sent now until registration gets successful*/
312 313
		linphone_event_terminate(cfg->presence_publish_event);
		cfg->presence_publish_event=NULL;
314 315
		cfg->send_publish=cfg->publish;
	}
316 317
}

318
static void vfu_request(SalOp *op) {
319
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
320 321 322 323 324 325
	if (!session)
		return;
	LinphonePrivate::MediaSession *mediaSession = dynamic_cast<LinphonePrivate::MediaSession *>(session);
	if (!mediaSession) {
		ms_warning("VFU request but no MediaSession!");
		return;
326
	}
327
	L_GET_PRIVATE(mediaSession)->sendVfu();
328 329
}

Ghislain MARY's avatar
Ghislain MARY committed
330 331 332 333 334 335 336 337 338 339
static void dtmf_received(SalOp *op, char dtmf) {
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
	if (!session)
		return;
	LinphonePrivate::MediaSession *mediaSession = dynamic_cast<LinphonePrivate::MediaSession *>(session);
	if (!mediaSession) {
		ms_warning("DTMF received but no MediaSession!");
		return;
	}
	L_GET_PRIVATE(mediaSession)->dtmfReceived(dtmf);
340 341
}

342 343 344 345 346 347 348 349 350 351 352 353
static void call_refer_received(SalOp *op, const SalAddress *referTo) {
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
	char *addrStr = sal_address_as_string_uri_only(referTo);
	Address referToAddr(addrStr);
	string method;
	if (referToAddr.isValid())
		method = referToAddr.getMethodParam();
	if (session && (method.empty() || (method == "INVITE"))) {
		L_GET_PRIVATE(session)->referred(referToAddr);
	} else {
		LinphoneCore *lc = reinterpret_cast<LinphoneCore *>(op->get_sal()->get_user_pointer());
		linphone_core_notify_refer_received(lc, addrStr);
354
	}
355
	bctbx_free(addrStr);
356 357
}

Ghislain MARY's avatar
Ghislain MARY committed
358
static void message_received(SalOp *op, const SalMessage *msg){
359 360
	LinphoneCore *lc=(LinphoneCore *)op->get_sal()->get_user_pointer();
	LinphoneCall *call=(LinphoneCall*)op->get_user_pointer();
361
	LinphoneReason reason = lc->chat_deny_code;
362 363
	if (reason == LinphoneReasonNone) {
		linphone_core_message_received(lc, op, msg);
364
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
365
	auto messageOp = dynamic_cast<SalMessageOpInterface *>(op);
366 367
	messageOp->reply(linphone_reason_to_sal(reason));
	if (!call) op->release();
368 369
}

370
static void parse_presence_requested(SalOp *op, const char *content_type, const char *content_subtype, const char *body, SalPresenceModel **result) {
371
	linphone_notify_parse_presence(content_type, content_subtype, body, result);
372 373
}

374
static void convert_presence_to_xml_requested(SalOp *op, SalPresenceModel *presence, const char *contact, char **content) {
375
	/*for backward compatibility because still used by notify. No loguer used for publish*/
376

377 378 379
	if(linphone_presence_model_get_presentity((LinphonePresenceModel*)presence) == NULL) {
		LinphoneAddress * presentity = linphone_address_new(contact);
		linphone_presence_model_set_presentity((LinphonePresenceModel*)presence, presentity);
jehan's avatar
jehan committed
380
		linphone_address_unref(presentity);
381 382
	}
	*content = linphone_presence_model_to_xml((LinphonePresenceModel*)presence);
383 384
}

385
static void notify_presence(SalOp *op, SalSubscribeStatus ss, SalPresenceModel *model, const char *msg){
386
	LinphoneCore *lc=(LinphoneCore *)op->get_sal()->get_user_pointer();
387
	linphone_notify_recv(lc,op,ss,model);
388 389
}

390 391
static void subscribe_presence_received(SalPresenceOp *op, const char *from){
	LinphoneCore *lc=(LinphoneCore *)op->get_sal()->get_user_pointer();
392
	linphone_subscription_new(lc,op,from);
393 394
}

395 396
static void subscribe_presence_closed(SalPresenceOp *op, const char *from){
	LinphoneCore *lc=(LinphoneCore *)op->get_sal()->get_user_pointer();
397
	linphone_subscription_closed(lc,op);
398 399
}

400
static void ping_reply(SalOp *op) {
401
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
402 403 404
	if (!session) {
		ms_warning("Ping reply without CallSession attached...");
		return;
Simon Morlat's avatar
Simon Morlat committed
405
	}
406
	L_GET_PRIVATE(session)->pingReply();
407
}
jehan's avatar
jehan committed
408

409
static bool_t fill_auth_info_with_client_certificate(LinphoneCore *lc, SalAuthInfo* sai) {
410 411
	const char *chain_file = linphone_core_get_tls_cert_path(lc);
	const char *key_file = linphone_core_get_tls_key_path(lc);
412

413
	if (key_file && chain_file) {
414
#ifndef _WIN32
415 416 417 418 419 420 421 422 423 424
		// optinal check for files
		struct stat st;
		if (stat(key_file, &st)) {
			ms_warning("No client certificate key found in %s", key_file);
			return FALSE;
		}
		if (stat(chain_file, &st)) {
			ms_warning("No client certificate chain found in %s", chain_file);
			return FALSE;
		}
425
#endif
426 427 428 429 430 431
		sal_certificates_chain_parse_file(sai, chain_file, SAL_CERTIFICATE_RAW_FORMAT_PEM);
		sal_signing_key_parse_file(sai, key_file, "");
	} else if (lc->tls_cert && lc->tls_key) {
		sal_certificates_chain_parse(sai, lc->tls_cert, SAL_CERTIFICATE_RAW_FORMAT_PEM);
		sal_signing_key_parse(sai, lc->tls_key, "");
	}
Simon Morlat's avatar
Simon Morlat committed
432
	return sai->certificates && sai->key;
433 434
}

jehan's avatar
jehan committed
435
static bool_t fill_auth_info(LinphoneCore *lc, SalAuthInfo* sai) {
436 437 438 439 440 441
	LinphoneAuthInfo *ai = NULL;
	if (sai->mode == SalAuthModeTls) {
		ai = (LinphoneAuthInfo*)_linphone_core_find_tls_auth_info(lc);
	} else {
		ai = (LinphoneAuthInfo*)_linphone_core_find_auth_info(lc,sai->realm,sai->username,sai->domain, FALSE);
	}
jehan's avatar
jehan committed
442
	if (ai) {
443
		if (sai->mode == SalAuthModeHttpDigest) {
444 445 446 447 448 449 450 451 452 453 454 455
			/*
			 * Compare algorithm of server(sai) with algorithm of client(ai), if they are not correspondant,
			 * exit. The default algorithm is MD5 if it's NULL.
			 */
			if (sai->algorithm && ai->algorithm) {
				if (strcmp(ai->algorithm, sai->algorithm))
					return TRUE;
			} else if (
				(ai->algorithm && strcmp(ai->algorithm, "MD5")) ||
				(sai->algorithm && strcmp(sai->algorithm, "MD5"))
			)
				return TRUE;
456

457 458 459
			sai->userid = ms_strdup(ai->userid ? ai->userid : ai->username);
			sai->password = ai->passwd?ms_strdup(ai->passwd) : NULL;
			sai->ha1 = ai->ha1 ? ms_strdup(ai->ha1) : NULL;
460 461 462 463 464 465 466 467 468 469 470
		} else if (sai->mode == SalAuthModeTls) {
			if (ai->tls_cert && ai->tls_key) {
				sal_certificates_chain_parse(sai, ai->tls_cert, SAL_CERTIFICATE_RAW_FORMAT_PEM);
				sal_signing_key_parse(sai, ai->tls_key, "");
			} else if (ai->tls_cert_path && ai->tls_key_path) {
				sal_certificates_chain_parse_file(sai, ai->tls_cert_path, SAL_CERTIFICATE_RAW_FORMAT_PEM);
				sal_signing_key_parse_file(sai, ai->tls_key_path, "");
			} else {
				fill_auth_info_with_client_certificate(lc, sai);
			}
		}
471

472 473 474 475 476
		if (sai->realm && !ai->realm){
			/*if realm was not known, then set it so that ha1 may eventually be calculated and clear text password dropped*/
			linphone_auth_info_set_realm(ai, sai->realm);
			linphone_core_write_auth_info(lc, ai);
		}
jehan's avatar
jehan committed
477 478
		return TRUE;
	} else {
479 480 481
		if (sai->mode == SalAuthModeTls) {
			return fill_auth_info_with_client_certificate(lc, sai);
		}
jehan's avatar
jehan committed
482 483 484
		return FALSE;
	}
}
jehan's avatar
jehan committed
485
static bool_t auth_requested(Sal* sal, SalAuthInfo* sai) {
486
	LinphoneCore *lc = (LinphoneCore *)sal->get_user_pointer();
487 488 489 490
	if (fill_auth_info(lc,sai)) {
		return TRUE;
	} else {
		LinphoneAuthMethod method = sai->mode == SalAuthModeHttpDigest ? LinphoneAuthHttpDigest : LinphoneAuthTls;
491 492
		LinphoneAuthInfo *ai = linphone_core_create_auth_info(lc, sai->username, NULL, NULL, NULL, sai->realm, sai->domain);
		linphone_core_notify_authentication_requested(lc, ai, method);
Ghislain MARY's avatar
Ghislain MARY committed
493
		linphone_auth_info_unref(ai);
494 495
		// Deprecated
		linphone_core_notify_auth_info_requested(lc, sai->realm, sai->username, sai->domain);
496
		if (fill_auth_info(lc, sai)) {
497
			return TRUE;
jehan's avatar
jehan committed
498 499 500 501
		}
		return FALSE;
	}
}
502

503 504 505 506 507
static void notify_refer(SalOp *op, SalReferStatus status) {
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
	if (!session) {
		ms_warning("Receiving notify_refer for unknown CallSession");
		return;
508
	}
509 510
	LinphoneCallState cstate;
	switch (status) {
511
		case SalReferTrying:
512 513
			cstate = LinphoneCallOutgoingProgress;
			break;
514
		case SalReferSuccess:
515 516
			cstate = LinphoneCallConnected;
			break;
517
		case SalReferFailed:
518 519
			cstate = LinphoneCallError;
			break;
520
		default:
521 522
			cstate = LinphoneCallError;
			break;
523
	}
524 525 526
	L_GET_PRIVATE(session)->setTransferState(cstate);
	if (cstate == LinphoneCallConnected)
		session->terminate(); // Automatically terminate the call as the transfer is complete
527 528
}

Ghislain MARY's avatar
Ghislain MARY committed
529
static LinphoneChatMessageState chatStatusSal2Linphone(SalMessageDeliveryStatus status){
530
	switch(status){
Ghislain MARY's avatar
Ghislain MARY committed
531
		case SalMessageDeliveryInProgress:
532
			return LinphoneChatMessageStateInProgress;
Ghislain MARY's avatar
Ghislain MARY committed
533
		case SalMessageDeliveryDone:
534
			return LinphoneChatMessageStateDelivered;
Ghislain MARY's avatar
Ghislain MARY committed
535
		case SalMessageDeliveryFailed:
536 537 538 539 540
			return LinphoneChatMessageStateNotDelivered;
	}
	return LinphoneChatMessageStateIdle;
}

Ghislain MARY's avatar
Ghislain MARY committed
541
static void message_delivery_update(SalOp *op, SalMessageDeliveryStatus status){
542
	LinphoneChatMessage *chat_msg=(LinphoneChatMessage* )op->get_user_pointer();
543 544 545 546 547

	if (chat_msg == NULL) {
		// Do not handle delivery status for isComposing messages.
		return;
	}
548
	// check that the message does not belong to an already destroyed chat room - if so, do not invoke callbacks
549
	if (linphone_chat_message_get_chat_room(chat_msg) != NULL) {
550
		linphone_chat_message_update_state(chat_msg, chatStatusSal2Linphone(status));
551 552 553
	}
}

554 555 556 557 558
static void info_received(SalOp *op, SalBodyHandler *body_handler) {
	LinphonePrivate::CallSession *session = reinterpret_cast<LinphonePrivate::CallSession *>(op->get_user_pointer());
	if (!session)
		return;
	L_GET_PRIVATE(session)->infoReceived(body_handler);
Simon Morlat's avatar
Simon Morlat committed
559 560
}

561
static void subscribe_response(SalOp *op, SalSubscribeStatus status, int will_retry){
562
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
563

564
	if (lev==NULL) return;
565

566 567 568 569 570
	if (status==SalSubscribeActive){
		linphone_event_set_state(lev,LinphoneSubscriptionActive);
	}else if (status==SalSubscribePending){
		linphone_event_set_state(lev,LinphoneSubscriptionPending);
	}else{
571
		if (will_retry){
Simon Morlat's avatar
Simon Morlat committed
572 573 574
			linphone_event_set_state(lev,LinphoneSubscriptionOutgoingProgress);
		}
		else linphone_event_set_state(lev,LinphoneSubscriptionError);
575 576 577
	}
}

578 579 580
static void notify(SalSubscribeOp *op, SalSubscribeStatus st, const char *eventname, SalBodyHandler *body_handler){
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
	LinphoneCore *lc=(LinphoneCore *)op->get_sal()->get_user_pointer();
581 582
	bool_t out_of_dialog = (lev==NULL);
	if (out_of_dialog) {
583 584
		/*out of dialog notify */
		lev = linphone_event_new_with_out_of_dialog_op(lc,op,LinphoneSubscriptionOutgoing,eventname);
585
	}
586
	{
587
		LinphoneContent *ct=linphone_content_from_sal_body_handler(body_handler);
Ghislain MARY's avatar
Ghislain MARY committed
588 589 590 591
		if (ct) {
			linphone_core_notify_notify_received(lc,lev,eventname,ct);
			linphone_content_unref(ct);
		}
592
	}
593 594 595
	if (out_of_dialog){
		/*out of dialog NOTIFY do not create an implicit subscription*/
		linphone_event_set_state(lev, LinphoneSubscriptionTerminated);
596
	}else if (st!=SalSubscribeNone){
597 598 599 600
		linphone_event_set_state(lev,linphone_subscription_state_from_sal(st));
	}
}

601 602 603
static void subscribe_received(SalSubscribeOp *op, const char *eventname, const SalBodyHandler *body_handler){
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
	LinphoneCore *lc=(LinphoneCore *)op->get_sal()->get_user_pointer();
604

605
	if (lev==NULL) {
Simon Morlat's avatar
Simon Morlat committed
606
		lev=linphone_event_new_with_op(lc,op,LinphoneSubscriptionIncoming,eventname);
607 608 609 610
		linphone_event_set_state(lev,LinphoneSubscriptionIncomingReceived);
	}else{
		/*subscribe refresh, unhandled*/
	}
611

612 613
}

614
static void incoming_subscribe_closed(SalOp *op){
615
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
616

617 618 619
	linphone_event_set_state(lev,LinphoneSubscriptionTerminated);
}

620
static void on_publish_response(SalOp* op){
621 622
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
	const SalErrorInfo *ei=op->get_error_info();
623

624
	if (lev==NULL) return;
625
	if (ei->reason==SalReasonNone){
626 627
		if (!lev->terminating)
			linphone_event_set_publish_state(lev,LinphonePublishOk);
628
		else
629 630
			linphone_event_set_publish_state(lev,LinphonePublishCleared);
	}else{
Simon Morlat's avatar
Simon Morlat committed
631 632 633 634 635
		if (lev->publish_state==LinphonePublishOk){
			linphone_event_set_publish_state(lev,LinphonePublishProgress);
		}else{
			linphone_event_set_publish_state(lev,LinphonePublishError);
		}
636 637 638
	}
}

639

640
static void on_expire(SalOp *op){
641
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
642

643
	if (lev==NULL) return;
644

645 646
	if (linphone_event_get_publish_state(lev)==LinphonePublishOk){
		linphone_event_set_publish_state(lev,LinphonePublishExpiring);
647 648
	}else if (linphone_event_get_subscription_state(lev)==LinphoneSubscriptionActive){
		linphone_event_set_state(lev,LinphoneSubscriptionExpiring);
649 650 651
	}
}

652
static void on_notify_response(SalOp *op){
653
	LinphoneEvent *lev=(LinphoneEvent*)op->get_user_pointer();
654 655 656 657 658 659

	if (lev==NULL) return;
	/*this is actually handling out of dialogs notify - for the moment*/
	if (!lev->is_out_of_dialog_op) return;
	switch (linphone_event_get_subscription_state(lev)){
		case LinphoneSubscriptionIncomingReceived:
660
			if (op->get_error_info()->reason == SalReasonNone){
661 662 663 664 665 666 667 668 669 670
				linphone_event_set_state(lev, LinphoneSubscriptionTerminated);
			}else{
				linphone_event_set_state(lev, LinphoneSubscriptionError);
			}
		break;
		default:
			ms_warning("Unhandled on_notify_response() case %s", linphone_subscription_state_to_string(linphone_event_get_subscription_state(lev)));
	}
}

671
static void refer_received(SalOp *op, const SalAddress *refer_to){
672
	if (sal_address_has_param(refer_to, "text")) {
673 674 675
		char *refer_uri = sal_address_as_string(refer_to);
		LinphonePrivate::Address addr(refer_uri);
		bctbx_free(refer_uri);
676 677
		if (addr.isValid()) {
			LinphoneCore *lc = reinterpret_cast<LinphoneCore *>(op->get_sal()->get_user_pointer());
678
			if (addr.hasUriParam("method") && (addr.getUriParamValue("method") == "BYE")) {
679 680
				if (linphone_core_conference_server_enabled(lc)) {
					// Removal of a participant at the server side
681
					shared_ptr<AbstractChatRoom> chatRoom = L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(
682
						ChatRoomId(IdentityAddress(op->get_to()), IdentityAddress(op->get_to()))
683
					);
684
					if (chatRoom) {
685
						std::shared_ptr<Participant> participant = chatRoom->findParticipant(IdentityAddress(op->get_from()));
686 687 688 689
						if (!participant || !participant->isAdmin()) {
							static_cast<SalReferOp *>(op)->reply(SalReasonDeclined);
							return;
						}
690
						participant = chatRoom->findParticipant(addr);
691
						if (participant)
692
							chatRoom->removeParticipant(participant);
693 694 695 696 697
						static_cast<SalReferOp *>(op)->reply(SalReasonNone);
						return;
					}
				} else {
					// The server asks a participant to leave a chat room
698
					LinphoneChatRoom *cr = L_GET_C_BACK_PTR(
699
						L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(ChatRoomId(addr, IdentityAddress(op->get_to())))
700
					);
701 702 703 704 705 706
					if (cr) {
						L_GET_CPP_PTR_FROM_C_OBJECT(cr)->leave();
						static_cast<SalReferOp *>(op)->reply(SalReasonNone);
						return;
					}
					static_cast<SalReferOp *>(op)->reply(SalReasonDeclined);
707 708
				}
			} else if (addr.hasParam("admin")) {
709
				LinphoneChatRoom *cr = L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(
710
					ChatRoomId(IdentityAddress(op->get_to()), IdentityAddress(op->get_to()))
711
				));
712
				if (cr) {
713 714 715 716 717 718 719
					Address fromAddr(op->get_from());
					std::shared_ptr<Participant> participant = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->findParticipant(fromAddr);
					if (!participant || !participant->isAdmin()) {
						static_cast<SalReferOp *>(op)->reply(SalReasonDeclined);
						return;
					}
					participant = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->findParticipant(addr);
720 721 722 723 724 725 726 727
					if (participant) {
						bool value = Utils::stob(addr.getParamValue("admin"));
						L_GET_CPP_PTR_FROM_C_OBJECT(cr)->setParticipantAdminStatus(participant, value);
					}
					static_cast<SalReferOp *>(op)->reply(SalReasonNone);
					return;
				}
			} else {
728
				LinphoneChatRoom *cr = L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(ChatRoomId(addr, IdentityAddress(op->get_to()))));
729
				if (!cr)
730
					cr = _linphone_client_group_chat_room_new(lc, addr.asString().c_str(), nullptr, FALSE);
731 732 733
				L_GET_CPP_PTR_FROM_C_OBJECT(cr)->join();
				static_cast<SalReferOp *>(op)->reply(SalReasonNone);
				return;
734 735 736 737
			}
		}
	}
	static_cast<SalReferOp *>(op)->reply(SalReasonDeclined);
738 739
}

740
Sal::Callbacks linphone_sal_callbacks={
741
	call_received,
742
	call_rejected,
743 744
	call_ringing,
	call_accepted,
745 746
	call_ack_received,
	call_ack_being_sent,
747
	call_updating,
748 749
	call_terminated,
	call_failure,
750
	call_released,
751
	call_cancel_done,