vcard.cc 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
vcard.cc
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 <bctoolbox/crypto.h>
21 22 23 24

#include <belcard/belcard_parser.hpp>
#include <belcard/belcard.hpp>

25
#include "linphone/factory.h"
26
#include "linphone/wrapper_utils.h"
27

28
#include "c-wrapper/c-wrapper.h"
29
#include "c-wrapper/internal/c-sal.h"
30 31
#include "vcard_private.h"

Ghislain MARY's avatar
Ghislain MARY committed
32 33
#define VCARD_MD5_HASH_SIZE 16

34
using namespace std;
Ghislain MARY's avatar
Ghislain MARY committed
35

36
struct _LinphoneVcardContext {
37
	shared_ptr<belcard::BelCardParser> parser;
38 39 40 41 42 43 44
	void *user_data;
};

extern "C" {

LinphoneVcardContext* linphone_vcard_context_new(void) {
	LinphoneVcardContext* context = ms_new0(LinphoneVcardContext, 1);
45
	new (&context->parser) shared_ptr<belcard::BelCardParser>(belcard::BelCardParser::getInstance());
46 47 48 49 50 51
	context->user_data = NULL;
	return context;
}

void linphone_vcard_context_destroy(LinphoneVcardContext *context) {
	if (context) {
52
		context->parser.~shared_ptr<belcard::BelCardParser>();
53 54 55 56 57 58 59 60 61 62 63 64 65
		ms_free(context);
	}
}

void* linphone_vcard_context_get_user_data(const LinphoneVcardContext *context) {
	return context ? context->user_data : NULL;
}


void linphone_vcard_context_set_user_data(LinphoneVcardContext *context, void *data) {
	if (context) context->user_data = data;
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
} // extern "C"


struct _LinphoneVcard {
	belle_sip_object_t base;
	shared_ptr<belcard::BelCard> belCard;
	char *etag;
	char *url;
	unsigned char md5[VCARD_MD5_HASH_SIZE];
	bctbx_list_t *sip_addresses_cache;
};

extern "C" {

static void _linphone_vcard_uninit(LinphoneVcard *vCard) {
	if (vCard->etag) ms_free(vCard->etag);
	if (vCard->url) ms_free(vCard->url);
	linphone_vcard_clean_cache(vCard);
84
	vCard->belCard.~shared_ptr<belcard::BelCard>();
85 86
}

Ghislain MARY's avatar
Ghislain MARY committed
87
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphoneVcard);
88 89 90 91 92 93 94 95
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneVcard);
BELLE_SIP_INSTANCIATE_VPTR(LinphoneVcard, belle_sip_object_t,
	_linphone_vcard_uninit, // destroy
	NULL, // clone
	NULL, // Marshall
	FALSE
);

Simon Morlat's avatar
Simon Morlat committed
96
LinphoneVcard* _linphone_vcard_new(void) {
97
	LinphoneVcard* vCard = belle_sip_object_new(LinphoneVcard);
98
	new (&vCard->belCard) shared_ptr<belcard::BelCard>(belcard::BelCardGeneric::create<belcard::BelCard>());
99 100 101
	return vCard;
}

102 103 104 105
LinphoneVcard *linphone_vcard_new(void) {
	return _linphone_vcard_new();
}

106
static LinphoneVcard* linphone_vcard_new_from_belcard(shared_ptr<belcard::BelCard> belcard) {
107
	LinphoneVcard* vCard = belle_sip_object_new(LinphoneVcard);
108
	new (&vCard->belCard) shared_ptr<belcard::BelCard>(belcard);
109 110 111 112
	return vCard;
}

void linphone_vcard_free(LinphoneVcard *vCard) {
113 114 115 116 117 118 119 120 121
	belle_sip_object_unref((belle_sip_object_t *)vCard);
}

LinphoneVcard *linphone_vcard_ref(LinphoneVcard *vCard) {
	return (LinphoneVcard *)belle_sip_object_ref((belle_sip_object_t *)vCard);
}

void linphone_vcard_unref(LinphoneVcard *vCard) {
	belle_sip_object_unref((belle_sip_object_t *)vCard);
122 123
}

