]> git.deb.at Git - pkg/abook.git/blobdiff - filter.c
pine import filter update
[pkg/abook.git] / filter.c
index d98ad117e3e1406b3a8148691d530586cd74d944..7dcfa329c42392141090b77c9d3928d578affe4f 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -49,6 +49,7 @@ static int      ldif_export_database(FILE *out, struct db_enumerator e);
 static int     html_export_database(FILE *out, struct db_enumerator e);
 static int     pine_export_database(FILE *out, struct db_enumerator e);
 static int     csv_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     mutt_alias_export(FILE *out, struct db_enumerator e);
 static int     elm_alias_export(FILE *out, struct db_enumerator e);
@@ -76,6 +77,7 @@ struct abook_output_filter e_filters[] = {
        { "pine", "pine addressbook", pine_export_database },
        { "gcrd", "GnomeCard (VCard) addressbook", gcrd_export_database },
        { "csv", "comma separated values", csv_export_database },
+       { "palmcsv", "Palm comma separated values", palm_export_database},
        { "elm", "elm alias", elm_alias_export },
        { "text", "plain text", text_export_database },
        { "spruce", "Spruce address book", spruce_export_database },
@@ -379,7 +381,7 @@ fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
        return (e_filters[i].func) (handle, e);
 }
 
-       
+
 
 int
 export_file(char filtname[FILTNAME_LEN], char *filename)
@@ -486,7 +488,7 @@ ldif_read_line(FILE *in)
                        buf = line;
                        continue;
                }
-               
+
                if(*line != ' ') {
                        fseek(in, pos, SEEK_SET);
                        free(line);
@@ -507,7 +509,7 @@ ldif_read_line(FILE *in)
                free(buf);
                return NULL;
        }
-               
+
        return buf;
 }
 
@@ -518,10 +520,10 @@ ldif_add_item(ldif_item ldif_item)
        int i;
 
        memset(abook_item, 0, sizeof(abook_item));
-       
+
        if( !ldif_item[LDIF_ITEM_FIELDS -1] )
                goto bail_out;
-       
+
 
        for(i=0; i < LDIF_ITEM_FIELDS; i++) {
                if(ldif_conv_table[i] >= 0 && ldif_item[i] && *ldif_item[i] )
@@ -576,7 +578,7 @@ ldif_parse_file(FILE *handle)
                        my_free(line);
                        continue; /* just skip the errors */
                }
-                               
+
                ldif_fix_string(value);
 
                ldif_convert(item, type, value);
@@ -627,7 +629,7 @@ mutt_read_line(FILE *in, char **alias, char **rest)
                free(line);
                return 1;
        }
-               
+
        ptr += 5;
 
        while( ISSPACE(*ptr) )
@@ -675,7 +677,7 @@ mutt_parse_email(list_item item)
                item[EMAIL] = email;
        else
                return;
-       
+
        while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
                tmp = strconcat("From: ", ++start, NULL);
                getname(tmp, &name, &email);
