friendlist.c 24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
linphone
Copyright (C) 2010-2015 Belledonne Communications SARL

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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "linphonecore.h"
#include "private.h"

23
#include <polarssl/md5.h>
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneFriendListCbs);

BELLE_SIP_INSTANCIATE_VPTR(LinphoneFriendListCbs, belle_sip_object_t,
	NULL, // destroy
	NULL, // clone
	NULL, // Marshall
	FALSE
);

static LinphoneFriendListCbs * linphone_friend_list_cbs_new(void) {
	return belle_sip_object_new(LinphoneFriendListCbs);
}

LinphoneFriendListCbs * linphone_friend_list_get_callbacks(const LinphoneFriendList *list) {
	return list->cbs;
}

LinphoneFriendListCbs * linphone_friend_list_cbs_ref(LinphoneFriendListCbs *cbs) {
	belle_sip_object_ref(cbs);
	return cbs;
}

void linphone_friend_list_cbs_unref(LinphoneFriendListCbs *cbs) {
	belle_sip_object_unref(cbs);
}

void *linphone_friend_list_cbs_get_user_data(const LinphoneFriendListCbs *cbs) {
	return cbs->user_data;
}

void linphone_friend_list_cbs_set_user_data(LinphoneFriendListCbs *cbs, void *ud) {
	cbs->user_data = ud;
}

LinphoneFriendListContactCreatedCb linphone_friend_list_cbs_get_contact_created(const LinphoneFriendListCbs *cbs) {
	return cbs->contact_created_cb;
}

void linphone_friend_list_cbs_set_contact_created(LinphoneFriendListCbs *cbs, LinphoneFriendListContactCreatedCb cb) {
	cbs->contact_created_cb = cb;
}

LinphoneFriendListContactDeletedCb linphone_friend_list_cbs_get_contact_deleted(const LinphoneFriendListCbs *cbs) {
	return cbs->contact_deleted_cb;
}

void linphone_friend_list_cbs_set_contact_deleted(LinphoneFriendListCbs *cbs, LinphoneFriendListContactDeletedCb cb) {
	cbs->contact_deleted_cb = cb;
}
74

75 76 77 78 79 80 81 82
LinphoneFriendListContactUpdatedCb linphone_friend_list_cbs_get_contact_updated(const LinphoneFriendListCbs *cbs) {
	return cbs->contact_updated_cb;
}

void linphone_friend_list_cbs_set_contact_updated(LinphoneFriendListCbs *cbs, LinphoneFriendListContactUpdatedCb cb) {
	cbs->contact_updated_cb = cb;
}

