carddav.c 28.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
carddav.c
Copyright (C) 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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20
#include "linphone/core.h"
21 22
#include "private.h"

23
using namespace LinphonePrivate;
24

25
LinphoneCardDavContext* linphone_carddav_context_new(LinphoneFriendList *lfl) {
26
	LinphoneCardDavContext *carddav_context = NULL;
27 28 29 30 31

	if (!linphone_core_vcard_supported()) {
		ms_error("[carddav] vCard isn't available (maybe it wasn't compiled), can't do CardDAV sync");
		return NULL;
	}
32
	if (!lfl || !lfl->uri) {
33 34
		return NULL;
	}
35

36
	carddav_context = (LinphoneCardDavContext *)ms_new0(LinphoneCardDavContext, 1);
37
	carddav_context->friend_list = linphone_friend_list_ref(lfl);
38 39 40
	return carddav_context;
}

41
void linphone_carddav_context_destroy(LinphoneCardDavContext *cdc) {
42
	if (cdc) {
43 44 45 46
		if (cdc->friend_list) {
			linphone_friend_list_unref(cdc->friend_list);
			cdc->friend_list = NULL;
		}
47
		if (cdc->auth_info) {
48
			linphone_auth_info_unref(cdc->auth_info);
49 50
			cdc->auth_info = NULL;
		}
51 52 53 54 55 56 57 58 59 60 61 62
		ms_free(cdc);
	}
}

void linphone_carddav_set_user_data(LinphoneCardDavContext *cdc, void *ud) {
	cdc->user_data = ud;
}

void* linphone_carddav_get_user_data(LinphoneCardDavContext *cdc) {
	return cdc->user_data;
}

63
void linphone_carddav_synchronize(LinphoneCardDavContext *cdc) {
64
	cdc->ctag = cdc->friend_list->revision;
65 66 67
	linphone_carddav_get_current_ctag(cdc);
}

68 69 70 71 72 73 74 75 76 77 78
static void linphone_carddav_client_to_server_sync_done(LinphoneCardDavContext *cdc, bool_t success, const char *msg) {
	if (!success) {
		ms_error("[carddav] CardDAV client to server sync failure: %s", msg);
	}
	
	if (cdc->sync_done_cb) {
		cdc->sync_done_cb(cdc, success, msg);
	}
}

static void linphone_carddav_server_to_client_sync_done(LinphoneCardDavContext *cdc, bool_t success, const char *msg) {
79
	if (success) {
80
		ms_debug("CardDAV sync successful, saving new cTag: %i", cdc->ctag);
81
		linphone_friend_list_update_revision(cdc->friend_list, cdc->ctag);
82
	} else {
83
		ms_error("[carddav] CardDAV server to client sync failure: %s", msg);
84 85 86 87 88 89 90
	}
	
	if (cdc->sync_done_cb) {
		cdc->sync_done_cb(cdc, success, msg);
	}
}

91
static int find_matching_friend(LinphoneFriend *lf1, LinphoneFriend *lf2) {
92 93
	LinphoneVcard *lvc1 = linphone_friend_get_vcard(lf1);
	LinphoneVcard *lvc2 = linphone_friend_get_vcard(lf2);
94 95 96 97 98 99
	const char *uid1 = NULL, *uid2 = NULL;
	if (!lvc1 || !lvc2) {
		return 1;
	}
	uid1 = linphone_vcard_get_uid(lvc1);
	uid2 = linphone_vcard_get_uid(lvc2);
Sylvain Berfini's avatar
Sylvain Berfini committed
100 101 102 103
	if (!uid1 || !uid2) {
		return 1;
	}
	return strcmp(uid1, uid2);
104 105
}

106
static void linphone_carddav_response_free(LinphoneCardDavResponse *response) {
Sylvain Berfini's avatar
Sylvain Berfini committed
107 108 109
	if (response->etag) ms_free(response->etag);
	if (response->url) ms_free(response->url);
	if (response->vcard) ms_free(response->vcard);
110 111 112
	ms_free(response);
}

