5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
16 #include <sys/types.h>
17 #include "abook_curses.h"
29 extern list_item *database;
30 extern struct abook_field abook_fields[];
33 * function declarations
37 * import filter prototypes
40 static int ldif_parse_file(FILE *handle);
41 static int mutt_parse_file(FILE *in);
42 static int pine_parse_file(FILE *in);
43 static int csv_parse_file(FILE *in);
44 static int allcsv_parse_file(FILE *in);
47 * export filter prototypes
50 static int ldif_export_database(FILE *out, struct db_enumerator e);
51 static int html_export_database(FILE *out, struct db_enumerator e);
52 static int pine_export_database(FILE *out, struct db_enumerator e);
53 static int csv_export_database(FILE *out, struct db_enumerator e);
54 static int allcsv_export_database(FILE *out, struct db_enumerator e);
55 static int palm_export_database(FILE *out, struct db_enumerator e);
56 static int gcrd_export_database(FILE *out, struct db_enumerator e);
57 static int mutt_alias_export(FILE *out, struct db_enumerator e);
58 static int elm_alias_export(FILE *out, struct db_enumerator e);
59 static int text_export_database(FILE *out, struct db_enumerator e);
60 static int spruce_export_database(FILE *out, struct db_enumerator e);
63 * end of function declarations
66 struct abook_input_filter i_filters[] = {
67 { "abook", "abook native format", parse_database },
68 { "ldif", "ldif / Netscape addressbook", ldif_parse_file },
69 { "mutt", "mutt alias", mutt_parse_file },
70 { "pine", "pine addressbook", pine_parse_file },
71 { "csv", "comma separated values", csv_parse_file },
72 { "allcsv", "comma separated all values", allcsv_parse_file },
76 struct abook_output_filter e_filters[] = {
77 { "abook", "abook native format", write_database },
78 { "ldif", "ldif / Netscape addressbook (.4ld)", ldif_export_database },
79 { "mutt", "mutt alias", mutt_alias_export },
80 { "html", "html document", html_export_database },
81 { "pine", "pine addressbook", pine_export_database },
82 { "gcrd", "GnomeCard (VCard) addressbook", gcrd_export_database },
83 { "csv", "comma separated values", csv_export_database },
84 { "allcsv", "comma separated all values", allcsv_export_database },
85 { "palmcsv", "Palm comma separated values", palm_export_database},
86 { "elm", "elm alias", elm_alias_export },
87 { "text", "plain text", text_export_database },
88 { "spruce", "Spruce address book", spruce_export_database },
102 for(i=0; *i_filters[i].filtname ; i++)
103 printf("\t%s\t%s\n", i_filters[i].filtname,
109 for(i=0; *e_filters[i].filtname ; i++)
110 printf("\t%s\t%s\n", e_filters[i].filtname,
117 number_of_output_filters()
121 for(i=0; *e_filters[i].filtname ; i++)
128 number_of_input_filters()
132 for(i=0; *i_filters[i].filtname ; i++)
141 char *username = getenv("USER");
142 struct passwd *pwent;
146 pwent = getpwnam(username);
148 if((tmp = xstrdup(pwent->pw_gecos)) == NULL)
149 return xstrdup(username);
151 rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
152 if (rtn == EOF || rtn == 0) {
154 return xstrdup(username);
163 static int i_read_file(char *filename, int (*func) (FILE *in));
172 refresh_statusline();
173 headerline("import database");
175 mvaddstr(3, 1, "please select a filter");
178 for(i=0; *i_filters[i].filtname ; i++)
179 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
180 i_filters[i].filtname,
183 mvprintw(6 + i, 6, "x -\tcancel");
195 filter = getch() - 'a';
196 if(filter == 'x' - 'a' ||
197 filter >= number_of_input_filters() || filter < 0) {
202 mvaddstr(5+filter, 2, "->");
204 filename = ask_filename("Filename: ");
210 if(i_read_file(filename, i_filters[filter].func ))
211 statusline_msg("Error occured while opening the file");
212 else if(tmp == items)
213 statusline_msg("Hmm.., file seems not to be a valid file");
224 i_read_file(char *filename, int (*func) (FILE *in))
229 if( (in = abook_fopen( filename, "r" )) == NULL )
240 import_file(char filtname[FILTNAME_LEN], char *filename)
247 if(! strncasecmp(i_filters[i].filtname, filtname,
250 if(! *i_filters[i].filtname) {
259 if(!strcmp(filename, "-")) {
261 if((fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
264 ret = (*i_filters[i].func) (stdin);
266 ret = i_read_file(filename, i_filters[i].func);
278 static int e_write_file(char *filename,
279 int (*func) (FILE *in, struct db_enumerator e), int mode);
289 refresh_statusline();
290 headerline("export database");
292 mvaddstr(3, 1, "please select a filter");
295 for(i = 0; *e_filters[i].filtname ; i++)
296 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
297 e_filters[i].filtname,
300 mvprintw(6 + i, 6, "x -\tcancel");
307 int enum_mode = ENUM_ALL;
312 filter = getch() - 'a';
313 if(filter == 'x' - 'a' ||
314 filter >= number_of_output_filters() || filter < 0) {
319 mvaddstr(5+filter, 2, "->");
321 if(selected_items()) {
322 statusline_addstr("Export All/Selected/Cancel (A/s/c)");
323 switch( tolower(getch()) ) {
325 enum_mode = ENUM_SELECTED;
334 filename = ask_filename("Filename: ");
340 if( e_write_file(filename, e_filters[filter].func, enum_mode))
341 statusline_msg("Error occured while exporting");
350 e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e),
355 struct db_enumerator enumerator = init_db_enumerator(mode);
357 if((out = fopen(filename, "a")) == NULL)
363 ret = (*func) (out, enumerator);
371 fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
374 struct db_enumerator e = init_db_enumerator(enum_mode);
377 if(!strncasecmp(e_filters[i].filtname, filtname,
380 if(!*e_filters[i].filtname) {
386 return (e_filters[i].func) (handle, e);
392 export_file(char filtname[FILTNAME_LEN], char *filename)
394 const int mode = ENUM_ALL;
397 struct db_enumerator e = init_db_enumerator(mode);
400 if(!strncasecmp(e_filters[i].filtname, filtname,
403 if(!*e_filters[i].filtname) {
412 if(!strcmp(filename, "-"))
413 ret = (e_filters[i].func) (stdout, e);
415 ret = e_write_file(filename, e_filters[i].func, mode);
421 * end of common functions
430 static void ldif_fix_string(char *str);
432 #define LDIF_ITEM_FIELDS 16
434 typedef char *ldif_item[LDIF_ITEM_FIELDS];
436 static ldif_item ldif_field_names = {
448 "facsimiletelephonenumber",
452 "objectclass", /* this must be the last entry */
455 static int ldif_conv_table[LDIF_ITEM_FIELDS] = {
458 ADDRESS, /* "streetaddress" */
459 ADDRESS2, /* "streetaddress2" */
460 CITY, /* "locality" */
462 ZIP, /* "postalcode" */
463 COUNTRY, /* "countryname" */
464 PHONE, /* "homephone" */
465 NOTES, /* "description" */
467 FAX, /* "facsimiletelephonenumber" */
468 MOBILEPHONE, /* "cellphone" */
469 WORKPHONE, /* "xmozillaanyphone" */
470 NICK, /* "xmozillanickname" */
471 -1, /* "objectclass" */ /* this must be the last entry */
476 ldif_read_line(FILE *in)
489 if(feof(in) || !line)
498 fseek(in, pos, SEEK_SET); /* fixme ! */
508 buf = strconcat(buf, ptr, NULL);
513 if(buf && *buf == '#' ) {
522 ldif_add_item(ldif_item li)
524 list_item abook_item;
527 memset(abook_item, 0, sizeof(abook_item));
529 if(!li[LDIF_ITEM_FIELDS -1])
533 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
534 if(ldif_conv_table[i] >= 0 && li[i] && *li[i])
535 abook_item[ldif_conv_table[i]] = xstrdup(li[i]);
538 add_item2database(abook_item);
541 for(i=0; i < LDIF_ITEM_FIELDS; i++)
547 ldif_convert(ldif_item item, char *type, char *value)
551 if(!strcmp(type, "dn")) {
556 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
557 if(!safe_strcmp(ldif_field_names[i], type) && *value) {
558 if(i == LDIF_ITEM_FIELDS - 1) /* this is a dirty hack */
559 if(safe_strcmp("person", value))
563 item[i] = xstrdup(value);
569 ldif_parse_file(FILE *handle)
576 memset(item, 0, sizeof(item));
579 if( !(line = ldif_read_line(handle)) )
582 if(-1 == (str_parse_line(line, &type, &value, &vlen))) {
584 continue; /* just skip the errors */
587 ldif_fix_string(value);
589 ldif_convert(item, type, value);
592 } while ( !feof(handle) );
594 ldif_convert(item, "dn", "");
600 ldif_fix_string(char *str)
604 for( i = 0, j = 0; j < (int)strlen(str); i++, j++)
605 str[i] = ( str[j] == (char)0xc3 ?
606 (char) str[++j] + (char) 0x40 :
617 * mutt alias import filter
623 mutt_read_line(FILE *in, char **alias, char **rest)
625 char *line, *ptr, *tmp;
628 if( !(line = ptr = getaline(in)) )
629 return 1; /* error / EOF */
631 while( ISSPACE(*ptr) )
634 if(strncmp("alias", ptr, 5)) {
641 while( ISSPACE(*ptr) )
646 while( ! ISSPACE(*ptr) )
649 alias_len = (size_t)(ptr - tmp);
651 *alias = xmalloc_inc(alias_len, 1);
653 strncpy(*alias, tmp, alias_len);
654 *(*alias + alias_len) = 0;
659 *rest = xstrdup(ptr);
666 mutt_fix_quoting(char *p)
686 mutt_parse_email(list_item item)
688 char *line = item[NAME];
696 mutt_fix_quoting(line);
697 tmp = strconcat("From: ", line, NULL);
698 getname(tmp, &name, &email);
711 * this is completely broken
714 while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
715 tmp = strconcat("From: ", ++start, NULL);
716 getname(tmp, &name, &email);
721 tmp = strconcat(item[EMAIL], ",", email, NULL);
733 mutt_parse_file(FILE *in)
738 memset(item, 0, sizeof(item));
740 if(!mutt_read_line(in, &item[NICK],
742 mutt_parse_email(item);
745 free_list_item(item);
749 add_item2database(item);
756 * end of mutt alias import filter
765 ldif_fput_type_and_value(FILE *out,char *type, char *value )
769 tmp = ldif_type_and_value(type, value, strlen(value));
777 ldif_export_database(FILE *out, struct db_enumerator e)
779 char email[MAX_EMAILSTR_LEN];
781 fprintf(out, "version: 1\n");
783 db_enumerate_items(e) {
786 get_first_email(email, e.item);
788 tmp = mkstr("cn=%s,mail=%s", database[e.item][NAME], email);
789 ldif_fput_type_and_value(out, "dn", tmp);
792 for(j = 0; j < LDIF_ITEM_FIELDS; j++) {
793 if(ldif_conv_table[j] >= 0) {
794 if(ldif_conv_table[j] == EMAIL)
795 ldif_fput_type_and_value(out,
796 ldif_field_names[j], email);
797 else if(database[e.item][ldif_conv_table[j]])
798 ldif_fput_type_and_value(out,
800 database[e.item][ldif_conv_table[j]]);
804 fprintf(out, "objectclass: top\n"
805 "objectclass: person\n\n");
812 * end of ldif export filter
819 static void html_export_write_head(FILE *out, int extra_column);
820 static void html_export_write_tail(FILE *out);
822 extern int extra_column;
825 html_export_database(FILE *out, struct db_enumerator e)
827 char tmp[MAX_EMAILSTR_LEN];
832 extra_column = (extra_column > 2 && extra_column < ITEM_FIELDS) ?
833 extra_column : PHONE;
835 html_export_write_head(out, extra_column);
837 db_enumerate_items(e) {
838 get_first_email(tmp, e.item);
840 fprintf(out, "<tr>\n<td><a href=\"mailto:%s\">%s</a>\n",
842 database[e.item][NAME] );
844 fprintf(out, "<tr>\n<td>%s\n",
845 database[e.item][NAME] );
847 fprintf(out, "<td>%s\n<td>%s\n",
848 database[e.item][EMAIL],
849 safe_str(database[e.item][extra_column]) );
850 fprintf(out, "</tr>\n\n");
853 html_export_write_tail(out);
860 html_export_write_head(FILE *out, int extra_column)
862 char *realname = get_real_name();
864 fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
865 fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>", realname );
866 fprintf(out, "\n</head>\n<body>\n");
867 fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
868 fprintf(out, "<br><br>\n\n");
870 fprintf(out, "<table border=\"1\" align=\"center\">\n");
871 fprintf(out, "\n<tr><th>Name<th>E-mail address(es)<th>%s</tr>\n\n",
872 abook_fields[extra_column].name);
878 html_export_write_tail(FILE *out)
880 fprintf(out, "\n</table>\n");
881 fprintf(out, "\n</body>\n</html>\n");
885 * end of html export filter
890 * pine addressbook import filter
893 #define PINE_BUF_SIZE 2048
896 pine_fixbuf(char *buf)
900 for(i=0,j=0; j < (int)strlen(buf); i++, j++)
901 buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
905 pine_convert_emails(char *s)
910 if(s == NULL || *s != '(')
916 if( ( tmp = strchr(s,')')) )
919 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
920 if(i > MAX_EMAILS - 1) {
928 pine_parse_buf(char *buf)
933 char tmp[PINE_BUF_SIZE];
935 int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
937 memset(&item, 0, sizeof(item));
939 for(i=0, last=0; !last ; i++) {
940 if( !(end = strchr(start, '\t')) )
943 len = last ? strlen(start) : (int) (end-start);
944 len = min(len, PINE_BUF_SIZE - 1);
946 if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
947 && pine_conv_table[i] >= 0) {
948 strncpy(tmp, start, len);
951 item[pine_conv_table[i]] = xstrdup(tmp);
956 pine_convert_emails(item[EMAIL]);
957 add_item2database(item);
961 #define LINESIZE 1024
964 pine_parse_file(FILE *in)
971 fgets(line, LINESIZE, in);
975 buf = xrealloc(buf, i*LINESIZE);
978 fgets(line, LINESIZE, in);
980 if(*ptr != ' ' || feof(in))
1003 * end of pine addressbook import filter
1008 * pine addressbook export filter
1010 * filter doesn't wrap the lines as it should but Pine seems to handle
1011 * created files without problems - JH
1015 pine_export_database(FILE *out, struct db_enumerator e)
1017 db_enumerate_items(e) {
1018 fprintf(out, have_multiple_emails(e.item) ?
1019 "%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n",
1020 safe_str(database[e.item][NICK]),
1021 safe_str(database[e.item][NAME]),
1022 safe_str(database[e.item][EMAIL]),
1023 safe_str(database[e.item][NOTES])
1031 * end of pine addressbook export filter
1040 * these files should be parsed according to a certain
1041 * lay out, or the default if layout is not given, at
1042 * the moment only default is done...
1045 #define CSV_COMMENT_CHAR '#'
1047 static int csv_conv_table[] = {
1055 static int allcsv_conv_table[] = {
1079 csv_convert_emails(char *s)
1087 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1 )
1088 if(i > MAX_EMAILS - 1) {
1096 csv_remove_quotes(char *s)
1098 char *copy, *trimmed;
1101 copy = trimmed = xstrdup(s);
1104 len = strlen(trimmed);
1105 if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
1110 trimmed[len - 1] = 0;
1112 trimmed = xstrdup(trimmed);
1122 csv_store_field(list_item item, char *s, int field)
1124 char *newstr = NULL;
1129 if( !(newstr = csv_remove_quotes(s)) )
1132 if(field < (int)(sizeof(csv_conv_table) / sizeof(*csv_conv_table))
1133 && csv_conv_table[field] >= 0) {
1134 item[csv_conv_table[field]] = newstr;
1141 allcsv_store_field(list_item item, char *s, int field)
1143 char *newstr = NULL;
1148 if( !(newstr = csv_remove_quotes(s)) )
1151 if(field < (int)(sizeof(allcsv_conv_table) / sizeof(*allcsv_conv_table))
1152 && allcsv_conv_table[field] >= 0) {
1153 item[allcsv_conv_table[field]] = newstr;
1160 csv_is_valid_quote_end(char *p)
1168 else if(!ISSPACE(*p))
1176 csv_is_valid_quote_start(char *p)
1181 else if(!ISSPACE(*p))
1189 csv_parse_line(char *line)
1193 bool in_quote = FALSE;
1196 memset(item, 0, sizeof(item));
1198 for(p = start = line, field = 0; *p; p++) {
1200 if(csv_is_valid_quote_end(p))
1203 if ( (((p - start) / sizeof (char)) < 2 ) &&
1204 csv_is_valid_quote_start(p) )
1208 if( *p == ',' && !in_quote) {
1210 csv_store_field(item, start, field);
1218 csv_store_field(item, start, field);
1220 csv_convert_emails(item[EMAIL]);
1221 add_item2database(item);
1225 allcsv_parse_line(char *line)
1229 bool in_quote = FALSE;
1232 memset(item, 0, sizeof(item));
1234 for(p = start = line, field = 0; *p; p++) {
1236 if(csv_is_valid_quote_end(p))
1239 if ( (((p - start) / sizeof (char)) < 2 ) &&
1240 csv_is_valid_quote_start(p) )
1244 if( *p == ',' && !in_quote) {
1246 allcsv_store_field(item, start, field);
1254 allcsv_store_field(item, start, field);
1256 csv_convert_emails(item[EMAIL]);
1257 add_item2database(item);
1262 csv_parse_file(FILE *in)
1267 line = getaline(in);
1269 if(line && *line && *line != CSV_COMMENT_CHAR)
1270 csv_parse_line(line);
1279 allcsv_parse_file(FILE *in)
1284 line = getaline(in);
1286 if(line && *line && *line != CSV_COMMENT_CHAR)
1287 allcsv_parse_line(line);
1296 * end of csv import filter
1300 * csv addressbook export filters
1303 #define CSV_LAST (-1)
1304 #define CSV_UNDEFINED (-2)
1305 #define CSV_SPECIAL(X) (-3 - (X))
1306 #define CSV_IS_SPECIAL(X) ((X) <= -3)
1309 csv_export_common(FILE *out, struct db_enumerator e,
1310 int fields[], void (*special_func)(FILE *, int, int))
1314 db_enumerate_items(e) {
1315 for(i = 0; fields[i] != CSV_LAST; i++) {
1316 if(fields[i] == CSV_UNDEFINED)
1317 fprintf(out, "\"\"");
1318 else if(CSV_IS_SPECIAL(fields[i])) {
1320 (*special_func)(out, e.item, fields[i]);
1323 strchr(safe_str(database[e.item][fields[i]]), ',') ||
1324 strchr(safe_str(database[e.item][fields[i]]), '\"')) ?
1326 safe_str(database[e.item][fields[i]])
1328 fprintf(out, "\"%s\"",
1329 safe_str(database[e.item][fields[i]]));
1331 if(fields[i + 1] != CSV_LAST)
1341 csv_export_database(FILE *out, struct db_enumerator e)
1343 int csv_export_fields[] = {
1352 csv_export_common(out, e, csv_export_fields, NULL);
1358 allcsv_export_database(FILE *out, struct db_enumerator e)
1361 * TODO: Should get these atomatically from abook_fileds
1364 int allcsv_export_fields[] = {
1389 fprintf(out, "\"NAME\",");
1390 fprintf(out, "\"EMAIL\",");
1391 fprintf(out, "\"ADDRESS\",");
1392 fprintf(out, "\"ADDRESS2\",");
1393 fprintf(out, "\"CITY\",");
1394 fprintf(out, "\"STATE\",");
1395 fprintf(out, "\"ZIP\",");
1396 fprintf(out, "\"COUNTRY\",");
1397 fprintf(out, "\"PHONE\",");
1398 fprintf(out, "\"WORKPHONE\",");
1399 fprintf(out, "\"FAX\",");
1400 fprintf(out, "\"MOBILEPHONE\",");
1401 fprintf(out, "\"NICK\",");
1402 fprintf(out, "\"URL\",");
1403 fprintf(out, "\"NOTES\",");
1404 fprintf(out, "\"CUSTOM1\",");
1405 fprintf(out, "\"CUSTOM2\",");
1406 fprintf(out, "\"CUSTOM3\",");
1407 fprintf(out, "\"CUSTOM4\",");
1408 fprintf(out, "\"CUSTOM5\"\n");
1410 csv_export_common(out, e, allcsv_export_fields, NULL);
1419 #define PALM_CSV_NAME CSV_SPECIAL(0)
1420 #define PALM_CSV_END CSV_SPECIAL(1)
1421 #define PALM_CSV_CAT CSV_SPECIAL(2)
1424 palm_split_and_write_name(FILE *out, char *name)
1430 if ( (p = strchr(name, ' ')) ) {
1434 fprintf(out, "\"%s\",\"" , p + 1);
1435 fwrite((void *)name, p - name, sizeof(char), out);
1438 fprintf(out, "\"%s\"", safe_str(name));
1443 palm_csv_handle_specials(FILE *out, int item, int field)
1447 palm_split_and_write_name(out, database[item][NAME]);
1450 fprintf(out, "\"abook\"");
1453 fprintf(out, "\"0\"");
1461 palm_export_database(FILE *out, struct db_enumerator e)
1463 int palm_export_fields[] = {
1464 PALM_CSV_NAME, /* LASTNAME, FIRSTNAME */
1465 CSV_UNDEFINED, /* TITLE */
1466 CSV_UNDEFINED, /* COMPANY */
1467 WORKPHONE, /* WORK PHONE */
1468 PHONE, /* HOME PHONE */
1470 MOBILEPHONE, /* OTHER */
1472 ADDRESS, /* ADDRESS */
1476 COUNTRY, /* COUNTRY */
1477 NICK, /* DEFINED 1 */
1478 URL, /* DEFINED 2 */
1479 CSV_UNDEFINED, /* DEFINED 3 */
1480 CSV_UNDEFINED, /* DEFINED 4 */
1482 PALM_CSV_END, /* "0" */
1483 PALM_CSV_CAT, /* CATEGORY */
1487 csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
1493 * end of csv export filters
1497 * GnomeCard (VCard) addressbook export filter
1501 gcrd_export_database(FILE *out, struct db_enumerator e)
1503 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1507 db_enumerate_items(e) {
1508 fprintf(out, "BEGIN:VCARD\nFN:%s\n",
1509 safe_str(database[e.item][NAME]));
1511 name = get_surname(database[e.item][NAME]);
1512 for( j = strlen(database[e.item][NAME]) - 1; j >= 0; j-- ) {
1513 if(database[e.item][NAME][j] == ' ')
1516 fprintf(out, "N:%s;%.*s\n",
1519 safe_str(database[e.item][NAME])
1524 if ( database[e.item][ADDRESS] )
1525 fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\n",
1526 safe_str(database[e.item][ADDRESS]),
1527 safe_str(database[e.item][ADDRESS2]),
1528 safe_str(database[e.item][CITY]),
1529 safe_str(database[e.item][STATE]),
1530 safe_str(database[e.item][ZIP]),
1531 safe_str(database[e.item][COUNTRY])
1534 if (database[e.item][PHONE])
1535 fprintf(out, "TEL;HOME:%s\n", database[e.item][PHONE]);
1536 if (database[e.item][WORKPHONE])
1537 fprintf(out, "TEL;WORK:%s\n", database[e.item][WORKPHONE]);
1538 if (database[e.item][FAX])
1539 fprintf(out, "TEL;FAX:%s\n", database[e.item][FAX]);
1540 if (database[e.item][MOBILEPHONE])
1541 fprintf(out, "TEL;CELL:%s\n", database[e.item][MOBILEPHONE]);
1543 if ( database[e.item][EMAIL] ) {
1544 split_emailstr(e.item, emails);
1545 for(j=0; j < MAX_EMAILS ; j++) {
1547 fprintf(out, "EMAIL;INTERNET:%s\n",
1552 if ( database[e.item][NOTES] )
1553 fprintf(out, "NOTE:%s\n", database[e.item][NOTES]);
1554 if (database[e.item][URL])
1555 fprintf(out, "URL:%s\n", database[e.item][URL]);
1557 fprintf(out, "END:VCARD\n\n");
1565 * end of GnomeCard export filter
1570 * mutt alias export filter
1574 mutt_alias_genalias(int i)
1578 if(database[i][NICK])
1579 return xstrdup(database[i][NICK]);
1581 tmp = xstrdup(database[i][NAME]);
1583 if( ( pos = strchr(tmp, ' ') ) )
1592 mutt_alias_export(FILE *out, struct db_enumerator e)
1594 char email[MAX_EMAIL_LEN];
1597 db_enumerate_items(e) {
1598 alias = mutt_alias_genalias(e.item);
1600 get_first_email(email, e.item);
1601 fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n",
1603 database[e.item][NAME],
1612 * end of mutt alias export filter
1617 * printable export filter
1622 text_write_address_us(FILE *out, int i) {
1623 fprintf(out, "\n%s", database[i][ADDRESS]);
1625 if (database[i][ADDRESS2])
1626 fprintf(out, "\n%s", database[i][ADDRESS2]);
1628 if (database[i][CITY])
1629 fprintf(out, "\n%s", database[i][CITY]);
1631 if (database[i][STATE] || database[i][ZIP]) {
1634 if(database[i][STATE]) {
1635 fprintf(out, "%s", database[i][STATE]);
1636 if(database[i][ZIP])
1640 if(database[i][ZIP])
1641 fprintf(out, "%s", database[i][ZIP]);
1644 if (database[i][COUNTRY])
1645 fprintf(out, "\n%s", database[i][COUNTRY]);
1650 text_write_address_uk(FILE *out, int i) {
1653 for (j = ADDRESS; j <= COUNTRY; j++)
1655 fprintf(out, "\n%s", database[i][j]);
1659 text_write_address_eu(FILE *out, int i) {
1660 fprintf(out, "\n%s", database[i][ADDRESS]);
1662 if (database[i][ADDRESS2])
1663 fprintf(out, "\n%s", database[i][ADDRESS2]);
1665 if (database[i][ZIP] || database[i][CITY]) {
1668 if(database[i][ZIP]) {
1669 fprintf(out, "%s", database[i][ZIP]);
1670 if(database[i][CITY])
1674 if(database[i][CITY])
1675 fprintf(out, "%s", database[i][CITY]);
1678 if (database[i][STATE])
1679 fprintf(out, "\n%s", database[i][STATE]);
1681 if (database[i][COUNTRY])
1682 fprintf(out, "\n%s", database[i][COUNTRY]);
1686 text_export_database(FILE * out, struct db_enumerator e)
1688 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1690 char *realname = get_real_name();
1691 char *style = opt_get_str(STR_ADDRESS_STYLE);
1694 "-----------------------------------------\n%s's address book\n"
1695 "-----------------------------------------\n\n\n",
1699 db_enumerate_items(e) {
1701 "-----------------------------------------\n\n");
1702 fprintf(out, "%s", database[e.item][NAME]);
1703 if (database[e.item][NICK] && *database[e.item][NICK])
1704 fprintf(out, "\n(%s)", database[e.item][NICK]);
1707 if (*database[e.item][EMAIL]) {
1709 split_emailstr(e.item, emails);
1710 for (j = 0; j < MAX_EMAILS; j++)
1712 fprintf(out, "%s\n", emails[j]);
1715 if (database[e.item][ADDRESS]) {
1716 if (!safe_strcmp(style, "us")) /* US like */
1717 text_write_address_us(out, e.item);
1718 else if (!safe_strcmp(style, "uk")) /* UK like */
1719 text_write_address_uk(out, e.item);
1721 text_write_address_eu(out, e.item);
1726 if ((database[e.item][PHONE]) ||
1727 (database[e.item][WORKPHONE]) ||
1728 (database[e.item][FAX]) ||
1729 (database[e.item][MOBILEPHONE])) {
1731 for (j = PHONE; j <= MOBILEPHONE; j++)
1732 if (database[e.item][j])
1733 fprintf(out, "%s: %s\n",
1734 abook_fields[j].name,
1735 database[e.item][j]);
1738 if (database[e.item][URL])
1739 fprintf(out, "\n%s\n", database[e.item][URL]);
1740 if (database[e.item][NOTES])
1741 fprintf(out, "\n%s\n", database[e.item][NOTES]);
1746 fprintf(out, "-----------------------------------------\n");
1752 * end of printable export filter
1756 * elm alias export filter
1760 elm_alias_export(FILE *out, struct db_enumerator e)
1762 char email[MAX_EMAIL_LEN];
1765 db_enumerate_items(e) {
1766 alias = mutt_alias_genalias(e.item);
1767 get_first_email(email, e.item);
1768 fprintf(out, "%s = %s = %s\n",
1770 database[e.item][NAME],
1779 * end of elm alias export filter
1784 * Spruce export filter
1788 spruce_export_database (FILE *out, struct db_enumerator e)
1790 char email[MAX_EMAIL_LEN];
1792 fprintf (out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
1794 db_enumerate_items(e) {
1795 if(strcmp (safe_str(database[e.item][EMAIL]), "")) {
1796 get_first_email(email, e.item);
1797 fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
1799 database[e.item][NAME],
1801 safe_str(database[e.item][NOTES])
1806 fprintf (out, "# End of address book file.\n");
1812 * end of Spruce export filter