3 * $Id: filter.c,v 1.48.2.1.2.2 2006/04/10 16:02:10 jheinonen Exp $
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);
64 static int wl_export_database(FILE *out, struct db_enumerator e);
67 * end of function declarations
70 struct abook_input_filter i_filters[] = {
71 { "abook", N_("abook native format"), parse_database },
72 { "ldif", N_("ldif / Netscape addressbook"), ldif_parse_file },
73 { "mutt", N_("mutt alias"), mutt_parse_file },
74 { "pine", N_("pine addressbook"), pine_parse_file },
75 { "csv", N_("comma separated values"), csv_parse_file },
76 { "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file },
77 { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file },
81 struct abook_output_filter e_filters[] = {
82 { "abook", N_("abook native format"), write_database },
83 { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database },
84 { "mutt", N_("mutt alias"), mutt_alias_export },
85 { "html", N_("html document"), html_export_database },
86 { "pine", N_("pine addressbook"), pine_export_database },
87 { "gcrd", N_("GnomeCard (VCard) addressbook"), gcrd_export_database },
88 { "csv", N_("comma separated values"), csv_export_database },
89 { "allcsv", N_("comma separated values (all fields)"), allcsv_export_database },
90 { "palmcsv", N_("Palm comma separated values"), palm_export_database},
91 { "elm", N_("elm alias"), elm_alias_export },
92 { "text", N_("plain text"), text_export_database },
93 { "wl", N_("Wanderlust address book"), wl_export_database },
94 { "spruce", N_("Spruce address book"), spruce_export_database },
108 for(i=0; *i_filters[i].filtname ; i++)
109 printf("\t%s\t%s\n", i_filters[i].filtname,
110 gettext(i_filters[i].desc));
115 for(i=0; *e_filters[i].filtname ; i++)
116 printf("\t%s\t%s\n", e_filters[i].filtname,
117 gettext(e_filters[i].desc));
123 number_of_output_filters()
127 for(i=0; *e_filters[i].filtname ; i++)
134 number_of_input_filters()
138 for(i=0; *i_filters[i].filtname ; i++)
147 char *username = getenv("USER");
148 struct passwd *pwent;
152 pwent = getpwnam(username);
154 if((tmp = xstrdup(pwent->pw_gecos)) == NULL)
155 return xstrdup(username);
157 rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
158 if (rtn == EOF || rtn == 0) {
160 return xstrdup(username);
169 static int i_read_file(char *filename, int (*func) (FILE *in));
178 refresh_statusline();
179 headerline(_("import database"));
181 mvaddstr(3, 1, _("please select a filter"));
184 for(i=0; *i_filters[i].filtname ; i++)
185 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
186 i_filters[i].filtname,
187 gettext(i_filters[i].desc));
189 mvprintw(6 + i, 6, _("x -\tcancel"));
201 filter = getch() - 'a';
202 if(filter == 'x' - 'a' ||
203 filter >= number_of_input_filters() || filter < 0) {
208 mvaddstr(5+filter, 2, "->");
210 filename = ask_filename(_("Filename: "));
216 if(i_read_file(filename, i_filters[filter].func ))
217 statusline_msg(_("Error occured while opening the file"));
218 else if(tmp == items)
219 statusline_msg(_("File does not seem to be a valid addressbook"));
230 i_read_file(char *filename, int (*func) (FILE *in))
235 if( (in = abook_fopen( filename, "r" )) == NULL )
246 import_file(char filtname[FILTNAME_LEN], char *filename)
253 if(! strncasecmp(i_filters[i].filtname, filtname,
256 if(! *i_filters[i].filtname) {
265 if(!strcmp(filename, "-")) {
267 if((fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
270 ret = (*i_filters[i].func) (stdin);
272 ret = i_read_file(filename, i_filters[i].func);
284 static int e_write_file(char *filename,
285 int (*func) (FILE *in, struct db_enumerator e), int mode);
295 refresh_statusline();
296 headerline(_("export database"));
298 mvaddstr(3, 1, _("please select a filter"));
301 for(i = 0; *e_filters[i].filtname ; i++)
302 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
303 e_filters[i].filtname,
304 gettext(e_filters[i].desc));
306 mvprintw(6 + i, 6, _("x -\tcancel"));
313 int enum_mode = ENUM_ALL;
318 filter = getch() - 'a';
319 if(filter == 'x' - 'a' ||
320 filter >= number_of_output_filters() || filter < 0) {
325 mvaddstr(5 + filter, 2, "->");
327 if(selected_items()) {
328 switch(statusline_askchoice(
329 _("Export <a>ll, export <s>elected, or <c>ancel?"),
330 S_("keybindings:all/selected/cancel|asc"), 3)) {
334 enum_mode = ENUM_SELECTED;
344 filename = ask_filename(_("Filename: "));
350 if( e_write_file(filename, e_filters[filter].func, enum_mode))
351 statusline_msg(_("Error occured while exporting"));
360 e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e),
365 struct db_enumerator enumerator = init_db_enumerator(mode);
367 if((out = fopen(filename, "a")) == NULL)
373 ret = (*func) (out, enumerator);
381 fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
384 struct db_enumerator e = init_db_enumerator(enum_mode);
387 if(!strncasecmp(e_filters[i].filtname, filtname,
390 if(!*e_filters[i].filtname) {
396 return (e_filters[i].func) (handle, e);
402 export_file(char filtname[FILTNAME_LEN], char *filename)
404 const int mode = ENUM_ALL;
407 struct db_enumerator e = init_db_enumerator(mode);
410 if(!strncasecmp(e_filters[i].filtname, filtname,
413 if(!*e_filters[i].filtname) {
422 if(!strcmp(filename, "-"))
423 ret = (e_filters[i].func) (stdout, e);
425 ret = e_write_file(filename, e_filters[i].func, mode);
431 * end of common functions
440 static void ldif_fix_string(char *str);
442 #define LDIF_ITEM_FIELDS 16
444 typedef char *ldif_item[LDIF_ITEM_FIELDS];
446 static ldif_item ldif_field_names = {
458 "facsimiletelephonenumber",
462 "objectclass", /* this must be the last entry */
465 static int ldif_conv_table[LDIF_ITEM_FIELDS] = {
468 ADDRESS, /* "streetaddress" */
469 ADDRESS2, /* "streetaddress2" */
470 CITY, /* "locality" */
472 ZIP, /* "postalcode" */
473 COUNTRY, /* "countryname" */
474 PHONE, /* "homephone" */
475 NOTES, /* "description" */
477 FAX, /* "facsimiletelephonenumber" */
478 MOBILEPHONE, /* "cellphone" */
479 WORKPHONE, /* "xmozillaanyphone" */
480 NICK, /* "xmozillanickname" */
481 -1, /* "objectclass" */ /* this must be the last entry */
486 ldif_read_line(FILE *in)
499 if(feof(in) || !line)
508 fseek(in, pos, SEEK_SET); /* fixme ! */
518 buf = strconcat(buf, ptr, NULL);
523 if(buf && *buf == '#' ) {
532 ldif_add_item(ldif_item li)
534 list_item abook_item;
537 memset(abook_item, 0, sizeof(abook_item));
539 if(!li[LDIF_ITEM_FIELDS -1])
543 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
544 if(ldif_conv_table[i] >= 0 && li[i] && *li[i])
545 abook_item[ldif_conv_table[i]] = xstrdup(li[i]);
548 add_item2database(abook_item);
551 for(i=0; i < LDIF_ITEM_FIELDS; i++)
557 ldif_convert(ldif_item item, char *type, char *value)
561 if(!strcmp(type, "dn")) {
566 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
567 if(!safe_strcmp(ldif_field_names[i], type) && *value) {
568 if(i == LDIF_ITEM_FIELDS - 1) /* this is a dirty hack */
569 if(safe_strcmp("person", value))
573 item[i] = xstrdup(value);
579 ldif_parse_file(FILE *handle)
586 memset(item, 0, sizeof(item));
589 if( !(line = ldif_read_line(handle)) )
592 if(-1 == (str_parse_line(line, &type, &value, &vlen))) {
594 continue; /* just skip the errors */
597 ldif_fix_string(value);
599 ldif_convert(item, type, value);
602 } while ( !feof(handle) );
604 ldif_convert(item, "dn", "");
610 ldif_fix_string(char *str)
614 for(i = 0, j = 0; j < (int)strlen(str); i++, j++)
615 str[i] = ( str[j] == (char)0xc3 ?
616 (char) str[++j] + (char) 0x40 :
627 * mutt alias import filter
633 mutt_read_line(FILE *in, char **alias, char **rest)
635 char *line, *ptr, *tmp;
638 if( !(line = ptr = getaline(in)) )
639 return 1; /* error / EOF */
641 while( ISSPACE(*ptr) )
644 if(strncmp("alias", ptr, 5)) {
651 while( ISSPACE(*ptr) )
656 while( ! ISSPACE(*ptr) )
659 alias_len = (size_t)(ptr - tmp);
661 *alias = xmalloc_inc(alias_len, 1);
663 strncpy(*alias, tmp, alias_len);
664 *(*alias + alias_len) = 0;
669 *rest = xstrdup(ptr);
676 mutt_fix_quoting(char *p)
696 mutt_parse_email(list_item item)
698 char *line = item[NAME];
706 mutt_fix_quoting(line);
707 tmp = strconcat("From: ", line, NULL);
708 getname(tmp, &name, &email);
721 * this is completely broken
724 while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
725 tmp = strconcat("From: ", ++start, NULL);
726 getname(tmp, &name, &email);
731 tmp = strconcat(item[EMAIL], ",", email, NULL);
743 mutt_parse_file(FILE *in)
748 memset(item, 0, sizeof(item));
750 if(!mutt_read_line(in, &item[NICK],
752 mutt_parse_email(item);
755 free_list_item(item);
759 add_item2database(item);
766 * end of mutt alias import filter
775 ldif_fput_type_and_value(FILE *out,char *type, char *value )
779 tmp = ldif_type_and_value(type, value, strlen(value));
787 ldif_export_database(FILE *out, struct db_enumerator e)
789 char email[MAX_EMAILSTR_LEN];
791 fprintf(out, "version: 1\n");
793 db_enumerate_items(e) {
796 get_first_email(email, e.item);
798 tmp = strdup_printf("cn=%s,mail=%s", database[e.item][NAME], email);
799 ldif_fput_type_and_value(out, "dn", tmp);
802 for(j = 0; j < LDIF_ITEM_FIELDS; j++) {
803 if(ldif_conv_table[j] >= 0) {
804 if(ldif_conv_table[j] == EMAIL)
805 ldif_fput_type_and_value(out,
806 ldif_field_names[j], email);
807 else if(database[e.item][ldif_conv_table[j]])
808 ldif_fput_type_and_value(out,
810 database[e.item][ldif_conv_table[j]]);
814 fprintf(out, "objectclass: top\n"
815 "objectclass: person\n\n");
822 * end of ldif export filter
829 static void html_export_write_head(FILE *out, int extra);
830 static void html_export_write_tail(FILE *out);
832 extern int extra_column;
835 html_export_database(FILE *out, struct db_enumerator e)
837 char tmp[MAX_EMAILSTR_LEN];
843 extra = (extra_column > 2 && extra_column < ITEM_FIELDS) ?
844 extra_column : PHONE;
846 html_export_write_head(out, extra);
848 db_enumerate_items(e) {
849 get_first_email(tmp, e.item);
851 fprintf(out, "<tr>\n<td><a href=\"mailto:%s\">%s</a>\n",
853 database[e.item][NAME] );
855 fprintf(out, "<tr>\n<td>%s\n",
856 database[e.item][NAME] );
858 fprintf(out, "<td>%s\n<td>%s\n",
859 database[e.item][EMAIL],
860 safe_str(database[e.item][extra]) );
861 fprintf(out, "</tr>\n\n");
864 html_export_write_tail(out);
871 html_export_write_head(FILE *out, int extra)
873 char *realname = get_real_name();
875 fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
876 fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>", realname );
877 fprintf(out, "\n</head>\n<body>\n");
878 fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
879 fprintf(out, "<br><br>\n\n");
881 fprintf(out, "<table border=\"1\" align=\"center\">\n");
882 fprintf(out, "\n<tr><th>Name<th>E-mail address(es)<th>%s</tr>\n\n",
883 abook_fields[extra].name);
889 html_export_write_tail(FILE *out)
891 fprintf(out, "\n</table>\n");
892 fprintf(out, "\n</body>\n</html>\n");
896 * end of html export filter
901 * pine addressbook import filter
904 #define PINE_BUF_SIZE 2048
907 pine_fixbuf(char *buf)
911 for(i = 0,j = 0; j < (int)strlen(buf); i++, j++)
912 buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
916 pine_convert_emails(char *s)
921 if(s == NULL || *s != '(')
924 for(i = 0; s[i]; i++)
927 if( ( tmp = strchr(s,')')) )
930 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
931 if(i > MAX_EMAILS - 1) {
938 pine_parse_buf(char *buf)
943 char tmp[PINE_BUF_SIZE];
945 int pine_conv_table[] = {NICK, NAME, EMAIL, -1, NOTES};
947 memset(&item, 0, sizeof(item));
949 for(i = 0, last=0; !last ; i++) {
950 if( !(end = strchr(start, '\t')) )
953 len = last ? strlen(start) : (int) (end-start);
954 len = min(len, PINE_BUF_SIZE - 1);
956 if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
957 && pine_conv_table[i] >= 0) {
958 strncpy(tmp, start, len);
961 item[pine_conv_table[i]] = xstrdup(tmp);
966 pine_convert_emails(item[EMAIL]);
967 add_item2database(item);
971 #define LINESIZE 1024
974 pine_parse_file(FILE *in)
981 fgets(line, LINESIZE, in);
985 buf = xrealloc(buf, i*LINESIZE);
988 fgets(line, LINESIZE, in);
990 if(*ptr != ' ' || feof(in))
1004 pine_parse_buf(buf);
1013 * end of pine addressbook import filter
1018 * pine addressbook export filter
1020 * filter doesn't wrap the lines as it should but Pine seems to handle
1021 * created files without problems - JH
1025 pine_export_database(FILE *out, struct db_enumerator e)
1027 db_enumerate_items(e) {
1028 fprintf(out, have_multiple_emails(e.item) ?
1029 "%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n",
1030 safe_str(database[e.item][NICK]),
1031 safe_str(database[e.item][NAME]),
1032 safe_str(database[e.item][EMAIL]),
1033 safe_str(database[e.item][NOTES])
1041 * end of pine addressbook export filter
1050 * these files should be parsed according to a certain
1051 * lay out, or the default if layout is not given, at
1052 * the moment only default is done...
1055 #define CSV_COMMENT_CHAR '#'
1056 #define CSV_DUPLICATE_SEPARATOR " "
1057 #define CSV_TABLE_SIZE(t) (sizeof (t) / sizeof *(t))
1059 static int csv_conv_table[] = {
1067 static int allcsv_conv_table[] = {
1090 static int palmcsv_conv_table[] = {
1091 NAME, /* Last name */
1092 NAME, /* First name */
1113 csv_convert_emails(char *s)
1121 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
1122 if(i > MAX_EMAILS - 1) {
1130 csv_remove_quotes(char *s)
1132 char *copy, *trimmed;
1135 copy = trimmed = xstrdup(s);
1138 len = strlen(trimmed);
1139 if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
1144 trimmed[len - 1] = 0;
1146 trimmed = xstrdup(trimmed);
1156 csv_field_to_item(int *table_base, size_t table_size, int field)
1158 if(field < table_size)
1159 return table_base[field];
1165 csv_store_item(list_item item, int i, char *s)
1167 char *newstr = NULL;
1172 if( !(newstr = csv_remove_quotes(s)) )
1176 if (item[i] != NULL) {
1177 char *oldstr = item[i];
1179 item[i] = strconcat(newstr, CSV_DUPLICATE_SEPARATOR,
1192 csv_is_valid_quote_end(char *p)
1200 else if(!ISSPACE(*p))
1208 csv_is_valid_quote_start(char *p)
1213 else if(!ISSPACE(*p))
1221 csv_parse_line(char *line, int *table_base, size_t table_size)
1225 bool in_quote = FALSE;
1228 memset(item, 0, sizeof(item));
1230 for(p = start = line, field = 0; *p; p++) {
1232 if(csv_is_valid_quote_end(p))
1235 if ( (((p - start) / sizeof (char)) < 2 ) &&
1236 csv_is_valid_quote_start(p) )
1240 if(*p == ',' && !in_quote) {
1242 csv_field_to_item(table_base, table_size, field);
1243 csv_store_item(item,
1244 csv_field_to_item(table_base,
1254 csv_store_item(item, csv_field_to_item(table_base, table_size, field),
1257 csv_convert_emails(item[EMAIL]);
1258 add_item2database(item);
1262 csv_parse_file_common(FILE *in, int *conv_table, size_t table_size)
1267 line = getaline(in);
1269 if(line && *line && *line != CSV_COMMENT_CHAR)
1270 csv_parse_line(line, conv_table, table_size);
1279 csv_parse_file(FILE *in)
1281 return csv_parse_file_common(in, csv_conv_table,
1282 CSV_TABLE_SIZE(csv_conv_table));
1286 allcsv_parse_file(FILE *in)
1288 return csv_parse_file_common(in, allcsv_conv_table,
1289 CSV_TABLE_SIZE(allcsv_conv_table));
1293 palmcsv_parse_file(FILE *in)
1295 return csv_parse_file_common(in, palmcsv_conv_table,
1296 CSV_TABLE_SIZE(palmcsv_conv_table));
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\r\nFN:%s\r\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\r\n",
1523 safe_str(database[e.item][NAME])
1528 if ( database[e.item][ADDRESS] )
1529 fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\r\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\r\n",
1540 database[e.item][PHONE]);
1541 if (database[e.item][WORKPHONE])
1542 fprintf(out, "TEL;WORK:%s\r\n",
1543 database[e.item][WORKPHONE]);
1545 if (database[e.item][FAX])
1546 fprintf(out, "TEL;FAX:%s\r\n", database[e.item][FAX]);
1547 if (database[e.item][MOBILEPHONE])
1548 fprintf(out, "TEL;CELL:%s\r\n", database[e.item][MOBILEPHONE]);
1550 if ( database[e.item][EMAIL] ) {
1551 split_emailstr(e.item, emails);
1552 for(j=0; j < MAX_EMAILS ; j++) {
1554 fprintf(out, "EMAIL;INTERNET:%s\r\n",
1559 if ( database[e.item][NOTES] )
1560 fprintf(out, "NOTE:%s\r\n", database[e.item][NOTES]);
1561 if (database[e.item][URL])
1562 fprintf(out, "URL:%s\r\n", database[e.item][URL]);
1564 fprintf(out, "END:VCARD\r\n\r\n");
1572 * end of GnomeCard export filter
1577 * mutt alias export filter
1581 mutt_alias_genalias(int i)
1585 if(database[i][NICK])
1586 return xstrdup(database[i][NICK]);
1588 tmp = xstrdup(database[i][NAME]);
1590 if( ( pos = strchr(tmp, ' ') ) )
1599 mutt_alias_export(FILE *out, struct db_enumerator e)
1601 char email[MAX_EMAIL_LEN];
1604 db_enumerate_items(e) {
1605 alias = mutt_alias_genalias(e.item);
1607 get_first_email(email, e.item);
1608 fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n",
1610 database[e.item][NAME],
1619 * end of mutt alias export filter
1624 * printable export filter
1629 text_write_address_us(FILE *out, int i) {
1630 fprintf(out, "\n%s", database[i][ADDRESS]);
1632 if (database[i][ADDRESS2])
1633 fprintf(out, "\n%s", database[i][ADDRESS2]);
1635 if (database[i][CITY])
1636 fprintf(out, "\n%s", database[i][CITY]);
1638 if (database[i][STATE] || database[i][ZIP]) {
1641 if(database[i][STATE]) {
1642 fprintf(out, "%s", database[i][STATE]);
1643 if(database[i][ZIP])
1647 if(database[i][ZIP])
1648 fprintf(out, "%s", database[i][ZIP]);
1651 if (database[i][COUNTRY])
1652 fprintf(out, "\n%s", database[i][COUNTRY]);
1657 text_write_address_uk(FILE *out, int i) {
1660 for (j = ADDRESS; j <= COUNTRY; j++)
1662 fprintf(out, "\n%s", database[i][j]);
1666 text_write_address_eu(FILE *out, int i) {
1667 fprintf(out, "\n%s", database[i][ADDRESS]);
1669 if (database[i][ADDRESS2])
1670 fprintf(out, "\n%s", database[i][ADDRESS2]);
1672 if (database[i][ZIP] || database[i][CITY]) {
1675 if(database[i][ZIP]) {
1676 fprintf(out, "%s", database[i][ZIP]);
1677 if(database[i][CITY])
1681 if(database[i][CITY])
1682 fprintf(out, "%s", database[i][CITY]);
1685 if (database[i][STATE])
1686 fprintf(out, "\n%s", database[i][STATE]);
1688 if (database[i][COUNTRY])
1689 fprintf(out, "\n%s", database[i][COUNTRY]);
1693 text_export_database(FILE * out, struct db_enumerator e)
1695 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1697 char *realname = get_real_name();
1698 char *style = opt_get_str(STR_ADDRESS_STYLE);
1701 "-----------------------------------------\n%s's address book\n"
1702 "-----------------------------------------\n\n\n",
1706 db_enumerate_items(e) {
1708 "-----------------------------------------\n\n");
1709 fprintf(out, "%s", database[e.item][NAME]);
1710 if (database[e.item][NICK] && *database[e.item][NICK])
1711 fprintf(out, "\n(%s)", database[e.item][NICK]);
1714 if (*database[e.item][EMAIL]) {
1716 split_emailstr(e.item, emails);
1717 for (j = 0; j < MAX_EMAILS; j++)
1719 fprintf(out, "%s\n", emails[j]);
1722 if (database[e.item][ADDRESS]) {
1723 if (!safe_strcmp(style, "us")) /* US like */
1724 text_write_address_us(out, e.item);
1725 else if (!safe_strcmp(style, "uk")) /* UK like */
1726 text_write_address_uk(out, e.item);
1728 text_write_address_eu(out, e.item);
1733 if ((database[e.item][PHONE]) ||
1734 (database[e.item][WORKPHONE]) ||
1735 (database[e.item][FAX]) ||
1736 (database[e.item][MOBILEPHONE])) {
1738 for (j = PHONE; j <= MOBILEPHONE; j++)
1739 if (database[e.item][j])
1740 fprintf(out, "%s: %s\n",
1741 abook_fields[j].name,
1742 database[e.item][j]);
1745 if (database[e.item][URL])
1746 fprintf(out, "\n%s\n", database[e.item][URL]);
1747 if (database[e.item][NOTES])
1748 fprintf(out, "\n%s\n", database[e.item][NOTES]);
1753 fprintf(out, "-----------------------------------------\n");
1759 * end of printable export filter
1763 * elm alias export filter
1767 elm_alias_export(FILE *out, struct db_enumerator e)
1769 char email[MAX_EMAIL_LEN];
1772 db_enumerate_items(e) {
1773 alias = mutt_alias_genalias(e.item);
1774 get_first_email(email, e.item);
1775 fprintf(out, "%s = %s = %s\n",
1777 database[e.item][NAME],
1786 * end of elm alias export filter
1791 * Spruce export filter
1795 spruce_export_database (FILE *out, struct db_enumerator e)
1797 char email[MAX_EMAIL_LEN];
1799 fprintf (out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
1801 db_enumerate_items(e) {
1802 if(strcmp (safe_str(database[e.item][EMAIL]), "")) {
1803 get_first_email(email, e.item);
1804 fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
1806 database[e.item][NAME],
1808 safe_str(database[e.item][NOTES])
1813 fprintf (out, "# End of address book file.\n");
1819 * end of Spruce export filter
1823 * wanderlust addressbook export filter
1827 wl_export_database(FILE *out, struct db_enumerator e)
1829 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
1831 fprintf (out, "# Wanderlust address book written by %s\n\n", PACKAGE);
1832 db_enumerate_items(e) {
1833 split_emailstr(e.item, emails);
1836 "%s\t\"%s\"\t\"%s\"\n",
1838 safe_str(database[e.item][NICK]),
1839 safe_str(database[e.item][NAME])
1844 fprintf (out, "\n# End of address book file.\n");
1850 * end of wanderlust addressbook export filter