5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
18 #include <sys/types.h>
19 #include "abook_curses.h"
32 extern abook_field_list *fields_list;
33 extern int fields_count;
36 * function declarations
40 * import filter prototypes
43 static int ldif_parse_file(FILE *handle);
44 static int mutt_parse_file(FILE *in);
45 static int pine_parse_file(FILE *in);
46 static int csv_parse_file(FILE *in);
47 static int allcsv_parse_file(FILE *in);
48 static int palmcsv_parse_file(FILE *in);
49 static int vcard_parse_file(FILE *in);
52 * export filter prototypes
55 static int ldif_export_database(FILE *out, struct db_enumerator e);
56 static int html_export_database(FILE *out, struct db_enumerator e);
57 static int pine_export_database(FILE *out, struct db_enumerator e);
58 static int csv_export_database(FILE *out, struct db_enumerator e);
59 static int allcsv_export_database(FILE *out, struct db_enumerator e);
60 static int palm_export_database(FILE *out, struct db_enumerator e);
61 static int vcard_export_database(FILE *out, struct db_enumerator e);
62 static int mutt_alias_export(FILE *out, struct db_enumerator e);
63 static int elm_alias_export(FILE *out, struct db_enumerator e);
64 static int text_export_database(FILE *out, struct db_enumerator e);
65 static int spruce_export_database(FILE *out, struct db_enumerator e);
66 static int wl_export_database(FILE *out, struct db_enumerator e);
67 static int bsdcal_export_database(FILE *out, struct db_enumerator e);
70 * end of function declarations
73 struct abook_input_filter i_filters[] = {
74 { "abook", N_("abook native format"), parse_database },
75 { "ldif", N_("ldif / Netscape addressbook"), ldif_parse_file },
76 { "mutt", N_("mutt alias"), mutt_parse_file },
77 { "pine", N_("pine addressbook"), pine_parse_file },
78 { "csv", N_("comma separated values"), csv_parse_file },
79 { "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file },
80 { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file },
81 { "vcard", N_("vCard file"), vcard_parse_file },
85 struct abook_output_filter e_filters[] = {
86 { "abook", N_("abook native format"), write_database },
87 { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database },
88 { "vcard", N_("vCard 2 file"), vcard_export_database },
89 { "mutt", N_("mutt alias"), mutt_alias_export },
90 { "html", N_("html document"), html_export_database },
91 { "pine", N_("pine addressbook"), pine_export_database },
92 { "csv", N_("comma separated values"), csv_export_database },
93 { "allcsv", N_("comma separated values (all fields)"), allcsv_export_database },
94 { "palmcsv", N_("Palm comma separated values"), palm_export_database},
95 { "elm", N_("elm alias"), elm_alias_export },
96 { "text", N_("plain text"), text_export_database },
97 { "wl", N_("Wanderlust address book"), wl_export_database },
98 { "spruce", N_("Spruce address book"), spruce_export_database },
99 { "bsdcal", N_("BSD calendar"), bsdcal_export_database },
113 for(i=0; *i_filters[i].filtname ; i++)
114 printf("\t%s\t%s\n", i_filters[i].filtname,
115 gettext(i_filters[i].desc));
120 for(i=0; *e_filters[i].filtname ; i++)
121 printf("\t%s\t%s\n", e_filters[i].filtname,
122 gettext(e_filters[i].desc));
128 number_of_output_filters()
132 for(i=0; *e_filters[i].filtname ; i++)
139 number_of_input_filters()
143 for(i=0; *i_filters[i].filtname ; i++)
152 char *username = getenv("USER");
153 struct passwd *pwent;
157 pwent = getpwnam(username);
159 if((tmp = xstrdup(pwent->pw_gecos)) == NULL)
160 return xstrdup(username);
162 rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
163 if (rtn == EOF || rtn == 0) {
165 return xstrdup(username);
174 static int i_read_file(char *filename, int (*func) (FILE *in));
183 refresh_statusline();
184 headerline(_("import database"));
186 mvaddstr(3, 1, _("please select a filter"));
189 for(i=0; *i_filters[i].filtname ; i++)
190 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
191 i_filters[i].filtname,
192 gettext(i_filters[i].desc));
194 mvprintw(6 + i, 6, _("x -\tcancel"));
202 int tmp = db_n_items();
206 filter = getch() - 'a';
207 if(filter == 'x' - 'a' ||
208 filter >= number_of_input_filters() || filter < 0) {
213 mvaddstr(5+filter, 2, "->");
215 filename = ask_filename(_("Filename: "));
221 if(i_read_file(filename, i_filters[filter].func ))
222 statusline_msg(_("Error occured while opening the file"));
223 else if(tmp == db_n_items())
224 statusline_msg(_("File does not seem to be a valid addressbook"));
235 i_read_file(char *filename, int (*func) (FILE *in))
240 if( (in = abook_fopen( filename, "r" )) == NULL )
251 import_file(char filtname[FILTNAME_LEN], char *filename)
254 int tmp = db_n_items();
258 if(! strncasecmp(i_filters[i].filtname, filtname,
261 if(! *i_filters[i].filtname) {
270 if(!strcmp(filename, "-")) {
272 if((fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
275 ret = (*i_filters[i].func) (stdin);
277 ret = i_read_file(filename, i_filters[i].func);
279 if(tmp == db_n_items())
289 static int e_write_file(char *filename,
290 int (*func) (FILE *in, struct db_enumerator e), int mode);
300 refresh_statusline();
301 headerline(_("export database"));
303 mvaddstr(3, 1, _("please select a filter"));
306 for(i = 0; *e_filters[i].filtname ; i++)
307 mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i,
308 e_filters[i].filtname,
309 gettext(e_filters[i].desc));
311 mvprintw(6 + i, 6, _("x -\tcancel"));
318 int enum_mode = ENUM_ALL;
323 filter = getch() - 'a';
324 if(filter == 'x' - 'a' ||
325 filter >= number_of_output_filters() || filter < 0) {
330 mvaddstr(5 + filter, 2, "->");
332 if(selected_items()) {
333 switch(statusline_askchoice(
334 _("Export <a>ll, export <s>elected, or <c>ancel?"),
335 S_("keybindings:all/selected/cancel|asc"), 3)) {
339 enum_mode = ENUM_SELECTED;
349 filename = ask_filename(_("Filename: "));
355 if( e_write_file(filename, e_filters[filter].func, enum_mode))
356 statusline_msg(_("Error occured while exporting"));
365 e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e),
370 struct db_enumerator enumerator = init_db_enumerator(mode);
372 if((out = fopen(filename, "a")) == NULL)
378 ret = (*func) (out, enumerator);
386 fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
389 struct db_enumerator e = init_db_enumerator(enum_mode);
392 if(!strncasecmp(e_filters[i].filtname, filtname,
395 if(!*e_filters[i].filtname) {
401 return (e_filters[i].func) (handle, e);
407 export_file(char filtname[FILTNAME_LEN], char *filename)
409 const int mode = ENUM_ALL;
412 struct db_enumerator e = init_db_enumerator(mode);
415 if(!strncasecmp(e_filters[i].filtname, filtname,
418 if(!*e_filters[i].filtname) {
427 if(!strcmp(filename, "-"))
428 ret = (e_filters[i].func) (stdout, e);
430 ret = e_write_file(filename, e_filters[i].func, mode);
436 * end of common functions
445 static void ldif_fix_string(char *str);
447 #define LDIF_ITEM_FIELDS 16
449 typedef char *ldif_item[LDIF_ITEM_FIELDS];
451 static ldif_item ldif_field_names = {
463 "facsimiletelephonenumber",
467 "objectclass", /* this must be the last entry */
470 static int ldif_conv_table[LDIF_ITEM_FIELDS] = {
473 ADDRESS, /* "streetaddress" */
474 ADDRESS2, /* "streetaddress2" */
475 CITY, /* "locality" */
477 ZIP, /* "postalcode" */
478 COUNTRY, /* "countryname" */
479 PHONE, /* "homephone" */
480 NOTES, /* "description" */
482 FAX, /* "facsimiletelephonenumber" */
483 MOBILEPHONE, /* "cellphone" */
484 WORKPHONE, /* "xmozillaanyphone" */
485 NICK, /* "xmozillanickname" */
486 -1, /* "objectclass" */ /* this must be the last entry */
491 ldif_read_line(FILE *in)
504 if(feof(in) || !line)
513 fseek(in, pos, SEEK_SET); /* fixme ! */
523 buf = strconcat(buf, ptr, NULL);
528 if(buf && *buf == '#' ) {
537 ldif_add_item(ldif_item li)
542 item = item_create();
544 if(!li[LDIF_ITEM_FIELDS -1])
548 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
549 if(ldif_conv_table[i] >= 0 && li[i] && *li[i])
550 item_fput(item,ldif_conv_table[i],xstrdup(li[i]));
553 add_item2database(item);
556 for(i=0; i < LDIF_ITEM_FIELDS; i++)
563 ldif_convert(ldif_item item, char *type, char *value)
567 if(!strcmp(type, "dn")) {
572 for(i=0; i < LDIF_ITEM_FIELDS; i++) {
573 if(!safe_strcmp(ldif_field_names[i], type) && *value) {
574 if(i == LDIF_ITEM_FIELDS - 1) /* this is a dirty hack */
575 if(safe_strcmp("person", value))
578 if(item_fget(item, i))
579 free(item_fget(item, i));
581 item_fput(item, i, xstrdup(value));
587 ldif_parse_file(FILE *handle)
594 memset(item, 0, sizeof(item));
597 if( !(line = ldif_read_line(handle)) )
600 if(-1 == (str_parse_line(line, &type, &value, &vlen))) {
602 continue; /* just skip the errors */
605 ldif_fix_string(value);
607 ldif_convert(item, type, value);
610 } while ( !feof(handle) );
612 ldif_convert(item, "dn", "");
618 ldif_fix_string(char *str)
622 for(i = 0, j = 0; j < (int)strlen(str); i++, j++)
623 str[i] = ( str[j] == (char)0xc3 ?
624 (char) str[++j] + (char) 0x40 :
635 * mutt alias import filter
641 mutt_read_line(FILE *in, char **alias, char **rest)
643 char *line, *ptr, *tmp;
646 if( !(line = ptr = getaline(in)) )
647 return 1; /* error / EOF */
651 if(strncmp("alias", ptr, 5)) {
662 while( ! ISSPACE(*ptr) )
665 alias_len = (size_t)(ptr - tmp);
668 *alias = xmalloc_inc(alias_len, 1);
670 strncpy(*alias, tmp, alias_len);
671 *(*alias + alias_len) = 0;
675 *rest = xstrdup(ptr);
682 mutt_fix_quoting(char *p)
702 mutt_parse_email(list_item item)
704 char *line = item_fget(item, NAME);
712 mutt_fix_quoting(line);
713 tmp = strconcat("From: ", line, NULL);
714 getname(tmp, &name, &email);
718 item_fput(item, NAME, name);
723 item_fput(item, EMAIL, email);
728 * this is completely broken
731 while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
732 tmp = strconcat("From: ", ++start, NULL);
733 getname(tmp, &name, &email);
738 tmp = strconcat(item[EMAIL], ",", email, NULL);
750 mutt_parse_file(FILE *in)
752 list_item item = item_create();
755 memset(item, 0, fields_count * sizeof(char *));
757 if(!mutt_read_line(in,
758 (field_id(NICK) != -1) ?
759 &item[field_id(NICK)] : NULL,
760 &item[field_id(NAME)]))
761 mutt_parse_email(item);
768 add_item2database(item);
776 * end of mutt alias import filter
785 ldif_fput_type_and_value(FILE *out,char *type, char *value )
789 tmp = ldif_type_and_value(type, value, strlen(value));
797 ldif_export_database(FILE *out, struct db_enumerator e)
799 char email[MAX_EMAILSTR_LEN];
801 fprintf(out, "version: 1\n");
803 db_enumerate_items(e) {
806 get_first_email(email, e.item);
808 tmp = strdup_printf("cn=%s,mail=%s",db_name_get(e.item),email);
810 ldif_fput_type_and_value(out, "dn", tmp);
813 for(j = 0; j < LDIF_ITEM_FIELDS; j++) {
814 if(ldif_conv_table[j] >= 0) {
815 if(ldif_conv_table[j] == EMAIL)
816 ldif_fput_type_and_value(out,
817 ldif_field_names[j], email);
818 else if(db_fget(e.item,ldif_conv_table[j]))
819 ldif_fput_type_and_value(out,
822 ldif_conv_table[j]));
826 fprintf(out, "objectclass: top\n"
827 "objectclass: person\n\n");
834 * end of ldif export filter
841 static void html_export_write_head(FILE *out);
842 static void html_export_write_tail(FILE *out);
844 extern struct index_elem *index_elements;
847 html_print_emails(FILE *out, struct list_field *f)
849 abook_list *l = csv_to_abook_list(f->data);
851 for(; l; l = l->next) {
852 fprintf(out, "<a href=\"mailto:%s\">%s</a>", l->data, l->data);
861 html_export_database(FILE *out, struct db_enumerator e)
864 struct index_elem *cur;
871 html_export_write_head(out);
873 db_enumerate_items(e) {
874 fprintf(out, "<tr>");
875 for(cur = index_elements; cur; cur = cur->next) {
876 if(cur->type != INDEX_FIELD)
879 get_list_field(e.item, cur, &f);
881 if(f.type == FIELD_EMAILS) {
882 fprintf(out, "<td>");
883 html_print_emails(out, &f);
884 fprintf(out, "</td>");
887 fprintf(out, "<td>%s</td>", safe_str(f.data));
890 fprintf(out, "</tr>\n");
893 html_export_write_tail(out);
899 html_export_write_head(FILE *out)
901 char *realname = get_real_name(), *str;
902 struct index_elem *cur;
904 fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
905 fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>",
907 fprintf(out, "\n</head>\n<body>\n");
908 fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
909 fprintf(out, "<br><br>\n\n");
911 fprintf(out, "<table border=\"1\" align=\"center\">\n<tr>");
912 for(cur = index_elements; cur; cur = cur->next) {
913 if(cur->type != INDEX_FIELD)
916 get_field_info(cur->d.field.id, NULL, &str, NULL);
917 fprintf(out, "<th>%s</th>", str);
919 fprintf(out, "</tr>\n\n");
925 html_export_write_tail(FILE *out)
927 fprintf(out, "\n</table>\n");
928 fprintf(out, "\n</body>\n</html>\n");
932 * end of html export filter
937 * pine addressbook import filter
940 #define PINE_BUF_SIZE 2048
943 pine_fixbuf(char *buf)
947 for(i = 0,j = 0; j < (int)strlen(buf); i++, j++)
948 buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
952 pine_convert_emails(char *s)
957 if(s == NULL || *s != '(')
960 for(i = 0; s[i]; i++)
963 if( ( tmp = strchr(s,')')) )
966 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
967 if(i > MAX_LIST_ITEMS - 1) {
975 pine_parse_buf(char *buf)
980 char tmp[PINE_BUF_SIZE];
982 int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
984 item = item_create();
986 for(i=0, last=0; !last ; i++) {
987 if( !(end = strchr(start, '\t')) )
990 len = last ? strlen(start) : (int) (end-start);
991 len = min(len, PINE_BUF_SIZE - 1);
993 if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
994 && pine_conv_table[i] >= 0) {
995 strncpy(tmp, start, len);
998 item_fput(item, pine_conv_table[i],
1004 pine_convert_emails(item_fget(item, EMAIL));
1005 add_item2database(item);
1010 #define LINESIZE 1024
1013 pine_parse_file(FILE *in)
1015 char line[LINESIZE];
1020 fgets(line, LINESIZE, in);
1024 buf = xrealloc(buf, i*LINESIZE);
1027 fgets(line, LINESIZE, in);
1029 if(*ptr != ' ' || feof(in))
1043 pine_parse_buf(buf);
1052 * end of pine addressbook import filter
1057 * pine addressbook export filter
1059 * filter doesn't wrap the lines as it should but Pine seems to handle
1060 * created files without problems - JH
1064 pine_export_database(FILE *out, struct db_enumerator e)
1068 db_enumerate_items(e) {
1069 emails = db_email_get(e.item);
1070 fprintf(out, strchr(emails, ',') /* multiple addresses? */ ?
1071 "%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n",
1072 safe_str(db_fget(e.item, NICK)),
1073 safe_str(db_name_get(e.item)),
1075 safe_str(db_fget(e.item, NOTES))
1084 * end of pine addressbook export filter
1093 * these files should be parsed according to a certain
1094 * lay out, or the default if layout is not given, at
1095 * the moment only default is done...
1098 #define CSV_COMMENT_CHAR '#'
1099 #define CSV_DUPLICATE_SEPARATOR " "
1100 #define CSV_TABLE_SIZE(t) (sizeof (t) / sizeof *(t))
1102 static int csv_conv_table[] = {
1110 static int allcsv_conv_table[] = {
1129 static int palmcsv_conv_table[] = {
1130 NAME, /* Last name */
1131 NAME, /* First name */
1148 csv_convert_emails(char *s)
1156 for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1)
1157 if(i > MAX_LIST_ITEMS - 1) {
1165 csv_remove_quotes(char *s)
1167 char *copy, *trimmed;
1170 copy = trimmed = xstrdup(s);
1173 len = strlen(trimmed);
1174 if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
1179 trimmed[len - 1] = 0;
1181 trimmed = xstrdup(trimmed);
1191 csv_field_to_item(int *table_base, size_t table_size, int field)
1193 if(field < table_size)
1194 return field_id(table_base[field]);
1200 csv_store_item(list_item item, int i, char *s)
1202 char *newstr = NULL;
1207 if( !(newstr = csv_remove_quotes(s)) )
1211 if (item[i] != NULL) {
1212 char *oldstr = item[i];
1214 item[i] = strconcat(newstr, CSV_DUPLICATE_SEPARATOR,
1227 csv_is_valid_quote_end(char *p)
1235 else if(!ISSPACE(*p))
1243 csv_is_valid_quote_start(char *p)
1248 else if(!ISSPACE(*p))
1256 csv_parse_line(char *line, int *table_base, size_t table_size)
1260 bool in_quote = FALSE;
1263 item = item_create();
1265 for(p = start = line, field = 0; *p; p++) {
1267 if(csv_is_valid_quote_end(p))
1270 if ( (((p - start) / sizeof (char)) < 2 ) &&
1271 csv_is_valid_quote_start(p) )
1275 if(*p == ',' && !in_quote) {
1277 csv_store_item(item,
1278 csv_field_to_item(table_base,table_size,field),
1287 csv_store_item(item, csv_field_to_item(table_base, table_size, field),
1290 csv_convert_emails(item_fget(item, EMAIL));
1291 add_item2database(item);
1296 csv_parse_file_common(FILE *in, int *conv_table, size_t table_size)
1301 line = getaline(in);
1303 if(line && *line && *line != CSV_COMMENT_CHAR)
1304 csv_parse_line(line, conv_table, table_size);
1313 csv_parse_file(FILE *in)
1315 return csv_parse_file_common(in, csv_conv_table,
1316 CSV_TABLE_SIZE(csv_conv_table));
1320 allcsv_parse_file(FILE *in)
1322 return csv_parse_file_common(in, allcsv_conv_table,
1323 CSV_TABLE_SIZE(allcsv_conv_table));
1327 palmcsv_parse_file(FILE *in)
1329 return csv_parse_file_common(in, palmcsv_conv_table,
1330 CSV_TABLE_SIZE(palmcsv_conv_table));
1334 * end of csv import filter
1338 * vCard import filter
1341 static char *vcard_fields[] = {
1342 "FN", /* FORMATTED NAME */
1343 "EMAIL", /* EMAIL */
1344 "ADR", /* ADDRESS */
1345 "ADR", /* ADDRESS2 - not used */
1349 "ADR", /* COUNTRY */
1351 "TEL", /* WORKPHONE */
1353 "TEL", /* MOBILEPHONE */
1354 "NICKNAME", /* NICK */
1357 "N", /* NAME: special case/mapping in vcard_parse_line() */
1358 NULL /* not implemented: ANNIVERSARY, ITEM_FIELDS */
1362 * mappings between vCard ADR field and abook's ADDRESS
1363 * see rfc2426 section 3.2.1
1365 static int vcard_address_fields[] = {
1366 -1, /* vCard(post office box) - not used */
1367 -1, /* vCard(the extended address) - not used */
1368 2, /* vCard(the street address) - ADDRESS */
1369 4, /* vCard(the locality) - CITY */
1370 5, /* vCard(the region) - STATE */
1371 6, /* vCard(the postal code) - ZIP */
1372 7 /* vCard(the country name) - COUNTRY */
1377 VCARD_KEY_ATTRIBUTE,
1382 vcard_get_line_element(char *line, int element)
1385 char *line_copy = 0;
1391 line_copy = xstrdup(line);
1393 /* change newline characters, if present, to end of string */
1394 for(i=0; line_copy[i]; i++) {
1395 if(line_copy[i] == '\r' || line_copy[i] == '\n') {
1396 line_copy[i] = '\0';
1401 /* separate key from value */
1402 for(i=0; line_copy[i]; i++) {
1403 if(line_copy[i] == ':') {
1404 line_copy[i] = '\0';
1406 value = &line_copy[i+1];
1411 /* separate key from key attributes */
1412 /* works for vCard 2 as well (automagically) */
1414 for(i=0; key[i]; i++) {
1417 key_attr = &key[i+1];
1426 result = xstrdup(key);
1428 case VCARD_KEY_ATTRIBUTE:
1430 result = xstrdup(key_attr);
1434 result = xstrdup(value);
1443 vcard_parse_email(list_item item, char *line)
1447 email = vcard_get_line_element(line, VCARD_VALUE);
1450 item[1] = strconcat(item[1], ",", email, 0);
1459 vcard_parse_address(list_item item, char *line)
1464 char *address_field;
1466 value = vcard_get_line_element(line, VCARD_VALUE);
1470 address_field = value;
1471 for(i=k=0; value[i]; i++) {
1472 if(value[i] == ';') {
1474 if(vcard_address_fields[k] >= 0) {
1475 item[vcard_address_fields[k]] = xstrdup(address_field);
1477 address_field = &value[i+1];
1479 if((k+1)==(sizeof(vcard_address_fields)/sizeof(*vcard_address_fields)))
1483 item[vcard_address_fields[k]] = xstrdup(address_field);
1488 vcard_parse_name(list_item item, char *line)
1490 // store the "N" field into "NAME" *if* no "FN:"
1491 // value has already been stored here
1495 item[0] = vcard_get_line_element(line, VCARD_VALUE);
1496 // "N:" can be multivalued => replace ';' separators by ' '
1497 while(item[0][++i]) if(item[0][i] == ';') item[0][i] = ' ';
1499 // http://www.daniweb.com/software-development/c/code/216919
1500 char *original = item[0], *p = original;
1503 if (*original != ' ' || trimmed) {
1504 trimmed = 1; *p++ = *original;
1506 } while(*original++);
1510 vcard_parse_phone(list_item item, char *line)
1512 char *type = vcard_get_line_element(line, VCARD_KEY_ATTRIBUTE);
1513 char *value = vcard_get_line_element(line, VCARD_VALUE);
1515 /* set the standard number */
1516 if (!type) item_fput(item, PHONE, value);
1519 * see rfc2426 section 3.3.1
1520 * Note: we probably support both vCard 2 and 3
1523 if (strcasestr(type, "home") != NULL)
1524 item_fput(item, PHONE, xstrdup(value));
1525 else if (strcasestr(type, "work") != NULL)
1526 item_fput(item, WORKPHONE, xstrdup(value));
1527 else if (strcasestr(type, "fax") != NULL)
1528 item_fput(item, FAX, xstrdup(value));
1529 else if (strcasestr(type, "cell") != NULL)
1530 item_fput(item, MOBILEPHONE, xstrdup(value));
1538 vcard_parse_line(list_item item, char *line)
1543 for(i=0; vcard_fields[i]; i++) {
1544 key = vcard_fields[i];
1546 if(0 == strncmp(key, line, strlen(key))) {
1547 if(0 == strcmp(key, "EMAIL"))
1548 vcard_parse_email(item, line);
1550 vcard_parse_address(item, line);
1551 else if(0 == strcmp(key, "TEL"))
1552 vcard_parse_phone(item, line);
1553 else if(0 == strcmp(key, "N"))
1554 vcard_parse_name(item, line);
1556 item[i] = vcard_get_line_element(line, VCARD_VALUE);
1563 vcard_parse_item(FILE *in)
1566 list_item item = item_create();
1569 line = getaline(in);
1571 if(line && !strncmp("END:VCARD", line, 9)) {
1576 vcard_parse_line(item, line);
1581 add_item2database(item);
1586 vcard_parse_file(FILE *in)
1591 line = getaline(in);
1593 if(line && !strncmp("BEGIN:VCARD", line, 11)) {
1595 vcard_parse_item(in);
1606 * end of vCard import filter
1610 * csv addressbook export filters
1613 #define CSV_LAST (-1)
1614 #define CSV_UNDEFINED (-2)
1615 #define CSV_SPECIAL(X) (-3 - (X))
1616 #define CSV_IS_SPECIAL(X) ((X) <= -3)
1619 csv_export_common(FILE *out, struct db_enumerator e,
1620 int fields[], void (*special_func)(FILE *, int, int))
1624 db_enumerate_items(e) {
1625 for(i = 0; fields[i] != CSV_LAST; i++) {
1626 if(fields[i] == CSV_UNDEFINED)
1627 fprintf(out, "\"\"");
1628 else if(CSV_IS_SPECIAL(fields[i])) {
1630 (*special_func)(out, e.item, fields[i]);
1633 strchr(safe_str(database[e.item][field_idx(fields[i])]), ',') ||
1634 strchr(safe_str(database[e.item][field_idx(fields[i])]), '\"')) ?
1636 safe_str(database[e.item][field_idx(fields[i])])
1638 fprintf(out, "\"%s\"",
1639 safe_str(db_fget(e.item,fields[i])));
1641 if(fields[i + 1] != CSV_LAST)
1651 csv_export_database(FILE *out, struct db_enumerator e)
1653 int csv_export_fields[] = {
1662 csv_export_common(out, e, csv_export_fields, NULL);
1668 allcsv_export_database(FILE *out, struct db_enumerator e)
1671 * TODO: Should get these atomatically from abook_fileds
1674 int allcsv_export_fields[] = {
1695 fprintf(out, "\"NAME\",");
1696 fprintf(out, "\"EMAIL\",");
1697 fprintf(out, "\"ADDRESS\",");
1698 fprintf(out, "\"ADDRESS2\",");
1699 fprintf(out, "\"CITY\",");
1700 fprintf(out, "\"STATE\",");
1701 fprintf(out, "\"ZIP\",");
1702 fprintf(out, "\"COUNTRY\",");
1703 fprintf(out, "\"PHONE\",");
1704 fprintf(out, "\"WORKPHONE\",");
1705 fprintf(out, "\"FAX\",");
1706 fprintf(out, "\"MOBILEPHONE\",");
1707 fprintf(out, "\"NICK\",");
1708 fprintf(out, "\"URL\",");
1709 fprintf(out, "\"NOTES\",");
1710 fprintf(out, "\"ANNIVERSARY\"\n");
1712 csv_export_common(out, e, allcsv_export_fields, NULL);
1721 #define PALM_CSV_NAME CSV_SPECIAL(0)
1722 #define PALM_CSV_END CSV_SPECIAL(1)
1723 #define PALM_CSV_CAT CSV_SPECIAL(2)
1726 palm_split_and_write_name(FILE *out, char *name)
1732 if ( (p = strchr(name, ' ')) ) {
1736 fprintf(out, "\"%s\",\"" , p + 1);
1737 fwrite((void *)name, p - name, sizeof(char), out);
1740 fprintf(out, "\"%s\"", safe_str(name));
1745 palm_csv_handle_specials(FILE *out, int item, int field)
1749 palm_split_and_write_name(out, db_name_get(item));
1752 fprintf(out, "\"abook\"");
1755 fprintf(out, "\"0\"");
1763 palm_export_database(FILE *out, struct db_enumerator e)
1765 int palm_export_fields[] = {
1766 PALM_CSV_NAME, /* LASTNAME, FIRSTNAME */
1767 CSV_UNDEFINED, /* TITLE */
1768 CSV_UNDEFINED, /* COMPANY */
1769 WORKPHONE, /* WORK PHONE */
1770 PHONE, /* HOME PHONE */
1772 MOBILEPHONE, /* OTHER */
1774 ADDRESS, /* ADDRESS */
1778 COUNTRY, /* COUNTRY */
1779 NICK, /* DEFINED 1 */
1780 URL, /* DEFINED 2 */
1781 CSV_UNDEFINED, /* DEFINED 3 */
1782 CSV_UNDEFINED, /* DEFINED 4 */
1784 PALM_CSV_END, /* "0" */
1785 PALM_CSV_CAT, /* CATEGORY */
1789 csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
1795 * end of csv export filters
1799 * vCard 2 addressbook export filter
1803 vcard_export_database(FILE *out, struct db_enumerator e)
1807 abook_list *emails, *em;
1809 db_enumerate_items(e) {
1810 fprintf(out, "BEGIN:VCARD\r\nFN:%s\r\n",
1811 safe_str(db_name_get(e.item)));
1813 name = get_surname(db_name_get(e.item));
1814 for( j = strlen(db_name_get(e.item)) - 1; j >= 0; j-- ) {
1815 if((db_name_get(e.item))[j] == ' ')
1818 fprintf(out, "N:%s;%.*s\r\n",
1821 safe_str(db_name_get(e.item))
1826 if(db_fget(e.item, ADDRESS))
1827 fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\r\n",
1828 safe_str(db_fget(e.item, ADDRESS)),
1829 safe_str(db_fget(e.item, ADDRESS2)),
1830 safe_str(db_fget(e.item, CITY)),
1831 safe_str(db_fget(e.item, STATE)),
1832 safe_str(db_fget(e.item, ZIP)),
1833 safe_str(db_fget(e.item, COUNTRY))
1836 if(db_fget(e.item, PHONE))
1837 fprintf(out, "TEL;HOME:%s\r\n",
1838 db_fget(e.item, PHONE));
1839 if(db_fget(e.item, WORKPHONE))
1840 fprintf(out, "TEL;WORK:%s\r\n",
1841 db_fget(e.item, WORKPHONE));
1842 if(db_fget(e.item, FAX))
1843 fprintf(out, "TEL;FAX:%s\r\n",
1844 db_fget(e.item, FAX));
1845 if(db_fget(e.item, MOBILEPHONE))
1846 fprintf(out, "TEL;CELL:%s\r\n",
1847 db_fget(e.item, MOBILEPHONE));
1849 tmp = db_email_get(e.item);
1851 emails = csv_to_abook_list(tmp);
1853 for(em = emails; em; em = em->next)
1854 fprintf(out, "EMAIL;INTERNET:%s\r\n", em->data);
1856 abook_list_free(&emails);
1860 if(db_fget(e.item, NOTES))
1861 fprintf(out, "NOTE:%s\r\n",
1862 db_fget(e.item, NOTES));
1863 if(db_fget(e.item, URL))
1864 fprintf(out, "URL:%s\r\n",
1865 db_fget(e.item, URL));
1867 fprintf(out, "END:VCARD\r\n\r\n");
1875 * end of vCard export filter
1880 * mutt alias export filter
1884 mutt_alias_genalias(int i)
1888 if(db_fget(i, NICK))
1889 return xstrdup(db_fget(i, NICK));
1891 tmp = xstrdup(db_name_get(i));
1893 if( ( pos = strchr(tmp, ' ') ) )
1902 mutt_alias_export(FILE *out, struct db_enumerator e)
1904 char email[MAX_EMAIL_LEN];
1906 int email_addresses;
1909 db_enumerate_items(e) {
1910 alias = mutt_alias_genalias(e.item);
1911 get_first_email(email, e.item);
1913 /* do not output contacts without email address */
1914 /* cause this does not make sense in mutt aliases */
1917 /* output first email address */
1918 fprintf(out, "alias %s %s <%s>\n",
1920 db_name_get(e.item),
1923 /* number of email addresses */
1924 email_addresses = 1;
1925 ptr = db_email_get(e.item);
1926 while (*ptr != '\0') {
1933 /* output other email addresses */
1934 while (email_addresses-- > 1) {
1935 roll_emails(e.item, ROTATE_RIGHT);
1936 get_first_email(email, e.item);
1937 fprintf(out, "alias %s__%s %s <%s>\n",
1940 db_name_get(e.item),
1943 roll_emails(e.item, ROTATE_RIGHT);
1952 * end of mutt alias export filter
1957 * printable export filter
1962 text_write_address_us(FILE *out, int i) {
1963 fprintf(out, "\n%s", db_fget(i, ADDRESS));
1965 if(db_fget(i, ADDRESS2))
1966 fprintf(out, "\n%s", db_fget(i, ADDRESS2));
1968 if(db_fget(i, CITY))
1969 fprintf(out, "\n%s", db_fget(i, CITY));
1971 if(db_fget(i, STATE) || db_fget(i, ZIP)) {
1974 if(db_fget(i, STATE)) {
1975 fprintf(out, "%s", db_fget(i, STATE));
1981 fprintf(out, "%s", db_fget(i, ZIP));
1984 if(db_fget(i, COUNTRY))
1985 fprintf(out, "\n%s", db_fget(i, COUNTRY));
1990 text_write_address_uk(FILE *out, int i) {
1993 for(j = ADDRESS; j <= COUNTRY; j++)
1995 fprintf(out, "\n%s", db_fget(i, j));
1999 text_write_address_eu(FILE *out, int i) {
2000 fprintf(out, "\n%s", db_fget(i, ADDRESS));
2002 if(db_fget(i, ADDRESS2))
2003 fprintf(out, "\n%s", db_fget(i, ADDRESS2));
2005 if(db_fget(i, ZIP) || db_fget(i, CITY)) {
2008 if(db_fget(i, ZIP)) {
2009 fprintf(out, "%s", db_fget(i, ZIP));
2010 if(db_fget(i, CITY))
2014 fprintf(out, "%s", safe_str(db_fget(i, CITY)));
2017 if(db_fget(i, STATE))
2018 fprintf(out, "\n%s", db_fget(i, STATE));
2020 if(db_fget(i, COUNTRY))
2021 fprintf(out, "\n%s", db_fget(i, COUNTRY));
2025 text_export_database(FILE * out, struct db_enumerator e)
2027 abook_list *emails, *em;
2029 char *realname = get_real_name(), *str = NULL, *tmp;
2030 char *style = opt_get_str(STR_ADDRESS_STYLE);
2033 "-----------------------------------------\n%s's address book\n"
2034 "-----------------------------------------\n\n\n",
2038 db_enumerate_items(e) {
2040 "-----------------------------------------\n\n");
2041 fprintf(out, "%s", db_name_get(e.item));
2042 if(db_fget(e.item, NICK) && *db_fget(e.item, NICK))
2043 fprintf(out, "\n(%s)", db_fget(e.item, NICK));
2046 tmp = db_email_get(e.item);
2048 emails = csv_to_abook_list(tmp);
2051 for(em = emails; em; em = em->next)
2052 fprintf(out, "%s\n", em->data);
2054 abook_list_free(&emails);
2058 if(db_fget(e.item, ADDRESS)) {
2059 if(!safe_strcmp(style, "us")) /* US like */
2060 text_write_address_us(out, e.item);
2061 else if(!safe_strcmp(style, "uk")) /* UK like */
2062 text_write_address_uk(out, e.item);
2064 text_write_address_eu(out, e.item);
2069 if((db_fget(e.item, PHONE)) ||
2070 (db_fget(e.item, WORKPHONE)) ||
2071 (db_fget(e.item, FAX)) ||
2072 (db_fget(e.item, MOBILEPHONE))) {
2074 for(j = PHONE; j <= MOBILEPHONE; j++)
2075 if(db_fget(e.item, j)) {
2076 get_field_info(field_id(j),
2078 fprintf(out, "%s: %s\n", str,
2079 db_fget(e.item, j));
2083 if(db_fget(e.item, URL))
2084 fprintf(out, "\n%s\n", db_fget(e.item, URL));
2085 if(db_fget(e.item, NOTES))
2086 fprintf(out, "\n%s\n", db_fget(e.item, NOTES));
2091 fprintf(out, "-----------------------------------------\n");
2097 * end of printable export filter
2101 * elm alias export filter
2105 elm_alias_export(FILE *out, struct db_enumerator e)
2107 char email[MAX_EMAIL_LEN];
2110 db_enumerate_items(e) {
2111 alias = mutt_alias_genalias(e.item);
2112 get_first_email(email, e.item);
2113 fprintf(out, "%s = %s = %s\n",alias,db_name_get(e.item),email);
2121 * end of elm alias export filter
2126 * Spruce export filter
2130 spruce_export_database (FILE *out, struct db_enumerator e)
2132 char email[MAX_EMAIL_LEN];
2134 fprintf(out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
2136 db_enumerate_items(e) {
2137 get_first_email(email, e.item);
2138 if(strcmp(email, "")) {
2139 fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
2141 db_name_get(e.item),
2143 safe_str(db_fget(e.item, NOTES))
2148 fprintf (out, "# End of address book file.\n");
2154 * end of Spruce export filter
2158 * wanderlust addressbook export filter
2162 wl_export_database(FILE *out, struct db_enumerator e)
2164 char email[MAX_EMAIL_LEN];
2166 fprintf(out, "# Wanderlust address book written by %s\n\n", PACKAGE);
2167 db_enumerate_items(e) {
2168 get_first_email(email, e.item);
2171 "%s\t\"%s\"\t\"%s\"\n",
2173 safe_str(db_fget(e.item, NICK)),
2174 safe_str(db_name_get(e.item))
2179 fprintf (out, "\n# End of address book file.\n");
2185 * end of wanderlust addressbook export filter
2189 * BSD calendar export filter
2193 bsdcal_export_database(FILE *out, struct db_enumerator e)
2195 db_enumerate_items(e) {
2196 int year, month = 0, day = 0;
2197 char *anniversary = db_fget(e.item, ANNIVERSARY);
2200 if(!parse_date_string(anniversary, &day, &month, &year))
2204 _("%02d/%02d\tAnniversary of %s\n"),
2207 safe_str(db_name_get(e.item))
2216 * end of BSD calendar export filter