X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=filter.c;h=51445ee1f1bf9947c323bc478a4f7fcafb22f9da;hb=a6762de0a3ccf6ff7a11f43c3cfe517f38fbb111;hp=c5c69cfb7a07cac41661187ee330124f74c1c730;hpb=f937f7246af81dac1401685e3e3384646bdd5ccd;p=pkg%2Fabook.git diff --git a/filter.c b/filter.c index c5c69cf..51445ee 100644 --- a/filter.c +++ b/filter.c @@ -100,6 +100,10 @@ struct abook_output_filter e_filters[] = { { "\0", NULL, NULL } }; +struct abook_output_item_filter u_filters[] = { + { "\0", NULL } +}; + /* * common functions */ @@ -122,6 +126,13 @@ print_filters() gettext(e_filters[i].desc)); putchar('\n'); + + puts(_("output (with query):")); + for(i=0; *u_filters[i].filtname ; i++) + printf("\t%s\t%s\n", u_filters[i].filtname, + gettext(u_filters[i].desc)); + + putchar('\n'); } static int @@ -361,6 +372,25 @@ export_database() return 0; } +struct abook_output_item_filter select_output_item_filter(char filtname[FILTNAME_LEN]) { + int i; + for(i=0;; i++) { + if(!strncasecmp(u_filters[i].filtname, filtname, FILTNAME_LEN)) + break; + if(!*u_filters[i].filtname) { + i = -1; + break; + } + } + return u_filters[i]; +} + +void +e_write_item(FILE *out, int item, void (*func) (FILE *in, int item)) +{ + (*func) (out, item); +} + static int e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e), int mode) @@ -638,10 +668,11 @@ ldif_fix_string(char *str) #include "getname.h" static int -mutt_read_line(FILE *in, char **alias, char **rest) +mutt_read_line(FILE *in, char **groups, char **alias, char **rest) { - char *line, *ptr, *tmp; - size_t alias_len; + char *line, *ptr; + char *start, *end; + abook_list *glist = NULL; if( !(line = ptr = getaline(in)) ) return 1; /* error / EOF */ @@ -654,27 +685,39 @@ mutt_read_line(FILE *in, char **alias, char **rest) } ptr += 5; - SKIPWS(ptr); - tmp = ptr; - - while( ! ISSPACE(*ptr) ) - ptr++; - - alias_len = (size_t)(ptr - tmp); + /* If the group option is used, save the groups */ + *groups = NULL; + start = ptr; + int n_groups; + for(n_groups = 0; 0 == strncmp("-group", ptr, 6); n_groups++) { + ptr += 6; + SKIPWS(ptr); + start = ptr; + SKIPNONWS(ptr); + end = ptr; + abook_list_append(&glist,xstrndup(start, end - start)); + SKIPWS(ptr); + } - if(alias) - *alias = xmalloc_inc(alias_len, 1); + if(n_groups && groups) + *groups = abook_list_to_csv(glist); - strncpy(*alias, tmp, alias_len); - *(*alias + alias_len) = 0; + abook_list_free(&glist); + /* alias */ + start = ptr; + SKIPNONWS(ptr); + end = ptr; SKIPWS(ptr); + if(alias) + *alias = xstrndup(start, end - start); + /* rest (email) */ *rest = xstrdup(ptr); - free(line); + xfree(line); return 0; } @@ -755,9 +798,9 @@ mutt_parse_file(FILE *in) memset(item, 0, fields_count * sizeof(char *)); if(!mutt_read_line(in, - (field_id(NICK) != -1) ? - &item[field_id(NICK)] : NULL, - &item[field_id(NAME)])) + (field_id(GROUPS) != -1) ? &item[field_id(GROUPS)] : NULL, + (field_id(NICK) != -1) ? &item[field_id(NICK)] : NULL, + &item[field_id(NAME)]) ) mutt_parse_email(item); if(feof(in)) { @@ -1339,7 +1382,7 @@ palmcsv_parse_file(FILE *in) */ static char *vcard_fields[] = { - "FN", /* NAME */ + "FN", /* FORMATTED NAME */ "EMAIL", /* EMAIL */ "ADR", /* ADDRESS */ "ADR", /* ADDRESS2 - not used */ @@ -1354,6 +1397,7 @@ static char *vcard_fields[] = { "NICKNAME", /* NICK */ "URL", /* URL */ "NOTE", /* NOTES */ + "N", /* NAME: special case/mapping in vcard_parse_line() */ NULL /* not implemented: ANNIVERSARY, ITEM_FIELDS */ }; @@ -1483,35 +1527,50 @@ vcard_parse_address(list_item item, char *line) xfree(value); } +static void +vcard_parse_name(list_item item, char *line) +{ + // store the "N" field into "NAME" *if* no "FN:" + // value has already been stored here + if(item[0]) return; + + int i = -1; + item[0] = vcard_get_line_element(line, VCARD_VALUE); + // "N:" can be multivalued => replace ';' separators by ' ' + while(item[0][++i]) if(item[0][i] == ';') item[0][i] = ' '; + + // http://www.daniweb.com/software-development/c/code/216919 + char *original = item[0], *p = original; + int trimmed = 0; + do { + if (*original != ' ' || trimmed) { + trimmed = 1; *p++ = *original; + } + } while(*original++); +} + 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; - } + if (!type) item_fput(item, PHONE, 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); - } + if (strcasestr(type, "home") != NULL) + item_fput(item, PHONE, xstrdup(value)); + else if (strcasestr(type, "work") != NULL) + item_fput(item, WORKPHONE, xstrdup(value)); + else if (strcasestr(type, "fax") != NULL) + item_fput(item, FAX, xstrdup(value)); + else if (strcasestr(type, "cell") != NULL) + item_fput(item, MOBILEPHONE, xstrdup(value)); xfree(type); xfree(value); @@ -1534,6 +1593,8 @@ vcard_parse_line(list_item item, char *line) vcard_parse_address(item, line); else if(0 == strcmp(key, "TEL")) vcard_parse_phone(item, line); + else if(0 == strcmp(key, "N")) + vcard_parse_name(item, line); else item[i] = vcard_get_line_element(line, VCARD_VALUE); return; @@ -1670,6 +1731,7 @@ allcsv_export_database(FILE *out, struct db_enumerator e) URL, NOTES, ANNIVERSARY, + GROUPS, CSV_LAST }; @@ -1689,7 +1751,8 @@ allcsv_export_database(FILE *out, struct db_enumerator e) fprintf(out, "\"NICK\","); fprintf(out, "\"URL\","); fprintf(out, "\"NOTES\","); - fprintf(out, "\"ANNIVERSARY\"\n"); + fprintf(out, "\"ANNIVERSARY\","); + fprintf(out, "\"GROUPS\"\n"); csv_export_common(out, e, allcsv_export_fields, NULL); @@ -1880,16 +1943,51 @@ mutt_alias_genalias(int i) return tmp; } +/* + * This function is a variant of abook_list_to_csv + * */ +static char * +mutt_alias_gengroups(int i) +{ + char *groups, *res = NULL; + char groupstr[7] = "-group "; + abook_list *list, *tmp; + + groups = db_fget(i, GROUPS); + + if(!groups) + return NULL; + + list = csv_to_abook_list(groups); + for(tmp = list; tmp; tmp = tmp->next) { + if(tmp == list) { + res = xmalloc(strlen(groupstr)+strlen(tmp->data)+1); + res = strcpy(res, groupstr); + } else { + res = xrealloc(res, strlen(res)+1+strlen(groupstr)+strlen(tmp->data)+1); + strcat(res, " "); + strcat(res, groupstr); + } + strcat(res, tmp->data); + } + abook_list_free(&list); + xfree(groups); + + return res; +} + static int mutt_alias_export(FILE *out, struct db_enumerator e) { char email[MAX_EMAIL_LEN]; char *alias = NULL; + char *groups = NULL; int email_addresses; char *ptr; db_enumerate_items(e) { - alias = mutt_alias_genalias(e.item); + alias = (field_id(NICK) != -1) ? mutt_alias_genalias(e.item) : NULL; + groups = (field_id(GROUPS) != -1) ? mutt_alias_gengroups(e.item) : NULL; get_first_email(email, e.item); /* do not output contacts without email address */ @@ -1897,8 +1995,12 @@ mutt_alias_export(FILE *out, struct db_enumerator e) if (*email) { /* output first email address */ - fprintf(out, "alias %s %s <%s>\n", - alias, + fprintf(out,"alias "); + if(groups) + fprintf(out, "%s ", groups); + if(alias) + fprintf(out, "%s ", alias); + fprintf(out, "%s <%s>\n", db_name_get(e.item), email); @@ -1916,14 +2018,20 @@ mutt_alias_export(FILE *out, struct db_enumerator e) 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, + fprintf(out,"alias "); + if( groups ) + fprintf(out, "%s ", groups); + if(alias) + fprintf(out, "%s__%s ", alias, email); + else + fprintf(out, "%s__%s ", db_name_get(e.item), email); + fprintf(out, "%s <%s>\n", db_name_get(e.item), email); } roll_emails(e.item, ROTATE_RIGHT); xfree(alias); + xfree(groups); } }