Ronan's avatar
Ronan committed
124 125 126
LinphoneVcard *linphone_vcard_clone(const LinphoneVcard *vCard) {
	LinphoneVcard *copy = belle_sip_object_new(LinphoneVcard);

127
	new (&copy->belCard) shared_ptr<belcard::BelCard>(belcard::BelCardParser::getInstance()->parseOne(vCard->belCard->toFoldedString()));
Ronan's avatar
Ronan committed
128 129 130 131 132 133 134 135 136

	if (vCard->url) copy->url = ms_strdup(vCard->url);
	if (vCard->etag) copy->etag = ms_strdup(vCard->etag);

	memcpy(copy->md5, vCard->md5, sizeof *vCard->md5);

	return copy;
}

137 138 139 140
bctbx_list_t* linphone_vcard_context_get_vcard_list_from_file(LinphoneVcardContext *context, const char *filename) {
	bctbx_list_t *result = NULL;
	if (context && filename) {
		if (!context->parser) {
141
			context->parser = belcard::BelCardParser::getInstance();
142 143 144
		}
		shared_ptr<belcard::BelCardList> belCards = context->parser->parseFile(filename);
		if (belCards) {
145 146
			for (auto &belCard : belCards->getCards())
				result = bctbx_list_append(result, linphone_vcard_new_from_belcard(belCard));
147 148 149 150 151 152 153 154 155
		}
	}
	return result;
}

bctbx_list_t* linphone_vcard_context_get_vcard_list_from_buffer(LinphoneVcardContext *context, const char *buffer) {
	bctbx_list_t *result = NULL;
	if (context && buffer) {
		if (!context->parser) {
156
			context->parser = belcard::BelCardParser::getInstance();
157 158 159
		}
		shared_ptr<belcard::BelCardList> belCards = context->parser->parse(buffer);
		if (belCards) {
160 161
			for (auto &belCard : belCards->getCards())
				result = bctbx_list_append(result, linphone_vcard_new_from_belcard(belCard));
162 163 164 165 166 167 168 169 170
		}
	}
	return result;
}

LinphoneVcard* linphone_vcard_context_get_vcard_from_buffer(LinphoneVcardContext *context, const char *buffer) {
	LinphoneVcard *vCard = NULL;
	if (context && buffer) {
		if (!context->parser) {
171
			context->parser = belcard::BelCardParser::getInstance();
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		}
		shared_ptr<belcard::BelCard> belCard = context->parser->parseOne(buffer);
		if (belCard) {
			vCard = linphone_vcard_new_from_belcard(belCard);
		} else {
			ms_error("Couldn't parse buffer %s", buffer);
		}
	}
	return vCard;
}

const char * linphone_vcard_as_vcard4_string(LinphoneVcard *vCard) {
	if (!vCard) return NULL;

	return vCard->belCard->toFoldedString().c_str();
}

