4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
21 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
39 static void init_abook();
40 static void quit_abook_sig(int i);
41 static void set_filenames();
42 static void parse_command_line(int argc, char **argv);
43 static void show_usage();
44 static void mutt_query(char *str);
45 static void init_mutt_query();
46 static void convert(char *srcformat, char *srcfile,
47 char *dstformat, char *dstfile);
48 static void add_email(int);
50 char *datafile = NULL;
51 static char *rcfile = NULL;
53 bool alternative_datafile = FALSE;
54 bool alternative_rcfile = FALSE;
62 assert(datafile != NULL);
64 if( (f = fopen(datafile, "a")) == NULL)
73 check_abook_directory()
78 assert(!is_ui_initialized());
80 if(alternative_datafile)
83 dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
86 if(stat(dir, &s) == -1) {
92 if(mkdir(dir, 0700) == -1) {
93 printf(_("Cannot create directory %s\n"), dir);
98 } else if(!S_ISDIR(s.st_mode)) {
99 printf(_("%s is not a directory\n"), dir);
108 xmalloc_error_handler(int err)
111 * We don't try to save addressbook here because we don't know
112 * if it's fully loaded to memory.
114 if(is_ui_initialized())
117 fprintf(stderr, _("Memory allocation failure: %s\n"), strerror(err));
125 check_abook_directory();
127 if(load_opts(rcfile) > 0) {
128 printf(_("Press enter to continue...\n"));
131 init_default_views();
133 signal(SIGTERM, quit_abook_sig);
138 umask(DEFAULT_UMASK);
140 if(!datafile_writeable()) {
141 char *s = strdup_printf(_("File %s is not writeable"), datafile);
145 if(load_database(datafile) || !statusline_ask_boolean(
146 _("If you continue all changes will "
147 "be lost. Do you want to continue?"), FALSE)) {
149 /*close_database();*/
154 load_database(datafile);
160 quit_abook(int save_db)
163 if(opt_get_bool(BOOL_AUTOSAVE))
165 else if(statusline_ask_boolean(_("Save database"), TRUE))
167 } else if(!statusline_ask_boolean(_("Quit without saving"), FALSE))
179 quit_abook_sig(int i)
181 quit_abook(QUIT_SAVE);
185 main(int argc, char **argv)
187 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
188 setlocale(LC_ALL, "");
191 bindtextdomain(PACKAGE, LOCALEDIR);
194 xmalloc_set_error_handler(xmalloc_error_handler);
196 prepare_database_internals();
198 parse_command_line(argc, argv);
204 quit_abook(QUIT_SAVE);
222 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
223 fprintf(stderr,_("%s is not a valid HOME directory\n"), getenv("HOME") );
228 datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
232 rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
235 atexit(free_filenames);
245 MODE_ADD_EMAIL_QUIET,
251 change_mode(int *current, int mode)
253 if(*current != MODE_CONT) {
254 fprintf(stderr, _("Cannot combine options --mutt-query, "
256 "--add-email or --add-email-quiet\n"));
264 set_filename(char **var, char *path)
269 assert(*var == NULL); /* or else we probably leak memory */
270 assert(path != NULL);
273 *var = xstrdup(path);
279 *var = strconcat(cwd, "/", path, NULL);
284 #define set_convert_var(X) do { if(mode != MODE_CONVERT) {\
285 fprintf(stderr, _("please use option --%s after --convert option\n"),\
286 long_options[option_index].name);\
293 parse_command_line(int argc, char **argv)
295 int mode = MODE_CONT;
296 char *query_string = NULL;
297 char *informat = "abook",
304 int option_index = 0;
316 static struct option long_options[] = {
317 { "help", 0, 0, 'h' },
318 { "add-email", 0, 0, OPT_ADD_EMAIL },
319 { "add-email-quiet", 0, 0, OPT_ADD_EMAIL_QUIET },
320 { "datafile", 1, 0, 'f' },
321 { "mutt-query", 1, 0, OPT_MUTT_QUERY },
322 { "config", 1, 0, 'C' },
323 { "convert", 0, 0, OPT_CONVERT },
324 { "informat", 1, 0, OPT_INFORMAT },
325 { "outformat", 1, 0, OPT_OUTFORMAT },
326 { "infile", 1, 0, OPT_INFILE },
327 { "outfile", 1, 0, OPT_OUTFILE },
328 { "formats", 0, 0, OPT_FORMATS },
332 c = getopt_long(argc, argv, "hC:",
333 long_options, &option_index);
343 change_mode(&mode, MODE_ADD_EMAIL);
345 case OPT_ADD_EMAIL_QUIET:
346 change_mode(&mode, MODE_ADD_EMAIL_QUIET);
349 set_filename(&datafile, optarg);
350 alternative_datafile = TRUE;
353 query_string = optarg;
354 change_mode(&mode, MODE_QUERY);
357 set_filename(&rcfile, optarg);
358 alternative_rcfile = TRUE;
361 change_mode(&mode, MODE_CONVERT);
364 set_convert_var(informat);
367 set_convert_var(outformat);
370 set_convert_var(infile);
373 set_convert_var(outfile);
384 fprintf(stderr, _("%s: unrecognized arguments on command line\n"),
392 case MODE_ADD_EMAIL_QUIET:
395 mutt_query(query_string);
397 convert(informat, infile, outformat, outfile);
405 puts (PACKAGE " v " VERSION "\n");
406 puts (_(" -h --help show usage"));
407 puts (_(" -C --config <file> use an alternative configuration file"));
408 puts (_(" --datafile <file> use an alternative addressbook file"));
409 puts (_(" --mutt-query <string> make a query for mutt"));
410 puts (_(" --add-email "
411 "read an e-mail message from stdin and\n"
413 "add the sender to the addressbook"));
414 puts (_(" --add-email-quiet "
415 "same as --add-email but doesn't\n"
416 " require to confirm adding"));
418 puts (_(" --convert convert address book files"));
419 puts (_(" options to use with --convert:"));
420 puts (_(" --informat <format> format for input file"));
421 puts (_(" (default: abook)"));
422 puts (_(" --infile <file> source file"));
423 puts (_(" (default: stdin)"));
424 puts (_(" --outformat <format> format for output file"));
425 puts (_(" (default: text)"));
426 puts (_(" --outfile <file> destination file"));
427 puts (_(" (default: stdout)"));
428 puts (_(" --formats list available formats"));
437 quit_mutt_query(int status)
446 muttq_print_item(FILE *file, int item)
448 abook_list *emails, *e;
449 char *tmp = db_email_get(item);
451 emails = csv_to_abook_list(tmp);
454 for(e = emails; e; e = e->next) {
455 fprintf(file, "%s\t%s\t%s\n", e->data, db_name_get(item),
456 !db_fget(item, NOTES) ?" " :db_fget(item, NOTES)
458 if(!opt_get_bool(BOOL_MUTT_RETURN_ALL_EMAILS))
461 abook_list_free(&emails);
465 mutt_query(char *str)
469 if( str == NULL || !strcasecmp(str, "all") ) {
470 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
471 printf("All items\n");
472 db_enumerate_items(e)
473 muttq_print_item(stdout, e.item);
475 int search_fields[] = {NAME, EMAIL, NICK, -1};
477 if( (i = find_item(str, 0, search_fields)) < 0 ) {
478 printf("Not found\n");
479 quit_mutt_query(EXIT_FAILURE);
483 muttq_print_item(stdout, i);
484 i = find_item(str, i + 1, search_fields);
488 quit_mutt_query(EXIT_SUCCESS);
498 if( load_database(datafile) ) {
499 printf(_("Cannot open database\n"));
500 quit_mutt_query(EXIT_FAILURE);
507 make_mailstr(int item)
509 char email[MAX_EMAIL_LEN];
511 char *name = strdup_printf("\"%s\"", db_name_get(item));
513 get_first_email(email, item);
516 strdup_printf("%s <%s>", name, email) :
525 print_stderr(int item)
527 fprintf (stderr, "%c", '\n');
529 if( is_valid_item(item) )
530 muttq_print_item(stderr, item);
532 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
533 db_enumerate_items(e) {
534 muttq_print_item(stderr, e.item);
541 launch_mutt(int item)
543 char *cmd = NULL, *mailstr = NULL;
544 char *mutt_command = opt_get_str(STR_MUTT_COMMAND);
546 if(mutt_command == NULL || !*mutt_command)
549 if( is_valid_item(item) )
550 mailstr = make_mailstr(item);
552 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
554 db_enumerate_items(e) {
557 strconcat(tmp, ",", make_mailstr(e.item), NULL):
558 strconcat(make_mailstr(e.item), NULL);
563 cmd = strconcat(mutt_command, " \'", mailstr, "\'", NULL);
566 fprintf(stderr, "cmd: %s\n", cmd);
572 * we need to make sure that curses settings are correct
578 launch_wwwbrowser(int item)
582 if( !is_valid_item(item) )
585 if(db_fget(item, URL))
586 cmd = strdup_printf("%s '%s'",
587 opt_get_str(STR_WWW_COMMAND),
588 safe_str(db_fget(item, URL)));
598 * we need to make sure that curses settings are correct
604 abook_fopen (const char *path, const char *mode)
609 stat_ok = (stat(path, &s) != -1);
611 if(strchr(mode, 'r'))
612 return (stat_ok && S_ISREG(s.st_mode)) ?
613 fopen(path, mode) : NULL;
615 return (stat_ok && S_ISDIR(s.st_mode)) ?
616 NULL : fopen(path, mode);
620 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
624 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
625 fprintf(stderr, _("too few arguments to make conversion\n"));
626 fprintf(stderr, _("try --help\n"));
630 if( !strcasecmp(srcformat, dstformat) ) {
631 printf( _("input and output formats are the same\n"
640 init_standard_fields();
642 switch(import_file(srcformat, srcfile)) {
645 _("input format %s not supported\n"), srcformat);
649 fprintf(stderr, _("cannot read file %s\n"), srcfile);
655 switch(export_file(dstformat, dstfile)) {
658 _("output format %s not supported\n"),
664 _("cannot write file %s\n"), dstfile);
675 * --add-email handling
678 static int add_email_count = 0;
683 if(add_email_count > 0) {
684 if(save_database() < 0) {
685 fprintf(stderr, _("cannot open %s\n"), datafile);
688 printf(_("%d item(s) added to %s\n"), add_email_count, datafile);
690 puts(_("Valid sender address not found"));
697 quit_add_email_sig(int signal)
706 check_abook_directory();
712 * we don't actually care if loading fails or not
714 load_database(datafile);
716 atexit(close_database);
718 signal(SIGINT, quit_add_email_sig);
722 add_email_add_item(int quiet, char *name, char *email)
726 if(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
727 int search_fields[] = { EMAIL, -1 };
728 if(find_item(email, 0, search_fields) >= 0) {
730 printf(_("Address %s already in addressbook\n"),
737 FILE *in = fopen("/dev/tty", "r");
740 fprintf(stderr, _("cannot open /dev/tty\n"
741 "you may want to use --add-email-quiet\n"));
746 printf(_("Add \"%s <%s>\" to %s? (%c/%c)\n"),
750 *S_("keybinding for yes|y"),
751 *S_("keybinding for no|n"));
752 c = tolower(getc(in));
753 if(c == *S_("keybinding for no|n")) {
757 } while(c != *S_("keybinding for yes|y"));
761 item = item_create();
762 item_fput(item, NAME, xstrdup(name));
763 item_fput(item, EMAIL, xstrdup(email));
764 add_item2database(item);
774 char *name = NULL, *email = NULL;
777 if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode) ) {
778 fprintf(stderr, _("stdin is a directory or cannot stat stdin\n"));
785 line = getaline(stdin);
786 if(line && !strncasecmp("From:", line, 5) ) {
787 getname(line, &name, &email);
788 add_email_count += add_email_add_item(quiet,
794 } while( !feof(stdin) );
800 * end of --add-email handling