X-Git-Url: https://git.deb.at/?a=blobdiff_plain;ds=sidebyside;f=filter.c;h=c5c69cfb7a07cac41661187ee330124f74c1c730;hb=f937f7246af81dac1401685e3e3384646bdd5ccd;hp=04b9a3b5343f46f778bbd4044ec494214cc7521d;hpb=6a182e3e9a932830341308eb5fb58eb92f5c3682;p=pkg%2Fabook.git diff --git a/filter.c b/filter.c index 04b9a3b..c5c69cf 100644 --- a/filter.c +++ b/filter.c @@ -7,6 +7,8 @@ * Copyright (C) Jaakko Heinonen */ +#define _GNU_SOURCE + #include #include #include @@ -44,6 +46,7 @@ static int pine_parse_file(FILE *in); static int csv_parse_file(FILE *in); static int allcsv_parse_file(FILE *in); static int palmcsv_parse_file(FILE *in); +static int vcard_parse_file(FILE *in); /* * export filter prototypes @@ -55,7 +58,7 @@ static int pine_export_database(FILE *out, struct db_enumerator e); static int csv_export_database(FILE *out, struct db_enumerator e); static int allcsv_export_database(FILE *out, struct db_enumerator e); static int palm_export_database(FILE *out, struct db_enumerator e); -static int gcrd_export_database(FILE *out, struct db_enumerator e); +static int vcard_export_database(FILE *out, struct db_enumerator e); static int mutt_alias_export(FILE *out, struct db_enumerator e); static int elm_alias_export(FILE *out, struct db_enumerator e); static int text_export_database(FILE *out, struct db_enumerator e); @@ -75,16 +78,17 @@ struct abook_input_filter i_filters[] = { { "csv", N_("comma separated values"), csv_parse_file }, { "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file }, { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file }, + { "vcard", N_("vCard file"), vcard_parse_file }, { "\0", NULL, NULL } }; struct abook_output_filter e_filters[] = { { "abook", N_("abook native format"), write_database }, { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database }, + { "vcard", N_("vCard 2 file"), vcard_export_database }, { "mutt", N_("mutt alias"), mutt_alias_export }, { "html", N_("html document"), html_export_database }, { "pine", N_("pine addressbook"), pine_export_database }, - { "gcrd", N_("GnomeCard (VCard) addressbook"), gcrd_export_database }, { "csv", N_("comma separated values"), csv_export_database }, { "allcsv", N_("comma separated values (all fields)"), allcsv_export_database }, { "palmcsv", N_("Palm comma separated values"), palm_export_database}, @@ -752,7 +756,7 @@ mutt_parse_file(FILE *in) if(!mutt_read_line(in, (field_id(NICK) != -1) ? - &item[field_id(NICK)] : NULL, + &item[field_id(NICK)] : NULL, &item[field_id(NAME)])) mutt_parse_email(item); @@ -1330,6 +1334,260 @@ palmcsv_parse_file(FILE *in) * end of csv import filter */ +/* + * vCard import filter + */ + +static char *vcard_fields[] = { + "FN", /* NAME */ + "EMAIL", /* EMAIL */ + "ADR", /* ADDRESS */ + "ADR", /* ADDRESS2 - not used */ + "ADR", /* CITY */ + "ADR", /* STATE */ + "ADR", /* ZIP */ + "ADR", /* COUNTRY */ + "TEL", /* PHONE */ + "TEL", /* WORKPHONE */ + "TEL", /* FAX */ + "TEL", /* MOBILEPHONE */ + "NICKNAME", /* NICK */ + "URL", /* URL */ + "NOTE", /* NOTES */ + NULL /* not implemented: ANNIVERSARY, ITEM_FIELDS */ +}; + +/* + * mappings between vCard ADR field and abook's ADDRESS + * see rfc2426 section 3.2.1 + */ +static int vcard_address_fields[] = { + -1, /* vCard(post office box) - not used */ + -1, /* vCard(the extended address) - not used */ + 2, /* vCard(the street address) - ADDRESS */ + 4, /* vCard(the locality) - CITY */ + 5, /* vCard(the region) - STATE */ + 6, /* vCard(the postal code) - ZIP */ + 7 /* vCard(the country name) - COUNTRY */ +}; + +enum { + VCARD_KEY = 0, + VCARD_KEY_ATTRIBUTE, + VCARD_VALUE, +}; + +static char * +vcard_get_line_element(char *line, int element) +{ + int i; + char *line_copy = 0; + char *result = 0; + char *key = 0; + char *key_attr = 0; + char *value = 0; + + line_copy = xstrdup(line); + + /* change newline characters, if present, to end of string */ + for(i=0; line_copy[i]; i++) { + if(line_copy[i] == '\r' || line_copy[i] == '\n') { + line_copy[i] = '\0'; + break; + } + } + + /* separate key from value */ + for(i=0; line_copy[i]; i++) { + if(line_copy[i] == ':') { + line_copy[i] = '\0'; + key = line_copy; + value = &line_copy[i+1]; + break; + } + } + + /* separate key from key attributes */ + /* works for vCard 2 as well (automagically) */ + if (key) { + for(i=0; key[i]; i++) { + if(key[i] == ';') { + key[i] = '\0'; + key_attr = &key[i+1]; + break; + } + } + } + + switch(element) { + case VCARD_KEY: + if(key) + result = xstrdup(key); + break; + case VCARD_KEY_ATTRIBUTE: + if(key_attr) + result = xstrdup(key_attr); + break; + case VCARD_VALUE: + if(value) + result = xstrdup(value); + break; + } + + xfree(line_copy); + return result; +} + +static void +vcard_parse_email(list_item item, char *line) +{ + char *email; + + email = vcard_get_line_element(line, VCARD_VALUE); + + if(item[1]) { + item[1] = strconcat(item[1], ",", email, 0); + xfree(email); + } + else { + item[1] = email; + } +} + +static void +vcard_parse_address(list_item item, char *line) +{ + int i; + int k; + char *value; + char *address_field; + + value = vcard_get_line_element(line, VCARD_VALUE); + if(!value) + return; + + address_field = value; + for(i=k=0; value[i]; i++) { + if(value[i] == ';') { + value[i] = '\0'; + if(vcard_address_fields[k] >= 0) { + item[vcard_address_fields[k]] = xstrdup(address_field); + } + address_field = &value[i+1]; + k++; + if((k+1)==(sizeof(vcard_address_fields)/sizeof(*vcard_address_fields))) + break; + } + } + item[vcard_address_fields[k]] = xstrdup(address_field); + xfree(value); +} + +static void +vcard_parse_phone(list_item item, char *line) +{ + int index = 8; + char *type = vcard_get_line_element(line, VCARD_KEY_ATTRIBUTE); + char *value = vcard_get_line_element(line, VCARD_VALUE); + + /* set the standard number */ + if (!type) { + item[index] = value; + } + + /* + * see rfc2426 section 3.3.1 + * Note: we probably support both vCard 2 and 3 + */ + else { + if (strcasestr(type, "home") != NULL) { + item[index] = xstrdup(value); + } + if (strcasestr(type, "work") != NULL) { + item[index+1] = xstrdup(value); + } + if (strcasestr(type, "fax") != NULL) { + item[index+2] = xstrdup(value); + } + if (strcasestr(type, "cell") != NULL) { + item[index+3] = xstrdup(value); + } + + xfree(type); + xfree(value); + } +} + +static void +vcard_parse_line(list_item item, char *line) +{ + int i; + char *key; + + for(i=0; vcard_fields[i]; i++) { + key = vcard_fields[i]; + + if(0 == strncmp(key, line, strlen(key))) { + if(0 == strcmp(key, "EMAIL")) + vcard_parse_email(item, line); + else if(i == 2) + vcard_parse_address(item, line); + else if(0 == strcmp(key, "TEL")) + vcard_parse_phone(item, line); + else + item[i] = vcard_get_line_element(line, VCARD_VALUE); + return; + } + } +} + +static void +vcard_parse_item(FILE *in) +{ + char *line = NULL; + list_item item = item_create(); + + while(!feof(in)) { + line = getaline(in); + + if(line && !strncmp("END:VCARD", line, 9)) { + xfree(line); + break; + } + else if(line) { + vcard_parse_line(item, line); + xfree(line); + } + } + + add_item2database(item); + item_free(&item); +} + +static int +vcard_parse_file(FILE *in) +{ + char *line = NULL; + + while(!feof(in)) { + line = getaline(in); + + if(line && !strncmp("BEGIN:VCARD", line, 11)) { + xfree(line); + vcard_parse_item(in); + } + else if(line) { + xfree(line); + } + } + + return 0; +} + +/* + * end of vCard import filter + */ + /* * csv addressbook export filters */ @@ -1520,11 +1778,11 @@ palm_export_database(FILE *out, struct db_enumerator e) */ /* - * GnomeCard (VCard) addressbook export filter + * vCard 2 addressbook export filter */ static int -gcrd_export_database(FILE *out, struct db_enumerator e) +vcard_export_database(FILE *out, struct db_enumerator e) { int j; char *name, *tmp; @@ -1596,7 +1854,7 @@ gcrd_export_database(FILE *out, struct db_enumerator e) } /* - * end of GnomeCard export filter + * end of vCard export filter */ @@ -1627,15 +1885,46 @@ mutt_alias_export(FILE *out, struct db_enumerator e) { char email[MAX_EMAIL_LEN]; char *alias = NULL; + int email_addresses; + char *ptr; db_enumerate_items(e) { alias = mutt_alias_genalias(e.item); get_first_email(email, e.item); - fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n", - alias, - db_name_get(e.item), - email); - xfree(alias); + + /* do not output contacts without email address */ + /* cause this does not make sense in mutt aliases */ + if (*email) { + + /* output first email address */ + fprintf(out, "alias %s %s <%s>\n", + alias, + db_name_get(e.item), + email); + + /* number of email addresses */ + email_addresses = 1; + ptr = db_email_get(e.item); + while (*ptr != '\0') { + if (*ptr == ',') { + email_addresses++; + } + ptr++; + } + + /* output other email addresses */ + while (email_addresses-- > 1) { + roll_emails(e.item, ROTATE_RIGHT); + get_first_email(email, e.item); + fprintf(out, "alias %s__%s %s <%s>\n", + alias, + email, + db_name_get(e.item), + email); + } + roll_emails(e.item, ROTATE_RIGHT); + xfree(alias); + } } return 0; @@ -1890,7 +2179,8 @@ bsdcal_export_database(FILE *out, struct db_enumerator e) char *anniversary = db_fget(e.item, ANNIVERSARY); if(anniversary) { - parse_date_string(anniversary, &day, &month, &year); + if(!parse_date_string(anniversary, &day, &month, &year)) + continue; fprintf(out, _("%02d/%02d\tAnniversary of %s\n"),