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

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

23
LinphoneCardDavContext* linphone_carddav_context_new(LinphoneFriendList *lfl) {
24 25
	LinphoneCardDavContext *carddav_context = NULL;
	
26
	if (!lfl || !lfl->uri) {
27 28 29 30 31
		return NULL;
	}
	
#ifdef VCARD_ENABLED
	carddav_context = (LinphoneCardDavContext *)ms_new0(LinphoneCardDavContext, 1);
32
	carddav_context->friend_list = linphone_friend_list_ref(lfl);
33 34 35 36 37 38
#else
	ms_error("vCard isn't available (maybe it wasn't compiled), can't do CardDAV sync");
#endif
	return carddav_context;
}

39
void linphone_carddav_context_destroy(LinphoneCardDavContext *cdc) {
40
	if (cdc) {
41 42 43 44
		if (cdc->friend_list) {
			linphone_friend_list_unref(cdc->friend_list);
			cdc->friend_list = NULL;
		}
45 46 47 48 49 50 51 52 53 54 55 56
		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;
}

57 58 59 60 61 62
void linphone_carddav_synchronize(LinphoneCardDavContext *cdc) {
	linphone_carddav_get_current_ctag(cdc);
}

static void linphone_carddav_sync_done(LinphoneCardDavContext *cdc, bool_t success, const char *msg) {
	if (success) {
63
		ms_debug("CardDAV sync successful, saving new cTag: %i", cdc->ctag);
64
		linphone_friend_list_update_revision(cdc->friend_list, cdc->ctag);
65 66
	} else {
		ms_error("CardDAV sync failure: %s", msg);
67 68 69 70 71 72 73
	}
	
	if (cdc->sync_done_cb) {
		cdc->sync_done_cb(cdc, success, msg);
	}
}

74 75 76
static int find_matching_friend(LinphoneFriend *lf1, LinphoneFriend *lf2) {
	LinphoneVCard *lvc1 = linphone_friend_get_vcard(lf1);
	LinphoneVCard *lvc2 = linphone_friend_get_vcard(lf2);
77 78 79 80 81 82
	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
83 84 85 86
	if (!uid1 || !uid2) {
		return 1;
	}
	return strcmp(uid1, uid2);
87 88
}

89 90
static void linphone_carddav_vcards_pulled(LinphoneCardDavContext *cdc, MSList *vCards) {
	if (vCards != NULL && ms_list_size(vCards) > 0) {
91
		MSList *friends = cdc->friend_list->friends;
92 93 94 95
		while (vCards) {
			LinphoneCardDavResponse *vCard = (LinphoneCardDavResponse *)vCards->data;
			if (vCard) {
				LinphoneVCard *lvc = linphone_vcard_new_from_vcard4_buffer(vCard->vcard);
96 97 98 99 100 101 102
				LinphoneFriend *lf = NULL;
				MSList *local_friend = NULL;
				
				if (lvc) {
					// Compute downloaded vCards' URL and save it (+ eTag)
					char *vCard_name = strrchr(vCard->url, '/');
					char full_url[300];
103
					snprintf(full_url, sizeof(full_url), "%s%s", cdc->friend_list->uri, vCard_name);
104 105
					linphone_vcard_set_url(lvc, full_url);
					linphone_vcard_set_etag(lvc, vCard->etag);
106
					ms_debug("Downloaded vCard etag/url are %s and %s", vCard->etag, full_url);
107 108
				}
				lf = linphone_friend_new_from_vcard(lvc);
109
				local_friend = ms_list_find_custom(friends, (int (*)(const void*, const void*))find_matching_friend, lf);
110
				
111 112
				if (local_friend) {
					LinphoneFriend *lf2 = (LinphoneFriend *)local_friend->data;
113 114 115 116 117 118
					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;
					
119 120 121 122 123 124 125 126 127 128
					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);
					}
				}
129
				linphone_friend_unref(lf);
130 131 132
			}
			vCards = ms_list_next(vCards);
		}
133 134 135 136 137 138 139 140 141 142 143 144 145 146
	}
	ms_list_free(vCards);
	linphone_carddav_sync_done(cdc, TRUE, "");
}

