4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
20 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
35 static void init_abook();
36 static void quit_abook_sig(int i);
37 static void set_filenames();
38 static void parse_command_line(int argc, char **argv);
39 static void show_usage();
40 static void mutt_query(char *str);
41 static void init_mutt_query();
42 static void convert(char *srcformat, char *srcfile,
43 char *dstformat, char *dstfile);
44 static void add_email(int);
46 char *datafile = NULL;
49 bool alternative_datafile = FALSE;
50 bool alternative_rcfile = FALSE;
57 assert(datafile != NULL);
59 if( (f = fopen(datafile, "a")) == NULL)
68 check_abook_directory()
73 assert(!is_ui_initialized());
75 if(alternative_datafile)
78 dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
81 if(stat(dir, &s) == -1) {
87 if(mkdir(dir, 0700) == -1) {
88 printf("Cannot create directory %s\n", dir);
93 } else if(!S_ISDIR(s.st_mode)) {
94 printf("%s is not a directory\n", dir);
106 check_abook_directory();
108 if(load_opts(rcfile) > 0) {
109 printf("Press enter to continue...\n");
113 signal(SIGKILL, quit_abook_sig);
114 signal(SIGTERM, quit_abook_sig);
119 umask(DEFAULT_UMASK);
121 if(!datafile_writeable()) {
122 char *s = mkstr("File %s is not writeable", datafile);
126 if(load_database(datafile) || !statusline_ask_boolean(
127 "If you continue all changes will "
128 "be lost. Do you want to continue?", FALSE)) {
130 /*close_database();*/
135 load_database(datafile);
141 quit_abook(int save_db)
144 if(opt_get_bool(BOOL_AUTOSAVE))
146 else if(statusline_ask_boolean("Save database", TRUE))
148 } else if(!statusline_ask_boolean("Quit without saving", FALSE))
160 quit_abook_sig(int i)
162 quit_abook(QUIT_SAVE);
166 main(int argc, char **argv)
168 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
169 setlocale(LC_ALL, "");
172 parse_command_line(argc, argv);
178 quit_abook(QUIT_SAVE);
196 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
197 fprintf(stderr,"%s is not a valid HOME directory\n", getenv("HOME") );
202 datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
206 rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
209 atexit(free_filenames);
219 MODE_ADD_EMAIL_QUIET,
225 change_mode(int *current, int mode)
227 if(*current != MODE_CONT) {
228 fprintf(stderr, "Cannot combine options --mutt-query, "
230 "--add-email or --add-email-quiet\n");
238 set_filename(char **var, char *path)
243 assert(*var == NULL); /* or else we probably leak memory */
244 assert(path != NULL);
253 *var = strconcat(cwd, "/", path, NULL);
258 #define set_convert_var(X) do { if(mode != MODE_CONVERT) {\
259 fprintf(stderr, "please use option --%s after --convert option\n",\
260 long_options[option_index].name);\
267 parse_command_line(int argc, char **argv)
269 int mode = MODE_CONT;
270 char *query_string = NULL;
271 char *informat = "abook",
278 int option_index = 0;
290 static struct option long_options[] = {
291 { "help", 0, 0, 'h' },
292 { "add-email", 0, 0, OPT_ADD_EMAIL },
293 { "add-email-quiet", 0, 0, OPT_ADD_EMAIL_QUIET },
294 { "datafile", 1, 0, 'f' },
295 { "mutt-query", 1, 0, OPT_MUTT_QUERY },
296 { "config", 1, 0, 'C' },
297 { "convert", 0, 0, OPT_CONVERT },
298 { "informat", 1, 0, OPT_INFORMAT },
299 { "outformat", 1, 0, OPT_OUTFORMAT },
300 { "infile", 1, 0, OPT_INFILE },
301 { "outfile", 1, 0, OPT_OUTFILE },
302 { "formats", 0, 0, OPT_FORMATS },
306 c = getopt_long(argc, argv, "hC:",
307 long_options, &option_index);
317 change_mode(&mode, MODE_ADD_EMAIL);
319 case OPT_ADD_EMAIL_QUIET:
320 change_mode(&mode, MODE_ADD_EMAIL_QUIET);
323 set_filename(&datafile, optarg);
324 alternative_datafile = TRUE;
327 query_string = optarg;
328 change_mode(&mode, MODE_QUERY);
331 set_filename(&rcfile, optarg);
332 alternative_rcfile = TRUE;
335 change_mode(&mode, MODE_CONVERT);
338 set_convert_var(informat);
341 set_convert_var(outformat);
344 set_convert_var(infile);
347 set_convert_var(outfile);
358 fprintf(stderr, "%s: unrecognized arguments on command line\n",
366 case MODE_ADD_EMAIL_QUIET:
369 mutt_query(query_string);
371 convert(informat, infile, outformat, outfile);
379 puts (PACKAGE " v " VERSION "\n");
380 puts (" -h --help show usage");
381 puts (" -C --config <file> use an alternative configuration file");
382 puts (" --datafile <file> use an alternative addressbook file");
383 puts (" --mutt-query <string> make a query for mutt");
384 puts (" --add-email "
385 "read an e-mail message from stdin and\n"
387 "add the sender to the addressbook");
388 puts (" --add-email-quiet "
389 "same as --add-email but doesn't\n"
392 puts (" --convert convert address book files");
393 puts (" options to use with --convert:");
394 puts (" --informat <format> format for input file");
395 puts (" (default: abook)");
396 puts (" --infile <file> source file");
397 puts (" (default: stdin)");
398 puts (" --outformat <format> format for output file");
399 puts (" (default: text)");
400 puts (" --outfile <file> destination file");
401 puts (" (default: stdout)");
402 puts (" --formats list available formats");
409 extern list_item *database;
412 quit_mutt_query(int status)
421 muttq_print_item(FILE *file, int item)
423 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
426 split_emailstr(item, emails);
428 for(i = 0; i < (opt_get_bool(BOOL_MUTT_RETURN_ALL_EMAILS) ?
429 MAX_EMAILS : 1) ; i++)
431 fprintf(file, "%s\t%s\t%s\n", emails[i],
432 database[item][NAME],
433 database[item][NOTES] == NULL ? " " :
434 database[item][NOTES]
439 mutt_query(char *str)
443 if( str == NULL || !strcasecmp(str, "all") ) {
444 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
445 printf("All items\n");
446 db_enumerate_items(e)
447 muttq_print_item(stdout, e.item);
449 int search_fields[] = {NAME, EMAIL, NICK, -1};
451 if( (i = find_item(str, 0, search_fields)) < 0 ) {
452 printf("Not found\n");
457 muttq_print_item(stdout, i);
458 i = find_item(str, i + 1, search_fields);
472 if( load_database(datafile) ) {
473 printf("Cannot open database\n");
481 make_mailstr(int item)
483 char email[MAX_EMAIL_LEN];
485 char *name = mkstr("\"%s\"", database[item][NAME]);
487 get_first_email(email, item);
489 ret = *database[item][EMAIL] ?
490 mkstr("%s <%s>", name, email) :
499 print_stderr(int item)
501 fprintf (stderr, "%c", '\n');
503 if( is_valid_item(item) )
504 muttq_print_item(stderr, item);
506 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
507 db_enumerate_items(e) {
508 muttq_print_item(stderr, e.item);
515 launch_mutt(int item)
517 char *cmd = NULL, *mailstr = NULL;
518 char *mutt_command = opt_get_str(STR_MUTT_COMMAND);
520 if(mutt_command == NULL || !*mutt_command)
523 if( is_valid_item(item) )
524 mailstr = make_mailstr(item);
526 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
528 db_enumerate_items(e) {
531 strconcat(tmp, ",", make_mailstr(e.item), NULL):
532 strconcat(make_mailstr(e.item), NULL);
537 cmd = strconcat(mutt_command, " \'", mailstr, "\'", NULL);
540 fprintf(stderr, "cmd: %s\n", cmd);
546 * we need to make sure that curses settings are correct
552 launch_wwwbrowser(int item)
556 if( !is_valid_item(item) )
559 if( database[item][URL] )
560 cmd = mkstr("%s '%s'",
561 opt_get_str(STR_WWW_COMMAND),
562 safe_str(database[item][URL]));
572 * we need to make sure that curses settings are correct
578 abook_malloc(size_t size)
582 if ( (ptr = malloc(size)) == NULL ) {
583 if( is_ui_initialized() )
584 quit_abook(QUIT_SAVE);
585 perror("malloc() failed");
593 abook_realloc(void *ptr, size_t size)
595 ptr = realloc(ptr, size);
601 if(is_ui_initialized())
602 quit_abook(QUIT_SAVE);
603 perror("realloc() failed");
611 abook_fopen (const char *path, const char *mode)
616 stat_ok = (stat(path, &s) != -1);
618 if(strchr(mode, 'r'))
619 return (stat_ok && S_ISREG(s.st_mode)) ?
620 fopen(path, mode) : NULL;
622 return (stat_ok && S_ISDIR(s.st_mode)) ?
623 NULL : fopen(path, mode);
627 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
631 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
632 fprintf(stderr, "too few argumets to make conversion\n");
633 fprintf(stderr, "try --help\n");
637 if( !strcasecmp(srcformat, dstformat) ) {
638 printf( "input and output formats are the same\n"
648 switch(import_file(srcformat, srcfile)) {
651 "input format %s not supported\n", srcformat);
655 fprintf(stderr, "cannot read file %s\n", srcfile);
661 switch(export_file(dstformat, dstfile)) {
664 "output format %s not supported\n",
670 "cannot write file %s\n", dstfile);
681 * --add-email handling
684 static int add_email_count = 0;
689 if(add_email_count > 0) {
690 if(save_database() < 0) {
691 fprintf(stderr, "cannot open %s\n", datafile);
694 printf("%d item(s) added to %s\n", add_email_count, datafile);
696 puts("Valid sender address not found");
703 quit_add_email_sig(int signal)
712 atexit(free_filenames);
713 check_abook_directory();
719 * we don't actually care if loading fails or not
721 load_database(datafile);
723 atexit(close_database);
725 signal(SIGINT, quit_add_email_sig);
729 add_email_add_item(int quiet, char *name, char *email)
733 if(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
734 int search_fields[] = { EMAIL, -1 };
735 if(find_item(email, 0, search_fields) >= 0) {
737 printf("Address %s already in addressbook\n",
744 FILE *in = fopen("/dev/tty", "r");
747 fprintf(stderr, "cannot open /dev/tty\n"
748 "you may want to use --add-email-quiet\n");
753 printf("Add ``%s <%s>'' to %s ? (y/n)\n",
758 if(c == 'n' || c == 'N') {
762 } while(c != 'y' && c != 'Y');
766 memset(item, 0, sizeof(item));
767 item[NAME] = strdup(name);
768 item[EMAIL] = strdup(email);
769 add_item2database(item);
778 char *name = NULL, *email = NULL;
781 if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode) ) {
782 fprintf(stderr, "stdin is a directory or cannot stat stdin\n");
789 line = getaline(stdin);
790 if(line && !strncasecmp("From:", line, 5) ) {
791 getname(line, &name, &email);
792 add_email_count += add_email_add_item(quiet,
798 } while( !feof(stdin) );
804 * end of --add-email handling