113
static void linphone_carddav_vcards_pulled(LinphoneCardDavContext *cdc, bctbx_list_t *vCards) {
Sylvain Berfini's avatar
Sylvain Berfini committed
114
	bctbx_list_t *vCards_remember = vCards;
115 116
	if (vCards != NULL && bctbx_list_size(vCards) > 0) {
		bctbx_list_t *friends = cdc->friend_list->friends;
117 118 119
		while (vCards) {
			LinphoneCardDavResponse *vCard = (LinphoneCardDavResponse *)vCards->data;
			if (vCard) {
120
				LinphoneVcard *lvc = linphone_vcard_context_get_vcard_from_buffer(cdc->friend_list->lc->vcard_context, vCard->vcard);
121
				LinphoneFriend *lf = NULL;
122
				bctbx_list_t *local_friend = NULL;
123 124 125 126 127
				
				if (lvc) {
					// Compute downloaded vCards' URL and save it (+ eTag)
					char *vCard_name = strrchr(vCard->url, '/');
					char full_url[300];
128
					snprintf(full_url, sizeof(full_url), "%s%s", cdc->friend_list->uri, vCard_name);
129 130
					linphone_vcard_set_url(lvc, full_url);
					linphone_vcard_set_etag(lvc, vCard->etag);
131
					ms_debug("Downloaded vCard etag/url are %s and %s", vCard->etag, full_url);
132 133

					lf = linphone_friend_new_from_vcard(lvc);
134
					linphone_vcard_unref(lvc); /*ref is now owned by friend*/
135
					if (lf) {
136
						local_friend = bctbx_list_find_custom(friends, (int (*)(const void*, const void*))find_matching_friend, lf);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
						
						if (local_friend) {
							LinphoneFriend *lf2 = (LinphoneFriend *)local_friend->data;
							lf->storage_id = lf2->storage_id;
							lf->pol = lf2->pol;
							lf->subscribe = lf2->subscribe;
							lf->refkey = ms_strdup(lf2->refkey);
							lf->presence_received = lf2->presence_received;
							lf->lc = lf2->lc;
							lf->friend_list = lf2->friend_list;
							
							if (cdc->contact_updated_cb) {
								ms_debug("Contact updated: %s", linphone_friend_get_name(lf));
								cdc->contact_updated_cb(cdc, lf, lf2);
							}
						} else {
							if (cdc->contact_created_cb) {
								ms_debug("Contact created: %s", linphone_friend_get_name(lf));
								cdc->contact_created_cb(cdc, lf);
							}
						}
						linphone_friend_unref(lf);
					} else {
						ms_error("[carddav] Couldn't create a friend from vCard");
161 162
					}
				} else {
163
					ms_error("[carddav] Couldn't parse vCard %s", vCard->vcard);
164 165
				}
			}
166
			vCards = bctbx_list_next(vCards);
167
		}
Sylvain Berfini's avatar
Sylvain Berfini committed
168
		bctbx_list_free_with_data(vCards_remember, (void (*)(void *))linphone_carddav_response_free);
169
	}
170
	linphone_carddav_server_to_client_sync_done(cdc, TRUE, NULL);
171 172
}

173 174
static bctbx_list_t* parse_vcards_from_xml_response(const char *body) {
	bctbx_list_t *result = NULL;
175 176 177 178 179 180 181
	xmlparsing_context_t *xml_ctx = linphone_xmlparsing_context_new();
	xmlSetGenericErrorFunc(xml_ctx, linphone_xmlparsing_genericxml_error);
	xml_ctx->doc = xmlReadDoc((const unsigned char*)body, 0, NULL, 0);
	if (xml_ctx->doc != NULL) {
		if (linphone_create_xml_xpath_context(xml_ctx) < 0) goto end;
		linphone_xml_xpath_context_init_carddav_ns(xml_ctx);
		{
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
			xmlXPathObjectPtr responses = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/d:multistatus/d:response");
			if (responses != NULL && responses->nodesetval != NULL) {
				xmlNodeSetPtr responses_nodes = responses->nodesetval;
				if (responses_nodes->nodeNr >= 1) {
					int i;
					for (i = 0; i < responses_nodes->nodeNr; i++) {
						xmlNodePtr response_node = responses_nodes->nodeTab[i];
						xml_ctx->xpath_ctx->node = response_node;
						{
							char *etag = linphone_get_xml_text_content(xml_ctx, "d:propstat/d:prop/d:getetag");
							char *url =  linphone_get_xml_text_content(xml_ctx, "d:href");
							char *vcard = linphone_get_xml_text_content(xml_ctx, "d:propstat/d:prop/card:address-data");
							LinphoneCardDavResponse *response = ms_new0(LinphoneCardDavResponse, 1);
							response->etag = ms_strdup(etag);
							response->url = ms_strdup(url);
							response->vcard = ms_strdup(vcard);
198
							result = bctbx_list_append(result, response);
199
							ms_debug("Added vCard object with eTag %s, URL %s and vCard %s", etag, url, vcard);
Sylvain Berfini's avatar
Sylvain Berfini committed
200 201 202
							linphone_free_xml_text_content(etag);
							linphone_free_xml_text_content(url);
							linphone_free_xml_text_content(vcard);
203 204 205
						}
					}
				}
206
				xmlXPathFreeObject(responses);
207 208 209 210 211 212 213 214
			}
		}
	}
end:
	linphone_xmlparsing_context_destroy(xml_ctx);
	return result;
}