static MSList* parse_vcards_from_xml_response(const char *body) {
	MSList *result = NULL;
	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);
		{
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			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);
							result = ms_list_append(result, response);
							ms_debug("Added vCard object with eTag %s, URL %s and vCard %s", etag, url, vcard);
165 166 167
						}
					}
				}
168
				xmlXPathFreeObject(responses);
169 170 171 172 173 174 175 176
			}
		}
	}
end:
	linphone_xmlparsing_context_destroy(xml_ctx);
	return result;
}

177
static int find_matching_vcard(LinphoneCardDavResponse *response, LinphoneFriend *lf) {
178
	if (!response->url || !lf || !lf->vcard || !linphone_vcard_get_url(lf->vcard)) {
Sylvain Berfini's avatar
Sylvain Berfini committed
179 180
		return 1;
	}
181
	return strcmp(response->url, linphone_vcard_get_url(lf->vcard));
182 183
}

184 185
static void linphone_carddav_vcards_fetched(LinphoneCardDavContext *cdc, MSList *vCards) {
	if (vCards != NULL && ms_list_size(vCards) > 0) {
186
		MSList *friends = cdc->friend_list->friends;
187
		MSList *friends_to_remove = NULL;
188
		MSList *temp_list = NULL;
189
		
190 191 192 193 194
		while (friends) {
			LinphoneFriend *lf = (LinphoneFriend *)friends->data;
			if (lf) {
				MSList *vCard = ms_list_find_custom(vCards, (int (*)(const void*, const void*))find_matching_vcard, lf);
				if (!vCard) {
195 196
					ms_debug("Local friend %s isn't in the remote vCard list, delete it", linphone_friend_get_name(lf));
					temp_list = ms_list_append(temp_list, linphone_friend_ref(lf));
197 198 199 200 201
				} 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) {
						LinphoneVCard *lvc = linphone_friend_get_vcard(lf);
202 203 204
						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) {
205
							ms_list_remove(vCards, vCard);
Sylvain Berfini's avatar
Sylvain Berfini committed
206
							ms_free(response);
207 208 209 210 211 212
						}
					}
				}
			}
			friends = ms_list_next(friends);
		}
213
		friends_to_remove = temp_list;
214 215 216 217
		while(friends_to_remove) {
			LinphoneFriend *lf = (LinphoneFriend *)friends_to_remove->data;
			if (lf) {
				if (cdc->contact_removed_cb) {
218
					ms_debug("Contact removed: %s", linphone_friend_get_name(lf));
219 220 221 222 223
					cdc->contact_removed_cb(cdc, lf);
				}
			}
			friends_to_remove = ms_list_next(friends_to_remove);
		}
224
		temp_list = ms_list_free_with_data(temp_list, (void (*)(void *))linphone_friend_unref);
225
		
226
		linphone_carddav_pull_vcards(cdc, vCards);
227
	}
228 229 230 231 232 233 234 235 236 237 238 239
	ms_list_free(vCards);
}

static MSList* parse_vcards_etags_from_xml_response(const char *body) {
	MSList *result = NULL;
	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);
		{
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
			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);
							result = ms_list_append(result, response);
							ms_debug("Added vCard object with eTag %s and URL %s", etag, url);
256 257 258
						}
					}
				}
259
				xmlXPathFreeObject(responses);
260 261
			}
		}
262
	}
263 264 265
end:
	linphone_xmlparsing_context_destroy(xml_ctx);
	return result;
266 267 268
}