83 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
static char * create_resource_list_xml(const LinphoneFriendList *list) {
	char *xml_content = NULL;
	MSList *elem;
	xmlBufferPtr buf;
	xmlTextWriterPtr writer;
	int err;

	if (ms_list_size(list->friends) <= 0) return NULL;

	buf = xmlBufferCreate();
	if (buf == NULL) {
		ms_error("%s: Error creating the XML buffer", __FUNCTION__);
		return NULL;
	}
	writer = xmlNewTextWriterMemory(buf, 0);
	if (writer == NULL) {
		ms_error("%s: Error creating the XML writer", __FUNCTION__);
		return NULL;
	}

	xmlTextWriterSetIndent(writer,1);
	err = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
	if (err >= 0) {
		err = xmlTextWriterStartElementNS(writer, NULL, (const xmlChar *)"resource-lists", (const xmlChar *)"urn:ietf:params:xml:ns:resource-lists");
	}
	if (err >= 0) {
		err = xmlTextWriterWriteAttributeNS(writer, (const xmlChar *)"xmlns", (const xmlChar *)"xsi",
						    NULL, (const xmlChar *)"http://www.w3.org/2001/XMLSchema-instance");
	}

	if (err>= 0) {
		err = xmlTextWriterStartElement(writer, (const xmlChar *)"list");
	}
	for (elem = list->friends; elem != NULL; elem = elem->next) {
117 118
		LinphoneFriend *lf = (LinphoneFriend *)elem->data;
		char *uri = linphone_address_as_string_uri_only(lf->uri);
119 120 121 122 123 124 125 126 127 128
		if (err >= 0) {
			err = xmlTextWriterStartElement(writer, (const xmlChar *)"entry");
		}
		if (err >= 0) {
			err = xmlTextWriterWriteAttribute(writer, (const xmlChar *)"uri", (const xmlChar *)uri);
		}
		if (err >= 0) {
			/* Close the "entry" element. */
			err = xmlTextWriterEndElement(writer);
		}
Ghislain MARY's avatar
Ghislain MARY committed
129
		if (uri) ms_free(uri);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	}
	if (err >= 0) {
		/* Close the "list" element. */
		err = xmlTextWriterEndElement(writer);
	}

	if (err >= 0) {
		/* Close the "resource-lists" element. */
		err = xmlTextWriterEndElement(writer);
	}
	if (err >= 0) {
		err = xmlTextWriterEndDocument(writer);
	}
	if (err > 0) {
		/* xmlTextWriterEndDocument returns the size of the content. */
		xml_content = ms_strdup((char *)buf->content);
	}
	xmlFreeTextWriter(writer);
	xmlBufferFree(buf);

	return xml_content;
}