215
static int find_matching_vcard(LinphoneCardDavResponse *response, LinphoneFriend *lf) {
216
	if (!response->url || !lf || !lf->vcard || !linphone_vcard_get_url(lf->vcard)) {
Sylvain Berfini's avatar
Sylvain Berfini committed
217 218
		return 1;
	}
219
	return strcmp(response->url, linphone_vcard_get_url(lf->vcard));
220 221
}

222 223 224 225 226
static void linphone_carddav_vcards_fetched(LinphoneCardDavContext *cdc, bctbx_list_t *vCards) {
	if (vCards != NULL && bctbx_list_size(vCards) > 0) {
		bctbx_list_t *friends = cdc->friend_list->friends;
		bctbx_list_t *friends_to_remove = NULL;
		bctbx_list_t *temp_list = NULL;
227
		
228 229 230
		while (friends) {
			LinphoneFriend *lf = (LinphoneFriend *)friends->data;
			if (lf) {
231
				bctbx_list_t *vCard = bctbx_list_find_custom(vCards, (int (*)(const void*, const void*))find_matching_vcard, lf);
232
				if (!vCard) {
233
					ms_debug("Local friend %s isn't in the remote vCard list, delete it", linphone_friend_get_name(lf));
234
					temp_list = bctbx_list_append(temp_list, linphone_friend_ref(lf));
235 236 237 238
				} else {
					LinphoneCardDavResponse *response = (LinphoneCardDavResponse *)vCard->data;
					ms_debug("Local friend %s is in the remote vCard list, check eTag", linphone_friend_get_name(lf));
					if (response) {
239
						LinphoneVcard *lvc = linphone_friend_get_vcard(lf);
240 241 242
						const char *etag = linphone_vcard_get_etag(lvc);
						ms_debug("Local friend eTag is %s, remote vCard eTag is %s", etag, response->etag);
						if (lvc && etag && strcmp(etag, response->etag) == 0) {
243
							bctbx_list_remove(vCards, vCard);
244
							linphone_carddav_response_free(response);
245 246 247 248
						}
					}
				}
			}
249
			friends = bctbx_list_next(friends);
250
		}
251
		friends_to_remove = temp_list;
252 253 254 255
		while(friends_to_remove) {
			LinphoneFriend *lf = (LinphoneFriend *)friends_to_remove->data;
			if (lf) {
				if (cdc->contact_removed_cb) {
256
					ms_debug("Contact removed: %s", linphone_friend_get_name(lf));
257 258 259
					cdc->contact_removed_cb(cdc, lf);
				}
			}
260
			friends_to_remove = bctbx_list_next(friends_to_remove);
261
		}
262
		temp_list = bctbx_list_free_with_data(temp_list, (void (*)(void *))linphone_friend_unref);
263
		
264
		linphone_carddav_pull_vcards(cdc, vCards);
265
		bctbx_list_free_with_data(vCards, (void (*)(void *))linphone_carddav_response_free);
266
	}
267 268
}

269 270
static bctbx_list_t* parse_vcards_etags_from_xml_response(const char *body) {
	bctbx_list_t *result = NULL;
271 272 273 274 275 276 277
	xmlparsing_context_t *xml_ctx = linphone_xmlparsing_context_new();
	xmlSetGenericErrorFunc(xml_ctx, linphone_xmlparsing_genericxml_error);
	xml_ctx->doc = xmlReadDoc((const unsigned char*)body, 0, NULL, 0);
	if (xml_ctx->doc != NULL) {
		if (linphone_create_xml_xpath_context(xml_ctx) < 0) goto end;
		linphone_xml_xpath_context_init_carddav_ns(xml_ctx);
		{
278 279 280 281 282 283 284 285 286 287 288 289 290 291
			xmlXPathObjectPtr responses = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/d:multistatus/d:response");
			if (responses != NULL && responses->nodesetval != NULL) {
				xmlNodeSetPtr responses_nodes = responses->nodesetval;
				if (responses_nodes->nodeNr >= 1) {
					int i;
					for (i = 0; i < responses_nodes->nodeNr; i++) {
						xmlNodePtr response_node = responses_nodes->nodeTab[i];
						xml_ctx->xpath_ctx->node = response_node;
						{
							char *etag = linphone_get_xml_text_content(xml_ctx, "d:propstat/d:prop/d:getetag");
							char *url =  linphone_get_xml_text_content(xml_ctx, "d:href");
							LinphoneCardDavResponse *response = ms_new0(LinphoneCardDavResponse, 1);
							response->etag = ms_strdup(etag);
							response->url = ms_strdup(url);
292
							result = bctbx_list_append(result, response);
293
							ms_debug("Added vCard object with eTag %s and URL %s", etag, url);
Sylvain Berfini's avatar
Sylvain Berfini committed
294 295
							linphone_free_xml_text_content(etag);
							linphone_free_xml_text_content(url);
296 297 298
						}
					}
				}
299
				xmlXPathFreeObject(responses);
300 301
			}
		}
302
	}
303 304 305
end:
	linphone_xmlparsing_context_destroy(xml_ctx);
	return result;
306 307 308
}