static void linphone_carddav_ctag_fetched(LinphoneCardDavContext *cdc, int ctag) {
269
	ms_debug("Remote cTag for CardDAV addressbook is %i, local one is %i", ctag, cdc->ctag);
270 271 272 273 274
	if (ctag == -1 || ctag > cdc->ctag) {
		cdc->ctag = ctag;
		linphone_carddav_fetch_vcards(cdc);
	} else {
		ms_message("No changes found on server, skipping sync");
275
		linphone_carddav_sync_done(cdc, TRUE, "Synchronization skipped because cTag already up to date");
276 277 278
	}
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
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;
297 298
}

299 300 301 302 303 304 305 306 307 308 309 310 311
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;
	}
	
	ms_free(query);
}

312 313 314 315 316
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);
317
		if (code == 207 || code == 200 || code == 201 || code == 204) {
318 319 320
			const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response);
			switch(query->type) {
			case LinphoneCardDavQueryTypePropfind:
321
				linphone_carddav_ctag_fetched(query->context, parse_ctag_value_from_xml_response(body));
322 323
				break;
			case LinphoneCardDavQueryTypeAddressbookQuery:
324
				linphone_carddav_vcards_fetched(query->context, parse_vcards_etags_from_xml_response(body));
325 326
				break;
			case LinphoneCardDavQueryTypeAddressbookMultiget:
327
				linphone_carddav_vcards_pulled(query->context, parse_vcards_from_xml_response(body));
328
				break;
329 330 331 332
			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;
333 334 335
					LinphoneVCard *lvc = linphone_friend_get_vcard(lf);
					if (lf && lvc) {
						if (header) {
336
							const char *etag = belle_sip_header_get_unparsed_value(header);
337 338 339 340 341
							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);
							}
342
							linphone_vcard_set_etag(lvc, etag);
343 344 345 346 347 348 349 350 351 352 353 354 355

							linphone_carddav_sync_done(query->context, TRUE, "");
							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
							MSList *vcard = NULL;
							LinphoneCardDavResponse *response = (LinphoneCardDavResponse *)ms_new0(LinphoneCardDavResponse, 1);
							response->url = linphone_vcard_get_url(lvc);
							response->context = query->context;
							vcard = ms_list_append(vcard, response);
							linphone_carddav_pull_vcards(query->context, vcard);
							ms_list_free(vcard);
356
						}
357 358
					}
					else {
359 360 361 362 363 364 365 366 367 368
						linphone_carddav_sync_done(query->context, FALSE, "No LinphoneFriend found in user_date field of query");
					}
				}
				break;
			case LinphoneCardDavQueryTypeDelete:
				linphone_carddav_sync_done(query->context, TRUE, "");
				break;
			default:
				ms_error("Unknown request: %i", query->type);
				break;
369 370
			}
		} else {
371 372 373
			char msg[100];
			snprintf(msg, sizeof(msg), "Unexpected HTTP response code: %i", code);
			linphone_carddav_sync_done(query->context, FALSE, msg);
374
		}
375 376
	} else {
		linphone_carddav_sync_done(query->context, FALSE, "No response found");
377
	}
378
	linphone_carddav_query_free(query);
379 380 381 382
}

static void process_io_error_from_carddav_request(void *data, const belle_sip_io_error_event_t *event) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)data;
383
	ms_error("I/O error during CardDAV request sending");
384
	linphone_carddav_query_free(query);
385
	linphone_carddav_sync_done(query->context, FALSE, "I/O error during CardDAV request sending");
386 387 388
}

static void process_auth_requested_from_carddav_request(void *data, belle_sip_auth_event_t *event) {
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	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);
	LinphoneCore *lc = cdc->friend_list->lc;
	const MSList *auth_infos = linphone_core_get_auth_info_list(lc);
	
	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);
				break;
			}
		}
		auth_infos = ms_list_next(auth_infos);
	}
	
	if (!auth_infos) {
		ms_error("Authentication requested during CardDAV request sending, and username/password weren't provided");
		linphone_carddav_sync_done(query->context, FALSE, "Authentication requested during CardDAV request sending, and username/password weren't provided");
		linphone_carddav_query_free(query);
	}
