vcard.cc 9.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

20 21
#include "vcard.h"
#include "belcard/belcard.hpp"
22
#include "belcard/belcard_parser.hpp"
23
#include "sal/sal.h"
24
#include <bctoolbox/crypto.h>
25

26
struct _LinphoneVcard {
27
	shared_ptr<belcard::BelCard> belCard;
28 29
	char *etag;
	char *url;
30
	unsigned char *md5;
31 32
};

33 34 35 36
#ifdef __cplusplus
extern "C" {
#endif

37 38
LinphoneVcard* linphone_vcard_new(void) {
	LinphoneVcard* vCard = (LinphoneVcard*) ms_new0(LinphoneVcard, 1);
39 40
	vCard->belCard = belcard::BelCardGeneric::create<belcard::BelCard>();
	return vCard;
41 42
}

43
void linphone_vcard_free(LinphoneVcard *vCard) {
44 45
	if (!vCard) return;
	
46 47
	vCard->belCard.reset();
	ms_free(vCard);
48 49
}

50
MSList* linphone_vcard_list_from_vcard4_file(const char *filename) {
51
	MSList *result = NULL;
52
	if (filename && ortp_file_exist(filename) == 0) {
53 54
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCardList> belCards = parser.parseFile(filename);
55
		if (belCards) {
56 57
			for (auto it = belCards->getCards().begin(); it != belCards->getCards().end(); ++it) {
				shared_ptr<belcard::BelCard> belcard = (*it);
58
				LinphoneVcard *vCard = linphone_vcard_new();
59 60
				vCard->belCard = belcard;
				result = ms_list_append(result, vCard);
61 62 63 64 65 66
			}
		}
	}
	return result;
}

67
MSList* linphone_vcard_list_from_vcard4_buffer(const char *buffer) {
68 69
	MSList *result = NULL;
	if (buffer) {
70 71
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCardList> belCards = parser.parse(buffer);
72 73 74
		if (belCards) {
			for (auto it = belCards->getCards().begin(); it != belCards->getCards().end(); ++it) {
				shared_ptr<belcard::BelCard> belCard = (*it);
75
				LinphoneVcard *vCard = linphone_vcard_new();
76 77 78 79 80 81 82 83
				vCard->belCard = belCard;
				result = ms_list_append(result, vCard);
			}
		}
	}
	return result;
}

84 85
LinphoneVcard* linphone_vcard_new_from_vcard4_buffer(const char *buffer) {
	LinphoneVcard *vCard = NULL;
86
	if (buffer) {
87 88
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCard> belCard = parser.parseOne(buffer);
89 90 91
		if (belCard) {
			vCard = linphone_vcard_new();
			vCard->belCard = belCard;
Sylvain Berfini's avatar
Sylvain Berfini committed
92 93
		} else {
			ms_error("Couldn't parse buffer %s", buffer);
94 95 96 97 98
		}
	}
	return vCard;
}

99
const char * linphone_vcard_as_vcard4_string(LinphoneVcard *vCard) {
100 101
	if (!vCard) return NULL;
	
102
	return vCard->belCard->toFoldedString().c_str();
103 104
}

105
void linphone_vcard_set_full_name(LinphoneVcard *vCard, const char *name) {
106 107
	if (!vCard || !name) return;
	
108 109
	shared_ptr<belcard::BelCardFullName> fn = belcard::BelCardGeneric::create<belcard::BelCardFullName>();
	fn->setValue(name);
110
	vCard->belCard->setFullName(fn);
111 112
}

113
const char* linphone_vcard_get_full_name(const LinphoneVcard *vCard) {
114 115
	if (!vCard) return NULL;
	
116
	const char *result = vCard->belCard->getFullName() ? vCard->belCard->getFullName()->getValue().c_str() : NULL;
117
	return result;
118 119
}

120
void linphone_vcard_add_sip_address(LinphoneVcard *vCard, const char *sip_address) {
121 122
	if (!vCard || !sip_address) return;
	
123 124 125 126 127
	shared_ptr<belcard::BelCardImpp> impp = belcard::BelCardGeneric::create<belcard::BelCardImpp>();
	impp->setValue(sip_address);
	vCard->belCard->addImpp(impp);
}

128
void linphone_vcard_remove_sip_address(LinphoneVcard *vCard, const char *sip_address) {
129 130 131 132 133 134 135 136 137 138 139
	if (!vCard) return;
	
	for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
		const char *value = (*it)->getValue().c_str();
		if (strcmp(value, sip_address) == 0) {
			vCard->belCard->removeImpp(*it);
			break;
		}
	}
}

140
void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_address) {
141 142 143 144 145 146 147 148 149 150 151 152
	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);
	}
}