static void linphone_carddav_ctag_fetched(LinphoneCardDavContext *cdc, int ctag) {
309
	ms_debug("Remote cTag for CardDAV addressbook is %i, local one is %i", ctag, cdc->ctag);
310 311 312 313 314
	if (ctag == -1 || ctag > cdc->ctag) {
		cdc->ctag = ctag;
		linphone_carddav_fetch_vcards(cdc);
	} else {
		ms_message("No changes found on server, skipping sync");
315
		linphone_carddav_server_to_client_sync_done(cdc, TRUE, "Synchronization skipped because cTag already up to date");
316 317 318
	}
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
static int parse_ctag_value_from_xml_response(const char *body) {
	int result = -1;
	xmlparsing_context_t *xml_ctx = linphone_xmlparsing_context_new();
	xmlSetGenericErrorFunc(xml_ctx, linphone_xmlparsing_genericxml_error);
	xml_ctx->doc = xmlReadDoc((const unsigned char*)body, 0, NULL, 0);
	if (xml_ctx->doc != NULL) {
		char *response = NULL;
		if (linphone_create_xml_xpath_context(xml_ctx) < 0) goto end;
		linphone_xml_xpath_context_init_carddav_ns(xml_ctx);
		response = linphone_get_xml_text_content(xml_ctx, "/d:multistatus/d:response/d:propstat/d:prop/x1:getctag");
		if (response) {
			result = atoi(response);
			linphone_free_xml_text_content(response);
		}
	}
end:
	linphone_xmlparsing_context_destroy(xml_ctx);
	return result;
337 338
}

339 340 341 342 343 344 345 346 347 348
static void linphone_carddav_query_free(LinphoneCardDavQuery *query) {
	if (!query) {
		return;
	}
	
	if (query->http_request_listener) {
		belle_sip_object_unref(query->http_request_listener);
		query->http_request_listener = NULL;
	}
	
349 350 351
	// Context will be freed later (in sync_done)
	query->context = NULL;
	
Sylvain Berfini's avatar
Sylvain Berfini committed
352 353 354 355 356 357 358
	if (query->url) {
		ms_free(query->url);
	}
	if (query->body) {
		ms_free(query->body);
	}
	
359 360 361
	ms_free(query);
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
static bool_t is_query_client_to_server_sync(LinphoneCardDavQuery *query) {
	if (!query) {
		ms_error("[carddav] query is NULL...");
		return FALSE;
	}
	switch(query->type) {
		case LinphoneCardDavQueryTypePropfind:
		case LinphoneCardDavQueryTypeAddressbookQuery:
		case LinphoneCardDavQueryTypeAddressbookMultiget:
			return FALSE;
		case LinphoneCardDavQueryTypePut:
		case LinphoneCardDavQueryTypeDelete:
			return TRUE;
		default:
			ms_error("[carddav] Unknown request: %i", query->type);
	}
	return FALSE;
}

381 382 383 384 385
static void process_response_from_carddav_request(void *data, const belle_http_response_event_t *event) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)data;
	
	if (event->response) {
		int code = belle_http_response_get_status_code(event->response);
386
		if (code == 207 || code == 200 || code == 201 || code == 204) {
387 388 389
			const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response);
			switch(query->type) {
			case LinphoneCardDavQueryTypePropfind:
390
				linphone_carddav_ctag_fetched(query->context, parse_ctag_value_from_xml_response(body));
391 392
				break;
			case LinphoneCardDavQueryTypeAddressbookQuery:
393
				linphone_carddav_vcards_fetched(query->context, parse_vcards_etags_from_xml_response(body));
394 395
				break;
			case LinphoneCardDavQueryTypeAddressbookMultiget:
396
				linphone_carddav_vcards_pulled(query->context, parse_vcards_from_xml_response(body));
397
				break;
398 399 400 401
			case LinphoneCardDavQueryTypePut:
				{
					belle_sip_header_t *header = belle_sip_message_get_header((belle_sip_message_t *)event->response, "ETag");
					LinphoneFriend *lf = (LinphoneFriend *)query->user_data;
402
					LinphoneVcard *lvc = linphone_friend_get_vcard(lf);
403 404
					if (lf && lvc) {
						if (header) {
405
							const char *etag = belle_sip_header_get_unparsed_value(header);
406 407 408 409 410
							if (!linphone_vcard_get_etag(lvc)) {
								ms_debug("eTag for newly created vCard is: %s", etag);
							} else {
								ms_debug("eTag for updated vCard is: %s", etag);
							}
411
							linphone_vcard_set_etag(lvc, etag);
412

413
							linphone_carddav_client_to_server_sync_done(query->context, TRUE, NULL);
414 415 416 417
							linphone_friend_unref(lf);
						} else {
							// For some reason, server didn't return the eTag of the updated/created vCard
							// We need to do a GET on the vCard to get the correct one
418
							bctbx_list_t *vcard = NULL;
419
							LinphoneCardDavResponse *response = (LinphoneCardDavResponse *)ms_new0(LinphoneCardDavResponse, 1);
Sylvain Berfini's avatar
Sylvain Berfini committed
420
							response->url = ms_strdup(linphone_vcard_get_url(lvc));
421
							vcard = bctbx_list_append(vcard, response);
422
							linphone_carddav_pull_vcards(query->context, vcard);
423
							bctbx_list_free_with_data(vcard, (void (*)(void *))linphone_carddav_response_free);
424
						}
425 426
					}
					else {
427
						linphone_carddav_client_to_server_sync_done(query->context, FALSE, "No LinphoneFriend found in user_data field of query");
428 429 430 431
					}
				}
				break;
			case LinphoneCardDavQueryTypeDelete:
432
				linphone_carddav_client_to_server_sync_done(query->context, TRUE, NULL);
433 434
				break;
			default:
435
				ms_error("[carddav] Unknown request: %i", query->type);
436
				break;
437 438
			}
		} else {
439 440
			char msg[100];
			snprintf(msg, sizeof(msg), "Unexpected HTTP response code: %i", code);
441 442 443 444 445
			if (is_query_client_to_server_sync(query)) {
				linphone_carddav_client_to_server_sync_done(query->context, FALSE, msg);
			} else {
				linphone_carddav_server_to_client_sync_done(query->context, FALSE, msg);
			}
446
		}
447
	} else {
448 449 450 451 452
		if (is_query_client_to_server_sync(query)) {
			linphone_carddav_client_to_server_sync_done(query->context, FALSE, "No response found");
		} else {
			linphone_carddav_server_to_client_sync_done(query->context, FALSE, "No response found");
		}
453
	}