416 417 418 419 420 421 422 423 424 425
}

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;

	uri = belle_generic_uri_parse(query->url);
	if (!uri) {
426 427 428 429
		LinphoneCardDavContext *cdc = query->context;
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, "Could not send request, URL is invalid");
		}
430 431 432
		belle_sip_error("Could not send request, URL %s is invalid", query->url);
		return;
	}
433 434
	req = belle_http_request_create(query->method, uri, belle_sip_header_content_type_create("application", "xml; charset=utf-8"), NULL);
	
435
	if (!req) {
436 437 438 439
		LinphoneCardDavContext *cdc = query->context;
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, "Could not create belle_http_request_t");
		}
440 441 442 443 444
		belle_sip_object_unref(uri);
		belle_sip_error("Could not create belle_http_request_t");
		return;
	}
	
445 446 447 448 449 450 451 452
	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", "*"));
	}
	
453 454 455 456
	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);
	}
457 458 459 460
	
	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;
461
	query->http_request_listener = belle_http_request_listener_create_from_callbacks(&cbs, query);
462
	belle_http_provider_send_request(query->context->friend_list->lc->http_provider, req, query->http_request_listener);
463 464
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static LinphoneCardDavQuery* linphone_carddav_create_put_query(LinphoneCardDavContext *cdc, LinphoneVCard *lvc) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = NULL;
	query->ifmatch = linphone_vcard_get_etag(lvc);
	query->body = linphone_vcard_as_vcard4_string(lvc);
	query->method = "PUT";
	query->url = linphone_vcard_get_url(lvc);
	query->type = LinphoneCardDavQueryTypePut;
	return query;
}

static char* generate_url_from_server_address_and_uid(const char *server_url) {
	if (server_url) {
		char uuid[32];
		if (sal_generate_uuid(uuid, sizeof(uuid)) == 0) {
			char url[300];
			snprintf(url, sizeof(url), "%s/linphone-%s.vcf", server_url, uuid);
			ms_debug("Generated url is %s", url);
			return ms_strdup(url);
		}
	}
	return NULL;
}

490
void linphone_carddav_put_vcard(LinphoneCardDavContext *cdc, LinphoneFriend *lf) {
491 492 493 494 495
	LinphoneVCard *lvc = linphone_friend_get_vcard(lf);
	if (lvc && linphone_vcard_get_uid(lvc)) {
		LinphoneCardDavQuery *query = NULL;
		
		if (!linphone_vcard_get_url(lvc)) {
496
			char *url = generate_url_from_server_address_and_uid(cdc->friend_list->uri);
497 498 499 500 501
			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
502
				ms_warning("%s", msg);
503 504 505 506 507
				if (cdc && cdc->sync_done_cb) {
					cdc->sync_done_cb(cdc, FALSE, msg);
				}
				return;
			}
508 509 510 511 512 513 514 515 516
		}
		
		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) {
			msg = "LinphoneVCard is NULL";
517
		} else if (!linphone_vcard_get_uid(lvc)) {
518
			msg = "LinphoneVCard doesn't have an UID";
519 520
		} else {
			msg = "Unknown error";
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
		}
		
		if (msg) {
			ms_error("%s", msg);
		}
		
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, msg);
		}
	}
}

static LinphoneCardDavQuery* linphone_carddav_create_delete_query(LinphoneCardDavContext *cdc, LinphoneVCard *lvc) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = NULL;
	query->ifmatch = linphone_vcard_get_etag(lvc);
538
	query->body = NULL;
539 540 541 542
	query->method = "DELETE";
	query->url = linphone_vcard_get_url(lvc);
	query->type = LinphoneCardDavQueryTypeDelete;
	return query;
543 544 545
}

