4 * Copyright 2019 Michael Rasmussen <mir@datanom.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 #include <glib/gprintf.h>
26 #include <vcard-parser.h>
33 VCardParserResponse state
;
39 static char *replaceWord(const char *s
, const char *oldW
, const char *newW
) {
42 int newWlen
= strlen(newW
);
43 int oldWlen
= strlen(oldW
);
45 for (i
= 0; s
[i
] != '\0'; i
++) {
46 if (strstr(&s
[i
], oldW
) == &s
[i
]) {
52 result
= (char *) malloc(i
+ cnt
* (newWlen
- oldWlen
) + 1);
56 if (strstr(s
, oldW
) == s
) {
57 strcpy(&result
[i
], newW
);
69 static gchar
* make_line(const gchar
* name
, const gchar
* value
) {
73 if (g_ascii_strncasecmp("TEL", name
, 3) == 0) {
74 line
= g_strdup_printf("%s;%s", name
, value
);
75 } else if (g_ascii_strncasecmp("ADR", name
, 3) == 0) {
76 line
= g_strdup_printf("%s;%s", name
, value
);
77 } else if (g_ascii_strncasecmp("PHOTO", name
, 3) == 0) {
78 line
= g_strdup_printf("%s;%s", name
, value
);
79 } else if (g_ascii_strncasecmp("EMAIL", name
, 3) == 0) {
80 line
= g_strdup_printf("%s;%s", name
, value
);
82 line
= g_strdup_printf("%s:%s", name
, value
);
89 static void hash_table_cb(gpointer key
, gpointer value
, gpointer data
) {
90 GSList
* list
= (GSList
*) value
;
91 gchar
** str
= (gchar
**) data
;
97 VCardProperty
* vp
= (VCardProperty
*) list
->data
;
99 gchar
* s0
= replaceWord(vp
->value
, "\n", "\n ");
100 gchar
* line
= make_line(vp
->name
, s0
);
105 s
= g_strdup_printf("%s\n%s", *str
, line
);
114 static gchar
** make_chunks(const gchar
* text
) {
115 gchar
** chunks
= NULL
;
116 gchar
* v_text
= g_strdup(text
);
119 gchar
** tmp
= g_strsplit(v_text
, "END:VCARD", 0);
121 guint lines
= g_strv_length(tmp
);
125 if (strlen(tmp
[lines
-1]) < 1) {
126 chunks
= g_new0(gchar
*, lines
);
129 chunks
= g_new0(gchar
*, lines
+ 1);
131 for(int i
= 0; i
< lines
; i
++) {
133 chunks
[i
] = g_strconcat(tmp
[i
], "END:VCARD", NULL
);
140 static VCardProperty
* VCardProperty_new(const gchar
* key
, const gchar
* value
) {
141 VCardProperty
* vp
= g_new0(VCardProperty
, 1);
142 vp
->name
= g_strdup(key
);
143 vp
->value
= g_strdup(value
);
148 static void remove_tel_prop(GSList
* list
, TelefonType type
) {
149 VCardProperty
* vp
= NULL
;
150 GSList
* search
= list
;
154 vp
= (VCardProperty
*) search
->data
;
156 gchar
* tel
= (gchar
*)vp
->value
;
160 if (g_strstr_len(tel
, -1, "TYPE=MOBIL")) {
161 list
= g_slist_remove(list
, vp
);
162 vcard_property_free(vp
);
166 if (g_strstr_len(tel
, -1, "TYPE=HOME")) {
167 list
= g_slist_remove(list
, vp
);
168 vcard_property_free(vp
);
172 if (g_strstr_len(tel
, -1, "TYPE=WORK")) {
173 list
= g_slist_remove(list
, vp
);
174 vcard_property_free(vp
);
180 search
= search
->next
;
185 static void remove_adr_prop(GSList
* list
, AddressType type
) {
186 VCardProperty
* vp
= NULL
;
187 GSList
* search
= list
;
191 vp
= (VCardProperty
*) search
->data
;
193 gchar
* adr
= (gchar
*)vp
->value
;
197 if (g_strstr_len(adr
, -1, "TYPE=HOME")) {
198 list
= g_slist_remove(list
, vp
);
199 vcard_property_free(vp
);
203 if (g_strstr_len(adr
, -1, "TYPE=WORK")) {
204 list
= g_slist_remove(list
, vp
);
205 vcard_property_free(vp
);
209 if (g_strstr_len(adr
, -1, "TYPE=OTHER")) {
210 list
= g_slist_remove(list
, vp
);
211 vcard_property_free(vp
);
217 search
= search
->next
;
222 static void remove_email_prop(GSList
* list
, EmailType type
) {
223 VCardProperty
* vp
= NULL
;
224 GSList
* search
= list
;
228 vp
= (VCardProperty
*) search
->data
;
230 gchar
* email
= (gchar
*)vp
->value
;
234 if (g_strstr_len(email
, -1, "TYPE=HOME")) {
235 list
= g_slist_remove(list
, vp
);
236 vcard_property_free(vp
);
240 if (g_strstr_len(email
, -1, "TYPE=WORK")) {
241 list
= g_slist_remove(list
, vp
);
242 vcard_property_free(vp
);
246 if (g_strstr_len(email
, -1, "TYPE=OTHER")) {
247 list
= g_slist_remove(list
, vp
);
248 vcard_property_free(vp
);
254 search
= search
->next
;
259 gchar
* VCard_get_as_string(VCard
* object
) {
261 if (object
== NULL
) return NULL
;
263 if (object
&& object
->vcard
) {
264 gchar
* version
= vcard_version_2_str(object
->version
);
265 gchar
* s
= g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version
);
267 g_hash_table_foreach(object
->vcard
, hash_table_cb
, &s
);
268 card
= g_strdup_printf("%s\nEND:VCARD", s
);
275 void vcard_slist_property_free(gpointer data
) {
276 if (data
== NULL
) return;
278 GSList
* list
= (GSList
*) data
;
279 g_slist_free_full(list
, (GDestroyNotify
) vcard_property_free
);
282 void vcard_property_free(gpointer data
) {
284 VCardProperty
* vp
= (VCardProperty
*) data
;
290 void print_slist(GSList
* list
) {
294 VCardProperty
* vp
= (VCardProperty
*) tmp
->data
;
295 g_print("Name: %s\n", vp
->name
);
296 g_print("Value: %s\n", vp
->value
);
301 static gpointer
slist_copy_func(gconstpointer src
, gpointer data
) {
302 const VCardProperty
* vp
= (const VCardProperty
*) src
;
303 VCardProperty
* new_vp
= VCardProperty_new(vp
->name
, vp
->value
);
305 return (gpointer
) new_vp
;
308 void VCard_free(VCard
* object
) {
309 if (object
== NULL
) return;
312 destroy_hash_table(object
->vcard
);
313 g_free(object
->etag
);
317 VCard
* VCard_version_new(VCardVersion version
, gboolean init
) {
318 VCard
* vc
= g_new0(VCard
, 1);
319 vc
->version
= version
;
321 vc
->vcard
= init_g_hash_table(vc
->version
);
322 gchar
* uuid
= g_uuid_string_random();
323 VCardProperty
* prop
= VCardProperty_new("UID", g_strdup(uuid
));
325 GSList
* uid
= g_slist_prepend(NULL
, prop
);
326 VCard_set_property(vc
, "UID", uid
);
333 return VCard_version_new(DEFAULT_VCARD_VERSION
, TRUE
);
336 GSList
* VCard_new_from_text(const gchar
* text
, VCardVersion version
, gboolean skip_broken
) {
340 if (!text
|| strlen(text
) < 1) return NULL
;
342 gchar
** chunks
= make_chunks(text
);
343 for (int i
= 0; i
< g_strv_length(chunks
); i
++) {
344 vc
= VCard_version_new(version
, FALSE
);
346 VCardParserResponse r
= vcard_parse_text(chunks
[i
], &vc
->version
, &vc
->vcard
);
347 if (r
!= VCARD_PARSER_OK
&& skip_broken
) {
352 list
= g_slist_prepend(list
, vc
);
359 GSList
* VCard_new_from_file(FILE* file
, VCardVersion version
, gboolean skip_broken
) {
364 GString
* text
= g_string_new(NULL
);
367 buf
= g_new0(gchar
, buf_size
);
368 while ((n
= fread(buf
, sizeof(char), buf_size
, file
)) == buf_size
) {
369 text
= g_string_append_len(text
, buf
, buf_size
);
371 buf
= g_new0(gchar
, buf_size
);
374 text
= g_string_append_len(text
, buf
, n
);
376 buf
= g_string_free_and_steal(text
);
377 list
= VCard_new_from_text(buf
, version
, skip_broken
);
384 void VCard_print_list(GSList
* list
) {
385 VCard_print_list_fd(list
, STDOUT_FILENO
);
388 void VCard_print_list_fd(GSList
* list
, int fd
) {
392 VCard
* object
= (VCard
*) tmp
->data
;
393 VCard_print_fd(object
, fd
);
395 ssize_t len
= strlen("\n");
396 if (write(fd
, "\n", len
) < len
)
397 g_warning(strerror(errno
));
404 void VCard_print(VCard
* object
){
405 VCard_print_fd(object
, STDOUT_FILENO
);
408 void VCard_print_fd(VCard
* object
, int fd
) {
409 gchar
*body
= NULL
, *s
= NULL
;
411 if (object
&& object
->vcard
&& fd
> 0) {
412 gchar
* version
= vcard_version_2_str(object
->version
);
413 s
= g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version
);
415 g_hash_table_foreach(object
->vcard
, hash_table_cb
, &s
);
416 body
= g_strdup_printf("%s\nEND:VCARD", s
);
418 ssize_t len
= strlen(body
);
419 if (write(fd
, body
, len
) < len
)
420 g_warning(strerror(errno
));
425 gchar
* VCard_get_state(VCard
* object
) {
426 gchar
* message
= NULL
;
428 if (!object
) return NULL
;
430 switch (object
->state
) {
431 case VCARD_PARSER_OK
:
432 message
= g_strdup("OK");
434 case VCARD_PARSER_VERSION_MISMATCH
:
435 message
= g_strdup("Version mismatch");
437 case VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH
:
438 message
= g_strdup("Attribute not supported in this version");
440 case VCARD_PARSER_BAD_FORMAT
:
441 message
= g_strdup("Bad input format");
443 case VCARD_PARSER_ERROR
:
444 message
= g_strdup("Unknown error");
447 message
= g_strdup("Unhandled error");
454 GSList
* VCard_get_property(VCard
* object
, const gchar
* property
) {
455 GSList
* values
= NULL
;
458 VCardProperty
* vp
= NULL
;
460 if (object
&& object
->vcard
&& property
) {
461 tmp
= g_hash_table_lookup(object
->vcard
, property
);
462 for (iter
= tmp
; iter
; iter
= g_slist_next(iter
)) {
463 vp
= (VCardProperty
*) iter
->data
;
464 VCardProperty
* prop
= VCardProperty_new(vp
->name
, vp
->value
);
465 values
= g_slist_prepend(values
, prop
);
472 void VCard_set_property(VCard
* object
, const gchar
* property
, GSList
* props
) {
473 GSList
*values
= NULL
;
475 if (object
&& object
->vcard
&& property
&& props
) {
476 //print_slist(props);
477 values
= g_hash_table_lookup(object
->vcard
, property
);
479 /* Remove before adding new */
480 g_hash_table_remove(object
->vcard
, property
);
482 g_hash_table_replace(object
->vcard
, g_strdup(property
), props
);
486 void VCard_set_etag(VCard
* vcard
, const gchar
* etag
) {
487 if (vcard
== NULL
) return;
492 vcard
->etag
= g_strdup(etag
);
495 void VCard_set_name(VCard
* vcard
, const gchar
* firstname
, const gchar
* lastname
) {
496 GSList
*fn_value
= NULL
;
497 GSList
*n_value
= NULL
;
499 if (vcard
== NULL
) return;
501 if (firstname
== NULL
|| lastname
== NULL
) {
502 VCard_set_property(vcard
, "N", NULL
);
503 VCard_set_property(vcard
, "FN", NULL
);
505 gchar
* name
= g_strconcat(firstname
, " ", lastname
, NULL
);
506 VCardProperty
* prop
= VCardProperty_new("FN", name
);
507 fn_value
= g_slist_prepend(fn_value
, prop
);
508 VCard_set_property(vcard
, "FN", fn_value
);
510 name
= g_strconcat(lastname
, ";", firstname
, ";", NULL
);
511 prop
= VCardProperty_new("N", name
);
512 n_value
= g_slist_prepend(n_value
, prop
);
513 VCard_set_property(vcard
, "N", n_value
);
518 void VCard_set_company(VCard
* vcard
, const gchar
* company
) {
519 GSList
* org_value
= NULL
;
520 if (vcard
== NULL
) return;
522 if (company
== NULL
) {
523 VCard_set_property(vcard
, "ORG", NULL
);
526 VCardProperty
* prop
= VCardProperty_new("ORG", company
);
527 org_value
= g_slist_prepend(org_value
, prop
);
528 VCard_set_property(vcard
, "ORG", org_value
);
531 void VCard_set_telefon(VCard
* vcard
, TelefonType type
, const gchar
* number
) {
533 GSList
* values
= NULL
;
534 if (vcard
== NULL
) return;
536 GSList
* tel_value
= VCard_get_property(vcard
, "TEL");
537 if (number
== NULL
) {
539 remove_tel_prop(tel_value
, type
);
540 VCard_set_property(vcard
, "TEL", tel_value
);
546 tel
= g_strconcat("TYPE=MOBIL,VOICE:", number
, NULL
);
549 tel
= g_strconcat("TYPE=HOME,VOICE:", number
, NULL
);
552 tel
= g_strconcat("TYPE=WORK,VOICE:", number
, NULL
);
556 values
= g_slist_copy_deep(tel_value
, slist_copy_func
, NULL
);
558 VCardProperty
* prop
= VCardProperty_new("TEL", tel
);
559 values
= g_slist_prepend(values
, prop
);
560 VCard_set_property(vcard
, "TEL", values
);
565 void VCard_set_address(VCard
* vcard
, AddressType type
, const gchar
* address
, const gchar
* state
, const gchar
* city
, const gchar
* zip
, const gchar
* country
) {
567 GSList
* values
= NULL
;
568 if (vcard
== NULL
) return;
570 GSList
* addr_value
= VCard_get_property(vcard
, "ADR");
571 if (address
== NULL
|| city
== NULL
|| zip
== NULL
|| country
== NULL
) {
573 remove_adr_prop(addr_value
, type
);
574 VCard_set_property(vcard
, "ADR", addr_value
);
579 state
= g_strconcat(state
, ";", NULL
);
581 state
= g_strdup("");
586 addr
= g_strdup_printf("TYPE=HOME:;;%s;%s;%s;%s;%s;", address
, city
, zip
, state
, country
);
587 //, address, ";", city, ";", zip, ";", state, ";", country, ";" , NULL);
590 addr
= g_strconcat("TYPE=WORK:;;", address
, ";", city
, ";", zip
, ";", state
, country
, ";" , NULL
);
593 addr
= g_strconcat("TYPE=OTHER:;;", address
, ";", city
, ";", zip
, ";", state
, country
, ";" , NULL
);
598 values
= g_slist_copy_deep(addr_value
, slist_copy_func
, NULL
);
600 VCardProperty
* prop
= VCardProperty_new("ADR", addr
);
601 values
= g_slist_prepend(values
, prop
);
602 VCard_set_property(vcard
, "ADR", values
);
607 void VCard_set_photo_uri(VCard
* vcard
, PhotoType type
, const gchar
* uri
) {
609 if (vcard
== NULL
) return;
611 GSList
* photo_value
= VCard_get_property(vcard
, "PHOTO");
614 VCard_set_property(vcard
, "PHOTO", NULL
);
620 photo
= g_strconcat("VALUE=URI;TYPE=", "JPG:;", uri
, NULL
);
623 photo
= g_strconcat("VALUE=URI;TYPE=", "GIF:;", uri
, NULL
);
626 photo
= g_strconcat("VALUE=URI;TYPE=", "PNG:;", uri
, NULL
);
629 VCardProperty
* prop
= VCardProperty_new("PHOTO", photo
);
630 photo_value
= g_slist_prepend(photo_value
, prop
);
631 VCard_set_property(vcard
, "PHOTO", photo_value
);
636 void VCard_set_photo_image(VCard
* vcard
, PhotoType type
, const gchar
* image
) {
638 if (vcard
== NULL
) return;
640 GSList
* photo_value
= VCard_get_property(vcard
, "PHOTO");
643 VCard_set_property(vcard
, "PHOTO", NULL
);
649 photo
= g_strconcat("TYPE=jpeg;", "ENCODING=b:", image
, NULL
);
652 photo
= g_strconcat("TYPE=gif;", "ENCODING=b:", image
, NULL
);
655 photo
= g_strconcat("TYPE=png;", "ENCODING=b:", image
, NULL
);
658 VCardProperty
* prop
= VCardProperty_new("PHOTO", photo
);
659 photo_value
= g_slist_prepend(photo_value
, prop
);
660 VCard_set_property(vcard
, "PHOTO", photo_value
);
665 gchar
* VCard_get_etag(VCard
* vcard
) {
666 if (vcard
== NULL
) return NULL
;
668 return g_strdup(vcard
->etag
);
671 gchar
* VCard_get_name(VCard
* vcard
) {
673 if (vcard
== NULL
) return NULL
;
675 GSList
* fn
= VCard_get_property(vcard
, "FN");
677 res
= g_strdup(fn
->data
);
678 vcard_slist_property_free(fn
);
683 gchar
* VCard_get_company(VCard
* vcard
) {
685 if (vcard
== NULL
) return NULL
;
687 GSList
* org
= VCard_get_property(vcard
, "ORG");
689 res
= g_strdup(org
->data
);
690 vcard_slist_property_free(org
);
695 GSList
* VCard_get_telefon(VCard
* vcard
, TelefonType type
) {
697 if (vcard
== NULL
) return NULL
;
699 // TODO: return only telefon according to type
701 tel
= VCard_get_property(vcard
, "TEL");
705 GSList
* VCard_get_address(VCard
* vcard
, AddressType type
) {
707 if (vcard
== NULL
) return NULL
;
709 // TODO: return only address according to type
711 addr
= VCard_get_property(vcard
, "ADR");
715 gchar
* VCard_get_photo(VCard
* vcard
) {
717 if (vcard
== NULL
) return NULL
;
719 GSList
* photo
= VCard_get_property(vcard
, "PHOTO");
721 res
= g_strdup(photo
->data
);
722 vcard_slist_property_free(photo
);
727 void VCard_set_email(VCard
* vcard
, EmailType type
, const gchar
* email_addr
) {
729 GSList
* values
= NULL
;
730 if (vcard
== NULL
) return;
732 GSList
* email_value
= VCard_get_property(vcard
, "EMAIL");
733 if (email_addr
== NULL
) {
735 remove_email_prop(email_value
, type
);
736 VCard_set_property(vcard
, "EMAIL", email_value
);
742 email
= g_strconcat("TYPE=HOME:", email_addr
, NULL
);
745 email
= g_strconcat("TYPE=WORK:", email_addr
, NULL
);
748 email
= g_strconcat("TYPE=OTHER:", email_addr
, NULL
);
752 //print_slist(email_value);
753 values
= g_slist_copy_deep(email_value
, slist_copy_func
, NULL
);
755 VCardProperty
* prop
= VCardProperty_new("EMAIL", email
);
756 values
= g_slist_prepend(values
, prop
);
757 VCard_set_property(vcard
, "EMAIL", values
);
762 GSList
* VCard_get_email(VCard
* vcard
, EmailType type
) {
763 GSList
* email
= NULL
;
764 if (vcard
== NULL
) return NULL
;
766 // TODO: return only email according to type
768 email
= VCard_get_property(vcard
, "EMAIL");