]> git.deb.at Git - pkg/abook.git/commitdiff
Mutt groups support
authorFabio Zanini <fabio.zanini@fastmail.fm>
Sun, 1 Jul 2012 15:59:37 +0000 (17:59 +0200)
committerRaphaël Droz <raphael.droz+floss@gmail.com>
Wed, 24 Oct 2012 14:11:37 +0000 (16:11 +0200)
README
abook.h
database.c
database.h
filter.c
views.c

diff --git a/README b/README
index ad56f59750bb07bf26652e3eb98861bd51492714..e739b53527b74ed9e9f7e3fa6b76b646952bd7e3 100644 (file)
--- 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 fc6c1bbc0486d6e54f5e62a01fbdc5248c25f45a..0631b8f0f858ff6fbbce6420e454d7687069f04e 100644 (file)
--- 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
index da6890f11aba80f44583370aaec7d94d37f8f027..ffbe2c9f7ac6a1fc2691ff2f78f05515cc428585 100644 (file)
@@ -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 */
 };
 
index a81bd6e4e751de9780aa3addd2b971342d1dd071..457ee3617670d79e3f8245111a279314382d975e 100644 (file)
@@ -23,6 +23,7 @@ enum {
        URL,
        NOTES,
        ANNIVERSARY,
+       GROUPS,
        ITEM_FIELDS /* this is the last */
 };
 
index a22d69edf3bd94c82585b6415d5783449033c44a..184128017b9b4bdd8c4c6942be77535334f90ac6 100644 (file)
--- 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 1a32409186da97d083e1699473e580f6f199eb06..52adadea3b55570c150d2001e8a45237aa02af9a 100644 (file)
--- 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 }
        };