X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=filter.c;h=9a18975faff485c6bdf25fe9cb67a7da746839a1;hb=5d50b3ba7f2e35013e2236219b11fcf75a058770;hp=a0bc2e485f4a120f9b154d135691a5101768900f;hpb=64af20bd7059ff7f66fbc5ce22617340bda578d2;p=pkg%2Fabook.git diff --git a/filter.c b/filter.c index a0bc2e4..9a18975 100644 --- a/filter.c +++ b/filter.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "abook_curses.h" #include "filter.h" @@ -48,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); @@ -61,7 +63,7 @@ static int spruce_export_database(FILE *out, struct db_enumerator e); struct abook_input_filter i_filters[] = { { "abook", "abook native format", parse_database }, { "ldif", "ldif / Netscape addressbook", ldif_parse_file }, - { "mutt", "mutt alias (beta)", mutt_parse_file }, + { "mutt", "mutt alias", mutt_parse_file }, { "pine", "pine addressbook", pine_parse_file }, { "csv", "comma separated values", csv_parse_file }, { "\0", NULL, NULL } @@ -75,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 }, @@ -193,7 +196,7 @@ import_database() mvaddstr(5+filter, 2, "->"); - filename = ask_filename("Filename: ", 1); + filename = ask_filename("Filename: "); if( !filename ) { refresh_screen(); return 2; @@ -201,8 +204,7 @@ import_database() if( i_read_file(filename, i_filters[filter].func ) ) statusline_msg("Error occured while opening the file"); - else - if( tmp == items ) + else if( tmp == items ) statusline_msg("Hmm.., file seems not to be a valid file"); refresh_screen(); @@ -219,7 +221,7 @@ i_read_file(char *filename, int (*func) (FILE *in)) FILE *in; int ret = 0; - if( ( in = fopen( filename, "r" ) ) == NULL ) + if( ( in = abook_fopen( filename, "r" ) ) == NULL ) return 1; ret = (*func) (in); @@ -230,14 +232,15 @@ i_read_file(char *filename, int (*func) (FILE *in)) } int -import(char filtname[FILTNAME_LEN], char *filename) +import_file(char filtname[FILTNAME_LEN], char *filename) { int i; int tmp = items; int ret = 0; for(i=0;; i++) { - if( ! strncmp(i_filters[i].filtname, filtname, FILTNAME_LEN) ) + if( ! strncasecmp(i_filters[i].filtname, filtname, + FILTNAME_LEN) ) break; if( ! *i_filters[i].filtname ) { i = -1; @@ -248,9 +251,13 @@ import(char filtname[FILTNAME_LEN], char *filename) if( i<0 ) return -1; - if( !strcmp(filename, "-") ) - ret = (*i_filters[i].func) (stdin); - else + if( !strcmp(filename, "-") ) { + struct stat s; + if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode)) + ret = 1; + else + ret = (*i_filters[i].func) (stdin); + } else ret = i_read_file(filename, i_filters[i].func); if( tmp == items ) @@ -299,7 +306,7 @@ export_database() filter = getch() - 'a'; if(filter == 'x' - 'a' || - filter >= number_of_output_filters(e_filters) || filter < 0) { + filter >= number_of_output_filters() || filter < 0) { refresh_screen(); return 1; } @@ -319,7 +326,7 @@ export_database() clear_statusline(); } - filename = ask_filename("Filename: ", 0); + filename = ask_filename("Filename: "); if( !filename ) { refresh_screen(); return 2; @@ -362,7 +369,8 @@ fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode) struct db_enumerator e = init_db_enumerator(enum_mode); for(i=0;; i++) { - if( ! strncmp(e_filters[i].filtname, filtname, FILTNAME_LEN) ) + if( ! strncasecmp(e_filters[i].filtname, filtname, + FILTNAME_LEN) ) break; if( ! *e_filters[i].filtname ) { i = -1; @@ -373,10 +381,10 @@ fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode) return (e_filters[i].func) (handle, e); } - + int -export(char filtname[FILTNAME_LEN], char *filename) +export_file(char filtname[FILTNAME_LEN], char *filename) { const int mode = ENUM_ALL; int i; @@ -384,7 +392,8 @@ export(char filtname[FILTNAME_LEN], char *filename) struct db_enumerator e = init_db_enumerator(mode); for(i=0;; i++) { - if( ! strncmp(e_filters[i].filtname, filtname, FILTNAME_LEN) ) + if( ! strncasecmp(e_filters[i].filtname, filtname, + FILTNAME_LEN) ) break; if( ! *e_filters[i].filtname ) { i = -1; @@ -415,7 +424,7 @@ export(char filtname[FILTNAME_LEN], char *filename) static void ldif_fix_string(char *str); -#define LDIF_ITEM_FIELDS 15 +#define LDIF_ITEM_FIELDS 16 typedef char* ldif_item[LDIF_ITEM_FIELDS]; @@ -423,6 +432,7 @@ static ldif_item ldif_field_names = { "cn", "mail", "streetaddress", + "streetaddress2", "locality", "st", "postalcode", @@ -441,6 +451,7 @@ static int ldif_conv_table[LDIF_ITEM_FIELDS] = { NAME, /* "cn" */ EMAIL, /* "mail" */ ADDRESS, /* "streetaddress" */ + ADDRESS2, /* "streetaddress2" */ CITY, /* "locality" */ STATE, /* "st" */ ZIP, /* "postalcode" */ @@ -477,9 +488,9 @@ ldif_read_line(FILE *in) buf = line; continue; } - + if(*line != ' ') { - fseek(in, pos, SEEK_SET); + fseek(in, pos, SEEK_SET); /* fixme ! */ free(line); break; } @@ -494,11 +505,11 @@ ldif_read_line(FILE *in) free(line); } - if( *buf == '#' ) { + if(buf && *buf == '#' ) { free(buf); return NULL; } - + return buf; } @@ -509,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] ) @@ -567,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); @@ -585,7 +596,7 @@ ldif_fix_string(char *str) { int i, j; - for( i = 0, j = 0; j < strlen(str); i++, j++) + for( i = 0, j = 0; j < (int)strlen(str); i++, j++) str[i] = ( str[j] == (char)0xc3 ? (char) str[++j] + (char) 0x40 : str[j] ); @@ -601,18 +612,12 @@ ldif_fix_string(char *str) * mutt alias import filter */ -enum { - MUTT_ALIAS, - MUTT_NAME, - MUTT_EMAIL -}; +#include "getname.h" static int mutt_read_line(FILE *in, char **alias, char **rest) { - char *line; - char *ptr; - char *tmp; + char *line, *ptr, *tmp; if( !(line = ptr = getaline(in)) ) return 1; /* error / EOF */ @@ -624,7 +629,7 @@ mutt_read_line(FILE *in, char **alias, char **rest) free(line); return 1; } - + ptr += 5; while( ISSPACE(*ptr) ) @@ -641,7 +646,7 @@ mutt_read_line(FILE *in, char **alias, char **rest) } strncpy(*alias, tmp, ptr-tmp); - *(*alias+(ptr-tmp)) = 0; + *(*alias + (ptr - tmp)) = 0; while( ISSPACE(*ptr) ) ptr++; @@ -653,76 +658,92 @@ mutt_read_line(FILE *in, char **alias, char **rest) } static void -mutt_parse_email(char *mutt_item[3]) +mutt_fix_quoting(char *p) { - char *tmp; - int i; - - if( (tmp = strchr(mutt_item[MUTT_NAME], '<')) ) - *tmp = 0; - else - return; - - mutt_item[MUTT_EMAIL] = strdup(tmp+1); - - if( (tmp = strchr(mutt_item[MUTT_EMAIL], '>')) ) - *tmp = 0; - - tmp = mutt_item[MUTT_NAME]; - - for(i=strlen(tmp)-1; i>0; i--) - if(ISSPACE(tmp[i])) - tmp[i] = 0; - else - break; + char *escape = 0; - mutt_item[MUTT_NAME] = strdup(tmp); - - free(tmp); + for(; *p; p++) { + switch(*p) { + case '\"': + if(escape) + *escape = ' '; + break; + case '\\': + escape = p; + break; + default: + escape = 0; + } + } } static void -mutt_add_mutt_item(char *mutt_item[3]) +mutt_parse_email(list_item item) { - list_item abook_item; - - memset(abook_item, 0, sizeof(abook_item)); + char *line = item[NAME]; + char *tmp; + char *name, *email; +#if 0 + char *start = line; + int i = 0; +#endif + + mutt_fix_quoting(line); + tmp = strconcat("From: ", line, NULL); + getname(tmp, &name, &email); + free(tmp); - abook_item[NAME] = safe_strdup(mutt_item[MUTT_NAME]); - abook_item[EMAIL] = safe_strdup(mutt_item[MUTT_EMAIL]); - abook_item[NICK] = safe_strdup(mutt_item[MUTT_ALIAS]); + if(name) + item[NAME] = name; + else + return; + if(email) + item[EMAIL] = email; + else + return; - add_item2database(abook_item); + /* + * this is completely broken + */ +#if 0 + while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) { + tmp = strconcat("From: ", ++start, NULL); + getname(tmp, &name, &email); + free(tmp); + free(name); + if(email) { + if(*email) { + tmp = strconcat(item[EMAIL], ",", email, NULL); + free(item[EMAIL]); + item[EMAIL] = tmp; + } else { + my_free(email); + } + } + } +#endif } static int mutt_parse_file(FILE *in) { - char *mutt_item[3]; + list_item item; for(;;) { + memset(item, 0, sizeof(item)); - memset(mutt_item, 0, sizeof(mutt_item) ); - - if( !mutt_read_line(in, &mutt_item[MUTT_ALIAS], - &mutt_item[MUTT_NAME]) ) - mutt_parse_email(mutt_item); + if( !mutt_read_line(in, &item[NICK], + &item[NAME]) ) + mutt_parse_email(item); if( feof(in) ) { - free(mutt_item[MUTT_ALIAS]); - free(mutt_item[MUTT_NAME]); - free(mutt_item[MUTT_EMAIL]); + free_list_item(item); break; } - mutt_add_mutt_item(mutt_item); - - free(mutt_item[MUTT_ALIAS]); - free(mutt_item[MUTT_NAME]); - free(mutt_item[MUTT_EMAIL]); + add_item2database(item); } - return 0; } @@ -793,11 +814,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; @@ -814,7 +836,7 @@ html_export_database(FILE *out, struct db_enumerator e) tmp, database[e.item][NAME] ); else - fprintf(out, "\n%s>\n", + fprintf(out, "\n%s\n", database[e.item][NAME] ); fprintf(out, "%s\n%s\n", @@ -834,14 +856,13 @@ html_export_write_head(FILE *out, int extra_column) { char *realname = get_real_name(); - fprintf(out, "\n"); - fprintf(out, "\n\n %s's addressbook", - realname ); + fprintf(out, "\n"); + fprintf(out, "\n\n %s's addressbook", realname ); fprintf(out, "\n\n\n"); fprintf(out, "\n