189 190 191 192
void *linphone_vcard_get_belcard(LinphoneVcard *vcard) {
	return &vcard->belCard;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
void linphone_vcard_set_full_name(LinphoneVcard *vCard, const char *name) {
	if (!vCard || !name) return;

	if (vCard->belCard->getFullName()) {
		vCard->belCard->getFullName()->setValue(name);
	} else {
		shared_ptr<belcard::BelCardFullName> fn = belcard::BelCardGeneric::create<belcard::BelCardFullName>();
		fn->setValue(name);
		vCard->belCard->setFullName(fn);
	}
}

const char* linphone_vcard_get_full_name(const LinphoneVcard *vCard) {
	if (!vCard) return NULL;

	const char *result = vCard->belCard->getFullName() ? vCard->belCard->getFullName()->getValue().c_str() : NULL;
	return result;
}

212 213 214
void linphone_vcard_set_skip_validation(LinphoneVcard *vCard, bool_t skip) {
	if (!vCard || !vCard->belCard) return;

215
	vCard->belCard->setSkipFieldValidation((skip == TRUE) ? true : false);
216 217 218 219 220 221 222 223 224
}

bool_t linphone_vcard_get_skip_validation(const LinphoneVcard *vCard) {
	if (!vCard) return FALSE;

	bool_t result = vCard->belCard->getSkipFieldValidation();
	return result;
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
void linphone_vcard_set_family_name(LinphoneVcard *vCard, const char *name) {
	if (!vCard || !name) return;

	if (vCard->belCard->getName()) {
		vCard->belCard->getName()->setFamilyName(name);
	} else {
		shared_ptr<belcard::BelCardName> n = belcard::BelCardGeneric::create<belcard::BelCardName>();
		n->setFamilyName(name);
		vCard->belCard->setName(n);
	}
}

const char* linphone_vcard_get_family_name(const LinphoneVcard *vCard) {
	if (!vCard) return NULL;

	const char *result = vCard->belCard->getName() ? vCard->belCard->getName()->getFamilyName().c_str() : NULL;
	return result;
}

void linphone_vcard_set_given_name(LinphoneVcard *vCard, const char *name) {
	if (!vCard || !name) return;

	if (vCard->belCard->getName()) {
		vCard->belCard->getName()->setGivenName(name);
	} else {
		shared_ptr<belcard::BelCardName> n = belcard::BelCardGeneric::create<belcard::BelCardName>();
		n->setGivenName(name);
		vCard->belCard->setName(n);
	}
}

const char* linphone_vcard_get_given_name(const LinphoneVcard *vCard) {
	if (!vCard) return NULL;

	const char *result = vCard->belCard->getName() ? vCard->belCard->getName()->getGivenName().c_str() : NULL;
	return result;
}

void linphone_vcard_add_sip_address(LinphoneVcard *vCard, const char *sip_address) {
	if (!vCard || !sip_address) return;

	shared_ptr<belcard::BelCardImpp> impp = belcard::BelCardGeneric::create<belcard::BelCardImpp>();
	impp->setValue(sip_address);
	vCard->belCard->addImpp(impp);
}

void linphone_vcard_remove_sip_address(LinphoneVcard *vCard, const char *sip_address) {
	if (!vCard) return;

274 275
	for (auto &impp : vCard->belCard->getImpp()) {
		const char *value = impp->getValue().c_str();
276
		if (strcmp(value, sip_address) == 0) {
277
			vCard->belCard->removeImpp(impp);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
			break;
		}
	}
}

void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_address) {
	if (!vCard || !sip_address) return;

	if (vCard->belCard->getImpp().size() > 0) {
		const shared_ptr<belcard::BelCardImpp> impp = vCard->belCard->getImpp().front();
		impp->setValue(sip_address);
	} else {
		shared_ptr<belcard::BelCardImpp> impp = belcard::BelCardGeneric::create<belcard::BelCardImpp>();
		impp->setValue(sip_address);
		vCard->belCard->addImpp(impp);
	}
}

296
const bctbx_list_t* linphone_vcard_get_sip_addresses(LinphoneVcard *vCard) {
297
	if (!vCard) return NULL;
298
	if (!vCard->sip_addresses_cache) {
299 300
		for (auto &impp : vCard->belCard->getImpp()) {
			LinphoneAddress* addr = linphone_address_new(impp->getValue().c_str());
301 302 303
			if (addr) {
				vCard->sip_addresses_cache = bctbx_list_append(vCard->sip_addresses_cache, addr);
			}
304 305
		}
	}
306
	return vCard->sip_addresses_cache;
307 308 309 310 311 312 313 314 315 316 317 318 319 320
}

void linphone_vcard_add_phone_number(LinphoneVcard *vCard, const char *phone) {
	if (!vCard || !phone) return;

	shared_ptr<belcard::BelCardPhoneNumber> phone_number = belcard::BelCardGeneric::create<belcard::BelCardPhoneNumber>();
	phone_number->setValue(phone);
	vCard->belCard->addPhoneNumber(phone_number);
}

void linphone_vcard_remove_phone_number(LinphoneVcard *vCard, const char *phone) {
	if (!vCard) return;

	shared_ptr<belcard::BelCardPhoneNumber> tel;
321 322
	for (auto &phoneNumber : vCard->belCard->getPhoneNumbers()) {
		const char *value = phoneNumber->getValue().c_str();
323
		if (strcmp(value, phone) == 0) {
324
			vCard->belCard->removePhoneNumber(phoneNumber);
325 326 327 328 329 330 331 332 333
			break;
		}
	}
}

bctbx_list_t* linphone_vcard_get_phone_numbers(const LinphoneVcard *vCard) {
	bctbx_list_t *result = NULL;
	if (!vCard) return NULL;

334 335
	for (auto &phoneNumber : vCard->belCard->getPhoneNumbers()) {
		const char *value = phoneNumber->getValue().c_str();
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
		result = bctbx_list_append(result, (char *)value);
	}
	return result;
}

void linphone_vcard_set_organization(LinphoneVcard *vCard, const char *organization) {
	if (!vCard) return;

	if (vCard->belCard->getOrganizations().size() > 0) {
		const shared_ptr<belcard::BelCardOrganization> org = vCard->belCard->getOrganizations().front();
		org->setValue(organization);
	} else {
		shared_ptr<belcard::BelCardOrganization> org = belcard::BelCardGeneric::create<belcard::BelCardOrganization>();
		org->setValue(organization);
		vCard->belCard->addOrganization(org);
	}
}

const char* linphone_vcard_get_organization(const LinphoneVcard *vCard) {
	if (vCard && vCard->belCard->getOrganizations().size() > 0) {
		const shared_ptr<belcard::BelCardOrganization> org = vCard->belCard->getOrganizations().front();
		return org->getValue().c_str();
	}

	return NULL;
}

bool_t linphone_vcard_generate_unique_id(LinphoneVcard *vCard) {
	if (vCard) {
		if (linphone_vcard_get_uid(vCard)) {
			return FALSE;
		}
Ghislain MARY's avatar
Ghislain MARY committed
368 369 370
		string uuid = "urn:" + LinphonePrivate::Sal::generateUuid();
		linphone_vcard_set_uid(vCard, uuid.c_str());
		return TRUE;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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 416 417 418 419 420 421 422 423
	}
	return FALSE;
}

void linphone_vcard_set_uid(LinphoneVcard *vCard, const char *uid) {
	if (!vCard || !uid) return;

	shared_ptr<belcard::BelCardUniqueId> uniqueId = belcard::BelCardGeneric::create<belcard::BelCardUniqueId>();
	uniqueId->setValue(uid);
	vCard->belCard->setUniqueId(uniqueId);
}

const char* linphone_vcard_get_uid(const LinphoneVcard *vCard) {
	if (vCard && vCard->belCard->getUniqueId()) {
		return vCard->belCard->getUniqueId()->getValue().c_str();
	}
	return NULL;
}

void linphone_vcard_set_etag(LinphoneVcard *vCard, const char * etag) {
	if (!vCard) {
		return;
	}
	if (vCard->etag) {
		ms_free(vCard->etag);
		vCard->etag = NULL;
	}
	vCard->etag = ms_strdup(etag);
}

const char* linphone_vcard_get_etag(const LinphoneVcard *vCard) {
	if (!vCard) return NULL;
	return vCard->etag;
}

void linphone_vcard_set_url(LinphoneVcard *vCard, const char * url) {
	if (!vCard) {
		return;
	}
	if (vCard->url) {
		ms_free(vCard->url);
		vCard->url = NULL;
	}
	vCard->url = ms_strdup(url);
}

const char* linphone_vcard_get_url(const LinphoneVcard *vCard) {
	if (!vCard) return NULL;
	return vCard->url;
}

void linphone_vcard_compute_md5_hash(LinphoneVcard *vCard) {
	const char *text = NULL;
Ghislain MARY's avatar
Ghislain MARY committed
424
	if (!vCard) return;
425
	text = linphone_vcard_as_vcard4_string(vCard);
Ghislain MARY's avatar
Ghislain MARY committed
426
	bctbx_md5((unsigned char *)text, strlen(text), vCard->md5);
427 428 429
}

bool_t linphone_vcard_compare_md5_hash(LinphoneVcard *vCard) {
Ghislain MARY's avatar
Ghislain MARY committed
430 431
	unsigned char previous_md5[VCARD_MD5_HASH_SIZE];
	memcpy(previous_md5, vCard->md5, VCARD_MD5_HASH_SIZE);
432
	linphone_vcard_compute_md5_hash(vCard);
433
	return !!memcmp(vCard->md5, previous_md5, VCARD_MD5_HASH_SIZE);
434 435 436 437 438 439
}

bool_t linphone_core_vcard_supported(void) {
	return TRUE;
}

440 441 442 443 444
void linphone_vcard_clean_cache(LinphoneVcard *vCard) {
	if (vCard->sip_addresses_cache) bctbx_list_free_with_data(vCard->sip_addresses_cache, (void (*)(void*))linphone_address_unref);
	vCard->sip_addresses_cache = NULL;
}

445
} // extern "C"