153 154 155 156 157 158
static void linphone_friend_list_parse_multipart_related_body(LinphoneFriendList *list, const LinphoneContent *body, const char *first_part_body) {
	xmlparsing_context_t *xml_ctx = linphone_xmlparsing_context_new();
	xmlSetGenericErrorFunc(xml_ctx, linphone_xmlparsing_genericxml_error);
	xml_ctx->doc = xmlReadDoc((const unsigned char*)first_part_body, 0, NULL, 0);
	if (xml_ctx->doc != NULL) {
		char xpath_str[MAX_XPATH_LENGTH];
159
		LinphoneFriend *lf;
160 161
		LinphoneContent *presence_part;
		xmlXPathObjectPtr resource_object;
162 163
		const char *version_str = NULL;
		const char *full_state_str = NULL;
164
		const char *uri = NULL;
165 166
		bool_t full_state = FALSE;
		int version;
167 168 169 170
		int i;

		if (linphone_create_xml_xpath_context(xml_ctx) < 0) goto end;
		xmlXPathRegisterNs(xml_ctx->xpath_ctx, (const xmlChar *)"rlmi", (const xmlChar *)"urn:ietf:params:xml:ns:rlmi");
171 172 173 174 175 176 177

		version_str = linphone_get_xml_attribute_text_content(xml_ctx, "/rlmi:list", "version");
		if (version_str == NULL) {
			ms_warning("rlmi+xml: No version attribute in list");
			goto end;
		}
		version = atoi(version_str);
Ghislain MARY's avatar
Ghislain MARY committed
178
		linphone_free_xml_text_content(version_str);
179 180
		if (version < list->expected_notification_version) {
			ms_warning("rlmi+xml: Discarding received notification with version %d because %d was expected", version, list->expected_notification_version);
181
			linphone_friend_list_update_subscriptions(list, NULL, FALSE); /* Refresh subscription to get new full state notify. */
182 183 184 185 186 187 188 189 190 191 192
			goto end;
		}

		full_state_str = linphone_get_xml_attribute_text_content(xml_ctx, "/rlmi:list", "fullState");
		if (full_state_str == NULL) {
			ms_warning("rlmi+xml: No fullState attribute in list");
			goto end;
		}
		if ((strcmp(full_state_str, "true") == 0) || (strcmp(full_state_str, "1") == 0)) {
			MSList *l = list->friends;
			for (; l != NULL; l = l->next) {
193 194
				lf = (LinphoneFriend *)l->data;
				linphone_friend_set_presence_model(lf, NULL);
195 196 197 198 199 200 201 202 203 204
			}
			full_state = TRUE;
		}
		linphone_free_xml_text_content(full_state_str);
		if ((list->expected_notification_version == 0) && (full_state == FALSE)) {
			ms_warning("rlmi+xml: Notification with version 0 is not full state, this is not valid");
			goto end;
		}
		list->expected_notification_version = version + 1;

205 206 207 208 209 210
		resource_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/rlmi:list/rlmi:resource");
		if ((resource_object != NULL) && (resource_object->nodesetval != NULL)) {
			for (i = 1; i <= resource_object->nodesetval->nodeNr; i++) {
				snprintf(xpath_str, sizeof(xpath_str), "/rlmi:list/rlmi:resource[%i]/@uri", i);
				uri = linphone_get_xml_text_content(xml_ctx, xpath_str);
				if (uri == NULL) continue;
211 212
				lf = linphone_friend_list_find_friend_by_uri(list, uri);
				if (lf != NULL) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
					const char *state = NULL;
					snprintf(xpath_str, sizeof(xpath_str),"/rlmi:list/rlmi:resource[%i]/rlmi:instance/@state", i);
					state = linphone_get_xml_text_content(xml_ctx, xpath_str);
					if ((state != NULL) && (strcmp(state, "active") == 0)) {
						const char *cid = NULL;
						snprintf(xpath_str, sizeof(xpath_str),"/rlmi:list/rlmi:resource[%i]/rlmi:instance/@cid", i);
						cid = linphone_get_xml_text_content(xml_ctx, xpath_str);
						if (cid != NULL) {
							presence_part = linphone_content_find_part_by_header(body, "Content-Id", cid);
							if (presence_part == NULL) {
								ms_warning("rlmi+xml: Cannot find part with Content-Id: %s", cid);
							} else {
								SalPresenceModel *presence = NULL;
								linphone_notify_parse_presence(linphone_content_get_type(presence_part), linphone_content_get_subtype(presence_part), linphone_content_get_string_buffer(presence_part), &presence);
								if (presence != NULL) {
228 229
									lf->presence_received = TRUE;
									linphone_friend_set_presence_model(lf, (LinphonePresenceModel *)presence);
230
									if (full_state == FALSE) {
231
										linphone_core_notify_notify_presence_received(list->lc, lf);
232
									}
233
								}
Ghislain MARY's avatar
Ghislain MARY committed
234
								linphone_content_unref(presence_part);
235 236 237 238 239
							}
						}
						if (cid != NULL) linphone_free_xml_text_content(cid);
					}
					if (state != NULL) linphone_free_xml_text_content(state);
240
					lf->subscribe_active = TRUE;
241 242 243 244 245
				}
				linphone_free_xml_text_content(uri);
			}
		}
		if (resource_object != NULL) xmlXPathFreeObject(resource_object);
246 247 248 249

		if (full_state == TRUE) {
			MSList *l = list->friends;
			for (; l != NULL; l = l->next) {
250 251 252
				lf = (LinphoneFriend *)l->data;
				if (linphone_friend_is_presence_received(lf) == TRUE) {
					linphone_core_notify_notify_presence_received(list->lc, lf);
253 254 255
				}
			}
		}
256 257 258 259 260 261 262 263
	} else {
		ms_warning("Wrongly formatted rlmi+xml body: %s", xml_ctx->errorBuffer);
	}

end:
	linphone_xmlparsing_context_destroy(xml_ctx);
}

264 265 266 267
static bool_t linphone_friend_list_has_subscribe_inactive(const LinphoneFriendList *list) {
	MSList *l = list->friends;
	bool_t has_subscribe_inactive = FALSE;
	for (; l != NULL; l = l->next) {
268 269
		LinphoneFriend *lf = (LinphoneFriend *)l->data;
		if (lf->subscribe_active != TRUE) {
270 271 272 273 274 275 276
			has_subscribe_inactive = TRUE;
			break;
		}
	}
	return has_subscribe_inactive;
}

