5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
16 #include <sys/types.h>
17 #include "abook_curses.h"
31 extern list_item *database;
32 extern struct abook_field abook_fields[];
35 * function declarations
39 * import filter prototypes
42 static int ldif_parse_file(FILE *handle);
43 static int mutt_parse_file(FILE *in);
44 static int pine_parse_file(FILE *in);
45 static int csv_parse_file(FILE *in);
46 static int allcsv_parse_file(FILE *in);
47 static int palmcsv_parse_file(FILE *in);
50 * export filter prototypes
53 static int ldif_export_database(FILE *out, struct db_enumerator e);
54 static int html_export_database(FILE *out, struct db_enumerator e);
55 static int pine_export_database(FILE *out, struct db_enumerator e);
56 static int csv_export_database(FILE *out, struct db_enumerator e);
57 static int allcsv_export_database(FILE *out, struct db_enumerator e);
58 static int palm_export_database(FILE *out, struct db_enumerator e);
59 static int gcrd_export_database(FILE *out, struct db_enumerator e);
60 static int mutt_alias_export(FILE *out, struct db_enumerator e);
61 static int elm_alias_export(FILE *out, struct db_enumerator e);
62 static int text_export_database(FILE *out, struct db_enumerator e);
63 static int spruce_export_database(FILE *out, struct db_enumerator e);
66 * end of function declarations
69 struct abook_input_filter i_filters[] = {
70 { "abook", N_("abook native format"), parse_database },
71 { "ldif", N_("ldif / Netscape addressbook"), ldif_parse_file },
72 { "mutt", N_("mutt alias"), mutt_parse_file },
73 { "pine", N_("pine addressbook"), pine_parse_file },
74 { "csv", N_("comma separated values"), csv_parse_file },
75 { "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file },
76 { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file },
80 struct abook_output_filter e_filters[] = {
81 { "abook", N_("abook native format"), write_database },
82 { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database },
83 { "mutt", N_("mutt alias"), mutt_alias_export },
84 { "html", N_("html document"), html_export_database },
85 { "pine", N_("pine addressbook"), pine_export_database },
86 { "gcrd", N_("GnomeCard (VCard) addressbook"), gcrd_export_database },
87 { "csv", N_("comma separated values"), csv_export_database },
88 { "allcsv", N_("comma separated values (all fields)"), allcsv_export_database },
89 { "palmcsv", N_("Palm comma separated values"), palm_export_database},
90 { "elm", N_("elm alias"), elm_alias_export },
91 { "text", N_("plain text"), text_export_database },
92 { "spruce", N_("Spruce address book"), spruce_export_database },
106 for(i=0; *i_filters[i].filtname ; i++)
107 printf("\t%s\t%s\n", i_filters[i].filtname,
108 gettext(i_filters[i].desc));
113 for(i=0; *e_filters[i].filtname ; i++)
114 printf("\t%s\t%s\n", e_filters[i].filtname,
115 gettext(e_filters[i].desc));
121 number_of_output_filters()
125 for(i=0; *e_filters[i].filtname ; i++)
132 number_of_input_filters()
136 for(i=0; *i_filters[i].filtname ; i++)
145 char *username = getenv("USER");
146 struct passwd *pwent;
150 pwent = getpwnam(username);
152 if((tmp = xstrdup(pwent->pw_gecos)) == NULL)
153 return xstrdup(username);
155 rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
156 if (rtn == EOF || rtn == 0) {
158 return xstrdup(username);
167 static int i_read_file(char *filename, int (*func) (FILE *in));
176 refresh_statusline();
177 headerline(_("import database"));
179 mvaddstr(3, 1, _("please select a filter"));
182 for(i=0; *i_filters[i].filtname ; i++)
183 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
184 i_filters[i].filtname,
185 gettext(i_filters[i].desc));
187 mvprintw(6 + i, 6, _("x -\tcancel"));
199 filter = getch() - 'a';
200 if(filter == 'x' - 'a' ||
201 filter >= number_of_input_filters() || filter < 0) {
206 mvaddstr(5+filter, 2, "->");
208 filename = ask_filename(_("Filename: "));
214 if(i_read_file(filename, i_filters[filter].func ))
215 statusline_msg(_("Error occured while opening the file"));
216 else if(tmp == items)
217 statusline_msg(_("File does not seem to be a valid addressbook"));
228 i_read_file(char *filename, int (*func) (FILE *in))
233 if( (in = abook_fopen( filename, "r" )) == NULL )
244 import_file(char filtname[FILTNAME_LEN], char *filename)
251 if(! strncasecmp(i_filters[i].filtname, filtname,
254 if(! *i_filters[i].filtname) {
263 if(!strcmp(filename, "-")) {
265 if((fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
268 ret = (*i_filters[i].func) (stdin);
270 ret = i_read_file(filename, i_filters[i].func);
282 static int e_write_file(char *filename,
283 int (*func) (FILE *in, struct db_enumerator e), int mode);
293 refresh_statusline();
294 headerline(_("export database"));
296 mvaddstr(3, 1, _("please select a filter"));
299 for(i = 0; *e_filters[i].filtname ; i++)
300 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
301 e_filters[i].filtname,
302 gettext(e_filters[i].desc));
304 mvprintw(6 + i, 6, _("x -\tcancel"));
311 int enum_mode = ENUM_ALL;
316 filter = getch() - 'a';
317 if(filter == 'x' - 'a' ||
318 filter >= number_of_output_filters() || filter < 0) {
323 mvaddstr(5 + filter, 2, "->");
325 if(selected_items()) {
326 switch(statusline_askchoice(
327 _("Export <a>ll, export <s>elected, or <c>ancel?"),
328 S_("keybindings:all/selected/cancel|asc"), 3)) {
332 enum_mode = ENUM_SELECTED;
342 filename = ask_filename(_("Filename: "));
348 if( e_write_file(filename, e_filters[filter].func, enum_mode))
349 statusline_msg(_("Error occured while exporting"));
358 e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e),
363 struct db_enumerator enumerator = init_db_enumerator(mode);
365 if((out = fopen(filename, "a")) == NULL)
371 ret = (*func) (out, enumerator);
379 fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
382 struct db_enumerator e = init_db_enumerator(enum_mode);
385 if(!strncasecmp(e_filters[i].filtname, filtname,
388 if(!*e_filters[i].filtname) {
394 return (e_filters[i].func) (handle, e);
400 export_file(char filtname[FILTNAME_LEN], char *filename)
402 const int mode = ENUM_ALL;
405 struct db_enumerator e = init_db_enumerator(mode);
408 if(!strncasecmp(e_filters[i].filtname, filtname,
411 if(!*e_filters[i].filtname) {
420 if(!strcmp(filename, "-"))
421 ret = (e_filters[i].func) (stdout, e);
423 ret = e_write_file(filename, e_filters[i].func, mode);
429 * end of common functions
438 static void ldif_fix_string(char *str);
440 #define LDIF_ITEM_FIELDS 16
442 typedef char *ldif_item[LDIF_ITEM_FIELDS];
444 static ldif_item ldif_field_names = {
456 "facsimiletelephonenumber",
460 "objectclass", /* this must be the last entry */
463 static int ldif_conv_table[LDIF_ITEM_FIELDS] = {
466 ADDRESS, /* "streetaddress" */
467 ADDRESS2, /* "streetaddress2" */
468 CITY, /* "locality" */
470 ZIP, /* "postalcode" */
471 COUNTRY, /* "countryname" */
472 PHONE, /* "homephone" */
473 NOTES, /* "description" */
475 FAX, /* "facsimiletelephonenumber" */
476 MOBILEPHONE, /* "cellphone" */
477 WORKPHONE, /* "xmozillaanyphone" */
478 NICK, /* "xmozillanickname" */
479 -1, /* "objectclass" */ /* this must be the last entry */
484 ldif_read_line(FILE *in)
497 if(feof(in) || !line)
506 fseek(in, pos, SEEK_SET); /* fixme ! */
516 buf = strconcat(buf, ptr, NULL);
521 if(buf && *buf == '#' ) {
530 ldif_add_item(ldif_item li)
532 list_item abook_item;
535 memset(abook_item, 0, sizeof(abook_item));
537 if(!li[LDIF_ITEM_FIELDS -1])
541 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
542 if(ldif_conv_table[i] >= 0 && li[i] && *li[i])
543 abook_item[ldif_conv_table[i]] = xstrdup(li[i]);
546 add_item2database(abook_item);
549 for(i=0; i < LDIF_ITEM_FIELDS; i++)
555 ldif_convert(ldif_item item, char *type, char *value)
559 if(!strcmp(type, "dn")) {
564 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
565 if(!safe_strcmp(ldif_field_names[i], type) && *value) {
566 if(i == LDIF_ITEM_FIELDS - 1) /* this is a dirty hack */
567 if(safe_strcmp("person", value))
571 item[i] = xstrdup(value);
577 ldif_parse_file(FILE *handle)
584 memset(item, 0, sizeof(item));
587 if( !(line = ldif_read_line(handle)) )
590 if(-1 == (str_parse_line(line, &type, &value, &vlen))) {
592 continue; /* just skip the errors */
595 ldif_fix_string(value);
597 ldif_convert(item, type, value);
600 } while ( !feof(handle) );
602 ldif_convert(item, "dn", "");
608 ldif_fix_string(char *str)
612 for(i = 0, j = 0; j < (int)strlen(str); i++, j++)
613 str[i] = ( str[j] == (char)0xc3 ?
614 (char) str[++j] + (char) 0x40 :
625 * mutt alias import filter
631 mutt_read_line(FILE *in, char **alias, char **rest)
633 char *line, *ptr, *tmp;
636 if( !(line = ptr = getaline(in)) )
637 return 1; /* error / EOF */
639 while( ISSPACE(*ptr) )
642 if(strncmp("alias", ptr, 5)) {
649 while( ISSPACE(*ptr) )
654 while( ! ISSPACE(*ptr) )
657 alias_len = (size_t)(ptr - tmp);
659 *alias = xmalloc_inc(alias_len, 1);
661 strncpy(*alias, tmp, alias_len);
662 *(*alias + alias_len) = 0;
667 *rest = xstrdup(ptr);
674 mutt_fix_quoting(char *p)
694 mutt_parse_email(list_item item)
696 char *line = item[NAME];
704 mutt_fix_quoting(line);
705 tmp = strconcat("From: ", line, NULL);
706 getname(tmp, &name, &email);
719 * this is completely broken
722 while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
723 tmp = strconcat("From: ", ++start, NULL);
724 getname(tmp, &name, &email);
729 tmp = strconcat(item[EMAIL], ",", email, NULL);
741 mutt_parse_file(FILE *in)
746 memset(item, 0, sizeof(item));
748 if(!mutt_read_line(in, &item[NICK],
750 mutt_parse_email(item);
753 free_list_item(item);
757 add_item2database(item);
764 * end of mutt alias import filter
773 ldif_fput_type_and_value(FILE *out,char *type, char *value )
777 tmp = ldif_type_and_value(type, value, strlen(value));
785 ldif_export_database(FILE *out, struct db_enumerator e)
787 char email[MAX_EMAILSTR_LEN];
789 fprintf(out, "version: 1\n");
791 db_enumerate_items(e) {
794 get_first_email(email, e.item);
796 tmp = mkstr("cn=%s,mail=%s", database[e.item][NAME], email);
797 ldif_fput_type_and_value(out, "dn", tmp);
800 for(j = 0; j < LDIF_ITEM_FIELDS; j++) {
801 if(ldif_conv_table[j] >= 0) {
802 if(ldif_conv_table[j] == EMAIL)
803 ldif_fput_type_and_value(out,
804 ldif_field_names[j], email);
805 else if(database[e.item][ldif_conv_table[j]])
806 ldif_fput_type_and_value(out,
808 database[e.item][ldif_conv_table[j]]);
812 fprintf(out, "objectclass: top\n"
813 "objectclass: person\n\n");
820 * end of ldif export filter
827 static void html_export_write_head(FILE *out, int extra_column);
828 static void html_export_write_tail(FILE *out);
830 extern int extra_column;
833 html_export_database(FILE *out, struct db_enumerator e)
835 char tmp[MAX_EMAILSTR_LEN];
840 extra_column = (extra_column > 2 && extra_column < ITEM_FIELDS) ?
841 extra_column : PHONE;
843 html_export_write_head(out, extra_column);
845 db_enumerate_items(e) {
846 get_first_email(tmp, e.item);
848 fprintf(out, "<tr>\n<td><a href=\"mailto:%s\">%s</a>\n",
850 database[e.item][NAME] );
852 fprintf(out, "<tr>\n<td>%s\n",
853 database[e.item][NAME] );
855 fprintf(out, "<td>%s\n<td>%s\n",
856 database[e.item][EMAIL],
857 safe_str(database[e.item][extra_column]) );
858 fprintf(out, "</tr>\n\n");
861 html_export_write_tail(out);
868 html_export_write_head(FILE *out, int extra_column)
870 char *realname = get_real_name();
872 fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
873 fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>", realname );
874 fprintf(out, "\n</head>\n<body>\n");
875 fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
876 fprintf(out, "<br><br>\n\n");
878 fprintf(out, "<table border=\"1\" align=\"center\">\n");
879 fprintf(out, "\n<tr><th>Name<th>E-mail address(es)<th>%s</tr>\n\n",
880 abook_fields[extra_column].name);
886 html_export_write_tail(FILE *out)
888 fprintf(out, "\n</table>\n");
889 fprintf(out, "\n</body>\n</html>\n");
893 * end of html export filter
898 * pine addressbook import filter
901 #define PINE_BUF_SIZE 2048
904 pine_fixbuf(char *buf)
908 for(i=0,j=0; j < (int)strlen(buf); i++, j++)
909 buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
913 pine_convert_emails(char *s)
918 if(s == NULL || *s != '(')
924 if( ( tmp = strchr(s,')')) )
927 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
928 if(i > MAX_EMAILS - 1) {
936 pine_parse_buf(char *buf)
941 char tmp[PINE_BUF_SIZE];
943 int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
945 memset(&item, 0, sizeof(item));
947 for(i=0, last=0; !last ; i++) {
948 if( !(end = strchr(start, '\t')) )
951 len = last ? strlen(start) : (int) (end-start);
952 len = min(len, PINE_BUF_SIZE - 1);
954 if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
955 && pine_conv_table[i] >= 0) {
956 strncpy(tmp, start, len);
959 item[pine_conv_table[i]] = xstrdup(tmp);
964 pine_convert_emails(item[EMAIL]);
965 add_item2database(item);
969 #define LINESIZE 1024
972 pine_parse_file(FILE *in)
979 fgets(line, LINESIZE, in);
983 buf = xrealloc(buf, i*LINESIZE);
986 fgets(line, LINESIZE, in);
988 if(*ptr != ' ' || feof(in))
1002 pine_parse_buf(buf);
1011 * end of pine addressbook import filter
1016 * pine addressbook export filter
1018 * filter doesn't wrap the lines as it should but Pine seems to handle
1019 * created files without problems - JH
1023 pine_export_database(FILE *out, struct db_enumerator e)
1025 db_enumerate_items(e) {
1026 fprintf(out, have_multiple_emails(e.item) ?
1027 "%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n",
1028 safe_str(database[e.item][NICK]),
1029 safe_str(database[e.item][NAME]),
1030 safe_str(database[e.item][EMAIL]),
1031 safe_str(database[e.item][NOTES])
1039 * end of pine addressbook export filter
1048 * these files should be parsed according to a certain
1049 * lay out, or the default if layout is not given, at
1050 * the moment only default is done...
1053 #define CSV_COMMENT_CHAR '#'
1054 #define CSV_DUPLICATE_SEPARATOR " "
1055 #define CSV_TABLE_SIZE(t) (sizeof (t) / sizeof *(t))
1057 static int csv_conv_table[] = {
1065 static int allcsv_conv_table[] = {
1088 static int palmcsv_conv_table[] = {
1089 NAME, /* Last name */
1090 NAME, /* First name */
1111 csv_convert_emails(char *s)
1119 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
1120 if(i > MAX_EMAILS - 1) {
1128 csv_remove_quotes(char *s)
1130 char *copy, *trimmed;
1133 copy = trimmed = xstrdup(s);
1136 len = strlen(trimmed);
1137 if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
1142 trimmed[len - 1] = 0;
1144 trimmed = xstrdup(trimmed);
1154 csv_field_to_item(int *table_base, size_t table_size, int field)
1156 if(field < table_size)
1157 return table_base[field];
1163 csv_store_item(list_item item, int i, char *s)
1165 char *newstr = NULL;
1170 if( !(newstr = csv_remove_quotes(s)) )
1174 if (item[i] != NULL) {
1175 char *oldstr = item[i];
1177 item[i] = strconcat(newstr, CSV_DUPLICATE_SEPARATOR,
1190 csv_is_valid_quote_end(char *p)
1198 else if(!ISSPACE(*p))
1206 csv_is_valid_quote_start(char *p)
1211 else if(!ISSPACE(*p))
1219 csv_parse_line(char *line, int *table_base, size_t table_size)
1223 bool in_quote = FALSE;
1226 memset(item, 0, sizeof(item));
1228 for(p = start = line, field = 0; *p; p++) {
1230 if(csv_is_valid_quote_end(p))
1233 if ( (((p - start) / sizeof (char)) < 2 ) &&
1234 csv_is_valid_quote_start(p) )
1238 if(*p == ',' && !in_quote) {
1240 csv_field_to_item(table_base, table_size, field);
1241 csv_store_item(item,
1242 csv_field_to_item(table_base,
1252 csv_store_item(item, csv_field_to_item(table_base, table_size, field),
1255 csv_convert_emails(item[EMAIL]);
1256 add_item2database(item);
1260 csv_parse_file_common(FILE *in, int *conv_table, size_t table_size)
1265 line = getaline(in);
1267 if(line && *line && *line != CSV_COMMENT_CHAR)
1268 csv_parse_line(line, conv_table, table_size);
1277 csv_parse_file(FILE *in)
1279 return csv_parse_file_common(in, csv_conv_table,
1280 CSV_TABLE_SIZE(csv_conv_table));
1284 allcsv_parse_file(FILE *in)
1286 return csv_parse_file_common(in, allcsv_conv_table,
1287 CSV_TABLE_SIZE(allcsv_conv_table));
1291 palmcsv_parse_file(FILE *in)
1293 return csv_parse_file_common(in, palmcsv_conv_table,
1294 CSV_TABLE_SIZE(palmcsv_conv_table));
1298 * end of csv import filter
1302 * csv addressbook export filters
1305 #define CSV_LAST (-1)
1306 #define CSV_UNDEFINED (-2)
1307 #define CSV_SPECIAL(X) (-3 - (X))
1308 #define CSV_IS_SPECIAL(X) ((X) <= -3)
1311 csv_export_common(FILE *out, struct db_enumerator e,
1312 int fields[], void (*special_func)(FILE *, int, int))
1316 db_enumerate_items(e) {
1317 for(i = 0; fields[i] != CSV_LAST; i++) {
1318 if(fields[i] == CSV_UNDEFINED)
1319 fprintf(out, "\"\"");
1320 else if(CSV_IS_SPECIAL(fields[i])) {
1322 (*special_func)(out, e.item, fields[i]);
1325 strchr(safe_str(database[e.item][fields[i]]), ',') ||
1326 strchr(safe_str(database[e.item][fields[i]]), '\"')) ?
1328 safe_str(database[e.item][fields[i]])
1330 fprintf(out, "\"%s\"",
1331 safe_str(database[e.item][fields[i]]));
1333 if(fields[i + 1] != CSV_LAST)
1343 csv_export_database(FILE *out, struct db_enumerator e)
1345 int csv_export_fields[] = {
1354 csv_export_common(out, e, csv_export_fields, NULL);
1360 allcsv_export_database(FILE *out, struct db_enumerator e)
1363 * TODO: Should get these atomatically from abook_fileds
1366 int allcsv_export_fields[] = {
1391 fprintf(out, "\"NAME\",");
1392 fprintf(out, "\"EMAIL\",");
1393 fprintf(out, "\"ADDRESS\",");
1394 fprintf(out, "\"ADDRESS2\",");
1395 fprintf(out, "\"CITY\",");
1396 fprintf(out, "\"STATE\",");
1397 fprintf(out, "\"ZIP\",");
1398 fprintf(out, "\"COUNTRY\",");
1399 fprintf(out, "\"PHONE\",");
1400 fprintf(out, "\"WORKPHONE\",");
1401 fprintf(out, "\"FAX\",");
1402 fprintf(out, "\"MOBILEPHONE\",");
1403 fprintf(out, "\"NICK\",");
1404 fprintf(out, "\"URL\",");
1405 fprintf(out, "\"NOTES\",");
1406 fprintf(out, "\"CUSTOM1\",");
1407 fprintf(out, "\"CUSTOM2\",");
1408 fprintf(out, "\"CUSTOM3\",");
1409 fprintf(out, "\"CUSTOM4\",");
1410 fprintf(out, "\"CUSTOM5\"\n");
1412 csv_export_common(out, e, allcsv_export_fields, NULL);
1421 #define PALM_CSV_NAME CSV_SPECIAL(0)
1422 #define PALM_CSV_END CSV_SPECIAL(1)
1423 #define PALM_CSV_CAT CSV_SPECIAL(2)
1426 palm_split_and_write_name(FILE *out, char *name)
1432 if ( (p = strchr(name, ' ')) ) {
1436 fprintf(out, "\"%s\",\"" , p + 1);
1437 fwrite((void *)name, p - name, sizeof(char), out);
1440 fprintf(out, "\"%s\"", safe_str(name));
1445 palm_csv_handle_specials(FILE *out, int item, int field)
1449 palm_split_and_write_name(out, database[item][NAME]);
1452 fprintf(out, "\"abook\"");
1455 fprintf(out, "\"0\"");
1463 palm_export_database(FILE *out, struct db_enumerator e)
1465 int palm_export_fields[] = {
1466 PALM_CSV_NAME, /* LASTNAME, FIRSTNAME */
1467 CSV_UNDEFINED, /* TITLE */
1468 CSV_UNDEFINED, /* COMPANY */
1469 WORKPHONE, /* WORK PHONE */
1470 PHONE, /* HOME PHONE */
1472 MOBILEPHONE, /* OTHER */
1474 ADDRESS, /* ADDRESS */
1478 COUNTRY, /* COUNTRY */
1479 NICK, /* DEFINED 1 */
1480 URL, /* DEFINED 2 */
1481 CSV_UNDEFINED, /* DEFINED 3 */
1482 CSV_UNDEFINED, /* DEFINED 4 */
1484 PALM_CSV_END, /* "0" */
1485 PALM_CSV_CAT, /* CATEGORY */
1489 csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
1495 * end of csv export filters
1499 * GnomeCard (VCard) addressbook export filter
1503 gcrd_export_database(FILE *out, struct db_enumerator e)
1505 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1509 db_enumerate_items(e) {
1510 fprintf(out, "BEGIN:VCARD\nFN:%s\n",
1511 safe_str(database[e.item][NAME]));
1513 name = get_surname(database[e.item][NAME]);
1514 for( j = strlen(database[e.item][NAME]) - 1; j >= 0; j-- ) {
1515 if(database[e.item][NAME][j] == ' ')
1518 fprintf(out, "N:%s;%.*s\n",
1521 safe_str(database[e.item][NAME])
1526 if ( database[e.item][ADDRESS] )
1527 fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\n",
1528 safe_str(database[e.item][ADDRESS]),
1529 safe_str(database[e.item][ADDRESS2]),
1530 safe_str(database[e.item][CITY]),
1531 safe_str(database[e.item][STATE]),
1532 safe_str(database[e.item][ZIP]),
1533 safe_str(database[e.item][COUNTRY])
1536 if (database[e.item][PHONE])
1537 fprintf(out, "TEL;HOME:%s\n", database[e.item][PHONE]);
1538 if (database[e.item][WORKPHONE])
1539 fprintf(out, "TEL;WORK:%s\n", database[e.item][WORKPHONE]);
1540 if (database[e.item][FAX])
1541 fprintf(out, "TEL;FAX:%s\n", database[e.item][FAX]);
1542 if (database[e.item][MOBILEPHONE])
1543 fprintf(out, "TEL;CELL:%s\n", database[e.item][MOBILEPHONE]);
1545 if ( database[e.item][EMAIL] ) {
1546 split_emailstr(e.item, emails);
1547 for(j=0; j < MAX_EMAILS ; j++) {
1549 fprintf(out, "EMAIL;INTERNET:%s\n",
1554 if ( database[e.item][NOTES] )
1555 fprintf(out, "NOTE:%s\n", database[e.item][NOTES]);
1556 if (database[e.item][URL])
1557 fprintf(out, "URL:%s\n", database[e.item][URL]);
1559 fprintf(out, "END:VCARD\n\n");
1567 * end of GnomeCard export filter
1572 * mutt alias export filter
1576 mutt_alias_genalias(int i)
1580 if(database[i][NICK])
1581 return xstrdup(database[i][NICK]);
1583 tmp = xstrdup(database[i][NAME]);
1585 if( ( pos = strchr(tmp, ' ') ) )
1594 mutt_alias_export(FILE *out, struct db_enumerator e)
1596 char email[MAX_EMAIL_LEN];
1599 db_enumerate_items(e) {
1600 alias = mutt_alias_genalias(e.item);
1602 get_first_email(email, e.item);
1603 fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n",
1605 database[e.item][NAME],
1614 * end of mutt alias export filter
1619 * printable export filter
1624 text_write_address_us(FILE *out, int i) {
1625 fprintf(out, "\n%s", database[i][ADDRESS]);
1627 if (database[i][ADDRESS2])
1628 fprintf(out, "\n%s", database[i][ADDRESS2]);
1630 if (database[i][CITY])
1631 fprintf(out, "\n%s", database[i][CITY]);
1633 if (database[i][STATE] || database[i][ZIP]) {
1636 if(database[i][STATE]) {
1637 fprintf(out, "%s", database[i][STATE]);
1638 if(database[i][ZIP])
1642 if(database[i][ZIP])
1643 fprintf(out, "%s", database[i][ZIP]);
1646 if (database[i][COUNTRY])
1647 fprintf(out, "\n%s", database[i][COUNTRY]);
1652 text_write_address_uk(FILE *out, int i) {
1655 for (j = ADDRESS; j <= COUNTRY; j++)
1657 fprintf(out, "\n%s", database[i][j]);
1661 text_write_address_eu(FILE *out, int i) {
1662 fprintf(out, "\n%s", database[i][ADDRESS]);
1664 if (database[i][ADDRESS2])
1665 fprintf(out, "\n%s", database[i][ADDRESS2]);
1667 if (database[i][ZIP] || database[i][CITY]) {
1670 if(database[i][ZIP]) {
1671 fprintf(out, "%s", database[i][ZIP]);
1672 if(database[i][CITY])
1676 if(database[i][CITY])
1677 fprintf(out, "%s", database[i][CITY]);
1680 if (database[i][STATE])
1681 fprintf(out, "\n%s", database[i][STATE]);
1683 if (database[i][COUNTRY])
1684 fprintf(out, "\n%s", database[i][COUNTRY]);
1688 text_export_database(FILE * out, struct db_enumerator e)
1690 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1692 char *realname = get_real_name();
1693 char *style = opt_get_str(STR_ADDRESS_STYLE);
1696 "-----------------------------------------\n%s's address book\n"
1697 "-----------------------------------------\n\n\n",
1701 db_enumerate_items(e) {
1703 "-----------------------------------------\n\n");
1704 fprintf(out, "%s", database[e.item][NAME]);
1705 if (database[e.item][NICK] && *database[e.item][NICK])
1706 fprintf(out, "\n(%s)", database[e.item][NICK]);
1709 if (*database[e.item][EMAIL]) {
1711 split_emailstr(e.item, emails);
1712 for (j = 0; j < MAX_EMAILS; j++)
1714 fprintf(out, "%s\n", emails[j]);
1717 if (database[e.item][ADDRESS]) {
1718 if (!safe_strcmp(style, "us")) /* US like */
1719 text_write_address_us(out, e.item);
1720 else if (!safe_strcmp(style, "uk")) /* UK like */
1721 text_write_address_uk(out, e.item);
1723 text_write_address_eu(out, e.item);
1728 if ((database[e.item][PHONE]) ||
1729 (database[e.item][WORKPHONE]) ||
1730 (database[e.item][FAX]) ||
1731 (database[e.item][MOBILEPHONE])) {
1733 for (j = PHONE; j <= MOBILEPHONE; j++)
1734 if (database[e.item][j])
1735 fprintf(out, "%s: %s\n",
1736 abook_fields[j].name,
1737 database[e.item][j]);
1740 if (database[e.item][URL])
1741 fprintf(out, "\n%s\n", database[e.item][URL]);
1742 if (database[e.item][NOTES])
1743 fprintf(out, "\n%s\n", database[e.item][NOTES]);
1748 fprintf(out, "-----------------------------------------\n");
1754 * end of printable export filter
1758 * elm alias export filter
1762 elm_alias_export(FILE *out, struct db_enumerator e)
1764 char email[MAX_EMAIL_LEN];
1767 db_enumerate_items(e) {
1768 alias = mutt_alias_genalias(e.item);
1769 get_first_email(email, e.item);
1770 fprintf(out, "%s = %s = %s\n",
1772 database[e.item][NAME],
1781 * end of elm alias export filter
1786 * Spruce export filter
1790 spruce_export_database (FILE *out, struct db_enumerator e)
1792 char email[MAX_EMAIL_LEN];
1794 fprintf (out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
1796 db_enumerate_items(e) {
1797 if(strcmp (safe_str(database[e.item][EMAIL]), "")) {
1798 get_first_email(email, e.item);
1799 fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
1801 database[e.item][NAME],
1803 safe_str(database[e.item][NOTES])
1808 fprintf (out, "# End of address book file.\n");
1814 * end of Spruce export filter