%s's addressbook

\n", realname ); fprintf(out, "

\n\n"); - fprintf(out, "
\n"); + fprintf(out, "
\n"); fprintf(out, "\n\n\n", abook_fields[extra_column].name); @@ -851,7 +872,7 @@ html_export_write_head(FILE *out, int extra_column) static void html_export_write_tail(FILE *out) { - fprintf(out, "\n
NameE-mail address(es)%s
\n"); + fprintf(out, "\n\n"); fprintf(out, "\n\n\n"); } @@ -864,12 +885,14 @@ html_export_write_tail(FILE *out) * pine addressbook import filter */ +#define PINE_BUF_SIZE 2048 + static void pine_fixbuf(char *buf) { int i,j; - for(i=0,j=0; j < strlen(buf); i++, j++) + for(i=0,j=0; j < (int)strlen(buf); i++, j++) buf[i] = buf[j] == '\n' ? buf[++j] : buf[j]; } @@ -887,7 +910,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; @@ -902,7 +925,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}; @@ -911,23 +934,24 @@ 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); - - if(i < sizeof(pine_conv_table) / sizeof(*pine_conv_table) + 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); tmp[len] = 0; - item[pine_conv_table[i]] = strdup(tmp); + if(*tmp) + item[pine_conv_table[i]] = strdup(tmp); } start = end + 1; } - + pine_convert_emails(item[EMAIL]); add_item2database(item); } - + #define LINESIZE 1024 @@ -940,7 +964,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); @@ -953,7 +977,7 @@ pine_parse_file(FILE *in) else while( *ptr == ' ') ptr++; - + strcat(buf, ptr); } if( *buf == '#' ) { @@ -1045,10 +1069,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) { @@ -1077,9 +1101,11 @@ csv_store_field(list_item item, char *s, int field) if( !(newstr = csv_remove_quotes(s)) ) return; - if(field < (sizeof(csv_conv_table) / sizeof(*csv_conv_table)) + if(field < (int)(sizeof(csv_conv_table) / sizeof(*csv_conv_table)) && csv_conv_table[field] >= 0) { item[csv_conv_table[field]] = newstr; + } else { + my_free(newstr); } } @@ -1117,7 +1143,7 @@ csv_parse_line(char *line) { char *p, *start; int field; - int in_quote = FALSE; + bool in_quote = FALSE; list_item item; memset(item, 0, sizeof(item)); @@ -1171,47 +1197,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 */ @@ -1241,8 +1364,9 @@ gcrd_export_database(FILE *out, struct db_enumerator e) free(name); if ( database[e.item][ADDRESS] ) - fprintf(out, "ADR:;;%s;%s;%s;%s;%s\n", + fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\n", safe_str(database[e.item][ADDRESS]), + safe_str(database[e.item][ADDRESS2]), safe_str(database[e.item][CITY]), safe_str(database[e.item][STATE]), safe_str(database[e.item][ZIP]), @@ -1339,7 +1463,10 @@ mutt_alias_export(FILE *out, struct db_enumerator e) static void text_write_address_us(FILE *out, int i) { fprintf(out, "\n%s", database[i][ADDRESS]); - + + if (database[i][ADDRESS2]) + fprintf(out, "\n%s", database[i][ADDRESS2]); + if (database[i][CITY]) fprintf(out, "\n%s", database[i][CITY]); @@ -1373,7 +1500,10 @@ text_write_address_uk(FILE *out, int i) { static void text_write_address_eu(FILE *out, int i) { fprintf(out, "\n%s", database[i][ADDRESS]); - + + if (database[i][ADDRESS2]) + fprintf(out, "\n%s", database[i][ADDRESS2]); + if (database[i][ZIP] || database[i][CITY]) { fputc('\n', out); @@ -1400,7 +1530,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" @@ -1412,7 +1542,7 @@ text_export_database(FILE * out, struct db_enumerator e) fprintf(out, "-----------------------------------------\n\n"); fprintf(out, "%s", database[e.item][NAME]); - if (database[e.item][NICK]) + if (database[e.item][NICK] && *database[e.item][NICK]) fprintf(out, "\n(%s)", database[e.item][NICK]); fprintf(out, "\n");