153
MSList* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard) {
154
	MSList *result = NULL;
155 156
	if (!vCard) return NULL;
	
157
	for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
158 159 160 161 162 163
		const char *value = (*it)->getValue().c_str();
		if (strncmp(value, "sip:", 4) == 0) {
			result = ms_list_append(result, (char *)value);
		}
	}
	return result;
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
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;
	
	for (auto it = vCard->belCard->getPhoneNumbers().begin(); it != vCard->belCard->getPhoneNumbers().end(); ++it) {
		const char *value = (*it)->getValue().c_str();
		if (strcmp(value, phone) == 0) {
			vCard->belCard->removePhoneNumber(*it);
			break;
		}
	}
}

MSList* linphone_vcard_get_phone_numbers(const LinphoneVcard *vCard) {
	MSList *result = NULL;
	if (!vCard) return NULL;
	
	for (auto it = vCard->belCard->getPhoneNumbers().begin(); it != vCard->belCard->getPhoneNumbers().end(); ++it) {
		const char *value = (*it)->getValue().c_str();
		result = ms_list_append(result, (char *)value);
	}
	return result;
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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;
}

219
bool_t linphone_vcard_generate_unique_id(LinphoneVcard *vCard) {
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	char uuid[64];
	
	if (vCard) {
		if (linphone_vcard_get_uid(vCard)) {
			return FALSE;
		}
		if (sal_generate_uuid(uuid, sizeof(uuid)) == 0) {
			char vcard_uuid[sizeof(uuid)+4];
			snprintf(vcard_uuid, sizeof(vcard_uuid), "urn:%s", uuid);
			linphone_vcard_set_uid(vCard, ms_strdup(vcard_uuid));
			return TRUE;
		}
	}
	return FALSE;
}

236
void linphone_vcard_set_uid(LinphoneVcard *vCard, const char *uid) {
237 238 239 240 241 242 243
	if (!vCard || !uid) return;
	
	shared_ptr<belcard::BelCardUniqueId> uniqueId = belcard::BelCardGeneric::create<belcard::BelCardUniqueId>();
	uniqueId->setValue(uid);
	vCard->belCard->setUniqueId(uniqueId);
}

244
const char* linphone_vcard_get_uid(const LinphoneVcard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
245
	if (vCard && vCard->belCard->getUniqueId()) {
246 247 248 249 250
		return vCard->belCard->getUniqueId()->getValue().c_str();
	}
	return NULL;
}

251
void linphone_vcard_set_etag(LinphoneVcard *vCard, const char * etag) {
252 253 254 255 256 257
	if (!vCard) {
		return;
	}
	if (vCard->etag) {
		ms_free(vCard->etag);
	}
258 259 260
	vCard->etag = ms_strdup(etag);
}

261
const char* linphone_vcard_get_etag(const LinphoneVcard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
262
	if (!vCard) return NULL;
263 264 265
	return vCard->etag;
}

266
void linphone_vcard_set_url(LinphoneVcard *vCard, const char * url) {
267 268 269 270 271 272
	if (!vCard) {
		return;
	}
	if (vCard->url) {
		ms_free(vCard->url);
	}
273 274 275
	vCard->url = ms_strdup(url);
}

276
const char* linphone_vcard_get_url(const LinphoneVcard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
277
	if (!vCard) return NULL;
278
	return vCard->url;
279 280
}

281 282
#define VCARD_MD5_HASH_SIZE 16

283
void linphone_vcard_compute_md5_hash(LinphoneVcard *vCard) {
284
	unsigned char digest[VCARD_MD5_HASH_SIZE];
285
	const char *text = NULL;
286 287 288
	if (!vCard) {
		return;
	}
289
	text = linphone_vcard_as_vcard4_string(vCard);
jehan's avatar
jehan committed
290
	bctbx_md5((unsigned char *)text, strlen(text), digest);
291 292
	vCard->md5 = (unsigned char *)ms_malloc(sizeof(digest));
	memcpy(vCard->md5, digest, sizeof(digest));
293 294
}

295
bool_t linphone_vcard_compare_md5_hash(LinphoneVcard *vCard) {
296 297 298 299 300 301
	unsigned char *previous_md5 = vCard->md5;
	unsigned char *new_md5 = NULL;
	int result = -1;
	
	if (!previous_md5) {
		return result;
302
	}
303 304 305
	
	linphone_vcard_compute_md5_hash(vCard);
	new_md5 = vCard->md5;
306
	result = memcmp(new_md5, previous_md5, VCARD_MD5_HASH_SIZE);
307 308 309 310
	
	ms_free(previous_md5);
	ms_free(new_md5);
	return result;
311 312
}

313 314 315
#ifdef __cplusplus
}
#endif