/* * vcard.c * * Copyright 2019 Michael Rasmussen * * 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., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ #include #include #include #include #include #ifdef HAVE_CONFIG_H # include "config.h" #endif struct _VCard { VCardParserResponse state; VCardVersion version; GHashTable* vcard; gchar* etag; }; static char *replaceWord(const char *s, const char *oldW, const char *newW) { char *result; int i, cnt = 0; int newWlen = strlen(newW); int oldWlen = strlen(oldW); for (i = 0; s[i] != '\0'; i++) { if (strstr(&s[i], oldW) == &s[i]) { cnt++; i += oldWlen - 1; } } result = (char *) malloc(i + cnt * (newWlen - oldWlen) + 1); i = 0; while (*s) { if (strstr(s, oldW) == s) { strcpy(&result[i], newW); i += newWlen; s += oldWlen; } else result[i++] = *s++; } result[i] = '\0'; return result; } static gchar* make_line(const gchar* name, const gchar* value) { gchar* line = NULL; if (name != NULL) { if (g_ascii_strncasecmp("TEL", name, 3) == 0) { line = g_strdup_printf("%s;%s", name, value); } else if (g_ascii_strncasecmp("ADR", name, 3) == 0) { line = g_strdup_printf("%s;%s", name, value); } else if (g_ascii_strncasecmp("PHOTO", name, 3) == 0) { line = g_strdup_printf("%s;%s", name, value); } else if (g_ascii_strncasecmp("EMAIL", name, 3) == 0) { line = g_strdup_printf("%s;%s", name, value); } else { line = g_strdup_printf("%s:%s", name, value); } } return line; } static void hash_table_cb(gpointer key, gpointer value, gpointer data) { GSList* list = (GSList*) value; gchar** str = (gchar**) data; gchar* s = NULL; if (!list) return; while (list) { VCardProperty* vp = (VCardProperty*) list->data; gchar* s0 = replaceWord(vp->value, "\n", "\n "); gchar* line = make_line(vp->name, s0); g_free(s0); if (!*str) s = g_strdup(line); else { s = g_strdup_printf("%s\n%s", *str, line); g_free(*str); } *str = g_strdup(s); g_free(s); list = list->next; } } static gchar** make_chunks(const gchar* text) { gchar** chunks = NULL; gchar* v_text = g_strdup(text); g_strstrip(v_text); gchar** tmp = g_strsplit(v_text, "END:VCARD", 0); g_free(v_text); guint lines = g_strv_length(tmp); if (lines < 2) return tmp; if (strlen(tmp[lines-1]) < 1) { chunks = g_new0(gchar*, lines); lines--; } else chunks = g_new0(gchar*, lines + 1); for(int i = 0; i < lines; i++) { g_strchug(tmp[i]); chunks[i] = g_strconcat(tmp[i], "END:VCARD", NULL); } g_strfreev(tmp); return chunks; } static VCardProperty* VCardProperty_new(const gchar* key, const gchar* value) { VCardProperty* vp = g_new0(VCardProperty, 1); vp->name = g_strdup(key); vp->value = g_strdup(value); return vp; } static void remove_tel_prop(GSList* list, TelefonType type) { VCardProperty* vp = NULL; GSList* search = list; if (search) { while (search) { vp = (VCardProperty*) search->data; if (vp) { gchar* tel = (gchar*)vp->value; switch (type) { case TEL_DEFAULT: case TEL_MOBIL: if (g_strstr_len(tel, -1, "TYPE=MOBIL")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; case TEL_HOME: if (g_strstr_len(tel, -1, "TYPE=HOME")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; case TEL_WORK: if (g_strstr_len(tel, -1, "TYPE=WORK")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; } } search = search->next; } } } static void remove_adr_prop(GSList* list, AddressType type) { VCardProperty* vp = NULL; GSList* search = list; if (search) { while (search) { vp = (VCardProperty*) search->data; if (vp) { gchar* adr = (gchar*)vp->value; switch (type) { case ADDR_DEFAULT: case ADDR_HOME: if (g_strstr_len(adr, -1, "TYPE=HOME")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; case ADDR_WORK: if (g_strstr_len(adr, -1, "TYPE=WORK")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; case ADDR_OTHER: if (g_strstr_len(adr, -1, "TYPE=OTHER")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; } } search = search->next; } } } static void remove_email_prop(GSList* list, EmailType type) { VCardProperty* vp = NULL; GSList* search = list; if (search) { while (search) { vp = (VCardProperty*) search->data; if (vp) { gchar* email = (gchar*)vp->value; switch (type) { case EMAIL_DEFAULT: case EMAIL_HOME: if (g_strstr_len(email, -1, "TYPE=HOME")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; case EMAIL_WORK: if (g_strstr_len(email, -1, "TYPE=WORK")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; case EMAIL_OTHER: if (g_strstr_len(email, -1, "TYPE=OTHER")) { list = g_slist_remove(list, vp); vcard_property_free(vp); } break; } } search = search->next; } } } gchar* VCard_get_as_string(VCard* object) { gchar* card = NULL; if (object == NULL) return NULL; if (object && object->vcard) { gchar* version = vcard_version_2_str(object->version); gchar* s = g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version); g_free(version); g_hash_table_foreach(object->vcard, hash_table_cb, &s); card = g_strdup_printf("%s\nEND:VCARD", s); g_free(s); } return card; } void vcard_slist_property_free(gpointer data) { if (data == NULL) return; GSList* list = (GSList *) data; g_slist_free_full(list, (GDestroyNotify) vcard_property_free); } void vcard_property_free(gpointer data) { if (!data) return; VCardProperty* vp = (VCardProperty*) data; g_free(vp->name); g_free(vp->value); g_free(vp); } void print_slist(GSList* list) { GSList* tmp = list; while (tmp) { VCardProperty* vp = (VCardProperty*) tmp->data; g_print("Name: %s\n", vp->name); g_print("Value: %s\n", vp->value); tmp = tmp->next; } } static gpointer slist_copy_func(gconstpointer src, gpointer data) { const VCardProperty* vp = (const VCardProperty*) src; VCardProperty* new_vp = VCardProperty_new(vp->name, vp->value); return (gpointer) new_vp; } void VCard_free(VCard* object) { if (object == NULL) return; if (object->vcard) destroy_hash_table(object->vcard); g_free(object->etag); g_free(object); } VCard* VCard_version_new(VCardVersion version, gboolean init) { VCard* vc = g_new0(VCard, 1); vc->version = version; if (init) { vc->vcard = init_g_hash_table(vc->version); gchar* uuid = g_uuid_string_random(); VCardProperty* prop = VCardProperty_new("UID", g_strdup(uuid)); g_free(uuid); GSList* uid = g_slist_prepend(NULL, prop); VCard_set_property(vc, "UID", uid); } return vc; } VCard* VCard_new() { return VCard_version_new(DEFAULT_VCARD_VERSION, TRUE); } GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean skip_broken) { GSList* list = NULL; VCard* vc = NULL; if (!text || strlen(text) < 1) return NULL; gchar** chunks = make_chunks(text); for (int i = 0; i < g_strv_length(chunks); i++) { vc = VCard_version_new(version, FALSE); VCardParserResponse r = vcard_parse_text(chunks[i], &vc->version, &vc->vcard); if (r != VCARD_PARSER_OK && skip_broken) { VCard_free(vc); continue; } vc->state = r; list = g_slist_prepend(list, vc); } g_strfreev(chunks); return list; } GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken) { GSList* list = NULL; gchar* buf = NULL; size_t n = 0; int buf_size = 500; GString* text = g_string_new(NULL); if (file) { buf = g_new0(gchar, buf_size); while ((n = fread(buf, sizeof(char), buf_size, file)) == buf_size) { text = g_string_append_len(text, buf, buf_size); g_free(buf); buf = g_new0(gchar, buf_size); } if (n > 0) text = g_string_append_len(text, buf, n); g_free(buf); buf = g_string_free_and_steal(text); list = VCard_new_from_text(buf, version, skip_broken); g_free(buf); } return list; } void VCard_print_list(GSList* list) { VCard_print_list_fd(list, STDOUT_FILENO); } void VCard_print_list_fd(GSList* list, int fd) { GSList* tmp = list; if (fd > 0) { while(tmp) { VCard* object = (VCard*) tmp->data; VCard_print_fd(object, fd); if (tmp->next) { ssize_t len = strlen("\n"); if (write(fd, "\n", len) < len) g_warning(strerror(errno)); } tmp = tmp->next; } } } void VCard_print(VCard* object){ VCard_print_fd(object, STDOUT_FILENO); } void VCard_print_fd(VCard* object, int fd) { gchar *body = NULL, *s = NULL; if (object && object->vcard && fd > 0) { gchar* version = vcard_version_2_str(object->version); s = g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version); g_free(version); g_hash_table_foreach(object->vcard, hash_table_cb, &s); body = g_strdup_printf("%s\nEND:VCARD", s); g_free(s); ssize_t len = strlen(body); if (write(fd, body, len) < len) g_warning(strerror(errno)); g_free(body); } } gchar* VCard_get_state(VCard* object) { gchar* message = NULL; if (!object) return NULL; switch (object->state) { case VCARD_PARSER_OK: message = g_strdup("OK"); break; case VCARD_PARSER_VERSION_MISMATCH: message = g_strdup("Version mismatch"); break; case VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH: message = g_strdup("Attribute not supported in this version"); break; case VCARD_PARSER_BAD_FORMAT: message = g_strdup("Bad input format"); break; case VCARD_PARSER_ERROR: message = g_strdup("Unknown error"); break; default: message = g_strdup("Unhandled error"); break; } return message; } GSList* VCard_get_property(VCard* object, const gchar* property) { GSList* values = NULL; GSList* tmp = NULL; GSList *iter = NULL; VCardProperty* vp = NULL; if (object && object->vcard && property) { tmp = g_hash_table_lookup(object->vcard, property); for (iter = tmp; iter; iter = g_slist_next(iter)) { vp = (VCardProperty*) iter->data; VCardProperty* prop = VCardProperty_new(vp->name, vp->value); values = g_slist_prepend(values, prop); } } return values; } void VCard_set_property(VCard* object, const gchar* property, GSList* props) { GSList *values = NULL; if (object && object->vcard && property && props) { //print_slist(props); values = g_hash_table_lookup(object->vcard, property); if (values) { /* Remove before adding new */ g_hash_table_remove(object->vcard, property); } g_hash_table_replace(object->vcard, g_strdup(property), props); } } void VCard_set_etag(VCard* vcard, const gchar* etag) { if (vcard == NULL) return; if (vcard->etag) g_free(vcard->etag); vcard->etag = g_strdup(etag); } void VCard_set_name(VCard* vcard, const gchar* firstname, const gchar* lastname) { GSList *fn_value = NULL; GSList *n_value = NULL; if (vcard == NULL) return; if (firstname == NULL || lastname == NULL) { VCard_set_property(vcard, "N", NULL); VCard_set_property(vcard, "FN", NULL); } else { gchar* name = g_strconcat(firstname, " ", lastname, NULL); VCardProperty* prop = VCardProperty_new("FN", name); fn_value = g_slist_prepend(fn_value, prop); VCard_set_property(vcard, "FN", fn_value); g_free(name); name = g_strconcat(lastname, ";", firstname, ";", NULL); prop = VCardProperty_new("N", name); n_value = g_slist_prepend(n_value, prop); VCard_set_property(vcard, "N", n_value); g_free(name); } } void VCard_set_company(VCard* vcard, const gchar* company) { GSList* org_value = NULL; if (vcard == NULL) return; if (company == NULL) { VCard_set_property(vcard, "ORG", NULL); } VCardProperty* prop = VCardProperty_new("ORG", company); org_value = g_slist_prepend(org_value, prop); VCard_set_property(vcard, "ORG", org_value); } void VCard_set_telefon(VCard* vcard, TelefonType type, const gchar* number) { gchar* tel = NULL; GSList* values = NULL; if (vcard == NULL) return; GSList* tel_value = VCard_get_property(vcard, "TEL"); if (number == NULL) { if (tel_value) { remove_tel_prop(tel_value, type); VCard_set_property(vcard, "TEL", tel_value); } } else { switch (type) { case TEL_DEFAULT: case TEL_MOBIL: tel = g_strconcat("TYPE=MOBIL,VOICE:", number, NULL); break; case TEL_HOME: tel = g_strconcat("TYPE=HOME,VOICE:", number, NULL); break; case TEL_WORK: tel = g_strconcat("TYPE=WORK,VOICE:", number, NULL); break; } if (tel_value) { values = g_slist_copy_deep(tel_value, slist_copy_func, NULL); } VCardProperty* prop = VCardProperty_new("TEL", tel); values = g_slist_prepend(values, prop); VCard_set_property(vcard, "TEL", values); g_free(tel); } } void VCard_set_address(VCard* vcard, AddressType type, const gchar* address, const gchar* state, const gchar* city, const gchar* zip, const gchar* country) { gchar* addr = NULL; GSList* values = NULL; if (vcard == NULL) return; GSList* addr_value = VCard_get_property(vcard, "ADR"); if (address == NULL || city == NULL || zip == NULL || country == NULL) { if (addr_value) { remove_adr_prop(addr_value, type); VCard_set_property(vcard, "ADR", addr_value); } } else { gchar* state = NULL; if (state) state = g_strconcat(state, ";", NULL); else state = g_strdup(""); switch (type) { case ADDR_DEFAULT: case ADDR_HOME: addr = g_strdup_printf("TYPE=HOME:;;%s;%s;%s;%s;%s;", address, city, zip, state, country); //, address, ";", city, ";", zip, ";", state, ";", country, ";" , NULL); break; case ADDR_WORK: addr = g_strconcat("TYPE=WORK:;;", address, ";", city, ";", zip, ";", state, country, ";" , NULL); break; case ADDR_OTHER: addr = g_strconcat("TYPE=OTHER:;;", address, ";", city, ";", zip, ";", state, country, ";" , NULL); break; } g_free(state); if (addr_value) { values = g_slist_copy_deep(addr_value, slist_copy_func, NULL); } VCardProperty* prop = VCardProperty_new("ADR", addr); values = g_slist_prepend(values, prop); VCard_set_property(vcard, "ADR", values); g_free(addr); } } void VCard_set_photo_uri(VCard* vcard, PhotoType type, const gchar* uri) { gchar* photo = NULL; if (vcard == NULL) return; GSList* photo_value = VCard_get_property(vcard, "PHOTO"); if (uri == NULL) { if (photo_value) { VCard_set_property(vcard, "PHOTO", NULL); } } else { switch (type) { case PHOTO_DEFAULT: case PHOTO_JPG: photo = g_strconcat("VALUE=URI;TYPE=", "JPG:;", uri, NULL); break; case PHOTO_GIF: photo = g_strconcat("VALUE=URI;TYPE=", "GIF:;", uri, NULL); break; case PHOTO_PNG: photo = g_strconcat("VALUE=URI;TYPE=", "PNG:;", uri, NULL); break; } VCardProperty* prop = VCardProperty_new("PHOTO", photo); photo_value = g_slist_prepend(photo_value, prop); VCard_set_property(vcard, "PHOTO", photo_value); g_free(photo); } } void VCard_set_photo_image(VCard* vcard, PhotoType type, const gchar* image) { gchar* photo = NULL; if (vcard == NULL) return; GSList* photo_value = VCard_get_property(vcard, "PHOTO"); if (image == NULL) { if (photo_value) { VCard_set_property(vcard, "PHOTO", NULL); } } else { switch (type) { case PHOTO_DEFAULT: case PHOTO_JPG: photo = g_strconcat("TYPE=jpeg;", "ENCODING=b:", image, NULL); break; case PHOTO_GIF: photo = g_strconcat("TYPE=gif;", "ENCODING=b:", image, NULL); break; case PHOTO_PNG: photo = g_strconcat("TYPE=png;", "ENCODING=b:", image, NULL); break; } VCardProperty* prop = VCardProperty_new("PHOTO", photo); photo_value = g_slist_prepend(photo_value, prop); VCard_set_property(vcard, "PHOTO", photo_value); g_free(photo); } } gchar* VCard_get_etag(VCard* vcard) { if (vcard == NULL) return NULL; return g_strdup(vcard->etag); } gchar* VCard_get_name(VCard* vcard) { gchar* res = NULL; if (vcard == NULL) return NULL; GSList* fn = VCard_get_property(vcard, "FN"); if (fn) { res = g_strdup(fn->data); vcard_slist_property_free(fn); } return res; } gchar* VCard_get_company(VCard* vcard) { gchar* res = NULL; if (vcard == NULL) return NULL; GSList* org = VCard_get_property(vcard, "ORG"); if (org) { res = g_strdup(org->data); vcard_slist_property_free(org); } return res; } GSList* VCard_get_telefon(VCard* vcard, TelefonType type) { GSList* tel = NULL; if (vcard == NULL) return NULL; // TODO: return only telefon according to type tel = VCard_get_property(vcard, "TEL"); return tel; } GSList* VCard_get_address(VCard* vcard, AddressType type) { GSList* addr = NULL; if (vcard == NULL) return NULL; // TODO: return only address according to type addr = VCard_get_property(vcard, "ADR"); return addr; } gchar* VCard_get_photo(VCard* vcard) { gchar* res = NULL; if (vcard == NULL) return NULL; GSList* photo = VCard_get_property(vcard, "PHOTO"); if (photo) { res = g_strdup(photo->data); vcard_slist_property_free(photo); } return res; } void VCard_set_email(VCard* vcard, EmailType type, const gchar* email_addr) { gchar* email = NULL; GSList* values = NULL; if (vcard == NULL) return; GSList* email_value = VCard_get_property(vcard, "EMAIL"); if (email_addr == NULL) { if (email_value) { remove_email_prop(email_value, type); VCard_set_property(vcard, "EMAIL", email_value); } } else { switch (type) { case EMAIL_DEFAULT: case EMAIL_HOME: email = g_strconcat("TYPE=HOME:", email_addr, NULL); break; case EMAIL_WORK: email = g_strconcat("TYPE=WORK:", email_addr, NULL); break; case EMAIL_OTHER: email = g_strconcat("TYPE=OTHER:", email_addr, NULL); break; } if (email_value) { //print_slist(email_value); values = g_slist_copy_deep(email_value, slist_copy_func, NULL); } VCardProperty* prop = VCardProperty_new("EMAIL", email); values = g_slist_prepend(values, prop); VCard_set_property(vcard, "EMAIL", values); g_free(email); } } GSList* VCard_get_email(VCard* vcard, EmailType type) { GSList* email = NULL; if (vcard == NULL) return NULL; // TODO: return only email according to type email = VCard_get_property(vcard, "EMAIL"); return email; }