454
	linphone_carddav_query_free(query);
455 456 457 458
}

static void process_io_error_from_carddav_request(void *data, const belle_sip_io_error_event_t *event) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)data;
459
	ms_error("[carddav] I/O error during CardDAV request sending");
460 461 462 463 464
	if (is_query_client_to_server_sync(query)) {
		linphone_carddav_client_to_server_sync_done(query->context, FALSE, "I/O error during CardDAV request sending");
	} else {
		linphone_carddav_server_to_client_sync_done(query->context, FALSE, "I/O error during CardDAV request sending");
	}
465
	linphone_carddav_query_free(query);
466 467 468
}

static void process_auth_requested_from_carddav_request(void *data, belle_sip_auth_event_t *event) {
469 470 471 472 473 474
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)data;
	LinphoneCardDavContext *cdc = query->context;
	const char *realm = belle_sip_auth_event_get_realm(event);
	belle_generic_uri_t *uri = belle_generic_uri_parse(query->url);
	const char *domain = belle_generic_uri_get_host(uri);
	
475 476 477 478 479 480
	if (cdc->auth_info) {
		belle_sip_auth_event_set_username(event, cdc->auth_info->username);
		belle_sip_auth_event_set_passwd(event, cdc->auth_info->passwd);
		belle_sip_auth_event_set_ha1(event, cdc->auth_info->ha1);
	} else {
		LinphoneCore *lc = cdc->friend_list->lc;
481
		const bctbx_list_t *auth_infos = linphone_core_get_auth_info_list(lc);
482 483 484 485 486 487 488 489 490 491 492 493
		
		ms_debug("Looking for auth info for domain %s and realm %s", domain, realm);
		while (auth_infos) {
			LinphoneAuthInfo *auth_info = (LinphoneAuthInfo *)auth_infos->data;
			if (auth_info->domain && strcmp(domain, auth_info->domain) == 0) {
				if (!auth_info->realm || strcmp(realm, auth_info->realm) == 0) {
					belle_sip_auth_event_set_username(event, auth_info->username);
					belle_sip_auth_event_set_passwd(event, auth_info->passwd);
					belle_sip_auth_event_set_ha1(event, auth_info->ha1);
					cdc->auth_info = linphone_auth_info_clone(auth_info);
					break;
				}
494
			}
495
			auth_infos = bctbx_list_next(auth_infos);
496 497
		}
	
498 499 500 501 502 503 504 505
		if (!auth_infos) {
			ms_error("[carddav] Authentication requested during CardDAV request sending, and username/password weren't provided");
			if (is_query_client_to_server_sync(query)) {
				linphone_carddav_client_to_server_sync_done(query->context, FALSE, "Authentication requested during CardDAV request sending, and username/password weren't provided");
			} else {
				linphone_carddav_server_to_client_sync_done(query->context, FALSE, "Authentication requested during CardDAV request sending, and username/password weren't provided");
			}
			linphone_carddav_query_free(query);
506
		}
507
	}
