/*
* $Id$
*
- * by JH <jheinonen@bigfoot.com>
+ * by JH <jheinonen@users.sourceforge.net>
*
* Copyright (C) Jaakko Heinonen
*/
#include <string.h>
#include <ctype.h>
#include <pwd.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include "abook_curses.h"
#include "filter.h"
#include "list.h"
#include "misc.h"
#include "options.h"
+#include <assert.h>
extern int items;
extern list_item *database;
static int ldif_parse_file(FILE *handle);
static int mutt_parse_file(FILE *in);
static int pine_parse_file(FILE *in);
+static int csv_parse_file(FILE *in);
/*
* export filter prototypes
struct abook_input_filter i_filters[] = {
{ "abook", "abook native format", parse_database },
{ "ldif", "ldif / Netscape addressbook", ldif_parse_file },
- { "mutt", "mutt alias (beta)", mutt_parse_file },
+ { "mutt", "mutt alias", mutt_parse_file },
{ "pine", "pine addressbook", pine_parse_file },
+ { "csv", "comma separated values", csv_parse_file },
{ "\0", NULL, NULL }
};
pwent = getpwnam(username);
- if( (tmp = malloc(strlen(pwent->pw_gecos) +1)) == NULL)
+ if( (tmp = (char *)malloc(strlen(pwent->pw_gecos) +1)) == NULL)
return strdup(username);
rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp);
mvaddstr(5+filter, 2, "->");
- filename = ask_filename("Filename: ", 1);
+ filename = ask_filename("Filename: ");
if( !filename ) {
refresh_screen();
return 2;
if( i_read_file(filename, i_filters[filter].func ) )
statusline_msg("Error occured while opening the file");
- else
- if( tmp == items )
+ else if( tmp == items )
statusline_msg("Hmm.., file seems not to be a valid file");
refresh_screen();
FILE *in;
int ret = 0;
- if( ( in = fopen( filename, "r" ) ) == NULL )
+ if( ( in = abook_fopen( filename, "r" ) ) == NULL )
return 1;
ret = (*func) (in);
}
int
-import(char filtname[FILTNAME_LEN], char *filename)
+import_file(char filtname[FILTNAME_LEN], char *filename)
{
int i;
int tmp = items;
int ret = 0;
for(i=0;; i++) {
- if( ! strncmp(i_filters[i].filtname, filtname, FILTNAME_LEN) )
+ if( ! strncasecmp(i_filters[i].filtname, filtname,
+ FILTNAME_LEN) )
break;
if( ! *i_filters[i].filtname ) {
i = -1;
if( i<0 )
return -1;
- if( !strcmp(filename, "-") )
- ret = (*i_filters[i].func) (stdin);
- else
+ if( !strcmp(filename, "-") ) {
+ struct stat s;
+ if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
+ ret = 1;
+ else
+ ret = (*i_filters[i].func) (stdin);
+ } else
ret = i_read_file(filename, i_filters[i].func);
if( tmp == items )
filter = getch() - 'a';
if(filter == 'x' - 'a' ||
- filter >= number_of_output_filters(e_filters) || filter < 0) {
+ filter >= number_of_output_filters() || filter < 0) {
refresh_screen();
return 1;
}
clear_statusline();
}
- filename = ask_filename("Filename: ", 0);
+ filename = ask_filename("Filename: ");
if( !filename ) {
refresh_screen();
return 2;
struct db_enumerator e = init_db_enumerator(enum_mode);
for(i=0;; i++) {
- if( ! strncmp(e_filters[i].filtname, filtname, FILTNAME_LEN) )
+ if( ! strncasecmp(e_filters[i].filtname, filtname,
+ FILTNAME_LEN) )
break;
if( ! *e_filters[i].filtname ) {
i = -1;
int
-export(char filtname[FILTNAME_LEN], char *filename)
+export_file(char filtname[FILTNAME_LEN], char *filename)
{
const int mode = ENUM_ALL;
int i;
struct db_enumerator e = init_db_enumerator(mode);
for(i=0;; i++) {
- if( ! strncmp(e_filters[i].filtname, filtname, FILTNAME_LEN) )
+ if( ! strncasecmp(e_filters[i].filtname, filtname,
+ FILTNAME_LEN) )
break;
if( ! *e_filters[i].filtname ) {
i = -1;
static void ldif_fix_string(char *str);
-#ifndef LINESIZE
-# define LINESIZE 1024
-#endif
-
-#define LDIF_ITEM_FIELDS 15
+#define LDIF_ITEM_FIELDS 16
typedef char* ldif_item[LDIF_ITEM_FIELDS];
"cn",
"mail",
"streetaddress",
+ "streetaddress2",
"locality",
"st",
"postalcode",
NAME, /* "cn" */
EMAIL, /* "mail" */
ADDRESS, /* "streetaddress" */
+ ADDRESS2, /* "streetaddress2" */
CITY, /* "locality" */
STATE, /* "st" */
ZIP, /* "postalcode" */
static char *
ldif_read_line(FILE *in)
{
- char line[LINESIZE];
- char *buf=NULL;
+ char *buf = NULL;
char *ptr, *tmp;
long pos;
int i;
- for(i=1;;i++) {
+ for(i = 1;;i++) {
+ char *line;
+
pos = ftell(in);
- fgets(line, LINESIZE, in);
+ line = getaline(in);
- if( feof(in) )
+ if( feof(in) || !line )
break;
if(i == 1) {
- buf = strdup(line);
+ buf = line;
continue;
}
if(*line != ' ') {
fseek(in, pos, SEEK_SET);
+ free(line);
break;
}
- ptr = (char *)&line;
+ ptr = line;
while( *ptr == ' ')
ptr++;
tmp = buf;
buf = strconcat(buf, ptr, NULL);
free(tmp);
+ free(line);
}
- if( *buf == '#' ) {
+ if(buf && *buf == '#' ) {
free(buf);
return NULL;
}
- if(buf) {
- int i,j;
- for(i=0,j=0; j < strlen(buf); i++, j++)
- buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
- }
-
return buf;
}
{
int i, j;
- for( i = 0, j = 0; j < strlen(str); i++, j++)
+ for( i = 0, j = 0; j < (int)strlen(str); i++, j++)
str[i] = ( str[j] == (char)0xc3 ?
(char) str[++j] + (char) 0x40 :
str[j] );
* mutt alias import filter
*/
-enum {
- MUTT_ALIAS,
- MUTT_NAME,
- MUTT_EMAIL
-};
-
-static void
-remove_newlines(char *buf)
-{
- int i,j;
-
- for(i=0,j=0; j < strlen(buf); i++, j++)
- buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
-}
+#include "getname.h"
static int
mutt_read_line(FILE *in, char **alias, char **rest)
{
- char line[LINESIZE];
- char *ptr=(char *)&line;
- char *tmp;
+ char *line, *ptr, *tmp;
- fgets(line, LINESIZE, in);
- remove_newlines(line);
+ if( !(line = ptr = getaline(in)) )
+ return 1; /* error / EOF */
while( ISSPACE(*ptr) )
ptr++;
if( strncmp("alias", ptr, 5) ) {
+ free(line);
return 1;
}
while( ! ISSPACE(*ptr) )
ptr++;
- if( (*alias = malloc(ptr-tmp+1)) == NULL)
+ if( (*alias = (char *)malloc(ptr-tmp+1)) == NULL) {
+ free(line);
return 1;
+ }
strncpy(*alias, tmp, ptr-tmp);
- *(*alias+(ptr-tmp)) = 0;
+ *(*alias + (ptr - tmp)) = 0;
while( ISSPACE(*ptr) )
ptr++;
*rest = strdup(ptr);
-
+
+ free(line);
return 0;
}
static void
-mutt_parse_email(char *mutt_item[3])
+mutt_parse_email(list_item item)
{
- char *tmp;
- int i;
-
- if( (tmp = strchr(mutt_item[MUTT_NAME], '<')) )
- *tmp = 0;
- else
- return;
-
- mutt_item[MUTT_EMAIL] = strdup(tmp+1);
-
- if( (tmp = strchr(mutt_item[MUTT_EMAIL], '>')) )
- *tmp = 0;
-
- tmp = mutt_item[MUTT_NAME];
-
- for(i=strlen(tmp)-1; i>0; i--)
- if(ISSPACE(tmp[i]))
- tmp[i] = 0;
- else
- break;
-
- mutt_item[MUTT_NAME] = strdup(tmp);
+ char *line = item[NAME];
+ char *start = line, *tmp;
+ char *name, *email;
+ int i = 0;
+ tmp = strconcat("From: ", line, NULL);
+ getname(tmp, &name, &email);
free(tmp);
-}
-
-static void
-mutt_add_mutt_item(char *mutt_item[3])
-{
- list_item abook_item;
- memset(abook_item, 0, sizeof(abook_item));
-
- abook_item[NAME] = safe_strdup(mutt_item[MUTT_NAME]);
- abook_item[EMAIL] = safe_strdup(mutt_item[MUTT_EMAIL]);
- abook_item[NICK] = safe_strdup(mutt_item[MUTT_ALIAS]);
-
- add_item2database(abook_item);
+ if(name)
+ item[NAME] = name;
+ else
+ return;
+ if(email)
+ item[EMAIL] = email;
+ else
+ return;
+
+ while( (start = strchr(start, ',')) && i++ < MAX_EMAILS - 1) {
+ tmp = strconcat("From: ", ++start, NULL);
+ getname(tmp, &name, &email);
+ free(tmp);
+ free(name);
+ if(email) {
+ if(*email) {
+ tmp = strconcat(item[EMAIL], ",", email, NULL);
+ free(item[EMAIL]);
+ item[EMAIL] = tmp;
+ } else {
+ my_free(email);
+ }
+ }
+ }
}
static int
mutt_parse_file(FILE *in)
{
- char *mutt_item[3];
+ list_item item;
for(;;) {
-
- memset(mutt_item, 0, sizeof(mutt_item) );
+ memset(item, 0, sizeof(item));
- if( mutt_read_line(in, &mutt_item[MUTT_ALIAS],
- &mutt_item[MUTT_NAME]) )
- continue;
-
- mutt_parse_email(mutt_item);
+ if( !mutt_read_line(in, &item[NICK],
+ &item[NAME]) )
+ mutt_parse_email(item);
if( feof(in) ) {
- free(mutt_item[MUTT_ALIAS]);
- free(mutt_item[MUTT_NAME]);
- free(mutt_item[MUTT_EMAIL]);
+ free_list_item(item);
break;
}
- mutt_add_mutt_item(mutt_item);
-
- free(mutt_item[MUTT_ALIAS]);
- free(mutt_item[MUTT_NAME]);
- free(mutt_item[MUTT_EMAIL]);
+ add_item2database(item);
}
-
return 0;
}
static void html_export_write_head(FILE *out, int extra_column);
static void html_export_write_tail(FILE *out);
+extern int extra_column;
+
static int
html_export_database(FILE *out, struct db_enumerator e)
{
char tmp[MAX_EMAILSTR_LEN];
- int extra_column = options_get_int("extra_column");
if( items < 1 )
return 2;
db_enumerate_items(e) {
get_first_email(tmp, e.item);
- fprintf(out, "<tr>\n<td><a href=\"mailto:%s\">%s</a>\n",
- tmp,
- database[e.item][NAME] );
-
+ if (*tmp)
+ fprintf(out, "<tr>\n<td><a href=\"mailto:%s\">%s</a>\n",
+ tmp,
+ database[e.item][NAME] );
+ else
+ fprintf(out, "<tr>\n<td>%s\n",
+ database[e.item][NAME] );
+
fprintf(out, "<td>%s\n<td>%s\n",
database[e.item][EMAIL],
safe_str(database[e.item][extra_column]) );
{
char *realname = get_real_name();
- fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
- fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>",
- realname );
+ fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
+ fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>", realname );
fprintf(out, "\n</head>\n<body>\n");
fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
fprintf(out, "<br><br>\n\n");
- fprintf(out, "<center><table border>\n");
+ fprintf(out, "<table border=\"1\" align=\"center\">\n");
fprintf(out, "\n<tr><th>Name<th>E-mail address(es)<th>%s</tr>\n\n",
abook_fields[extra_column].name);
static void
html_export_write_tail(FILE *out)
{
- fprintf(out, "\n</table></center>\n");
+ fprintf(out, "\n</table>\n");
fprintf(out, "\n</body>\n</html>\n");
}
{
int i,j;
- for(i=0,j=0; j < strlen(buf); i++, j++)
+ for(i=0,j=0; j < (int)strlen(buf); i++, j++)
buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
}
*tmp=0;
for(i=1; ( tmp = strchr(s, ',') ) != NULL ; i++, s=tmp+1 )
- if( i > 3 ) {
+ if( i > MAX_EMAILS - 1 ) {
*tmp = 0;
break;
}
len = last ? strlen(start) : (int) (end-start);
len = min(len, 400-1);
- if(i < sizeof(pine_conv_table) / sizeof(*pine_conv_table)
+ if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
&& pine_conv_table[i] >= 0) {
strncpy(tmp, start, len);
tmp[len] = 0;
pine_parse_file(FILE *in)
{
char line[LINESIZE];
- char *buf=NULL;
+ char *buf = NULL;
char *ptr;
int i;
fgets(line, LINESIZE, in);
while(!feof(in)) {
- for(i=2;;i++) {
- buf = realloc(buf, i*LINESIZE);
- if(i==2)
+ for(i = 2;;i++) {
+ buf = (char *) realloc(buf, i*LINESIZE);
+ if(i == 2)
strcpy(buf, line);
fgets(line, LINESIZE, in);
ptr=(char *)&line;
if(*ptr != ' ' || feof(in) )
break;
else
- while( *ptr == ' ') ptr++;
+ while( *ptr == ' ')
+ ptr++;
strcat(buf, ptr);
}
- if( *buf == '#' )
+ if( *buf == '#' ) {
+ my_free(buf);
continue;
+ }
pine_fixbuf(buf);
pine_parse_buf(buf);
*/
+/*
+ * csv import filter
+ */
+
+/* FIXME
+ * these files should be parsed according to a certain
+ * lay out, or the default if layout is not given, at
+ * the moment only default is done...
+ */
+
+#define CSV_COMMENT_CHAR '#'
+
+static int csv_conv_table[] = {
+ NAME,
+ EMAIL,
+ PHONE,
+ NOTES,
+ NICK
+};
+
+static void
+csv_convert_emails(char *s)
+{
+ int i;
+ char *tmp;
+
+ if( s == NULL )
+ return;
+
+ for(i=1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1 )
+ if( i > MAX_EMAILS - 1 ) {
+ *tmp = 0;
+ break;
+ }
+
+}
+
+static char *
+csv_remove_quotes(char *s)
+{
+ char *copy, *trimmed;
+ int len;
+
+ copy = trimmed = strdup(s);
+ strtrim(trimmed);
+
+ len = strlen(trimmed);
+ if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
+ if(len < 3) {
+ my_free(copy);
+ return NULL;
+ }
+ trimmed[len - 1] = 0;
+ trimmed++;
+ trimmed = strdup(trimmed);
+ free(copy);
+ return trimmed;
+ }
+
+ my_free(copy);
+ return strdup(s);
+}
+
+static void
+csv_store_field(list_item item, char *s, int field)
+{
+ char *newstr = NULL;
+
+ if(!s || !*s)
+ return;
+
+ if( !(newstr = csv_remove_quotes(s)) )
+ return;
+
+ if(field < (int)(sizeof(csv_conv_table) / sizeof(*csv_conv_table))
+ && csv_conv_table[field] >= 0) {
+ item[csv_conv_table[field]] = newstr;
+ } else {
+ my_free(newstr);
+ }
+}
+
+static int
+csv_is_valid_quote_end(char *p)
+{
+ if(*p != '\"')
+ return FALSE;
+
+ for(p++; *p; p++) {
+ if(*p == ',')
+ return TRUE;
+ else if(!ISSPACE(*p))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static int
+csv_is_valid_quote_start(char *p)
+{
+ for(; *p; p++) {
+ if(*p == '\"')
+ return TRUE;
+ else if(!ISSPACE(*p))
+ return FALSE;
+ }
+
+ return FALSE;
+}
+
+static void
+csv_parse_line(char *line)
+{
+ char *p, *start;
+ int field;
+ bool in_quote = FALSE;
+ list_item item;
+
+ memset(item, 0, sizeof(item));
+
+ for(p = start = line, field = 0; *p; p++) {
+ if(in_quote) {
+ if(csv_is_valid_quote_end(p))
+ in_quote = FALSE;
+ } else {
+ if ( (((p - start) / sizeof (char)) < 2 ) &&
+ csv_is_valid_quote_start(p) )
+ in_quote = TRUE;
+ }
+
+ if( *p == ',' && !in_quote) {
+ *p = 0;
+ csv_store_field(item, start, field);
+ field++;
+ start = p + 1;
+ }
+ }
+ /*
+ * store last field
+ */
+ csv_store_field(item, start, field);
+
+ csv_convert_emails(item[EMAIL]);
+ add_item2database(item);
+}
+
+
+static int
+csv_parse_file(FILE *in)
+{
+ char *line = NULL;
+
+ while(!feof(in)) {
+ line = getaline(in);
+
+ if(line && *line && *line != CSV_COMMENT_CHAR)
+ csv_parse_line(line);
+
+ my_free(line);
+ }
+
+ return 0;
+}
+
+/*
+ * end of csv import filter
+ */
+
/*
* csv addressbook export filter
*/
EMAIL,
PHONE,
NOTES,
+ NICK,
-1
};
db_enumerate_items(e) {
for(j = 0; csv_export_fields[j] >= 0; j++) {
- fprintf(out, strchr(safe_str(database[e.item][csv_export_fields[j]]), ',') ?
+ fprintf(out,(
+ strchr(safe_str(database[e.item][csv_export_fields[j]]), ',') ||
+ strchr(safe_str(database[e.item][csv_export_fields[j]]), '\"')
+ ) ?
"\"%s\"" : "%s",
safe_str(database[e.item][csv_export_fields[j]])
);
free(name);
if ( database[e.item][ADDRESS] )
- fprintf(out, "ADR:;;%s;%s;%s;%s;%s\n",
+ fprintf(out, "ADR:;;%s;%s;%s;%s;%s;%s\n",
safe_str(database[e.item][ADDRESS]),
+ safe_str(database[e.item][ADDRESS2]),
safe_str(database[e.item][CITY]),
safe_str(database[e.item][STATE]),
safe_str(database[e.item][ZIP]),
* printable export filter
*/
+
+static void
+text_write_address_us(FILE *out, int i) {
+ fprintf(out, "\n%s", database[i][ADDRESS]);
+
+ if (database[i][ADDRESS2])
+ fprintf(out, "\n%s", database[i][ADDRESS2]);
+
+ if (database[i][CITY])
+ fprintf(out, "\n%s", database[i][CITY]);
+
+ if (database[i][STATE] || database[i][ZIP]) {
+ fputc('\n', out);
+
+ if(database[i][STATE]) {
+ fprintf(out, "%s", database[i][STATE]);
+ if(database[i][ZIP])
+ fputc(' ', out);
+ }
+
+ if(database[i][ZIP])
+ fprintf(out, "%s", database[i][ZIP]);
+ }
+
+ if (database[i][COUNTRY])
+ fprintf(out, "\n%s", database[i][COUNTRY]);
+}
+
+
+static void
+text_write_address_uk(FILE *out, int i) {
+ int j;
+
+ for (j = ADDRESS; j <= COUNTRY; j++)
+ if (database[i][j])
+ fprintf(out, "\n%s", database[i][j]);
+}
+
+static void
+text_write_address_eu(FILE *out, int i) {
+ fprintf(out, "\n%s", database[i][ADDRESS]);
+
+ if (database[i][ADDRESS2])
+ fprintf(out, "\n%s", database[i][ADDRESS2]);
+
+ if (database[i][ZIP] || database[i][CITY]) {
+ fputc('\n', out);
+
+ if(database[i][ZIP]) {
+ fprintf(out, "%s", database[i][ZIP]);
+ if(database[i][CITY])
+ fputc(' ', out);
+ }
+
+ if(database[i][CITY])
+ fprintf(out, "%s", database[i][CITY]);
+ }
+
+ if (database[i][STATE])
+ fprintf(out, "\n%s", database[i][STATE]);
+
+ if (database[i][COUNTRY])
+ fprintf(out, "\n%s", database[i][COUNTRY]);
+}
+
static int
text_export_database(FILE * out, struct db_enumerator e)
{
char emails[MAX_EMAILS][MAX_EMAIL_LEN];
int j;
char *realname = get_real_name();
- char *style = options_get_str("address_style");
+ char *style = opt_get_str(STR_ADDRESS_STYLE);
fprintf(out,
"-----------------------------------------\n%s's address book\n"
}
/* Print address */
if (database[e.item][ADDRESS]) {
- if (!safe_strcmp(style, "us")) { /* US like */
- fprintf(out, "\n%s", database[e.item][ADDRESS]);
- if (database[e.item][CITY])
- fprintf(out, "\n%s",
- database[e.item][CITY]);
- if (database[e.item][STATE] || database[e.item][ZIP])
- fprintf(out, "\n%s %s",
- safe_str(database[e.item]
- [STATE]),
- safe_str(database[e.item]
- [ZIP]));
- if (database[e.item][COUNTRY])
- fprintf(out, "\n%s",
- database[e.item][COUNTRY]);
- }
-
- else if (!safe_strcmp(style, "uk")) { /* UK like */
- for (j = ADDRESS; j <= COUNTRY; j++)
- if (database[e.item][j])
- fprintf(out, "\n%s",
- database[e.item][j]);
- } else { /* EU like */
- fprintf(out, "\n%s", database[e.item][ADDRESS]);
- if (database[e.item][ZIP] ||
- database[e.item][CITY])
- fprintf(out, "\n%s %s",
- safe_str(database[e.item][ZIP]),
- safe_str(database[e.item]
- [CITY]));
- if (database[e.item][STATE])
- fprintf(out, "\n%s",
- database[e.item][STATE]);
- if (database[e.item][COUNTRY])
- fprintf(out, "\n%s",
- database[e.item][COUNTRY]);
- }
+ if (!safe_strcmp(style, "us")) /* US like */
+ text_write_address_us(out, e.item);
+ else if (!safe_strcmp(style, "uk")) /* UK like */
+ text_write_address_uk(out, e.item);
+ else /* EU like */
+ text_write_address_eu(out, e.item);
fprintf(out, "\n");
}