/*
* $Id$
*
- * by JH <jheinonen@bigfoot.com>
+ * by JH <jheinonen@users.sourceforge.net>
*
* Copyright (C) Jaakko Heinonen
*/
#include "edit.h"
#include "misc.h"
#include "options.h"
+#include "getname.h"
static void init_abook();
static void set_filenames();
static void quit_mutt_query();
static void convert(char *srcformat, char *srcfile,
char *dstformat, char *dstfile);
+static void add_email(int);
char *datafile = NULL;
char *rcfile = NULL;
set_filenames();
init_options();
- signal(SIGINT, quit_abook);
signal(SIGKILL, quit_abook);
signal(SIGTERM, quit_abook);
{
if( options_get_int("autosave") )
save_database();
- else if( statusline_ask_boolean("Save database", FALSE) )
+ else if( statusline_ask_boolean("Save database", TRUE) )
save_database();
close_config();
if( !strcmp(argv[i], "--help") ) {
show_usage();
exit(1);
- } else
- if( !strcmp(argv[i], "--mutt-query") )
+ } else if( !strcmp(argv[i], "--mutt-query") ) {
mutt_query(argv[i + 1]);
- else
- if( !strcmp(argv[i], "--datafile") ) {
- if (argv[i+1]) {
+ } else if( !strcmp(argv[i], "--datafile") ) {
+ if (argc > i + 1 ) {
if (argv[i+1][0] != '/') {
char *cwd = my_getcwd();
datafile = strconcat(cwd, "/", argv[i+1], NULL);
show_usage();
exit(1);
}
- } else
- if( !strcmp(argv[i], "--convert") ) {
+ } else if( !strcmp(argv[i], "--convert") ) {
if( argc < 5 || argc > 6 ) {
fprintf(stderr, "incorrect number of argumets to make conversion\n");
fprintf(stderr, "try %s --help\n", argv[0]);
exit(1);
}
- if( argv[i+4] )
+ if( argc > i + 4 )
convert(argv[i+1], argv[i+2],
argv[i+3], argv[i+4]);
else
convert(argv[i+1], argv[i+2], argv[i+3], "-");
+ } else if( !strcmp(argv[i], "--add-email") ) {
+ add_email(0);
+ } else if( !strcmp(argv[i], "--add-email-quiet") ) {
+ add_email(1);
} else {
printf("option %s not recognized\n", argv[i]);
printf("try %s --help\n", argv[0]);
puts (" --datafile <filename> use an alternative addressbook file");
puts (" --mutt-query <string> make a query for mutt");
puts (" --convert <inputformat> <inputfile> "
- "<outputformat> <outputfile>");
+ "<outputformat> <outputfile>");
+ puts (" --add-email "
+ "read an e-mail message from stdin and\n"
+ " "
+ "add the sender to the addressbook");
+ puts (" --add-email-quiet "
+ "same as --add-email but doesn't\n"
+ " confirm adding");
putchar('\n');
puts ("available formats for --convert option:");
print_filters();
}
extern list_item *database;
-extern int items;
static void
-muttq_print_item(int item)
+muttq_print_item(FILE *file, int item)
{
char emails[MAX_EMAILS][MAX_EMAIL_LEN];
int i;
for(i = 0; i < (options_get_int("mutt_return_all_emails") ?
MAX_EMAILS : 1) ; i++)
if( *emails[i] )
- printf("%s\t%s\t%s\n", emails[i],
+ fprintf(file, "%s\t%s\t%s\n", emails[i],
database[item][NAME],
database[item][NOTES] == NULL ? " " :
database[item][NOTES]
);
}
-static int
-mutt_query_name(char *str)
-{
- int i, j;
- char *tmp;
-
- for(i = 0, j = 0 ; i < items; i++) {
- tmp = strdup(database[i][NAME]);
- if( strstr( strupper(tmp), strupper(str) ) != NULL ) {
- if( !j )
- putchar('\n');
- muttq_print_item(i);
- j++;
- }
- free(tmp);
- }
-
- return j;
-}
-
-static int
-mutt_query_email(char *str)
-{
- int i, j, k;
- char *tmp, emails[MAX_EMAILS][MAX_EMAIL_LEN];
-
- for(i = 0, j = 0; i < items; i++) {
- split_emailstr(i, emails);
- for(k = 0; k < MAX_EMAILS; k++) {
- if( *emails[k] ) {
- tmp = strdup( emails[k] );
- if( strstr( strupper(tmp), strupper(str) ) != NULL ) {
- if( !j )
- putchar('\n');
- j++;
- if( options_get_int("mutt_return_all_emails") ) {
- muttq_print_item(i);
- free(tmp);
- break;
- } else
- printf("%s\t%s\n", emails[k],
- database[i][NAME]);
- }
- free(tmp);
- }
- }
- }
-
- return j;
-}
-
static void
mutt_query(char *str)
{
- int i;
-
init_mutt_query();
if( str == NULL || !strcasecmp(str, "all") ) {
+ struct db_enumerator e = init_db_enumerator(ENUM_ALL);
printf("All items\n");
- for(i = 0; i < items; i++)
- muttq_print_item(i);
+ db_enumerate_items(e)
+ muttq_print_item(stdout, e.item);
} else {
- if( !mutt_query_name(str) && !mutt_query_email(str) ) {
+ int search_fields[] = {NAME, EMAIL, NICK, -1};
+ int i;
+ if( (i = find_item(str, 0, search_fields)) < 0 ) {
printf("Not found\n");
quit_mutt_query(1);
}
+ putchar('\n');
+ while(i >= 0) {
+ muttq_print_item(stdout, i);
+ i = find_item(str, i+1, search_fields);
+ }
}
quit_mutt_query(0);
}
-void
-launch_mutt()
+static char *
+make_mailstr(int item)
{
- int i;
char email[MAX_EMAIL_LEN];
- char *cmd;
- char *tmp = options_get_str("mutt_command");
+ char *ret;
+ char *name = mkstr("\"%s\"", database[item][NAME]);
- if( list_is_empty() )
- return;
+ get_first_email(email, item);
- cmd = strconcat(tmp, " '", NULL );
+ ret = *database[item][EMAIL] ?
+ mkstr("%s <%s>", name, email) :
+ strdup(name);
- for(i=0; i < items; i++) {
- if( ! is_selected(i) && i != list_current_item() )
- continue;
- get_first_email(email, i);
- tmp = mkstr("%s \"%s\"", cmd, database[i][NAME]);
- my_free(cmd);
- if( *database[i][EMAIL] ) {
- cmd = mkstr("%s <%s>", tmp, email);
+ free(name);
+
+ return ret;
+}
+
+void
+print_stderr(int item)
+{
+ fprintf (stderr, "%c", '\n');
+
+ if( is_valid_item(item) )
+ muttq_print_item(stderr, item);
+ else {
+ struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
+ db_enumerate_items(e) {
+ muttq_print_item(stderr, e.item);
+ }
+ }
+
+}
+
+void
+launch_mutt(int item)
+{
+ char *cmd = NULL, *mailstr = NULL;
+ char *mutt_command = options_get_str("mutt_command");
+
+ if(mutt_command == NULL || !*mutt_command)
+ return;
+
+ if( is_valid_item(item) )
+ mailstr = make_mailstr(item);
+ else {
+ struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
+ char *tmp = NULL;
+ db_enumerate_items(e) {
+ tmp = mailstr;
+ mailstr = tmp ?
+ strconcat(tmp, ",", make_mailstr(e.item), NULL):
+ strconcat(make_mailstr(e.item), NULL);
free(tmp);
- tmp = cmd;
}
- cmd = strconcat(tmp, " ", NULL);
- free(tmp);
}
- tmp = mkstr("%s%c", cmd, '\'');
- free(cmd);
- cmd = tmp;
+ cmd = strconcat(mutt_command, " \'", mailstr,
+ "\'", NULL);
+ free(mailstr);
#ifdef DEBUG
fprintf(stderr, "cmd: %s\n", cmd);
#endif
system(cmd);
-
free(cmd);
- refresh_screen();
+
+ /*
+ * we need to make sure that curses settings are correct
+ */
+ ui_init_curses();
}
void
-launch_lynx()
+launch_wwwbrowser(int item)
{
char *cmd = NULL;
- if( list_is_empty() )
+ if( !is_valid_item(item) )
return;
- if( database[list_current_item()][URL] )
+ if( database[item][URL] )
cmd = mkstr("%s '%s'",
options_get_str("www_command"),
- safe_str(database[list_current_item()][URL]));
+ safe_str(database[item][URL]));
else
return;
system(cmd);
free(cmd);
- refresh_screen();
+
+ /*
+ * we need to make sure that curses settings are correct
+ */
+ ui_init_curses();
}
void *
strlower(srcformat);
strlower(dstformat);
+#ifndef DEBUG
if( !strcmp(srcformat, dstformat) ) {
printf( "input and output formats are the same\n"
"exiting...\n");
exit(1);
}
+#endif
set_filenames();
init_options();
switch( import(srcformat, srcfile) ) {
case -1:
- printf("input format %s not supported\n", srcformat);
+ fprintf(stderr,
+ "input format %s not supported\n", srcformat);
ret = 1;
case 1:
- printf("cannot read file %s\n", srcfile);
+ fprintf(stderr, "cannot read file %s\n", srcfile);
ret = 1;
}
if(!ret)
switch( export(dstformat, dstfile) ) {
case -1:
- printf("output format %s not supported\n",
- dstformat);
+ fprintf(stderr,
+ "output format %s not supported\n",
+ dstformat);
ret = 1;
break;
case 1:
- printf("cannot write file %s\n", dstfile);
+ fprintf(stderr,
+ "cannot write file %s\n", dstfile);
ret = 1;
break;
}
exit(ret);
}
+/*
+ * --add-email handling
+ */
+
+static int add_email_count = 0;
+static void
+quit_add_email()
+{
+ if(add_email_count > 0) {
+ if(save_database() < 0) {
+ fprintf(stderr, "cannot open %s\n", datafile);
+ exit(1);
+ }
+ printf("%d item(s) added to %s\n", add_email_count, datafile);
+ } else {
+ puts("Valid sender address not found");
+ }
+
+ exit(0);
+}
+
+static void
+quit_add_email_sig(int signal)
+{
+ quit_add_email();
+}
+
+static void
+init_add_email()
+{
+ set_filenames();
+ atexit(free_filenames);
+ init_options();
+ atexit(close_config);
+
+ /*
+ * we don't actually care if loading fails or not
+ */
+ load_database(datafile);
+
+ atexit(close_database);
+
+ signal(SIGINT, quit_add_email_sig);
+}
+
+static int
+add_email_add_item(int quiet, char *name, char *email)
+{
+ list_item item;
+
+ if(!quiet) {
+ FILE *in = fopen("/dev/tty", "r");
+ char c;
+ if(!in) {
+ fprintf(stderr, "cannot open /dev/tty\n"
+ "you may want to use --add-email-quiet\n");
+ exit(1);
+ }
+ printf("Add ``%s <%s>'' to %s ? (y/n)\n",
+ name,
+ email,
+ datafile
+ );
+ do {
+ c = fgetc(in);
+ if(c == 'n' || c == 'N') {
+ fclose(in);
+ return 0;
+ }
+ } while(c != 'y' && c != 'Y');
+ fclose(in);
+ }
+
+ memset(item, 0, sizeof(item));
+ item[NAME] = strdup(name);
+ item[EMAIL] = strdup(email);
+ add_item2database(item);
+
+ return 1;
+}
+
+static void
+add_email(int quiet)
+{
+ char *line;
+ char *name = NULL, *email = NULL;
+
+ init_add_email();
+
+ do {
+ line = getaline(stdin);
+ if(line && !strncasecmp("From:", line, 5) ) {
+ getname(line, &name, &email);
+ add_email_count += add_email_add_item(quiet,
+ name, email);
+ my_free(name);
+ my_free(email);
+ }
+ my_free(line);
+ } while( !feof(stdin) );
+
+ quit_add_email();
+}
+
+/*
+ * end of --add-email handling
+ */