X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=filter.c;h=b70d918b181237ab331d612d64788bc49bc04610;hb=3be3adcf308ee7c9c8775e2176d0592138b220ed;hp=671d2a4c58103223078c7c1230cb485b26f618bf;hpb=216cf3ed9e60bea94d777029eb3b5e44c65f1b68;p=pkg%2Fabook.git
diff --git a/filter.c b/filter.c
index 671d2a4..b70d918 100644
--- a/filter.c
+++ b/filter.c
@@ -44,6 +44,7 @@ static int pine_parse_file(FILE *in);
static int csv_parse_file(FILE *in);
static int allcsv_parse_file(FILE *in);
static int palmcsv_parse_file(FILE *in);
+static int vcard_parse_file(FILE *in);
/*
* export filter prototypes
@@ -61,6 +62,7 @@ static int elm_alias_export(FILE *out, struct db_enumerator e);
static int text_export_database(FILE *out, struct db_enumerator e);
static int spruce_export_database(FILE *out, struct db_enumerator e);
static int wl_export_database(FILE *out, struct db_enumerator e);
+static int bsdcal_export_database(FILE *out, struct db_enumerator e);
/*
* end of function declarations
@@ -74,6 +76,7 @@ struct abook_input_filter i_filters[] = {
{ "csv", N_("comma separated values"), csv_parse_file },
{ "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file },
{ "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file },
+ { "vcard", N_("vCard file"), vcard_parse_file },
{ "\0", NULL, NULL }
};
@@ -91,6 +94,7 @@ struct abook_output_filter e_filters[] = {
{ "text", N_("plain text"), text_export_database },
{ "wl", N_("Wanderlust address book"), wl_export_database },
{ "spruce", N_("Spruce address book"), spruce_export_database },
+ { "bsdcal", N_("BSD calendar"), bsdcal_export_database },
{ "\0", NULL, NULL }
};
@@ -832,39 +836,56 @@ ldif_export_database(FILE *out, struct db_enumerator e)
* html export filter
*/
-static void html_export_write_head(FILE *out, int extra_column);
+static void html_export_write_head(FILE *out);
static void html_export_write_tail(FILE *out);
+extern struct index_elem *index_elements;
+
+static void
+html_print_emails(FILE *out, struct list_field *f)
+{
+ abook_list *l = csv_to_abook_list(f->data);
+
+ for(; l; l = l->next) {
+ fprintf(out, "%s", l->data, l->data);
+ if(l->next)
+ fprintf(out, ", ");
+ }
+
+ abook_list_free(&l);
+}
+
static int
html_export_database(FILE *out, struct db_enumerator e)
{
- char tmp[MAX_EMAILSTR_LEN], *emails;
- int extra_column;
+ struct list_field f;
+ struct index_elem *cur;
if(list_is_empty())
return 2;
- extra_column = init_extra_field(STR_EXTRA_COLUMN);
- html_export_write_head(out, extra_column);
+ init_index();
- db_enumerate_items(e) {
- get_first_email(tmp, e.item);
- if (*tmp)
- fprintf(out, "
\n"
- "%s"
- " | \n",
- tmp,
- db_name_get(e.item));
- else
- fprintf(out, "
\n%s | \n", db_name_get(e.item));
+ html_export_write_head(out);
- emails = db_email_get(e.item);
- fprintf(out, "%s | \n", emails);
- free(emails);
- if(extra_column >= 0)
- fprintf(out, "%s | \n",
- safe_str(db_fget_byid(e.item, extra_column)));
- fprintf(out, "
\n\n");
+ db_enumerate_items(e) {
+ fprintf(out, "");
+ for(cur = index_elements; cur; cur = cur->next) {
+ if(cur->type != INDEX_FIELD)
+ continue;
+
+ get_list_field(e.item, cur, &f);
+
+ if(f.type == FIELD_EMAILS) {
+ fprintf(out, "");
+ html_print_emails(out, &f);
+ fprintf(out, " | ");
+ continue;
+ } else {
+ fprintf(out, "%s | ", safe_str(f.data));
+ }
+ }
+ fprintf(out, "
\n");
}
html_export_write_tail(out);
@@ -873,9 +894,10 @@ html_export_database(FILE *out, struct db_enumerator e)
}
static void
-html_export_write_head(FILE *out, int extra_column)
+html_export_write_head(FILE *out)
{
- char *realname = get_real_name(), *extra_column_name = NULL;
+ char *realname = get_real_name(), *str;
+ struct index_elem *cur;
fprintf(out, "\n");
fprintf(out, "\n\n %s's addressbook",
@@ -884,11 +906,13 @@ html_export_write_head(FILE *out, int extra_column)
fprintf(out, "\n%s's addressbook
\n", realname );
fprintf(out, "
\n\n");
- fprintf(out, "\n");
- fprintf(out, "\nName | E-mail address(es) | ");
- if(extra_column >= 0) {
- get_field_keyname(extra_column, NULL, &extra_column_name);
- fprintf(out, "%s | ", safe_str(extra_column_name));
+ fprintf(out, "\n");
+ for(cur = index_elements; cur; cur = cur->next) {
+ if(cur->type != INDEX_FIELD)
+ continue;
+
+ get_field_info(cur->d.field.id, NULL, &str, NULL);
+ fprintf(out, "%s | ", str);
}
fprintf(out, "
\n\n");
@@ -1308,6 +1332,262 @@ palmcsv_parse_file(FILE *in)
* end of csv import filter
*/
+/*
+ * vCard import filter
+ */
+
+static char *vcard_fields[] = {
+ "FN", /* NAME */
+ "EMAIL", /* EMAIL */
+ "ADR", /* ADDRESS */
+ "ADR", /* ADDRESS2 - not used */
+ "ADR", /* CITY */
+ "ADR", /* STATE */
+ "ADR", /* ZIP */
+ "ADR", /* COUNTRY */
+ "TEL", /* PHONE */
+ "TEL", /* WORKPHONE */
+ "TEL", /* FAX */
+ "TEL", /* MOBILEPHONE */
+ "NICKNAME", /* NICK */
+ "URL", /* URL */
+ "NOTE", /* NOTES */
+ NULL /* not implemented: ANNIVERSARY, ITEM_FIELDS */
+};
+
+/*
+ * mappings between vCard ADR field and abook's ADDRESS
+ * see rfc2426 section 3.2.1
+ */
+static int vcard_address_fields[] = {
+ -1, /* vCard(post office box) - not used */
+ -1, /* vCard(the extended address) - not used */
+ 2, /* vCard(the street address) - ADDRESS */
+ 4, /* vCard(the locality) - CITY */
+ 5, /* vCard(the region) - STATE */
+ 6, /* vCard(the postal code) - ZIP */
+ 7 /* vCard(the country name) - COUNTRY */
+};
+
+enum {
+ VCARD_KEY = 0,
+ VCARD_KEY_ATTRIBUTE,
+ VCARD_VALUE,
+};
+
+static char *
+vcard_get_line_element(char *line, int element)
+{
+ int i;
+ char *line_copy = 0;
+ char *result = 0;
+ char *key = 0;
+ char *key_attr = 0;
+ char *value = 0;
+
+ line_copy = xstrdup(line);
+
+ /* make newline characters if exist end of string */
+ for(i=0; line_copy[i]; i++) {
+ if(line_copy[i] == '\r' || line_copy[i] == '\n') {
+ line_copy[i] = '\0';
+ break;
+ }
+ }
+
+ /* separate key from value */
+ for(i=0; line_copy[i]; i++) {
+ if(line_copy[i] == ':') {
+ line_copy[i] = '\0';
+ key = line_copy;
+ value = &line_copy[i+1];
+ break;
+ }
+ }
+
+ /* separate key from key attributes */
+ if (key) {
+ for(i=0; key[i]; i++) {
+ if(key[i] == ';') {
+ key[i] = '\0';
+ key_attr = &key[i+1];
+ break;
+ }
+ }
+ }
+
+ switch(element) {
+ case VCARD_KEY:
+ if(key)
+ result = xstrdup(key);
+ break;
+ case VCARD_KEY_ATTRIBUTE:
+ if(key_attr)
+ result = xstrdup(key_attr);
+ break;
+ case VCARD_VALUE:
+ if(value)
+ result = xstrdup(value);
+ break;
+ }
+
+ xfree(line_copy);
+ return result;
+}
+
+static void
+vcard_parse_email(list_item item, char *line)
+{
+ char *email;
+
+ email = vcard_get_line_element(line, VCARD_VALUE);
+
+ if(item[1]) {
+ item[1] = strconcat(item[1], ",", email, 0);
+ xfree(email);
+ }
+ else {
+ item[1] = email;
+ }
+}
+
+static void
+vcard_parse_address(list_item item, char *line)
+{
+ int i;
+ int k;
+ char *value;
+ char *address_field;
+
+ value = vcard_get_line_element(line, VCARD_VALUE);
+ if(!value)
+ return;
+
+ address_field = value;
+ for(i=k=0; value[i]; i++) {
+ if(value[i] == ';') {
+ value[i] = '\0';
+ if(vcard_address_fields[k] >= 0) {
+ item[vcard_address_fields[k]] = xstrdup(address_field);
+ }
+ address_field = &value[i+1];
+ k++;
+ if((k+1)==(sizeof(vcard_address_fields)/sizeof(*vcard_address_fields)))
+ break;
+ }
+ }
+ item[vcard_address_fields[k]] = xstrdup(address_field);
+ xfree(value);
+}
+
+static void
+vcard_parse_phone(list_item item, char *line)
+{
+ int index = 8;
+ char *type = vcard_get_line_element(line, VCARD_KEY_ATTRIBUTE);
+ char *value = vcard_get_line_element(line, VCARD_VALUE);
+
+ /* set the standard number */
+ if (!type) {
+ item[index] = value;
+ }
+
+ /*
+ * see rfc2426 section 3.3.1
+ */
+ else if (strstr(type, "TYPE=") == type){
+ if (strcasestr(type, "home")) {
+ item[index] = xstrdup(value);
+ }
+ if (strcasestr(type, "work")) {
+ item[index+1] = xstrdup(value);
+ }
+ if (strcasestr(type, "fax")) {
+ item[index+2] = xstrdup(value);
+ }
+ if (strcasestr(type, "cell")) {
+ item[index+3] = xstrdup(value);
+ }
+
+ xfree(type);
+ xfree(value);
+ }
+}
+
+static void
+vcard_parse_line(list_item item, char *line)
+{
+ int i;
+ char *key;
+
+ for(i=0; vcard_fields[i]; i++) {
+ key = vcard_fields[i];
+
+ if(!strncmp(key, line, strlen(key))) {
+ if(i == 1) {
+ vcard_parse_email(item, line);
+ }
+ else if(i == 2) {
+ vcard_parse_address(item, line);
+ }
+ else if(i == 8) {
+ vcard_parse_phone(item, line);
+ }
+ else {
+ item[i] = vcard_get_line_element(line, VCARD_VALUE);
+ }
+ break;
+ }
+ }
+}
+
+static void
+vcard_parse_item(FILE *in)
+{
+ char *line = NULL;
+ list_item item = item_create();
+
+ while(!feof(in)) {
+ line = getaline(in);
+
+ if(line && !strncmp("END:VCARD", line, 9)) {
+ xfree(line);
+ break;
+ }
+ else if(line) {
+ vcard_parse_line(item, line);
+ xfree(line);
+ }
+ }
+
+ add_item2database(item);
+ item_free(&item);
+}
+
+static int
+vcard_parse_file(FILE *in)
+{
+ char *line = NULL;
+
+ while(!feof(in)) {
+ line = getaline(in);
+
+ if(line && !strncmp("BEGIN:VCARD", line, 11)) {
+ xfree(line);
+ vcard_parse_item(in);
+ }
+ else if(line) {
+ xfree(line);
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * end of vCard import filter
+ */
+
/*
* csv addressbook export filters
*/
@@ -1605,15 +1885,46 @@ mutt_alias_export(FILE *out, struct db_enumerator e)
{
char email[MAX_EMAIL_LEN];
char *alias = NULL;
+ int email_addresses;
+ char *ptr;
db_enumerate_items(e) {
alias = mutt_alias_genalias(e.item);
get_first_email(email, e.item);
- fprintf(out, *email ? "alias %s %s <%s>\n": "alias %s %s%s\n",
- alias,
- db_name_get(e.item),
- email);
- xfree(alias);
+
+ /* do not output contacts without email address */
+ /* cause this does not make sense in mutt aliases */
+ if (*email) {
+
+ /* output first email address */
+ fprintf(out, "alias %s %s <%s>\n",
+ alias,
+ db_name_get(e.item),
+ email);
+
+ /* number of email addresses */
+ email_addresses = 1;
+ ptr = db_email_get(e.item);
+ while (*ptr != '\0') {
+ if (*ptr == ',') {
+ email_addresses++;
+ }
+ ptr++;
+ }
+
+ /* output other email addresses */
+ while (email_addresses-- > 1) {
+ roll_emails(e.item, ROTATE_RIGHT);
+ get_first_email(email, e.item);
+ fprintf(out, "alias %s__%s %s <%s>\n",
+ alias,
+ email,
+ db_name_get(e.item),
+ email);
+ }
+ roll_emails(e.item, ROTATE_RIGHT);
+ xfree(alias);
+ }
}
return 0;
@@ -1744,8 +2055,8 @@ text_export_database(FILE * out, struct db_enumerator e)
fprintf(out, "\n");
for(j = PHONE; j <= MOBILEPHONE; j++)
if(db_fget(e.item, j)) {
- get_field_keyname(field_id(j),
- NULL, &str);
+ get_field_info(field_id(j),
+ NULL, &str, NULL);
fprintf(out, "%s: %s\n", str,
db_fget(e.item, j));
}
@@ -1856,3 +2167,34 @@ wl_export_database(FILE *out, struct db_enumerator e)
* end of wanderlust addressbook export filter
*/
+/*
+ * BSD calendar export filter
+ */
+
+static int
+bsdcal_export_database(FILE *out, struct db_enumerator e)
+{
+ db_enumerate_items(e) {
+ int year, month = 0, day = 0;
+ char *anniversary = db_fget(e.item, ANNIVERSARY);
+
+ if(anniversary) {
+ if(!parse_date_string(anniversary, &day, &month, &year))
+ continue;
+
+ fprintf(out,
+ _("%02d/%02d\tAnniversary of %s\n"),
+ month,
+ day,
+ safe_str(db_name_get(e.item))
+ );
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * end of BSD calendar export filter
+ */
+
---|