+/*
+ * wanderlust addressbook export filter
+ */
+
+static int
+wl_export_database(FILE *out, struct db_enumerator e)
+{
+ char email[MAX_EMAIL_LEN];
+
+ fprintf(out, "# Wanderlust address book written by %s\n\n", PACKAGE);
+ db_enumerate_items(e) {
+ get_first_email(email, e.item);
+ if(*email) {
+ fprintf(out,
+ "%s\t\"%s\"\t\"%s\"\n",
+ email,
+ safe_str(db_fget(e.item, NICK)),
+ safe_str(db_name_get(e.item))
+ );
+ }
+ }
+
+ fprintf (out, "\n# End of address book file.\n");
+
+ return 0;
+}
+
+/*
+ * 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;
+}
+
+// see also enum field_types @database.h
+extern abook_field standard_fields[];
+static int
+find_field_enum(char *s) {
+ int i = -1;
+ while(standard_fields[++i].key)
+ if(!strcmp(standard_fields[i].key, s))
+ return i;
+ return -1;
+}
+
+/* Convert a string with named placeholders to
+ a *printf() compatible string.
+ Stores the abook field values into ft. */
+void
+parse_custom_format(char *s, char *fmt_string, enum field_types *ft)
+{
+ if(! fmt_string || ! ft) {
+ fprintf(stderr, _("parse_custom_format: fmt_string or ft not allocated\n"));
+ exit(EXIT_FAILURE);
+ }
+
+ char tmp[1] = { 0 };
+ char *p, *start, *field_name = NULL;
+ p = start = s;
+
+ while(*p) {
+ if(*p == '{') {
+ start = ++p;
+
+ if(! *start) goto cannotparse;
+ p = strchr(start, '}');
+ if(! p) goto cannotparse;
+ strcat(fmt_string, "%s");
+ field_name = strndup(start, (size_t)(p-start));
+ *ft = find_field_enum(field_name);
+ if(*ft == -1) {
+ fprintf(stderr, _("parse_custom_format: invalid placeholder: {%s}\n"), field_name);
+ exit(EXIT_FAILURE);
+ }
+
+ ft++;
+ start = ++p;
+ }
+
+ else if(*p == '\\') {
+ ++p;
+ if(! *p) tmp[0] = '\\'; // last char is a '\' ?
+ else if(*p == 'n') *tmp = '\n';
+ else if(*p == 't') *tmp = '\t';
+ else if(*p == 'r') *tmp = '\r';
+ else if(*p == 'v') *tmp = '\v';
+ else if(*p == 'b') *tmp = '\b';
+ else if(*p == 'a') *tmp = '\a';
+ else *tmp = *p;
+ strncat(fmt_string, tmp, 1);
+ start = ++p;
+ }
+
+ // if no '\' following: quick mode using strchr/strncat
+ else if(! strchr(start, '\\')) {
+ p = strchr(start, '{');
+ if(p) { // copy until the next placeholder
+ strncat(fmt_string, start, (size_t)(p-start));
+ start = p;
+ }
+ else { // copy till the end
+ strncat( fmt_string,
+ start,
+ FORMAT_STRING_LEN - strlen(fmt_string) - 1 );
+ break;
+ }
+ }
+
+ // otherwise character by character
+ else {
+ strncat(fmt_string, p, 1);
+ start = ++p;
+ }
+ }
+
+ *ft = ITEM_FIELDS;
+ return;
+
+ cannotparse:
+ fprintf(stderr, _("%s: invalid format, index %ld\n"), __FUNCTION__, (start - s));
+ free(fmt_string);
+ while(*ft) free(ft--);
+ exit(EXIT_FAILURE);
+}
+
+static int
+custom_export_item(FILE *out, int item, char *s, enum field_types *ft);
+
+
+// used to store the format string from --outformatstr when "custom" format is used
+// default value overriden in export_file()
+extern char *parsed_custom_format;
+extern enum field_types *custom_format_fields;
+
+/* wrapper for custom_export_item:
+ 1) avoid messing with extern pointer
+ 2) adds \n
+ 3) follow the prototype needed for an abook_output_item_filter entry */
+void
+custom_print_item(FILE *out, int item)
+{
+
+ if(custom_export_item(out, item, parsed_custom_format, custom_format_fields) == 0)
+ fprintf(out, "\n");
+}
+
+static int
+custom_export_item(FILE *out, int item, char *fmt, enum field_types *ft)
+{
+ char *p, *q = 0;
+
+ // if the first character is '!':
+ // we first check that all fields exist before continuing
+ if(*fmt == '!') {
+ enum field_types *ftp = ft;
+ while(*ft != ITEM_FIELDS) {
+ if(! db_fget(item, *ft) )
+ return 1;
+ ft++;
+ }
+ ft = ftp;
+ fmt++;
+ }
+
+ while (*fmt) {
+ if(!strncmp(fmt, "%s", 2)) {
+ fprintf(out, "%s", safe_str(db_fget(item, *ft)));
+ ft++;
+ fmt+=2;
+ } else if (*ft == ITEM_FIELDS) {
+ fprintf(out, "%s", fmt);
+ return 0;
+ } else {
+ p = strchr(fmt, '%');
+ if(*p) {
+ q = strndup(fmt, (size_t)(p-fmt));
+ fprintf(out, "%s", q);
+ free(q);
+ fmt = p;
+ }
+ else {
+ fprintf(out, "%s", fmt);
+ return 0;
+ }
+ }
+ }
+
+ return 0;
+}
+
+// used to store the format string from --outformatstr when "custom" format is used
+// default value overriden from abook.c
+extern char custom_format[FORMAT_STRING_LEN];
+
+static int
+custom_export_database(FILE *out, struct db_enumerator e)
+{
+ char *format_string =
+ (char *)malloc(FORMAT_STRING_LEN * sizeof(char*));
+
+ enum field_types *ft =
+ (enum field_types *)malloc(FORMAT_STRING_MAX_FIELDS * sizeof(enum field_types *));
+
+ parse_custom_format(custom_format, format_string, ft);
+
+ db_enumerate_items(e) {
+ if(custom_export_item(out, e.item, format_string, ft) == 0)
+ fprintf(out, "\n");
+ }
+ return 0;
+}
+
+/*
+ * end of BSD calendar export filter
+ */
+