void linphone_carddav_delete_vcard(LinphoneCardDavContext *cdc, LinphoneFriend *lf) {
546 547 548 549 550
	LinphoneVCard *lvc = linphone_friend_get_vcard(lf);
	if (lvc && linphone_vcard_get_uid(lvc) && linphone_vcard_get_etag(lvc)) {
		LinphoneCardDavQuery *query = NULL;
		
		if (!linphone_vcard_get_url(lvc)) {
551
			char *url = generate_url_from_server_address_and_uid(cdc->friend_list->uri);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
			linphone_vcard_set_url(lvc, url);
			ms_free(url);
		}
		
		query = linphone_carddav_create_delete_query(cdc, lvc);
		linphone_carddav_send_query(query);
	} else {
		const char *msg = NULL;
		if (!lvc) {
			msg = "LinphoneVCard is NULL";
		} else if (!linphone_vcard_get_uid(lvc)) {
			msg = "LinphoneVCard doesn't have an UID";
		} else if (!linphone_vcard_get_etag(lvc)) {
			msg = "LinphoneVCard doesn't have an eTag";
		}
		
		if (msg) {
			ms_error("%s", msg);
		}
		
		if (cdc && cdc->sync_done_cb) {
			cdc->sync_done_cb(cdc, FALSE, msg);
		}
	}
576 577 578 579 580 581
}

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

582 583 584 585 586 587 588 589 590 591 592 593
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;
}

594 595 596 597
static LinphoneCardDavQuery* linphone_carddav_create_propfind_query(LinphoneCardDavContext *cdc) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
	query->context = cdc;
	query->depth = "0";
598
	query->ifmatch = NULL;
599 600
	query->body = "<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\"><d:prop><cs:getctag /></d:prop></d:propfind>";
	query->method = "PROPFIND";
601
	query->url = cdc->friend_list->uri;
602 603 604 605 606 607 608 609 610 611 612 613 614
	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";
615
	query->ifmatch = NULL;
616
	query->body = "<card:addressbook-query xmlns:d=\"DAV:\" xmlns:card=\"urn:ietf:params:xml:ns:carddav\"><d:prop><d:getetag /></d:prop></card:addressbook-query>";
617
	query->method = "REPORT";
618
	query->url = cdc->friend_list->uri;
619 620 621 622 623 624 625 626 627 628 629
	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);
}

static LinphoneCardDavQuery* linphone_carddav_create_addressbook_multiget_query(LinphoneCardDavContext *cdc, MSList *vcards) {
	LinphoneCardDavQuery *query = (LinphoneCardDavQuery *)ms_new0(LinphoneCardDavQuery, 1);
Sylvain Berfini's avatar
Sylvain Berfini committed
630
	char *body = (char *)ms_malloc((ms_list_size(vcards) + 1) * 300 * sizeof(char));
631 632
	MSList *iterator = vcards;
	
633 634
	query->context = cdc;
	query->depth = "1";
635
	query->ifmatch = NULL;
636
	query->method = "REPORT";
637
	query->url = cdc->friend_list->uri;
638
	query->type = LinphoneCardDavQueryTypeAddressbookMultiget;
639

640
	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>");
641 642
	while (iterator) {
		LinphoneCardDavResponse *response = (LinphoneCardDavResponse *)iterator->data;
643
		if (response) {
644 645
			char temp_body[300];
			snprintf(temp_body, sizeof(temp_body), "<d:href>%s</d:href>", response->url);
646 647 648
			sprintf(body, "%s%s", body, temp_body);
			iterator = ms_list_next(iterator);
		}
649 650 651
	}
	sprintf(body, "%s%s", body, "</card:addressbook-multiget>");
	query->body = ms_strdup(body);
Sylvain Berfini's avatar
Sylvain Berfini committed
652
	ms_free(body);
653
	
654 655 656 657 658 659 660
	return query;
}

void linphone_carddav_pull_vcards(LinphoneCardDavContext *cdc, MSList *vcards_to_pull) {
	LinphoneCardDavQuery *query = linphone_carddav_create_addressbook_multiget_query(cdc, vcards_to_pull);
	linphone_carddav_send_query(query);
}