508 509 510 511 512 513 514
}

static void linphone_carddav_send_query(LinphoneCardDavQuery *query) {
	belle_http_request_listener_callbacks_t cbs = { 0 };
	belle_generic_uri_t *uri = NULL;
	belle_http_request_t *req = NULL;
	belle_sip_memory_body_handler_t *bh = NULL;
515 516
	LinphoneCardDavContext *cdc = query->context;
	char* ua = NULL;
517 518 519

	uri = belle_generic_uri_parse(query->url);
	if (!uri) {
520 521 522
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, "Could not send request, URL is invalid");
		}
523
		belle_sip_error("Could not send request, URL %s is invalid", query->url);
524
		linphone_carddav_query_free(query);
525 526
		return;
	}
527 528
	req = belle_http_request_create(query->method, uri, belle_sip_header_content_type_create("application", "xml; charset=utf-8"), NULL);
	
529
	if (!req) {
530 531 532
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, "Could not create belle_http_request_t");
		}
533 534
		belle_sip_object_unref(uri);
		belle_sip_error("Could not create belle_http_request_t");
535
		linphone_carddav_query_free(query);
536 537 538
		return;
	}
	
539 540 541
	ua = ms_strdup_printf("%s/%s", linphone_core_get_user_agent(cdc->friend_list->lc), linphone_core_get_version());
	belle_sip_message_add_header((belle_sip_message_t *)req, belle_sip_header_create("User-Agent", ua));
	ms_free(ua);
542 543 544 545 546 547 548 549
	if (query->depth) {
		belle_sip_message_add_header((belle_sip_message_t *)req, belle_sip_header_create("Depth", query->depth));
	} else if (query->ifmatch) {
		belle_sip_message_add_header((belle_sip_message_t *)req, belle_sip_header_create("If-Match", query->ifmatch));
	} else if (strcmp(query->method, "PUT")) {
		belle_sip_message_add_header((belle_sip_message_t *)req, belle_sip_header_create("If-None-Match", "*"));
	}
	
550 551 552 553
	if (query->body) {
		bh = belle_sip_memory_body_handler_new_copy_from_buffer(query->body, strlen(query->body), NULL, NULL);
		belle_sip_message_set_body_handler(BELLE_SIP_MESSAGE(req), bh ? BELLE_SIP_BODY_HANDLER(bh) : NULL);
	}
554 555 556 557
	
	cbs.process_response = process_response_from_carddav_request;
	cbs.process_io_error = process_io_error_from_carddav_request;
	cbs.process_auth_requested = process_auth_requested_from_carddav_request;
558
	query->http_request_listener = belle_http_request_listener_create_from_callbacks(&cbs, query);
559
	belle_http_provider_send_request(query->context->friend_list->lc->http_provider, req, query->http_request_listener);
560 561
}

562
static LinphoneCardDavQuery* linphone_carddav_create_put_query(LinphoneCardDavContext *cdc, LinphoneVcard *lvc) {
563 564 565 566
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = NULL;
	query->ifmatch = linphone_vcard_get_etag(lvc);
Sylvain Berfini's avatar
Sylvain Berfini committed
567
	query->body = ms_strdup(linphone_vcard_as_vcard4_string(lvc));
568
	query->method = "PUT";
Sylvain Berfini's avatar
Sylvain Berfini committed
569
	query->url = ms_strdup(linphone_vcard_get_url(lvc));
570 571 572 573 574
	query->type = LinphoneCardDavQueryTypePut;
	return query;
}

