From: Fabio Zanini Date: Sun, 1 Jul 2012 15:59:37 +0000 (+0200) Subject: Mutt groups support X-Git-Tag: upstream/0.6.1~2^2~59 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=8420245db43e7f7ac1c8414a275e633e1d56c883;p=pkg%2Fabook.git Mutt groups support --- diff --git a/README b/README index ad56f59..e739b53 100644 --- a/README +++ b/README @@ -46,6 +46,9 @@ command. (Of course you can choose another keybinding if you like.) It's also recommended to set pipe_decode variable in mutt configuration. See the mutt manual for details. +Abook can also convert from/to mutt alias files and a number of other formats. +Mutt groups are fully supported. + UPGRADING FROM VERSION 0.5 See RELEASE_NOTES . diff --git a/abook.h b/abook.h index fc6c1bb..0631b8f 100644 --- a/abook.h +++ b/abook.h @@ -50,6 +50,7 @@ int strncasecmp (const char *, const char *, size_t); #define ISSPACE(c) isspace((unsigned char)c) #define SKIPWS(c) while(*(c) && ISSPACE(*(c))) c++ +#define SKIPNONWS(c) while(*(c) && ! ISSPACE(*(c))) c++ #ifndef DEBUG # define NDEBUG 1 diff --git a/database.c b/database.c index da6890f..ffbe2c9 100644 --- a/database.c +++ b/database.c @@ -58,6 +58,7 @@ abook_field standard_fields[] = { {"url", N_("URL"), FIELD_STRING}, /* URL */ {"notes", N_("Notes"), FIELD_STRING}, /* NOTES */ {"anniversary", N_("Anniversary day"), FIELD_DATE}, /* ANNIVERSARY */ + {"groups", N_("Groups"), FIELD_LIST}, /* GROUPS */ {0} /* ITEM_FIELDS */ }; diff --git a/database.h b/database.h index a81bd6e..457ee36 100644 --- a/database.h +++ b/database.h @@ -23,6 +23,7 @@ enum { URL, NOTES, ANNIVERSARY, + GROUPS, ITEM_FIELDS /* this is the last */ }; diff --git a/filter.c b/filter.c index a22d69e..1841280 100644 --- a/filter.c +++ b/filter.c @@ -638,10 +638,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 +655,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 +768,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)) { @@ -1688,6 +1701,7 @@ allcsv_export_database(FILE *out, struct db_enumerator e) URL, NOTES, ANNIVERSARY, + GROUPS, CSV_LAST }; @@ -1707,7 +1721,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); @@ -1898,16 +1913,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 */ @@ -1915,8 +1965,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); @@ -1934,14 +1988,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); } } diff --git a/views.c b/views.c index 1a32409..52adade 100644 --- a/views.c +++ b/views.c @@ -140,7 +140,7 @@ init_default_views() { N_("ADDRESS"), { ADDRESS, ADDRESS2, CITY, STATE, ZIP, COUNTRY, -1 } }, { N_("PHONE"), { PHONE, WORKPHONE, FAX, MOBILEPHONE, -1 } }, - { N_("OTHER"), { NICK, URL, NOTES, ANNIVERSARY, -1 } }, + { N_("OTHER"), { NICK, URL, NOTES, ANNIVERSARY, GROUPS, -1 } }, { 0 } };