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 = strdup(pwent->pw_gecos)) == NULL)
149 return strdup(username);
151 rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
152 if (rtn == EOF || rtn == 0) {
154 return strdup(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 ldif_item)
524 list_item abook_item;
527 memset(abook_item, 0, sizeof(abook_item));
529 if( !ldif_item[LDIF_ITEM_FIELDS -1] )
533 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
534 if(ldif_conv_table[i] >= 0 && ldif_item[i] && *ldif_item[i] )
535 abook_item[ldif_conv_table[i]] = strdup(ldif_item[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] = strdup(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 /* includes also the trailing zero */
650 alias_len = (size_t)(ptr - tmp + 1);
652 if( (*alias = xmalloc(alias_len)) == NULL) {
657 strncpy(*alias, tmp, alias_len - 1);
658 *(*alias + alias_len - 1) = 0;
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]] = strdup(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 '#'
1051 static int csv_conv_table[] = {
1059 static int allcsv_conv_table[] = {
1083 csv_convert_emails(char *s)
1091 for(i=1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1 )
1092 if( i > MAX_EMAILS - 1 ) {
1100 csv_remove_quotes(char *s)
1102 char *copy, *trimmed;
1105 copy = trimmed = strdup(s);
1108 len = strlen(trimmed);
1109 if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
1114 trimmed[len - 1] = 0;
1116 trimmed = strdup(trimmed);
1126 csv_store_field(list_item item, char *s, int field)
1128 char *newstr = NULL;
1133 if( !(newstr = csv_remove_quotes(s)) )
1136 if(field < (int)(sizeof(csv_conv_table) / sizeof(*csv_conv_table))
1137 && csv_conv_table[field] >= 0) {
1138 item[csv_conv_table[field]] = newstr;
1145 allcsv_store_field(list_item item, char *s, int field)
1147 char *newstr = NULL;
1152 if( !(newstr = csv_remove_quotes(s)) )
1155 if(field < (int)(sizeof(allcsv_conv_table) / sizeof(*allcsv_conv_table))
1156 && allcsv_conv_table[field] >= 0) {
1157 item[allcsv_conv_table[field]] = newstr;
1164 csv_is_valid_quote_end(char *p)
1172 else if(!ISSPACE(*p))
1180 csv_is_valid_quote_start(char *p)
1185 else if(!ISSPACE(*p))
1193 csv_parse_line(char *line)
1197 bool in_quote = FALSE;
1200 memset(item, 0, sizeof(item));
1202 for(p = start = line, field = 0; *p; p++) {
1204 if(csv_is_valid_quote_end(p))
1207 if ( (((p - start) / sizeof (char)) < 2 ) &&
1208 csv_is_valid_quote_start(p) )
1212 if( *p == ',' && !in_quote) {
1214 csv_store_field(item, start, field);
1222 csv_store_field(item, start, field);
1224 csv_convert_emails(item[EMAIL]);
1225 add_item2database(item);
1229 allcsv_parse_line(char *line)
1233 bool in_quote = FALSE;
1236 memset(item, 0, sizeof(item));
1238 for(p = start = line, field = 0; *p; p++) {
1240 if(csv_is_valid_quote_end(p))
1243 if ( (((p - start) / sizeof (char)) < 2 ) &&
1244 csv_is_valid_quote_start(p) )
1248 if( *p == ',' && !in_quote) {
1250 allcsv_store_field(item, start, field);
1258 allcsv_store_field(item, start, field);
1260 csv_convert_emails(item[EMAIL]);
1261 add_item2database(item);
1266 csv_parse_file(FILE *in)
1271 line = getaline(in);
1273 if(line && *line && *line != CSV_COMMENT_CHAR)
1274 csv_parse_line(line);
1283 allcsv_parse_file(FILE *in)
1288 line = getaline(in);
1290 if(line && *line && *line != CSV_COMMENT_CHAR)
1291 allcsv_parse_line(line);
1300 * end of csv import filter
1304 * csv addressbook export filters
1307 #define CSV_LAST (-1)
1308 #define CSV_UNDEFINED (-2)
1309 #define CSV_SPECIAL(X) (-3 - (X))
1310 #define CSV_IS_SPECIAL(X) ((X) <= -3)
1313 csv_export_common(FILE *out, struct db_enumerator e,
1314 int fields[], void (*special_func)(FILE *, int, int))
1318 db_enumerate_items(e) {
1319 for(i = 0; fields[i] != CSV_LAST; i++) {
1320 if(fields[i] == CSV_UNDEFINED)
1321 fprintf(out, "\"\"");
1322 else if(CSV_IS_SPECIAL(fields[i])) {
1324 (*special_func)(out, e.item, fields[i]);
1327 strchr(safe_str(database[e.item][fields[i]]), ',') ||
1328 strchr(safe_str(database[e.item][fields[i]]), '\"')) ?
1330 safe_str(database[e.item][fields[i]])
1332 fprintf(out, "\"%s\"",
1333 safe_str(database[e.item][fields[i]]));
1335 if(fields[i + 1] != CSV_LAST)
1345 csv_export_database(FILE *out, struct db_enumerator e)
1347 int csv_export_fields[] = {
1356 csv_export_common(out, e, csv_export_fields, NULL);
1362 allcsv_export_database(FILE *out, struct db_enumerator e)
1365 * TODO: Should get these atomatically from abook_fileds
1368 int allcsv_export_fields[] = {
1393 fprintf(out, "\"NAME\",");
1394 fprintf(out, "\"EMAIL\",");
1395 fprintf(out, "\"ADDRESS\",");
1396 fprintf(out, "\"ADDRESS2\",");
1397 fprintf(out, "\"CITY\",");
1398 fprintf(out, "\"STATE\",");
1399 fprintf(out, "\"ZIP\",");
1400 fprintf(out, "\"COUNTRY\",");
1401 fprintf(out, "\"PHONE\",");
1402 fprintf(out, "\"WORKPHONE\",");
1403 fprintf(out, "\"FAX\",");
1404 fprintf(out, "\"MOBILEPHONE\",");
1405 fprintf(out, "\"NICK\",");
1406 fprintf(out, "\"URL\",");
1407 fprintf(out, "\"NOTES\",");
1408 fprintf(out, "\"CUSTOM1\",");
1409 fprintf(out, "\"CUSTOM2\",");
1410 fprintf(out, "\"CUSTOM3\",");
1411 fprintf(out, "\"CUSTOM4\",");
1412 fprintf(out, "\"CUSTOM5\"\n");
1414 csv_export_common(out, e, allcsv_export_fields, NULL);
1423 #define PALM_CSV_NAME CSV_SPECIAL(0)
1424 #define PALM_CSV_END CSV_SPECIAL(1)
1425 #define PALM_CSV_CAT CSV_SPECIAL(2)
1428 palm_split_and_write_name(FILE *out, char *name)
1434 if ( (p = strchr(name, ' ')) ) {
1438 fprintf(out, "\"%s\",\"" , p + 1);
1439 fwrite((void *)name, p - name, sizeof(char), out);
1442 fprintf(out, "\"%s\"", safe_str(name));
1447 palm_csv_handle_specials(FILE *out, int item, int field)
1451 palm_split_and_write_name(out, database[item][NAME]);
1454 fprintf(out, "\"abook\"");
1457 fprintf(out, "\"0\"");
1465 palm_export_database(FILE *out, struct db_enumerator e)
1467 int palm_export_fields[] = {
1468 PALM_CSV_NAME, /* LASTNAME, FIRSTNAME */
1469 CSV_UNDEFINED, /* TITLE */
1470 CSV_UNDEFINED, /* COMPANY */
1471 WORKPHONE, /* WORK PHONE */
1472 PHONE, /* HOME PHONE */
1474 MOBILEPHONE, /* OTHER */
1476 ADDRESS, /* ADDRESS */
1480 COUNTRY, /* COUNTRY */
1481 NICK, /* DEFINED 1 */
1482 URL, /* DEFINED 2 */
1483 CSV_UNDEFINED, /* DEFINED 3 */
1484 CSV_UNDEFINED, /* DEFINED 4 */
1486 PALM_CSV_END, /* "0" */
1487 PALM_CSV_CAT, /* CATEGORY */
1491 csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
1497 * end of csv export filters
1501 * GnomeCard (VCard) addressbook export filter
1505 gcrd_export_database(FILE *out, struct db_enumerator e)
1507 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1511 db_enumerate_items(e) {
1512 fprintf(out, "BEGIN:VCARD\nFN:%s\n",
1513 safe_str(database[e.item][NAME]));
1515 name = get_surname(database[e.item][NAME]);
1516 for( j = strlen(database[e.item][NAME]) - 1; j >= 0; j-- ) {
1517 if(database[e.item][NAME][j] == ' ')
1520 fprintf(out, "N:%s;%.*s\n",
1523 safe_str(database[e.item][NAME])
1528 if ( database[e.item][ADDRESS] )
1529 fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\n",
1530 safe_str(database[e.item][ADDRESS]),
1531 safe_str(database[e.item][ADDRESS2]),
1532 safe_str(database[e.item][CITY]),
1533 safe_str(database[e.item][STATE]),
1534 safe_str(database[e.item][ZIP]),
1535 safe_str(database[e.item][COUNTRY])
1538 if (database[e.item][PHONE])
1539 fprintf(out, "TEL;HOME:%s\n", database[e.item][PHONE]);
1540 if (database[e.item][WORKPHONE])
1541 fprintf(out, "TEL;WORK:%s\n", database[e.item][WORKPHONE]);
1542 if (database[e.item][FAX])
1543 fprintf(out, "TEL;FAX:%s\n", database[e.item][FAX]);
1544 if (database[e.item][MOBILEPHONE])
1545 fprintf(out, "TEL;CELL:%s\n", database[e.item][MOBILEPHONE]);
1547 if ( database[e.item][EMAIL] ) {
1548 split_emailstr(e.item, emails);
1549 for(j=0; j < MAX_EMAILS ; j++) {
1551 fprintf(out, "EMAIL;INTERNET:%s\n",
1556 if ( database[e.item][NOTES] )
1557 fprintf(out, "NOTE:%s\n", database[e.item][NOTES]);
1558 if (database[e.item][URL])
1559 fprintf(out, "URL:%s\n", database[e.item][URL]);
1561 fprintf(out, "END:VCARD\n\n");
1569 * end of GnomeCard export filter
1574 * mutt alias export filter
1578 mutt_alias_genalias(int i)
1582 if(database[i][NICK])
1583 return strdup(database[i][NICK]);
1585 tmp = strdup(database[i][NAME]);
1587 if( ( pos = strchr(tmp, ' ') ) )
1596 mutt_alias_export(FILE *out, struct db_enumerator e)
1598 char email[MAX_EMAIL_LEN];
1601 db_enumerate_items(e) {
1602 alias = mutt_alias_genalias(e.item);
1604 get_first_email(email, e.item);
1605 fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n",
1607 database[e.item][NAME],
1616 * end of mutt alias export filter
1621 * printable export filter
1626 text_write_address_us(FILE *out, int i) {
1627 fprintf(out, "\n%s", database[i][ADDRESS]);
1629 if (database[i][ADDRESS2])
1630 fprintf(out, "\n%s", database[i][ADDRESS2]);
1632 if (database[i][CITY])
1633 fprintf(out, "\n%s", database[i][CITY]);
1635 if (database[i][STATE] || database[i][ZIP]) {
1638 if(database[i][STATE]) {
1639 fprintf(out, "%s", database[i][STATE]);
1640 if(database[i][ZIP])
1644 if(database[i][ZIP])
1645 fprintf(out, "%s", database[i][ZIP]);
1648 if (database[i][COUNTRY])
1649 fprintf(out, "\n%s", database[i][COUNTRY]);
1654 text_write_address_uk(FILE *out, int i) {
1657 for (j = ADDRESS; j <= COUNTRY; j++)
1659 fprintf(out, "\n%s", database[i][j]);
1663 text_write_address_eu(FILE *out, int i) {
1664 fprintf(out, "\n%s", database[i][ADDRESS]);
1666 if (database[i][ADDRESS2])
1667 fprintf(out, "\n%s", database[i][ADDRESS2]);
1669 if (database[i][ZIP] || database[i][CITY]) {
1672 if(database[i][ZIP]) {
1673 fprintf(out, "%s", database[i][ZIP]);
1674 if(database[i][CITY])
1678 if(database[i][CITY])
1679 fprintf(out, "%s", database[i][CITY]);
1682 if (database[i][STATE])
1683 fprintf(out, "\n%s", database[i][STATE]);
1685 if (database[i][COUNTRY])
1686 fprintf(out, "\n%s", database[i][COUNTRY]);
1690 text_export_database(FILE * out, struct db_enumerator e)
1692 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1694 char *realname = get_real_name();
1695 char *style = opt_get_str(STR_ADDRESS_STYLE);
1698 "-----------------------------------------\n%s's address book\n"
1699 "-----------------------------------------\n\n\n",
1703 db_enumerate_items(e) {
1705 "-----------------------------------------\n\n");
1706 fprintf(out, "%s", database[e.item][NAME]);
1707 if (database[e.item][NICK] && *database[e.item][NICK])
1708 fprintf(out, "\n(%s)", database[e.item][NICK]);
1711 if (*database[e.item][EMAIL]) {
1713 split_emailstr(e.item, emails);
1714 for (j = 0; j < MAX_EMAILS; j++)
1716 fprintf(out, "%s\n", emails[j]);
1719 if (database[e.item][ADDRESS]) {
1720 if (!safe_strcmp(style, "us")) /* US like */
1721 text_write_address_us(out, e.item);
1722 else if (!safe_strcmp(style, "uk")) /* UK like */
1723 text_write_address_uk(out, e.item);
1725 text_write_address_eu(out, e.item);
1730 if ((database[e.item][PHONE]) ||
1731 (database[e.item][WORKPHONE]) ||
1732 (database[e.item][FAX]) ||
1733 (database[e.item][MOBILEPHONE])) {
1735 for (j = PHONE; j <= MOBILEPHONE; j++)
1736 if (database[e.item][j])
1737 fprintf(out, "%s: %s\n",
1738 abook_fields[j].name,
1739 database[e.item][j]);
1742 if (database[e.item][URL])
1743 fprintf(out, "\n%s\n", database[e.item][URL]);
1744 if (database[e.item][NOTES])
1745 fprintf(out, "\n%s\n", database[e.item][NOTES]);
1750 fprintf(out, "-----------------------------------------\n");
1756 * end of printable export filter
1760 * elm alias export filter
1764 elm_alias_export(FILE *out, struct db_enumerator e)
1766 char email[MAX_EMAIL_LEN];
1769 db_enumerate_items(e) {
1770 alias = mutt_alias_genalias(e.item);
1771 get_first_email(email, e.item);
1772 fprintf(out, "%s = %s = %s\n",
1774 database[e.item][NAME],
1783 * end of elm alias export filter
1788 * Spruce export filter
1792 spruce_export_database (FILE *out, struct db_enumerator e)
1794 char email[MAX_EMAIL_LEN];
1796 fprintf (out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
1798 db_enumerate_items(e) {
1799 if(strcmp (safe_str(database[e.item][EMAIL]), "")) {
1800 get_first_email(email, e.item);
1801 fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
1803 database[e.item][NAME],
1805 safe_str(database[e.item][NOTES])
1810 fprintf (out, "# End of address book file.\n");
1816 * end of Spruce export filter