static char* generate_url_from_server_address_and_uid(const char *server_url) {
575
	char *result = NULL;
576
	if (server_url) {
577
		char *uuid = reinterpret_cast<char *>(ms_malloc(64));
578
		if (Sal::generate_uuid(uuid, 64) == 0) {
579
			char *url = reinterpret_cast<char *>(ms_malloc(300));
580
			snprintf(url, 300, "%s/linphone-%s.vcf", server_url, uuid);
581
			ms_debug("Generated url is %s", url);
582 583
			result = ms_strdup(url);
			ms_free(url);
584
		}
585
		ms_free(uuid);
586
	}
587
	return result;
588 589
}

590
void linphone_carddav_put_vcard(LinphoneCardDavContext *cdc, LinphoneFriend *lf) {
591
	LinphoneVcard *lvc = linphone_friend_get_vcard(lf);
592
	if (lvc) {
593
		LinphoneCardDavQuery *query = NULL;
594 595 596
		if (!linphone_vcard_get_uid(lvc)) {
			linphone_vcard_generate_unique_id(lvc);
		}
597 598
		
		if (!linphone_vcard_get_url(lvc)) {
599
			char *url = generate_url_from_server_address_and_uid(cdc->friend_list->uri);
600 601 602 603 604
			if (url) {
				linphone_vcard_set_url(lvc, url);
				ms_free(url);
			} else {
				const char *msg = "vCard doesn't have an URL, and friendlist doesn't have a CardDAV server set either, can't push it";
Sylvain Berfini's avatar
Sylvain Berfini committed
605
				ms_warning("%s", msg);
606 607 608 609 610
				if (cdc && cdc->sync_done_cb) {
					cdc->sync_done_cb(cdc, FALSE, msg);
				}
				return;
			}
611 612 613 614 615 616 617 618
		}
		
		query = linphone_carddav_create_put_query(cdc, lvc);
		query->user_data = linphone_friend_ref(lf);
		linphone_carddav_send_query(query);
	} else {
		const char *msg = NULL;
		if (!lvc) {
619
			msg = "LinphoneVcard is NULL";
620 621
		} else {
			msg = "Unknown error";
622 623 624
		}
		
		if (msg) {
625
			ms_error("[carddav] %s", msg);
626 627 628 629 630 631 632 633
		}
		
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, msg);
		}
	}
}

634
static LinphoneCardDavQuery* linphone_carddav_create_delete_query(LinphoneCardDavContext *cdc, LinphoneVcard *lvc) {
635 636 637 638
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = NULL;
	query->ifmatch = linphone_vcard_get_etag(lvc);
639
	query->body = NULL;
640
	query->method = "DELETE";
Sylvain Berfini's avatar
Sylvain Berfini committed
641
	query->url = ms_strdup(linphone_vcard_get_url(lvc));
642 643
	query->type = LinphoneCardDavQueryTypeDelete;
	return query;
644 645 646
}

void linphone_carddav_delete_vcard(LinphoneCardDavContext *cdc, LinphoneFriend *lf) {
647
	LinphoneVcard *lvc = linphone_friend_get_vcard(lf);
648 649 650 651
	if (lvc && linphone_vcard_get_uid(lvc) && linphone_vcard_get_etag(lvc)) {
		LinphoneCardDavQuery *query = NULL;
		
		if (!linphone_vcard_get_url(lvc)) {
652
			char *url = generate_url_from_server_address_and_uid(cdc->friend_list->uri);
653 654 655 656 657 658 659 660 661 662 663
			if (url) {
				linphone_vcard_set_url(lvc, url);
				ms_free(url);
			} else {
				const char *msg = "vCard doesn't have an URL, and friendlist doesn't have a CardDAV server set either, can't delete it";
				ms_warning("%s", msg);
				if (cdc && cdc->sync_done_cb) {
					cdc->sync_done_cb(cdc, FALSE, msg);
				}
				return;
			}
664 665 666 667 668 669 670
		}
		
		query = linphone_carddav_create_delete_query(cdc, lvc);
		linphone_carddav_send_query(query);
	} else {
		const char *msg = NULL;
		if (!lvc) {
671
			msg = "LinphoneVcard is NULL";
672
		} else if (!linphone_vcard_get_uid(lvc)) {
673
			msg = "LinphoneVcard doesn't have an UID";
674
		} else if (!linphone_vcard_get_etag(lvc)) {
675
			msg = "LinphoneVcard doesn't have an eTag";
676 677 678
		}
		
		if (msg) {
679
			ms_error("[carddav] %s", msg);
680 681 682 683 684 685
		}
		
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, msg);
		}
	}
686 687 688 689 690 691
}

void linphone_carddav_set_synchronization_done_callback(LinphoneCardDavContext *cdc, LinphoneCardDavSynchronizationDoneCb cb) {
	cdc->sync_done_cb = cb;
}

