5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
16 #include <sys/types.h>
17 #include "abook_curses.h"
30 extern list_item *database;
31 extern struct abook_field abook_fields[];
34 * function declarations
38 * import filter prototypes
41 static int ldif_parse_file(FILE *handle);
42 static int mutt_parse_file(FILE *in);
43 static int pine_parse_file(FILE *in);
44 static int csv_parse_file(FILE *in);
45 static int allcsv_parse_file(FILE *in);
46 static int palmcsv_parse_file(FILE *in);
49 * export filter prototypes
52 static int ldif_export_database(FILE *out, struct db_enumerator e);
53 static int html_export_database(FILE *out, struct db_enumerator e);
54 static int pine_export_database(FILE *out, struct db_enumerator e);
55 static int csv_export_database(FILE *out, struct db_enumerator e);
56 static int allcsv_export_database(FILE *out, struct db_enumerator e);
57 static int palm_export_database(FILE *out, struct db_enumerator e);
58 static int gcrd_export_database(FILE *out, struct db_enumerator e);
59 static int mutt_alias_export(FILE *out, struct db_enumerator e);
60 static int elm_alias_export(FILE *out, struct db_enumerator e);
61 static int text_export_database(FILE *out, struct db_enumerator e);
62 static int spruce_export_database(FILE *out, struct db_enumerator e);
65 * end of function declarations
68 struct abook_input_filter i_filters[] = {
69 { "abook", N_("abook native format"), parse_database },
70 { "ldif", N_("ldif / Netscape addressbook"), ldif_parse_file },
71 { "mutt", N_("mutt alias"), mutt_parse_file },
72 { "pine", N_("pine addressbook"), pine_parse_file },
73 { "csv", N_("comma separated values"), csv_parse_file },
74 { "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file },
75 { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file },
79 struct abook_output_filter e_filters[] = {
80 { "abook", N_("abook native format"), write_database },
81 { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database },
82 { "mutt", N_("mutt alias"), mutt_alias_export },
83 { "html", N_("html document"), html_export_database },
84 { "pine", N_("pine addressbook"), pine_export_database },
85 { "gcrd", N_("GnomeCard (VCard) addressbook"), gcrd_export_database },
86 { "csv", N_("comma separated values"), csv_export_database },
87 { "allcsv", N_("comma separated values (all fields)"), allcsv_export_database },
88 { "palmcsv", N_("Palm comma separated values"), palm_export_database},
89 { "elm", N_("elm alias"), elm_alias_export },
90 { "text", N_("plain text"), text_export_database },
91 { "spruce", N_("Spruce address book"), spruce_export_database },
105 for(i=0; *i_filters[i].filtname ; i++)
106 printf("\t%s\t%s\n", i_filters[i].filtname,
107 gettext(i_filters[i].desc));
112 for(i=0; *e_filters[i].filtname ; i++)
113 printf("\t%s\t%s\n", e_filters[i].filtname,
114 gettext(e_filters[i].desc));
120 number_of_output_filters()
124 for(i=0; *e_filters[i].filtname ; i++)
131 number_of_input_filters()
135 for(i=0; *i_filters[i].filtname ; i++)
144 char *username = getenv("USER");
145 struct passwd *pwent;
149 pwent = getpwnam(username);
151 if((tmp = xstrdup(pwent->pw_gecos)) == NULL)
152 return xstrdup(username);
154 rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
155 if (rtn == EOF || rtn == 0) {
157 return xstrdup(username);
166 static int i_read_file(char *filename, int (*func) (FILE *in));
175 refresh_statusline();
176 headerline(_("import database"));
178 mvaddstr(3, 1, _("please select a filter"));
181 for(i=0; *i_filters[i].filtname ; i++)
182 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
183 i_filters[i].filtname,
184 gettext(i_filters[i].desc));
186 mvprintw(6 + i, 6, _("x -\tcancel"));
198 filter = getch() - 'a';
199 if(filter == 'x' - 'a' ||
200 filter >= number_of_input_filters() || filter < 0) {
205 mvaddstr(5+filter, 2, "->");
207 filename = ask_filename(_("Filename: "));
213 if(i_read_file(filename, i_filters[filter].func ))
214 statusline_msg(_("Error occured while opening the file"));
215 else if(tmp == items)
216 statusline_msg(_("File does not seem to be a valid addressbook"));
227 i_read_file(char *filename, int (*func) (FILE *in))
232 if( (in = abook_fopen( filename, "r" )) == NULL )
243 import_file(char filtname[FILTNAME_LEN], char *filename)
250 if(! strncasecmp(i_filters[i].filtname, filtname,
253 if(! *i_filters[i].filtname) {
262 if(!strcmp(filename, "-")) {
264 if((fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
267 ret = (*i_filters[i].func) (stdin);
269 ret = i_read_file(filename, i_filters[i].func);
281 static int e_write_file(char *filename,
282 int (*func) (FILE *in, struct db_enumerator e), int mode);
292 refresh_statusline();
293 headerline(_("export database"));
295 mvaddstr(3, 1, _("please select a filter"));
298 for(i = 0; *e_filters[i].filtname ; i++)
299 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
300 e_filters[i].filtname,
301 gettext(e_filters[i].desc));
303 mvprintw(6 + i, 6, _("x -\tcancel"));
310 int enum_mode = ENUM_ALL;
315 filter = getch() - 'a';
316 if(filter == 'x' - 'a' ||
317 filter >= number_of_output_filters() || filter < 0) {
322 mvaddstr(5+filter, 2, "->");
324 if(selected_items()) {
325 /* TODO gettext: handle translated keypresses? */
326 statusline_addstr(_("Export All/Selected/Cancel (A/s/c)?"));
327 switch( tolower(getch()) ) {
329 enum_mode = ENUM_SELECTED;
338 filename = ask_filename(_("Filename: "));
344 if( e_write_file(filename, e_filters[filter].func, enum_mode))
345 statusline_msg(_("Error occured while exporting"));
354 e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e),
359 struct db_enumerator enumerator = init_db_enumerator(mode);
361 if((out = fopen(filename, "a")) == NULL)
367 ret = (*func) (out, enumerator);
375 fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
378 struct db_enumerator e = init_db_enumerator(enum_mode);
381 if(!strncasecmp(e_filters[i].filtname, filtname,
384 if(!*e_filters[i].filtname) {
390 return (e_filters[i].func) (handle, e);
396 export_file(char filtname[FILTNAME_LEN], char *filename)
398 const int mode = ENUM_ALL;
401 struct db_enumerator e = init_db_enumerator(mode);
404 if(!strncasecmp(e_filters[i].filtname, filtname,
407 if(!*e_filters[i].filtname) {
416 if(!strcmp(filename, "-"))
417 ret = (e_filters[i].func) (stdout, e);
419 ret = e_write_file(filename, e_filters[i].func, mode);
425 * end of common functions
434 static void ldif_fix_string(char *str);
436 #define LDIF_ITEM_FIELDS 16
438 typedef char *ldif_item[LDIF_ITEM_FIELDS];
440 static ldif_item ldif_field_names = {
452 "facsimiletelephonenumber",
456 "objectclass", /* this must be the last entry */
459 static int ldif_conv_table[LDIF_ITEM_FIELDS] = {
462 ADDRESS, /* "streetaddress" */
463 ADDRESS2, /* "streetaddress2" */
464 CITY, /* "locality" */
466 ZIP, /* "postalcode" */
467 COUNTRY, /* "countryname" */
468 PHONE, /* "homephone" */
469 NOTES, /* "description" */
471 FAX, /* "facsimiletelephonenumber" */
472 MOBILEPHONE, /* "cellphone" */
473 WORKPHONE, /* "xmozillaanyphone" */
474 NICK, /* "xmozillanickname" */
475 -1, /* "objectclass" */ /* this must be the last entry */
480 ldif_read_line(FILE *in)
493 if(feof(in) || !line)
502 fseek(in, pos, SEEK_SET); /* fixme ! */
512 buf = strconcat(buf, ptr, NULL);
517 if(buf && *buf == '#' ) {
526 ldif_add_item(ldif_item li)
528 list_item abook_item;
531 memset(abook_item, 0, sizeof(abook_item));
533 if(!li[LDIF_ITEM_FIELDS -1])
537 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
538 if(ldif_conv_table[i] >= 0 && li[i] && *li[i])
539 abook_item[ldif_conv_table[i]] = xstrdup(li[i]);
542 add_item2database(abook_item);
545 for(i=0; i < LDIF_ITEM_FIELDS; i++)
551 ldif_convert(ldif_item item, char *type, char *value)
555 if(!strcmp(type, "dn")) {
560 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
561 if(!safe_strcmp(ldif_field_names[i], type) && *value) {
562 if(i == LDIF_ITEM_FIELDS - 1) /* this is a dirty hack */
563 if(safe_strcmp("person", value))
567 item[i] = xstrdup(value);
573 ldif_parse_file(FILE *handle)
580 memset(item, 0, sizeof(item));
583 if( !(line = ldif_read_line(handle)) )
586 if(-1 == (str_parse_line(line, &type, &value, &vlen))) {
588 continue; /* just skip the errors */
591 ldif_fix_string(value);
593 ldif_convert(item, type, value);
596 } while ( !feof(handle) );
598 ldif_convert(item, "dn", "");
604 ldif_fix_string(char *str)
608 for(i = 0, j = 0; j < (int)strlen(str); i++, j++)
609 str[i] = ( str[j] == (char)0xc3 ?
610 (char) str[++j] + (char) 0x40 :
621 * mutt alias import filter
627 mutt_read_line(FILE *in, char **alias, char **rest)
629 char *line, *ptr, *tmp;
632 if( !(line = ptr = getaline(in)) )
633 return 1; /* error / EOF */
635 while( ISSPACE(*ptr) )
638 if(strncmp("alias", ptr, 5)) {
645 while( ISSPACE(*ptr) )
650 while( ! ISSPACE(*ptr) )
653 alias_len = (size_t)(ptr - tmp);
655 *alias = xmalloc_inc(alias_len, 1);
657 strncpy(*alias, tmp, alias_len);
658 *(*alias + alias_len) = 0;
663 *rest = xstrdup(ptr);
670 mutt_fix_quoting(char *p)
690 mutt_parse_email(list_item item)
692 char *line = item[NAME];
700 mutt_fix_quoting(line);
701 tmp = strconcat("From: ", line, NULL);
702 getname(tmp, &name, &email);
715 * this is completely broken
718 while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
719 tmp = strconcat("From: ", ++start, NULL);
720 getname(tmp, &name, &email);
725 tmp = strconcat(item[EMAIL], ",", email, NULL);
737 mutt_parse_file(FILE *in)
742 memset(item, 0, sizeof(item));
744 if(!mutt_read_line(in, &item[NICK],
746 mutt_parse_email(item);
749 free_list_item(item);
753 add_item2database(item);
760 * end of mutt alias import filter
769 ldif_fput_type_and_value(FILE *out,char *type, char *value )
773 tmp = ldif_type_and_value(type, value, strlen(value));
781 ldif_export_database(FILE *out, struct db_enumerator e)
783 char email[MAX_EMAILSTR_LEN];
785 fprintf(out, "version: 1\n");
787 db_enumerate_items(e) {
790 get_first_email(email, e.item);
792 tmp = mkstr("cn=%s,mail=%s", database[e.item][NAME], email);
793 ldif_fput_type_and_value(out, "dn", tmp);
796 for(j = 0; j < LDIF_ITEM_FIELDS; j++) {
797 if(ldif_conv_table[j] >= 0) {
798 if(ldif_conv_table[j] == EMAIL)
799 ldif_fput_type_and_value(out,
800 ldif_field_names[j], email);
801 else if(database[e.item][ldif_conv_table[j]])
802 ldif_fput_type_and_value(out,
804 database[e.item][ldif_conv_table[j]]);
808 fprintf(out, "objectclass: top\n"
809 "objectclass: person\n\n");
816 * end of ldif export filter
823 static void html_export_write_head(FILE *out, int extra_column);
824 static void html_export_write_tail(FILE *out);
826 extern int extra_column;
829 html_export_database(FILE *out, struct db_enumerator e)
831 char tmp[MAX_EMAILSTR_LEN];
836 extra_column = (extra_column > 2 && extra_column < ITEM_FIELDS) ?
837 extra_column : PHONE;
839 html_export_write_head(out, extra_column);
841 db_enumerate_items(e) {
842 get_first_email(tmp, e.item);
844 fprintf(out, "<tr>\n<td><a href=\"mailto:%s\">%s</a>\n",
846 database[e.item][NAME] );
848 fprintf(out, "<tr>\n<td>%s\n",
849 database[e.item][NAME] );
851 fprintf(out, "<td>%s\n<td>%s\n",
852 database[e.item][EMAIL],
853 safe_str(database[e.item][extra_column]) );
854 fprintf(out, "</tr>\n\n");
857 html_export_write_tail(out);
864 html_export_write_head(FILE *out, int extra_column)
866 char *realname = get_real_name();
868 fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
869 fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>", realname );
870 fprintf(out, "\n</head>\n<body>\n");
871 fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
872 fprintf(out, "<br><br>\n\n");
874 fprintf(out, "<table border=\"1\" align=\"center\">\n");
875 fprintf(out, "\n<tr><th>Name<th>E-mail address(es)<th>%s</tr>\n\n",
876 abook_fields[extra_column].name);
882 html_export_write_tail(FILE *out)
884 fprintf(out, "\n</table>\n");
885 fprintf(out, "\n</body>\n</html>\n");
889 * end of html export filter
894 * pine addressbook import filter
897 #define PINE_BUF_SIZE 2048
900 pine_fixbuf(char *buf)
904 for(i=0,j=0; j < (int)strlen(buf); i++, j++)
905 buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
909 pine_convert_emails(char *s)
914 if(s == NULL || *s != '(')
920 if( ( tmp = strchr(s,')')) )
923 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
924 if(i > MAX_EMAILS - 1) {
932 pine_parse_buf(char *buf)
937 char tmp[PINE_BUF_SIZE];
939 int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
941 memset(&item, 0, sizeof(item));
943 for(i=0, last=0; !last ; i++) {
944 if( !(end = strchr(start, '\t')) )
947 len = last ? strlen(start) : (int) (end-start);
948 len = min(len, PINE_BUF_SIZE - 1);
950 if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
951 && pine_conv_table[i] >= 0) {
952 strncpy(tmp, start, len);
955 item[pine_conv_table[i]] = xstrdup(tmp);
960 pine_convert_emails(item[EMAIL]);
961 add_item2database(item);
965 #define LINESIZE 1024
968 pine_parse_file(FILE *in)
975 fgets(line, LINESIZE, in);
979 buf = xrealloc(buf, i*LINESIZE);
982 fgets(line, LINESIZE, in);
984 if(*ptr != ' ' || feof(in))
1007 * end of pine addressbook import filter
1012 * pine addressbook export filter
1014 * filter doesn't wrap the lines as it should but Pine seems to handle
1015 * created files without problems - JH
1019 pine_export_database(FILE *out, struct db_enumerator e)
1021 db_enumerate_items(e) {
1022 fprintf(out, have_multiple_emails(e.item) ?
1023 "%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n",
1024 safe_str(database[e.item][NICK]),
1025 safe_str(database[e.item][NAME]),
1026 safe_str(database[e.item][EMAIL]),
1027 safe_str(database[e.item][NOTES])
1035 * end of pine addressbook export filter
1044 * these files should be parsed according to a certain
1045 * lay out, or the default if layout is not given, at
1046 * the moment only default is done...
1049 #define CSV_COMMENT_CHAR '#'
1050 #define CSV_DUPLICATE_SEPARATOR " "
1051 #define CSV_TABLE_SIZE(t) (sizeof (t) / sizeof *(t))
1053 static int csv_conv_table[] = {
1061 static int allcsv_conv_table[] = {
1084 static int palmcsv_conv_table[] = {
1085 NAME, /* Last name */
1086 NAME, /* First name */
1107 csv_convert_emails(char *s)
1115 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
1116 if(i > MAX_EMAILS - 1) {
1124 csv_remove_quotes(char *s)
1126 char *copy, *trimmed;
1129 copy = trimmed = xstrdup(s);
1132 len = strlen(trimmed);
1133 if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
1138 trimmed[len - 1] = 0;
1140 trimmed = xstrdup(trimmed);
1150 csv_field_to_item(int *table_base, size_t table_size, int field)
1152 if(field < table_size)
1153 return table_base[field];
1159 csv_store_item(list_item item, int i, char *s)
1161 char *newstr = NULL;
1166 if( !(newstr = csv_remove_quotes(s)) )
1170 if (item[i] != NULL) {
1171 char *oldstr = item[i];
1173 item[i] = strconcat(newstr, CSV_DUPLICATE_SEPARATOR,
1186 csv_is_valid_quote_end(char *p)
1194 else if(!ISSPACE(*p))
1202 csv_is_valid_quote_start(char *p)
1207 else if(!ISSPACE(*p))
1215 csv_parse_line(char *line, int *table_base, size_t table_size)
1219 bool in_quote = FALSE;
1222 memset(item, 0, sizeof(item));
1224 for(p = start = line, field = 0; *p; p++) {
1226 if(csv_is_valid_quote_end(p))
1229 if ( (((p - start) / sizeof (char)) < 2 ) &&
1230 csv_is_valid_quote_start(p) )
1234 if(*p == ',' && !in_quote) {
1236 csv_field_to_item(table_base, table_size, field);
1237 csv_store_item(item,
1238 csv_field_to_item(table_base,
1248 csv_store_item(item, csv_field_to_item(table_base, table_size, field),
1251 csv_convert_emails(item[EMAIL]);
1252 add_item2database(item);
1256 csv_parse_file_common(FILE *in, int *conv_table, size_t table_size)
1261 line = getaline(in);
1263 if(line && *line && *line != CSV_COMMENT_CHAR)
1264 csv_parse_line(line, conv_table, table_size);
1273 csv_parse_file(FILE *in)
1275 return csv_parse_file_common(in, csv_conv_table,
1276 CSV_TABLE_SIZE(csv_conv_table));
1280 allcsv_parse_file(FILE *in)
1282 return csv_parse_file_common(in, allcsv_conv_table,
1283 CSV_TABLE_SIZE(allcsv_conv_table));
1287 palmcsv_parse_file(FILE *in)
1289 return csv_parse_file_common(in, palmcsv_conv_table,
1290 CSV_TABLE_SIZE(palmcsv_conv_table));
1294 * end of csv import filter
1298 * csv addressbook export filters
1301 #define CSV_LAST (-1)
1302 #define CSV_UNDEFINED (-2)
1303 #define CSV_SPECIAL(X) (-3 - (X))
1304 #define CSV_IS_SPECIAL(X) ((X) <= -3)
1307 csv_export_common(FILE *out, struct db_enumerator e,
1308 int fields[], void (*special_func)(FILE *, int, int))
1312 db_enumerate_items(e) {
1313 for(i = 0; fields[i] != CSV_LAST; i++) {
1314 if(fields[i] == CSV_UNDEFINED)
1315 fprintf(out, "\"\"");
1316 else if(CSV_IS_SPECIAL(fields[i])) {
1318 (*special_func)(out, e.item, fields[i]);
1321 strchr(safe_str(database[e.item][fields[i]]), ',') ||
1322 strchr(safe_str(database[e.item][fields[i]]), '\"')) ?
1324 safe_str(database[e.item][fields[i]])
1326 fprintf(out, "\"%s\"",
1327 safe_str(database[e.item][fields[i]]));
1329 if(fields[i + 1] != CSV_LAST)
1339 csv_export_database(FILE *out, struct db_enumerator e)
1341 int csv_export_fields[] = {
1350 csv_export_common(out, e, csv_export_fields, NULL);
1356 allcsv_export_database(FILE *out, struct db_enumerator e)
1359 * TODO: Should get these atomatically from abook_fileds
1362 int allcsv_export_fields[] = {
1387 fprintf(out, "\"NAME\",");
1388 fprintf(out, "\"EMAIL\",");
1389 fprintf(out, "\"ADDRESS\",");
1390 fprintf(out, "\"ADDRESS2\",");
1391 fprintf(out, "\"CITY\",");
1392 fprintf(out, "\"STATE\",");
1393 fprintf(out, "\"ZIP\",");
1394 fprintf(out, "\"COUNTRY\",");
1395 fprintf(out, "\"PHONE\",");
1396 fprintf(out, "\"WORKPHONE\",");
1397 fprintf(out, "\"FAX\",");
1398 fprintf(out, "\"MOBILEPHONE\",");
1399 fprintf(out, "\"NICK\",");
1400 fprintf(out, "\"URL\",");
1401 fprintf(out, "\"NOTES\",");
1402 fprintf(out, "\"CUSTOM1\",");
1403 fprintf(out, "\"CUSTOM2\",");
1404 fprintf(out, "\"CUSTOM3\",");
1405 fprintf(out, "\"CUSTOM4\",");
1406 fprintf(out, "\"CUSTOM5\"\n");
1408 csv_export_common(out, e, allcsv_export_fields, NULL);
1417 #define PALM_CSV_NAME CSV_SPECIAL(0)
1418 #define PALM_CSV_END CSV_SPECIAL(1)
1419 #define PALM_CSV_CAT CSV_SPECIAL(2)
1422 palm_split_and_write_name(FILE *out, char *name)
1428 if ( (p = strchr(name, ' ')) ) {
1432 fprintf(out, "\"%s\",\"" , p + 1);
1433 fwrite((void *)name, p - name, sizeof(char), out);
1436 fprintf(out, "\"%s\"", safe_str(name));
1441 palm_csv_handle_specials(FILE *out, int item, int field)
1445 palm_split_and_write_name(out, database[item][NAME]);
1448 fprintf(out, "\"abook\"");
1451 fprintf(out, "\"0\"");
1459 palm_export_database(FILE *out, struct db_enumerator e)
1461 int palm_export_fields[] = {
1462 PALM_CSV_NAME, /* LASTNAME, FIRSTNAME */
1463 CSV_UNDEFINED, /* TITLE */
1464 CSV_UNDEFINED, /* COMPANY */
1465 WORKPHONE, /* WORK PHONE */
1466 PHONE, /* HOME PHONE */
1468 MOBILEPHONE, /* OTHER */
1470 ADDRESS, /* ADDRESS */
1474 COUNTRY, /* COUNTRY */
1475 NICK, /* DEFINED 1 */
1476 URL, /* DEFINED 2 */
1477 CSV_UNDEFINED, /* DEFINED 3 */
1478 CSV_UNDEFINED, /* DEFINED 4 */
1480 PALM_CSV_END, /* "0" */
1481 PALM_CSV_CAT, /* CATEGORY */
1485 csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
1491 * end of csv export filters
1495 * GnomeCard (VCard) addressbook export filter
1499 gcrd_export_database(FILE *out, struct db_enumerator e)
1501 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1505 db_enumerate_items(e) {
1506 fprintf(out, "BEGIN:VCARD\nFN:%s\n",
1507 safe_str(database[e.item][NAME]));
1509 name = get_surname(database[e.item][NAME]);
1510 for( j = strlen(database[e.item][NAME]) - 1; j >= 0; j-- ) {
1511 if(database[e.item][NAME][j] == ' ')
1514 fprintf(out, "N:%s;%.*s\n",
1517 safe_str(database[e.item][NAME])
1522 if ( database[e.item][ADDRESS] )
1523 fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\n",
1524 safe_str(database[e.item][ADDRESS]),
1525 safe_str(database[e.item][ADDRESS2]),
1526 safe_str(database[e.item][CITY]),
1527 safe_str(database[e.item][STATE]),
1528 safe_str(database[e.item][ZIP]),
1529 safe_str(database[e.item][COUNTRY])
1532 if (database[e.item][PHONE])
1533 fprintf(out, "TEL;HOME:%s\n", database[e.item][PHONE]);
1534 if (database[e.item][WORKPHONE])
1535 fprintf(out, "TEL;WORK:%s\n", database[e.item][WORKPHONE]);
1536 if (database[e.item][FAX])
1537 fprintf(out, "TEL;FAX:%s\n", database[e.item][FAX]);
1538 if (database[e.item][MOBILEPHONE])
1539 fprintf(out, "TEL;CELL:%s\n", database[e.item][MOBILEPHONE]);
1541 if ( database[e.item][EMAIL] ) {
1542 split_emailstr(e.item, emails);
1543 for(j=0; j < MAX_EMAILS ; j++) {
1545 fprintf(out, "EMAIL;INTERNET:%s\n",
1550 if ( database[e.item][NOTES] )
1551 fprintf(out, "NOTE:%s\n", database[e.item][NOTES]);
1552 if (database[e.item][URL])
1553 fprintf(out, "URL:%s\n", database[e.item][URL]);
1555 fprintf(out, "END:VCARD\n\n");
1563 * end of GnomeCard export filter
1568 * mutt alias export filter
1572 mutt_alias_genalias(int i)
1576 if(database[i][NICK])
1577 return xstrdup(database[i][NICK]);
1579 tmp = xstrdup(database[i][NAME]);
1581 if( ( pos = strchr(tmp, ' ') ) )
1590 mutt_alias_export(FILE *out, struct db_enumerator e)
1592 char email[MAX_EMAIL_LEN];
1595 db_enumerate_items(e) {
1596 alias = mutt_alias_genalias(e.item);
1598 get_first_email(email, e.item);
1599 fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n",
1601 database[e.item][NAME],
1610 * end of mutt alias export filter
1615 * printable export filter
1620 text_write_address_us(FILE *out, int i) {
1621 fprintf(out, "\n%s", database[i][ADDRESS]);
1623 if (database[i][ADDRESS2])
1624 fprintf(out, "\n%s", database[i][ADDRESS2]);
1626 if (database[i][CITY])
1627 fprintf(out, "\n%s", database[i][CITY]);
1629 if (database[i][STATE] || database[i][ZIP]) {
1632 if(database[i][STATE]) {
1633 fprintf(out, "%s", database[i][STATE]);
1634 if(database[i][ZIP])
1638 if(database[i][ZIP])
1639 fprintf(out, "%s", database[i][ZIP]);
1642 if (database[i][COUNTRY])
1643 fprintf(out, "\n%s", database[i][COUNTRY]);
1648 text_write_address_uk(FILE *out, int i) {
1651 for (j = ADDRESS; j <= COUNTRY; j++)
1653 fprintf(out, "\n%s", database[i][j]);
1657 text_write_address_eu(FILE *out, int i) {
1658 fprintf(out, "\n%s", database[i][ADDRESS]);
1660 if (database[i][ADDRESS2])
1661 fprintf(out, "\n%s", database[i][ADDRESS2]);
1663 if (database[i][ZIP] || database[i][CITY]) {
1666 if(database[i][ZIP]) {
1667 fprintf(out, "%s", database[i][ZIP]);
1668 if(database[i][CITY])
1672 if(database[i][CITY])
1673 fprintf(out, "%s", database[i][CITY]);
1676 if (database[i][STATE])
1677 fprintf(out, "\n%s", database[i][STATE]);
1679 if (database[i][COUNTRY])
1680 fprintf(out, "\n%s", database[i][COUNTRY]);
1684 text_export_database(FILE * out, struct db_enumerator e)
1686 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1688 char *realname = get_real_name();
1689 char *style = opt_get_str(STR_ADDRESS_STYLE);
1692 "-----------------------------------------\n%s's address book\n"
1693 "-----------------------------------------\n\n\n",
1697 db_enumerate_items(e) {
1699 "-----------------------------------------\n\n");
1700 fprintf(out, "%s", database[e.item][NAME]);
1701 if (database[e.item][NICK] && *database[e.item][NICK])
1702 fprintf(out, "\n(%s)", database[e.item][NICK]);
1705 if (*database[e.item][EMAIL]) {
1707 split_emailstr(e.item, emails);
1708 for (j = 0; j < MAX_EMAILS; j++)
1710 fprintf(out, "%s\n", emails[j]);
1713 if (database[e.item][ADDRESS]) {
1714 if (!safe_strcmp(style, "us")) /* US like */
1715 text_write_address_us(out, e.item);
1716 else if (!safe_strcmp(style, "uk")) /* UK like */
1717 text_write_address_uk(out, e.item);
1719 text_write_address_eu(out, e.item);
1724 if ((database[e.item][PHONE]) ||
1725 (database[e.item][WORKPHONE]) ||
1726 (database[e.item][FAX]) ||
1727 (database[e.item][MOBILEPHONE])) {
1729 for (j = PHONE; j <= MOBILEPHONE; j++)
1730 if (database[e.item][j])
1731 fprintf(out, "%s: %s\n",
1732 abook_fields[j].name,
1733 database[e.item][j]);
1736 if (database[e.item][URL])
1737 fprintf(out, "\n%s\n", database[e.item][URL]);
1738 if (database[e.item][NOTES])
1739 fprintf(out, "\n%s\n", database[e.item][NOTES]);
1744 fprintf(out, "-----------------------------------------\n");
1750 * end of printable export filter
1754 * elm alias export filter
1758 elm_alias_export(FILE *out, struct db_enumerator e)
1760 char email[MAX_EMAIL_LEN];
1763 db_enumerate_items(e) {
1764 alias = mutt_alias_genalias(e.item);
1765 get_first_email(email, e.item);
1766 fprintf(out, "%s = %s = %s\n",
1768 database[e.item][NAME],
1777 * end of elm alias export filter
1782 * Spruce export filter
1786 spruce_export_database (FILE *out, struct db_enumerator e)
1788 char email[MAX_EMAIL_LEN];
1790 fprintf (out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
1792 db_enumerate_items(e) {
1793 if(strcmp (safe_str(database[e.item][EMAIL]), "")) {
1794 get_first_email(email, e.item);
1795 fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
1797 database[e.item][NAME],
1799 safe_str(database[e.item][NOTES])
1804 fprintf (out, "# End of address book file.\n");
1810 * end of Spruce export filter