1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 74 75 76 77 78 79 80 81 82 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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 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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 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 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* OBEX Server
*
* Copyright (C) 2009-2021 Intel Corporation
* Copyright (C) 2007-2021 Marcel Holtmann <marcel@holtmann.org>
* Copyright (C) 2021 Dylan Van Assche <me@dylanvanassche.be>
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <glib.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libebook/libebook.h>
#include <libedataserver/libedataserver.h>
#include "obexd/src/log.h"
#include "phonebook.h"
#define CONNECTION_TIMEOUT 30 // seconds
#define PB_FORMAT_VCARD21 0
#define PB_FORMAT_VCARD30 1
#define PB_FORMAT_NONE 2
ESourceRegistry *registry;
ESource *address_book;
EBookClient *book_client;
struct query_context {
const struct apparam_field *params;
phonebook_cb contacts_cb;
phonebook_entry_cb entry_cb;
phonebook_cache_ready_cb ready_cb;
gchar *query;
unsigned int count;
GString *buf;
char *uid;
unsigned queued_calls;
void *user_data;
gboolean canceled;
};
static const char *attribute_mask[] = {
/* 0 */ "VERSION",
"FN",
"N",
"PHOTO",
"BDAY",
"ADR",
"LABEL",
"TEL",
/* 8 */ "EMAIL",
"MAILER",
"TZ",
"GEO",
"TITLE",
"ROLE",
"LOGO",
"AGENT",
/* 16 */ "ORG",
"NOTE",
"REV",
"SOUND",
"URL",
"UID",
"KEY",
"NICKNAME",
/* 24 */ "CATEGORIES",
"PROID",
"CLASS",
"SORT-STRING",
/* 28 */ "X-IRMC-CALL-DATETIME",
NULL
};
static void free_query_context(struct query_context *data)
{
g_free(data->uid);
if (data->buf != NULL)
g_string_free(data->buf, TRUE);
if (data->query != NULL)
g_free(data->query);
g_free(data);
}
static char *evcard_to_string(EVCard *evcard, unsigned int format,
uint64_t filter)
{
EVCard *evcard2;
GList *l;
char *vcard;
if (!filter)
return e_vcard_to_string(evcard, format);
/*
* Mandatory attributes for vCard 2.1 are VERSION ,N and TEL.
* Mandatory attributes for vCard 3.0 are VERSION, N, FN and TEL
*/
filter = format == EVC_FORMAT_VCARD_30 ? filter | 0x87: filter | 0x85;
l = e_vcard_get_attributes(evcard);
evcard2 = e_vcard_new();
for (; l; l = g_list_next(l)) {
EVCardAttribute *attrib = l->data;
const char *name;
int i;
if (!attrib)
continue;
name = e_vcard_attribute_get_name(attrib);
for (i = 0; attribute_mask[i] != NULL; i++) {
if (!(filter & (1 << i)))
continue;
if (g_strcmp0(name, attribute_mask[i]) != 0)
continue;
e_vcard_add_attribute(evcard2,
e_vcard_attribute_copy(attrib));
}
}
vcard = e_vcard_to_string(evcard2, format);
g_object_unref(evcard2);
return vcard;
}
char *phonebook_set_folder(const char *current_folder, const char *new_folder,
uint8_t flags, int *err)
{
gboolean root, child;
char *fullname = NULL, *tmp1, *tmp2, *base;
int ret = 0, len;
root = (g_strcmp0("/", current_folder) == 0);
child = (new_folder && strlen(new_folder) != 0);
/* Evolution back-end will support /telecom/pb folder only */
switch (flags) {
case 0x02:
/* Go back to root */
if (!child) {
fullname = g_strdup("/");
goto done;
}
/* Go down 1 level */
fullname = g_build_filename(current_folder, new_folder, NULL);
if (strcmp(PB_TELECOM_FOLDER, fullname) != 0 &&
strcmp(PB_CONTACTS_FOLDER, fullname) != 0) {
g_free(fullname);
fullname = NULL;
ret = -ENOENT;
}
break;
case 0x03:
/* Go up 1 level */
if (root) {
/* Already root */
ret = -EBADR;
goto done;
}
/*
* Removing one level of the current folder. Current folder
* contains AT LEAST one level since it is not at root folder.
* Use glib utility functions to handle invalid chars in the
* folder path properly.
*/
tmp1 = g_path_get_basename(current_folder);
tmp2 = g_strrstr(current_folder, tmp1);
len = tmp2 - (current_folder + 1);
g_free(tmp1);
if (len == 0)
base = g_strdup("/");
else
base = g_strndup(current_folder, len);
/* Return one level only */
if (!child) {
fullname = base;
goto done;
}
fullname = g_build_filename(base, new_folder, NULL);
if (strcmp(fullname, PB_TELECOM_FOLDER) != 0 &&
strcmp(fullname, PB_CONTACTS_FOLDER) != 0) {
g_free(fullname);
fullname = NULL;
ret = -ENOENT;
}
g_free(base);
break;
default:
ret = -EBADR;
break;
}
done:
if (err)
*err = ret;
return fullname;
}
void phonebook_req_finalize(void *request)
{
/* Free resources after pull request */
struct query_context *data = request;
if (data->queued_calls == 0)
free_query_context(data);
else
data->canceled = TRUE;
}
void *phonebook_pull(const char *name, const struct apparam_field *params,
phonebook_cb cb, void *user_data, int *err)
{
struct query_context *data;
EBookQuery *query;
/* Request should be for '/telecom/pb.vcf', reject others */
if (g_strcmp0(PB_CONTACTS, name) != 0) {
if (err)
*err = -ENOENT;
return NULL;
}
data = g_new0(struct query_context, 1);
data->contacts_cb = cb;
data->params = params;
data->user_data = user_data;
data->buf = g_string_new("");
query = e_book_query_any_field_contains(""); // all contacts
data->query = e_book_query_to_string(query);
e_book_query_unref(query);
return data;
}
static void phonebook_pull_read_ready(GObject *source_object,
GAsyncResult *result, gpointer user_data)
{
struct query_context *data = user_data;
GSList *l = NULL;
GSList *contacts = NULL;
GError *gerr = NULL;
unsigned int count, maxcount;
/* Finish async call to retrieve contacts */
data->queued_calls--;
if (data->canceled)
goto canceled;
e_book_client_get_contacts_finish(E_BOOK_CLIENT(source_object),
result, &contacts, &gerr);
if (gerr != NULL) {
error("Failed to retrieve contacts, invalid query");
g_error_free(gerr);
goto done;
}
/*
* When MaxListCount is zero, PCE wants to know the number of used
* indexes in the phonebook of interest. All other parameters that
* may be present in the request shall be ignored.
*/
maxcount = data->params->maxlistcount;
if (maxcount == 0) {
data->count += g_slist_length(contacts);
goto done;
}
/*
* Convert each contact to a vCard and append the card to
* the buffer string.
*/
l = g_slist_nth(contacts, data->params->liststartoffset);
for (count = 0; l && count + data->count < maxcount;
l = g_slist_next(l), count++) {
EContact *contact = E_CONTACT(l->data);
EVCard *evcard = E_VCARD(contact);
char *vcard;
if (data->params->format == PB_FORMAT_VCARD30)
vcard = evcard_to_string(evcard, EVC_FORMAT_VCARD_30,
data->params->filter);
else if (data->params->format == PB_FORMAT_VCARD21)
vcard = evcard_to_string(evcard, EVC_FORMAT_VCARD_21,
data->params->filter);
else
error("unknown format: %d", data->params->format);
data->buf = g_string_append(data->buf, vcard);
data->buf = g_string_append(data->buf, "\r\n");
g_free(vcard);
}
DBG("collected %d contacts", count);
data->count += count;
g_slist_free_full(contacts, (GDestroyNotify) g_object_unref);
done:
if (data->queued_calls == 0) {
GString *buf = data->buf;
data->buf = NULL;
data->contacts_cb(buf->str, buf->len, data->count, 0, TRUE,
data->user_data);
g_string_free(buf, TRUE);
}
return;
canceled:
if (data->queued_calls == 0)
free_query_context(data);
}
int phonebook_pull_read(void *request)
{
struct query_context *data = request;
GError *gerr = NULL;
if (!data) {
error("Request data is empty");
return -ENOENT;
}
DBG("retrieving all contacts");
/* Fetch async contacts from default address book */
e_book_client_get_contacts(book_client, data->query, NULL,
(GAsyncReadyCallback) phonebook_pull_read_ready, data);
data->queued_calls++;
return 0;
}
static void phonebook_get_entry_ready(GObject *source_object,
GAsyncResult *result, gpointer user_data)
{
GError *gerr = NULL;
EContact *contact = NULL;
struct query_context *data = user_data;
EVCard *evcard;
char *vcard;
size_t len;
data->queued_calls--;
e_book_client_get_contact_finish(E_BOOK_CLIENT(source_object), result,
&contact, &gerr);
if (data->canceled)
goto done;
if (gerr != NULL) {
error("Getting contact failed: %s", gerr->message);
g_error_free(gerr);
goto done;
}
evcard = E_VCARD(contact);
if (data->params->format == PB_FORMAT_VCARD30)
vcard = evcard_to_string(evcard, EVC_FORMAT_VCARD_30,
data->params->filter);
else if (data->params->format == PB_FORMAT_VCARD21)
vcard = evcard_to_string(evcard, EVC_FORMAT_VCARD_21,
data->params->filter);
else
error("Unknown vCard format: %d", data->params->format);
len = vcard ? strlen(vcard) : 0;
data->count++;
data->contacts_cb(vcard, len, 1, 0, TRUE, data->user_data);
g_free(vcard);
DBG("retrieving entry successful");
done:
if (data->queued_calls == 0) {
if (data->count == 0)
data->contacts_cb(NULL, 0, 1, 0, TRUE,
data->user_data);
else if (data->canceled)
free_query_context(data);
}
g_object_unref(contact);
}
void *phonebook_get_entry(const char *folder, const char *id,
const struct apparam_field *params, phonebook_cb cb,
void *user_data, int *err)
{
struct query_context *data;
GSList *l;
DBG("retrieving entry: %s", id);
data = g_new0(struct query_context, 1);
data->contacts_cb = cb;
data->params = params;
data->user_data = user_data;
data->uid = g_strdup(id);
/* Fetch async contacts from default address book */
e_book_client_get_contact(book_client, data->uid, NULL,
(GAsyncReadyCallback) phonebook_get_entry_ready, data);
data->queued_calls++;
if (err)
*err = (data->queued_calls == 0 ? -ENOENT : 0);
return data;
}
static char *evcard_name_attribute_to_string(EVCard *evcard)
{
EVCardAttribute *attrib;
GList *l;
GString *name = NULL;
attrib = e_vcard_get_attribute(evcard, EVC_N);
if (!attrib)
return NULL;
for (l = e_vcard_attribute_get_values(attrib); l; l = l->next) {
const char *value = l->data;
if (!strlen(value))
continue;
if (!name)
name = g_string_new(value);
else {
name = g_string_append(name, ";");
name = g_string_append(name, l->data);
}
}
if (!name)
return NULL;
return g_string_free(name, FALSE);
}
static void phonebook_create_cache_ready(GObject *source_object,
GAsyncResult *result, gpointer user_data)
{
struct query_context *data = user_data;
GSList *l = NULL;
GSList *contacts = NULL;
GError *gerr = NULL;
data->queued_calls--;
if (data->canceled)
goto canceled;
e_book_client_get_contacts_finish(E_BOOK_CLIENT(source_object),
result, &contacts, &gerr);
if (gerr != NULL) {
error("Getting contacts failed: %s", gerr->message);
goto done;
}
for (l = contacts; l; l = g_slist_next(l)) {
EContact *contact = E_CONTACT(l->data);
EVCard *evcard = E_VCARD(contact);
EVCardAttribute *attrib;
char *uid, *tel, *name;
name = evcard_name_attribute_to_string(evcard);
if (!name)
continue;
attrib = e_vcard_get_attribute(evcard, EVC_UID);
if (!attrib)
continue;
uid = e_vcard_attribute_get_value(attrib);
if (!uid)
continue;
attrib = e_vcard_get_attribute(evcard, EVC_TEL);
if (attrib)
tel = e_vcard_attribute_get_value(attrib);
else
tel = g_strdup("");
data->entry_cb(uid, PHONEBOOK_INVALID_HANDLE, name, NULL,
tel, data->user_data);
g_free(name);
g_free(uid);
g_free(tel);
}
DBG("caching successful");
g_slist_free_full(contacts, (GDestroyNotify) g_object_unref);
done:
if (data->queued_calls == 0)
data->ready_cb(data->user_data);
return;
canceled:
if (data->queued_calls == 0)
free_query_context(data);
}
void *phonebook_create_cache(const char *name, phonebook_entry_cb entry_cb,
phonebook_cache_ready_cb ready_cb, void *user_data, int *err)
{
/* Build a cache of contacts */
struct query_context *data;
EBookQuery *query;
EContact *me;
EBookClient *me_client;
EVCard *evcard;
GError *gerr = NULL;
EVCardAttribute *attrib;
char *uid, *tel, *cname;
if (g_strcmp0(PB_CONTACTS_FOLDER, name) != 0) {
if (err)
*err = -ENOENT;
return NULL;
}
DBG("creating cache");
data = g_new0(struct query_context, 1);
data->entry_cb = entry_cb;
data->ready_cb = ready_cb;
data->user_data = user_data;
query = e_book_query_any_field_contains(""); // all contacts
data->query = e_book_query_to_string(query);
e_book_query_unref(query);
/* Myself as contact should always be 0.vcf if found in address book */
if (!e_book_client_get_self(registry, &me, &me_client, &gerr)) {
DBG("owner is not in address book: %s", gerr->message);
g_error_free(gerr);
goto next;
}
DBG("caching address book owner");
evcard = E_VCARD(me);
cname = evcard_name_attribute_to_string(evcard);
if (!cname)
cname = g_strdup("");
attrib = e_vcard_get_attribute(evcard, EVC_UID);
uid = e_vcard_attribute_get_value(attrib);
if (!uid)
uid = g_strdup("");
attrib = e_vcard_get_attribute(evcard, EVC_TEL);
if (attrib)
tel = e_vcard_attribute_get_value(attrib);
else
tel = g_strdup("");
data->entry_cb(uid, 0, cname, NULL, tel, data->user_data);
data->count++;
g_free(cname);
g_free(uid);
g_free(tel);
next:
/* Fetch async contacts from default address book */
DBG("caching contacts");
e_book_client_get_contacts(book_client, data->query, NULL,
(GAsyncReadyCallback) phonebook_create_cache_ready, data);
data->queued_calls++;
if (err)
*err = (data->queued_calls == 0 ? -ENOENT : 0);
return data;
}
int phonebook_init(void)
{
EClient *client;
GError *gerr = NULL;
/* Acquire ESource Registry */
registry = e_source_registry_new_sync(NULL, &gerr);
if (gerr != NULL) {
error("Unable to acquire registry: %s\n", gerr->message);
g_error_free(gerr);
return -1;
}
/* Get ref to default address book */
address_book = e_source_registry_ref_default_address_book(registry);
if (address_book == NULL) {
error("Unable to get reference to default address book");
return -2;
}
/* Allocate e-book client for address book */
gerr = NULL;
client = e_book_client_connect_sync(address_book, CONNECTION_TIMEOUT,
NULL, &gerr);
if (gerr != NULL || client == NULL) {
error("Cannot connect ebook client to EDS: %s",
gerr != NULL ? gerr->message : "NULL");
g_error_free(gerr);
return -3;
}
book_client = E_BOOK_CLIENT(client);
DBG("created address book client");
return 0;
}
void phonebook_exit(void)
{
g_object_unref(book_client);
g_object_unref(address_book);
g_object_unref(registry);
}
|