692 693 694 695 696 697 698 699 700 701 702 703
void linphone_carddav_set_new_contact_callback(LinphoneCardDavContext *cdc, LinphoneCardDavContactCreatedCb cb) {
	cdc->contact_created_cb = cb;
}

void linphone_carddav_set_updated_contact_callback(LinphoneCardDavContext *cdc, LinphoneCardDavContactUpdatedCb cb) {
	cdc->contact_updated_cb = cb;
}

void linphone_carddav_set_removed_contact_callback(LinphoneCardDavContext *cdc, LinphoneCardDavContactRemovedCb cb) {
	cdc->contact_removed_cb = cb;
}

704 705 706 707
static LinphoneCardDavQuery* linphone_carddav_create_propfind_query(LinphoneCardDavContext *cdc) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = "0";
708
	query->ifmatch = NULL;
Sylvain Berfini's avatar
Sylvain Berfini committed
709
	query->body = ms_strdup("<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\"><d:prop><cs:getctag /></d:prop></d:propfind>");
710
	query->method = "PROPFIND";
Sylvain Berfini's avatar
Sylvain Berfini committed
711
	query->url = ms_strdup(cdc->friend_list->uri);
712 713 714 715 716 717 718 719 720 721 722 723 724
	query->type = LinphoneCardDavQueryTypePropfind;
	return query;
}

void linphone_carddav_get_current_ctag(LinphoneCardDavContext *cdc) {
	LinphoneCardDavQuery *query = linphone_carddav_create_propfind_query(cdc);
	linphone_carddav_send_query(query);
}

static LinphoneCardDavQuery* linphone_carddav_create_addressbook_query(LinphoneCardDavContext *cdc) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = "1";
725
	query->ifmatch = NULL;
Sylvain Berfini's avatar
Sylvain Berfini committed
726
	query->body = ms_strdup("<card:addressbook-query xmlns:d=\"DAV:\" xmlns:card=\"urn:ietf:params:xml:ns:carddav\"><d:prop><d:getetag /></d:prop><card:filter></card:filter></card:addressbook-query>");
727
	query->method = "REPORT";
Sylvain Berfini's avatar
Sylvain Berfini committed
728
	query->url = ms_strdup(cdc->friend_list->uri);
729 730 731 732 733 734 735 736 737
	query->type = LinphoneCardDavQueryTypeAddressbookQuery;
	return query;
}

void linphone_carddav_fetch_vcards(LinphoneCardDavContext *cdc) {
	LinphoneCardDavQuery *query = linphone_carddav_create_addressbook_query(cdc);
	linphone_carddav_send_query(query);
}

738
static LinphoneCardDavQuery* linphone_carddav_create_addressbook_multiget_query(LinphoneCardDavContext *cdc, bctbx_list_t *vcards) {
739
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
740 741
	char *body = (char *)ms_malloc((bctbx_list_size(vcards) + 1) * 300 * sizeof(char));
	bctbx_list_t *iterator = vcards;
742
	
743 744
	query->context = cdc;
	query->depth = "1";
745
	query->ifmatch = NULL;
746
	query->method = "REPORT";
Sylvain Berfini's avatar
Sylvain Berfini committed
747
	query->url = ms_strdup(cdc->friend_list->uri);
748
	query->type = LinphoneCardDavQueryTypeAddressbookMultiget;
749

750
	sprintf(body, "%s", "<card:addressbook-multiget xmlns:d=\"DAV:\" xmlns:card=\"urn:ietf:params:xml:ns:carddav\"><d:prop><d:getetag /><card:address-data content-type='text/vcard' version='4.0'/></d:prop>");
751 752
	while (iterator) {
		LinphoneCardDavResponse *response = (LinphoneCardDavResponse *)iterator->data;
753
		if (response) {
754 755
			char temp_body[300];
			snprintf(temp_body, sizeof(temp_body), "<d:href>%s</d:href>", response->url);
Sylvain Berfini's avatar
Sylvain Berfini committed
756
			strcat(body, temp_body);
757
			iterator = bctbx_list_next(iterator);
758
		}
759
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
760
	strcat(body, "</card:addressbook-multiget>");
761
	query->body = ms_strdup(body);
Sylvain Berfini's avatar
Sylvain Berfini committed
762
	ms_free(body);
763
	
764 765 766
	return query;
}

767
void linphone_carddav_pull_vcards(LinphoneCardDavContext *cdc, bctbx_list_t *vcards_to_pull) {
768 769
	LinphoneCardDavQuery *query = linphone_carddav_create_addressbook_multiget_query(cdc, vcards_to_pull);
	linphone_carddav_send_query(query);
770
}