@@ -700,7 +702,7 @@ mutt_parse_file(FILE *in)
 
        for(;;) {
                memset(item, 0, sizeof(item));
-               
+
                if( !mutt_read_line(in, &item[NICK],
                                &item[NAME]) )
                        mutt_parse_email(item);
@@ -783,11 +785,12 @@ ldif_export_database(FILE *out, struct db_enumerator e)
 static void            html_export_write_head(FILE *out, int extra_column);
 static void            html_export_write_tail(FILE *out);
 
+extern int extra_column;
+
 static int
 html_export_database(FILE *out, struct db_enumerator e)
 {
        char tmp[MAX_EMAILSTR_LEN];
-       int extra_column = options_get_int("extra_column");
 
        if( items < 1 )
                return 2;
@@ -853,6 +856,8 @@ html_export_write_tail(FILE *out)
  * pine addressbook import filter
  */
 
+#define PINE_BUF_SIZE 2048
+
 static void
 pine_fixbuf(char *buf)
 {
@@ -876,7 +881,7 @@ pine_convert_emails(char *s)
 
        if( ( tmp = strchr(s,')')) )
                *tmp=0;
-       
+
        for(i=1; ( tmp = strchr(s, ',') ) != NULL ; i++, s=tmp+1 )
                if( i > MAX_EMAILS - 1 ) {
                        *tmp = 0;
@@ -891,7 +896,7 @@ pine_parse_buf(char *buf)
        list_item item;
        char *start = buf;
        char *end;
-       char tmp[400];
+       char tmp[PINE_BUF_SIZE];
        int i, len, last;
        int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
 
@@ -900,10 +905,10 @@ pine_parse_buf(char *buf)
        for(i=0, last=0; !last ; i++) {
                if( ! (end = strchr(start, '\t')) )
                        last=1;
-               
+
                len = last ? strlen(start) : (int) (end-start);
-               len = min(len, 400-1);
-       
+               len = min(len, PINE_BUF_SIZE - 1);
+
                if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
                                && pine_conv_table[i] >= 0) {
                        strncpy(tmp, start, len);
@@ -912,11 +917,11 @@ pine_parse_buf(char *buf)
                }
                start = end + 1;
        }
-       
+
        pine_convert_emails(item[EMAIL]);
        add_item2database(item);
 }
-               
+
 
 #define LINESIZE       1024
 
@@ -929,7 +934,7 @@ pine_parse_file(FILE *in)
        int i;
 
        fgets(line, LINESIZE, in);      
-       
+
        while(!feof(in)) {
                for(i = 2;;i++) {
                        buf = (char *) realloc(buf, i*LINESIZE);
@@ -942,7 +947,7 @@ pine_parse_file(FILE *in)
                        else
                                while( *ptr == ' ')
                                        ptr++;
-                               
+
                        strcat(buf, ptr);
                }
                if( *buf == '#' ) {
@@ -1034,10 +1039,10 @@ csv_remove_quotes(char *s)
 {
        char *copy, *trimmed;
        int len;
-       
+
        copy = trimmed = strdup(s);
        strtrim(trimmed);
-       
+
        len = strlen(trimmed);
        if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
                if(len < 3) {
@@ -1162,47 +1167,144 @@ csv_parse_file(FILE *in)
  */
 
 /*
- * csv addressbook export filter
+ * csv addressbook export filters
  */
 
+#define CSV_LAST               (-1)
+#define CSV_UNDEFINED          (-2)
+#define CSV_SPECIAL(X)         (-3 - (X))
+#define CSV_IS_SPECIAL(X)      ((X) <= -3)
+
+static int
+csv_export_common(FILE *out, struct db_enumerator e,
+               int fields[], void (*special_func)(FILE *, int, int))
+{
+       int i;
+
+       db_enumerate_items(e) {
+               for(i = 0; fields[i] != CSV_LAST; i++) {
+                       if(fields[i] == CSV_UNDEFINED)
+                               fprintf(out, "\"\"");
+                       else if(CSV_IS_SPECIAL(fields[i])) {
+                               if(special_func)
+                                       (*special_func)(out, e.item, fields[i]);
+                       } else
+                               /*fprintf(out,(
+                       strchr(safe_str(database[e.item][fields[i]]), ',') ||
+                       strchr(safe_str(database[e.item][fields[i]]), '\"')) ?
+                               "\"%s\"" : "%s",
+                               safe_str(database[e.item][fields[i]])
+                               );*/
+                               fprintf(out, "\"%s\"",
+                                       safe_str(database[e.item][fields[i]]));
+
+                       if(fields[i + 1] != CSV_LAST)
+                               fputc(',', out);
+               }
+               fputc('\n', out);
+       }
+
+       return 0;
+}
+
 static int
 csv_export_database(FILE *out, struct db_enumerator e)
 {
-       int j;
        int csv_export_fields[] = {
                NAME,
                EMAIL,
                PHONE,
                NOTES,
                NICK,
-               -1
+               CSV_LAST
        };
 
-       db_enumerate_items(e) {
-               for(j = 0; csv_export_fields[j] >= 0; j++) {
-                       fprintf(out,(
-               strchr(safe_str(database[e.item][csv_export_fields[j]]), ',') ||
-               strchr(safe_str(database[e.item][csv_export_fields[j]]), '\"')
-                       ) ?
-                               "\"%s\"" : "%s",
-                               safe_str(database[e.item][csv_export_fields[j]])
-                               );
-                       if(csv_export_fields[j+1] >= 0)
-                               fputc(',', out);
-               }
-               fputc('\n', out);
+       csv_export_common(out, e, csv_export_fields, NULL);
+       
+       return 0;
+}
+
+/*
+ * palm csv
+ */
+
+#define PALM_CSV_NAME  CSV_SPECIAL(0)
+#define PALM_CSV_END   CSV_SPECIAL(1)
+#define PALM_CSV_CAT   CSV_SPECIAL(2)
+
+static void 
+palm_split_and_write_name(FILE *out, char *name)
+{
+       char *p;
+
+       assert(name);
+
+       if ( (p = strchr(name, ' ')) ) {
+               /*
+                * last name first
+                */
+               fprintf(out, "\"%s\",\"" , p + 1);
+               fwrite((void *)name, p - name, sizeof(char), out);
+               fputc('\"', out);
+       } else {
+               fprintf(out, "\"%s\"", safe_str(name));
        }
-               
+}
 
+static void 
+palm_csv_handle_specials(FILE *out, int item, int field)
+{
+       switch(field) {
+               case PALM_CSV_NAME:
+                       palm_split_and_write_name(out, database[item][NAME]);
+                       break;
+               case PALM_CSV_CAT:
+                       fprintf(out, "\"abook\"");
+                       break;
+               case PALM_CSV_END:
+                       fprintf(out, "\"0\"");
+                       break;
+               default:
+                       assert(0);
+       }
+}
+
+static int
+palm_export_database(FILE *out, struct db_enumerator e)
+{
+       int palm_export_fields[] = {
+               PALM_CSV_NAME,          /* LASTNAME, FIRSTNAME  */
+               CSV_UNDEFINED,          /* TITLE                */
+               CSV_UNDEFINED,          /* COMPANY              */
+               WORKPHONE,              /* WORK PHONE           */
+               PHONE,                  /* HOME PHONE           */
+               FAX,                    /* FAX                  */
+               MOBILEPHONE,            /* OTHER                */
+               EMAIL,                  /* EMAIL                */
+               ADDRESS,                /* ADDRESS              */ 
+               CITY,                   /* CITY                 */
+               STATE,                  /* STATE                */
+               ZIP,                    /* ZIP                  */
+               COUNTRY,                /* COUNTRY              */
+               NICK,                   /* DEFINED 1            */
+               URL,                    /* DEFINED 2            */
+               CSV_UNDEFINED,          /* DEFINED 3            */
+               CSV_UNDEFINED,          /* DEFINED 4            */
+               NOTES,                  /* NOTE                 */
+               PALM_CSV_END,           /* "0"                  */
+               PALM_CSV_CAT,           /* CATEGORY             */
+               CSV_LAST
+       };
+
+       csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
 
        return 0;
 }
 
 /*
- * end of csv export filter
+ * end of csv export filters
  */
 
-
 /*
  * GnomeCard (VCard) addressbook export filter
  */
@@ -1398,7 +1500,7 @@ text_export_database(FILE * out, struct db_enumerator e)
        char emails[MAX_EMAILS][MAX_EMAIL_LEN];
        int j;
        char *realname = get_real_name();
-       char *style = options_get_str("address_style");
+       char *style = opt_get_str(STR_ADDRESS_STYLE);
 
        fprintf(out,
                "-----------------------------------------\n%s's address book\n"