277 278
static LinphoneFriendList * linphone_friend_list_new(void) {
	LinphoneFriendList *list = belle_sip_object_new(LinphoneFriendList);
279
	list->cbs = linphone_friend_list_cbs_new();
280 281 282 283
	belle_sip_object_ref(list);
	return list;
}

284 285 286
static void linphone_friend_list_destroy(LinphoneFriendList *list) {
	if (list->display_name != NULL) ms_free(list->display_name);
	if (list->rls_uri != NULL) ms_free(list->rls_uri);
287
	if (list->content_digest != NULL) ms_free(list->content_digest);
288
	if (list->event != NULL) linphone_event_unref(list->event);
289 290
	if (list->uri != NULL) ms_free(list->uri);
	if (list->cbs) linphone_friend_list_cbs_unref(list->cbs);
Sylvain Berfini's avatar
Sylvain Berfini committed
291 292
	if (list->dirty_friends_to_update) list->dirty_friends_to_update = ms_list_free_with_data(list->dirty_friends_to_update, (void (*)(void *))linphone_friend_unref);
	if (list->friends) list->friends = ms_list_free_with_data(list->friends, (void (*)(void *))_linphone_friend_release);
293 294 295 296 297 298 299 300 301 302 303 304
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneFriendList);

BELLE_SIP_INSTANCIATE_VPTR(LinphoneFriendList, belle_sip_object_t,
	(belle_sip_object_destroy_t)linphone_friend_list_destroy,
	NULL, // clone
	NULL, // marshal
	TRUE
);


305 306 307
LinphoneFriendList * linphone_core_create_friend_list(LinphoneCore *lc) {
	LinphoneFriendList *list = linphone_friend_list_new();
	list->lc = lc;
308 309 310 311 312 313 314 315
	return list;
}

LinphoneFriendList * linphone_friend_list_ref(LinphoneFriendList *list) {
	belle_sip_object_ref(list);
	return list;
}

316 317 318
void _linphone_friend_list_release(LinphoneFriendList *list){
	/*drops all references to core and unref*/
	list->lc = NULL;
Simon Morlat's avatar
Simon Morlat committed
319 320 321 322
	if (list->event != NULL) {
		linphone_event_unref(list->event);
		list->event = NULL;
	}
323 324 325 326
	if (list->cbs) {
		linphone_friend_list_cbs_unref(list->cbs);
		list->cbs = NULL;
	}
327 328 329 330 331 332
	if (list->dirty_friends_to_update) {
		list->dirty_friends_to_update = ms_list_free_with_data(list->dirty_friends_to_update, (void (*)(void *))linphone_friend_unref);
	}
	if (list->friends) {
		list->friends = ms_list_free_with_data(list->friends, (void (*)(void *))_linphone_friend_release);
	}
333
	linphone_friend_list_unref(list);
334 335
}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
void linphone_friend_list_unref(LinphoneFriendList *list) {
	belle_sip_object_unref(list);
}

void * linphone_friend_list_get_user_data(const LinphoneFriendList *list) {
	return list->user_data;
}

void linphone_friend_list_set_user_data(LinphoneFriendList *list, void *ud) {
	list->user_data = ud;
}

const char * linphone_friend_list_get_display_name(const LinphoneFriendList *list) {
	return list->display_name;
}

void linphone_friend_list_set_display_name(LinphoneFriendList *list, const char *display_name) {
	if (list->display_name != NULL) {
		ms_free(list->display_name);
		list->display_name = NULL;
	}
	if (display_name != NULL) {
		list->display_name = ms_strdup(display_name);
	}
}

const char * linphone_friend_list_get_rls_uri(const LinphoneFriendList *list) {
	return list->rls_uri;
}

void linphone_friend_list_set_rls_uri(LinphoneFriendList *list, const char *rls_uri) {
	if (list->rls_uri != NULL) {
		ms_free(list->rls_uri);
		list->rls_uri = NULL;
	}
	if (rls_uri != NULL) {
		list->rls_uri = ms_strdup(rls_uri);
	}
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
LinphoneFriendListStatus _linphone_friend_list_add_friend(LinphoneFriendList *list, LinphoneFriend *lf) {
	if (lf->uri == NULL || lf->friend_list) {
		if (!lf->uri)
			ms_error("linphone_friend_list_add_friend(): invalid friend, no sip uri");
		if (lf->friend_list)
			ms_error("linphone_friend_list_add_friend(): invalid friend, already in list");
		return LinphoneFriendListInvalidFriend;
	}
	list->friends = ms_list_append(list->friends, linphone_friend_ref(lf));
	lf->friend_list = list;
	lf->lc = list->lc;
#ifdef FRIENDS_SQL_STORAGE_ENABLED
	linphone_core_store_friend_in_db(lf->lc, lf);
#endif
	return LinphoneFriendListOK;
}

393
LinphoneFriendListStatus linphone_friend_list_add_friend(LinphoneFriendList *list, LinphoneFriend *lf) {
Sylvain Berfini's avatar
Sylvain Berfini committed
394 395 396
	if (!list || !lf->uri || lf->friend_list) {
		if (!list)
			ms_error("linphone_friend_list_add_friend(): invalid list, null");
397
		if (!lf->uri)
398
			ms_error("linphone_friend_list_add_friend(): invalid friend, no sip uri");
399
		if (lf->friend_list)
400
			ms_error("linphone_friend_list_add_friend(): invalid friend, already in list");
Simon Morlat's avatar
Simon Morlat committed
401 402
		return LinphoneFriendListInvalidFriend;
	}
403
	if (ms_list_find(list->friends, lf) != NULL) {
404
		char *tmp = NULL;
405
		const LinphoneAddress *addr = linphone_friend_get_address(lf);
406 407 408 409
		if (addr) tmp = linphone_address_as_string(addr);
		ms_warning("Friend %s already in list [%s], ignored.", tmp ? tmp : "unknown", list->display_name);
		if (tmp) ms_free(tmp);
	} else {
410
		return linphone_friend_list_import_friend(list, lf);
411 412 413 414
	}
	return LinphoneFriendListOK;
}

415
LinphoneFriendListStatus linphone_friend_list_import_friend(LinphoneFriendList *list, LinphoneFriend *lf) {
Sylvain Berfini's avatar
Sylvain Berfini committed
416 417 418 419
	if (!lf->uri) {
		ms_error("linphone_friend_list_import_friend(): invalid friend, no sip uri");
		return LinphoneFriendListInvalidFriend;
	}
420
	lf->friend_list = list;
421
	lf->lc = list->lc;
Sylvain Berfini's avatar
Sylvain Berfini committed
422 423
	list->friends = ms_list_append(list->friends, linphone_friend_ref(lf));
	list->dirty_friends_to_update = ms_list_append(list->dirty_friends_to_update, linphone_friend_ref(lf));
424 425 426
#ifdef FRIENDS_SQL_STORAGE_ENABLED
	linphone_core_store_friend_in_db(lf->lc, lf);
#endif
427 428 429
	return LinphoneFriendListOK;
}

430 431 432 433
static void carddav_done(LinphoneCardDavContext *cdc, bool_t success, const char *msg) {
	linphone_carddav_context_destroy(cdc);
}

434 435 436 437 438 439 440 441 442 443
LinphoneFriendListStatus _linphone_friend_list_remove_friend(LinphoneFriendList *list, MSList *elem, LinphoneFriend *lf) {
	if (!elem) {
		return LinphoneFriendListNonExistentFriend;
	}
	lf->friend_list = NULL;
	linphone_friend_unref(lf);
	list->friends = ms_list_remove_link(list->friends, elem);
	return LinphoneFriendListOK;
}

444 445
LinphoneFriendListStatus linphone_friend_list_remove_friend(LinphoneFriendList *list, LinphoneFriend *lf) {
	MSList *elem = ms_list_find(list->friends, lf);
446
	LinphoneCardDavContext *cdc = linphone_carddav_context_new(list);
447
	if (elem == NULL) return LinphoneFriendListNonExistentFriend;
448 449

#ifdef FRIENDS_SQL_STORAGE_ENABLED
450
	linphone_core_remove_friend_from_db(lf->lc, lf);
451
#endif
452 453 454 455
	if (cdc) {
		cdc->sync_done_cb = carddav_done;
		linphone_carddav_delete_vcard(cdc, lf);
	}
456
	
457
	return _linphone_friend_list_remove_friend(list, elem, lf);
458
}
459

460 461 462 463 464 465 466 467 468 469 470 471 472
void linphone_friend_list_update_dirty_friends(LinphoneFriendList *list) {
	LinphoneCardDavContext *cdc = linphone_carddav_context_new(list);
	MSList *dirty_friends = list->dirty_friends_to_update;
	
	if (cdc) {
		cdc->sync_done_cb = carddav_done;
		while (dirty_friends) {
			LinphoneFriend *lf = (LinphoneFriend *)dirty_friends->data;
			if (lf) {
				linphone_carddav_put_vcard(cdc, lf);
			}
			dirty_friends = ms_list_next(dirty_friends);
		}
473
		list->dirty_friends_to_update = ms_list_free_with_data(list->dirty_friends_to_update, (void (*)(void *))linphone_friend_unref);
474 475 476 477
	}
}

static void carddav_created(LinphoneCardDavContext *cdc, LinphoneFriend *lf) {
478
	if (cdc) {
479
		LinphoneFriendList *lfl = cdc->friend_list;
480
		_linphone_friend_list_add_friend(lfl, lf);
481
		if (cdc->friend_list->cbs->contact_created_cb) {
482
			cdc->friend_list->cbs->contact_created_cb(lfl, lf);
483
		}
484 485 486 487
	}
}

static void carddav_removed(LinphoneCardDavContext *cdc, LinphoneFriend *lf) {
488
	if (cdc) {
489
		LinphoneFriendList *lfl = cdc->friend_list;
490
		linphone_friend_list_remove_friend(lfl, lf);
491
		if (cdc->friend_list->cbs->contact_deleted_cb) {
492
			cdc->friend_list->cbs->contact_deleted_cb(lfl, lf);
493
		}
494 495 496
	}
}

497
static void carddav_updated(LinphoneCardDavContext *cdc, LinphoneFriend *lf_new, LinphoneFriend *lf_old) {
498 499 500 501 502 503 504 505 506 507 508
	if (cdc) {
		LinphoneFriendList *lfl = cdc->friend_list;
		MSList *elem = ms_list_find(lfl->friends, lf_old);
		if (elem) {
			_linphone_friend_list_remove_friend(lfl, elem, lf_old);
		}
		_linphone_friend_list_add_friend(lfl, lf_new);
		if (cdc->friend_list->cbs->contact_updated_cb) {
			cdc->friend_list->cbs->contact_updated_cb(lfl, lf_new, lf_old);
		}
	}
509 510
}

511 512 513 514 515 516
void linphone_friend_list_synchronize_friends_from_server(LinphoneFriendList *list) {
	LinphoneCardDavContext *cdc = linphone_carddav_context_new(list);
	
	if (cdc) {
		cdc->contact_created_cb = carddav_created;
		cdc->contact_removed_cb = carddav_removed;
517
		cdc->contact_updated_cb = carddav_updated;
518 519 520 521 522
		cdc->sync_done_cb = carddav_done;
		linphone_carddav_synchronize(cdc);
	}
}

523
LinphoneFriend * linphone_friend_list_find_friend_by_address(const LinphoneFriendList *list, const LinphoneAddress *address) {
524
	LinphoneFriend *lf = NULL;
525 526
	const MSList *elem;
	for (elem = list->friends; elem != NULL; elem = elem->next) {
527 528 529
		lf = (LinphoneFriend *)elem->data;
		if (linphone_address_weak_equal(lf->uri, address))
			return lf;
530 531 532 533 534 535
	}
	return NULL;
}

LinphoneFriend * linphone_friend_list_find_friend_by_uri(const LinphoneFriendList *list, const char *uri) {
	LinphoneAddress *address = linphone_address_new(uri);
536
	LinphoneFriend *lf = address ? linphone_friend_list_find_friend_by_address(list, address) : NULL;
537
	if (address) linphone_address_unref(address);
538
	return lf;
539 540 541 542 543 544
}

LinphoneFriend * linphone_friend_list_find_friend_by_ref_key(const LinphoneFriendList *list, const char *ref_key) {
	const MSList *elem;
	if (ref_key == NULL) return NULL;
	for (elem = list->friends; elem != NULL; elem = elem->next) {
545 546
		LinphoneFriend *lf = (LinphoneFriend *)elem->data;
		if ((lf->refkey != NULL) && (strcmp(lf->refkey, ref_key) == 0)) return lf;
547 548 549 550 551 552 553
	}
	return NULL;
}

LinphoneFriend * linphone_friend_list_find_friend_by_inc_subscribe(const LinphoneFriendList *list, SalOp *op) {
	const MSList *elem;
	for (elem = list->friends; elem != NULL; elem = elem->next) {
554 555
		LinphoneFriend *lf = (LinphoneFriend *)elem->data;
		if (ms_list_find(lf->insubs, op)) return lf;
556 557 558 559 560 561 562
	}
	return NULL;
}

LinphoneFriend * linphone_friend_list_find_friend_by_out_subscribe(const LinphoneFriendList *list, SalOp *op) {
	const MSList *elem;
	for (elem = list->friends; elem != NULL; elem = elem->next) {
563 564
		LinphoneFriend *lf = (LinphoneFriend *)elem->data;
		if (lf->outsub && ((lf->outsub == op) || sal_op_is_forked_of(lf->outsub, op))) return lf;
565 566 567 568 569 570 571 572 573 574 575 576
	}
	return NULL;
}

void linphone_friend_list_close_subscriptions(LinphoneFriendList *list) {
	 /* FIXME we should wait until subscription to complete. */
	if (list->friends)
		ms_list_for_each(list->friends, (void (*)(void *))linphone_friend_close_subscriptions);
}

void linphone_friend_list_update_subscriptions(LinphoneFriendList *list, LinphoneProxyConfig *cfg, bool_t only_when_registered) {
	const MSList *elem;
577
	if (list->rls_uri != NULL) {
578 579
		LinphoneAddress *address = linphone_address_new(list->rls_uri);
		char *xml_content = create_resource_list_xml(list);
580
		if ((address != NULL) && (xml_content != NULL) && (linphone_friend_list_has_subscribe_inactive(list) == TRUE)) {
581
			unsigned char digest[16];
582
			md5((unsigned char *)xml_content, strlen(xml_content), digest);
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
			if ((list->event != NULL) && (list->content_digest != NULL) && (memcmp(list->content_digest, digest, sizeof(digest)) == 0)) {
				/* The content has not changed, only refresh the event. */
				linphone_event_refresh_subscribe(list->event);
			} else {
				LinphoneContent *content;
				int expires = lp_config_get_int(list->lc->config, "sip", "rls_presence_expires", 3600);
				list->expected_notification_version = 0;
				if (list->content_digest != NULL) ms_free(list->content_digest);
				list->content_digest = ms_malloc(sizeof(digest));
				memcpy(list->content_digest, digest, sizeof(digest));
				if (list->event != NULL) {
					linphone_event_terminate(list->event);
					linphone_event_unref(list->event);
				}
				list->event = linphone_core_create_subscribe(list->lc, address, "presence", expires);
				linphone_event_set_internal(list->event, TRUE);
				linphone_event_add_custom_header(list->event, "Require", "recipient-list-subscribe");
				linphone_event_add_custom_header(list->event, "Supported", "eventlist");
				linphone_event_add_custom_header(list->event, "Accept", "multipart/related, application/pidf+xml, application/rlmi+xml");
				linphone_event_add_custom_header(list->event, "Content-Disposition", "recipient-list");
				content = linphone_core_create_content(list->lc);
				linphone_content_set_type(content, "application");
				linphone_content_set_subtype(content, "resource-lists+xml");
				linphone_content_set_string_buffer(content, xml_content);
				linphone_event_send_subscribe(list->event, content);
				linphone_content_unref(content);
			}
610 611 612 613 614
		}
		if (address != NULL) linphone_address_unref(address);
		if (xml_content != NULL) ms_free(xml_content);
	} else {
		for (elem = list->friends; elem != NULL; elem = elem->next) {
615 616
			LinphoneFriend *lf = (LinphoneFriend *)elem->data;
			linphone_friend_update_subscribes(lf, cfg, only_when_registered);
617
		}
618 619 620 621 622 623
	}
}

void linphone_friend_list_invalidate_subscriptions(LinphoneFriendList *list) {
	const MSList *elem;
	for (elem = list->friends; elem != NULL; elem = elem->next) {
624 625
		LinphoneFriend *lf = (LinphoneFriend *)elem->data;
		linphone_friend_invalidate_subscription(lf);
626 627 628 629 630 631
	}
}

void linphone_friend_list_notify_presence(LinphoneFriendList *list, LinphonePresenceModel *presence) {
	const MSList *elem;
	for(elem = list->friends; elem != NULL; elem = elem->next) {
632 633
		LinphoneFriend *lf = (LinphoneFriend *)elem->data;
		linphone_friend_notify(lf, presence);
634 635
	}
}
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

void linphone_friend_list_notify_presence_received(LinphoneFriendList *list, LinphoneEvent *lev, const LinphoneContent *body) {
	if (linphone_content_is_multipart(body)) {
		LinphoneContent *first_part;
		const char *type = linphone_content_get_type(body);
		const char *subtype = linphone_content_get_subtype(body);

		if ((strcmp(type, "multipart") != 0) || (strcmp(subtype, "related") != 0)) {
			ms_warning("multipart presence notified but it is not 'multipart/related'");
			return;
		}

		first_part = linphone_content_get_part(body, 0);
		if (first_part == NULL) {
			ms_warning("'multipart/related' presence notified but it doesn't contain any part");
			return;
		}

		type = linphone_content_get_type(first_part);
		subtype = linphone_content_get_subtype(first_part);
		if ((strcmp(type, "application") != 0) || (strcmp(subtype, "rlmi+xml") != 0)) {
			ms_warning("multipart presence notified but first part is not 'application/rlmi+xml'");
Ghislain MARY's avatar
Ghislain MARY committed
658
			linphone_content_unref(first_part);
659 660 661 662
			return;
		}

		linphone_friend_list_parse_multipart_related_body(list, body, linphone_content_get_string_buffer(first_part));
Ghislain MARY's avatar
Ghislain MARY committed
663
		linphone_content_unref(first_part);
664 665
	}
}
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

const char * linphone_friend_list_get_uri(const LinphoneFriendList *list) {
	return list->uri;
}

void linphone_friend_list_set_uri(LinphoneFriendList *list, const char *uri) {
	if (list->uri != NULL) {
		ms_free(list->uri);
		list->uri = NULL;
	}
	if (uri != NULL) {
		list->uri = ms_strdup(uri);
	}
}

void linphone_friend_list_update_revision(LinphoneFriendList *list, int rev) {
	list->revision = rev;
}