4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
20 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
36 static void init_abook();
37 static void quit_abook_sig(int i);
38 static void set_filenames();
39 static void parse_command_line(int argc, char **argv);
40 static void show_usage();
41 static void mutt_query(char *str);
42 static void init_mutt_query();
43 static void convert(char *srcformat, char *srcfile,
44 char *dstformat, char *dstfile);
45 static void add_email(int);
47 char *datafile = NULL;
50 bool alternative_datafile = FALSE;
51 bool alternative_rcfile = FALSE;
58 assert(datafile != NULL);
60 if( (f = fopen(datafile, "a")) == NULL)
69 check_abook_directory()
74 assert(!is_ui_initialized());
76 if(alternative_datafile)
79 dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
82 if(stat(dir, &s) == -1) {
88 if(mkdir(dir, 0700) == -1) {
89 printf("Cannot create directory %s\n", dir);
94 } else if(!S_ISDIR(s.st_mode)) {
95 printf("%s is not a directory\n", dir);
104 xmalloc_error_handler(int err)
107 * We don't try to save addressbook here because we don't know
108 * if it's fully loaded to to memory.
110 if(is_ui_initialized())
113 fprintf(stderr, "Memory allocation failure: %s\n", strerror(err));
121 check_abook_directory();
123 if(load_opts(rcfile) > 0) {
124 printf("Press enter to continue...\n");
128 signal(SIGKILL, quit_abook_sig);
129 signal(SIGTERM, quit_abook_sig);
134 umask(DEFAULT_UMASK);
136 if(!datafile_writeable()) {
137 char *s = mkstr("File %s is not writeable", datafile);
141 if(load_database(datafile) || !statusline_ask_boolean(
142 "If you continue all changes will "
143 "be lost. Do you want to continue?", FALSE)) {
145 /*close_database();*/
150 load_database(datafile);
156 quit_abook(int save_db)
159 if(opt_get_bool(BOOL_AUTOSAVE))
161 else if(statusline_ask_boolean("Save database", TRUE))
163 } else if(!statusline_ask_boolean("Quit without saving", FALSE))
175 quit_abook_sig(int i)
177 quit_abook(QUIT_SAVE);
181 main(int argc, char **argv)
183 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
184 setlocale(LC_ALL, "");
186 xmalloc_set_error_handler(xmalloc_error_handler);
188 parse_command_line(argc, argv);
194 quit_abook(QUIT_SAVE);
212 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
213 fprintf(stderr,"%s is not a valid HOME directory\n", getenv("HOME") );
218 datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
222 rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
225 atexit(free_filenames);
235 MODE_ADD_EMAIL_QUIET,
241 change_mode(int *current, int mode)
243 if(*current != MODE_CONT) {
244 fprintf(stderr, "Cannot combine options --mutt-query, "
246 "--add-email or --add-email-quiet\n");
254 set_filename(char **var, char *path)
259 assert(*var == NULL); /* or else we probably leak memory */
260 assert(path != NULL);
269 *var = strconcat(cwd, "/", path, NULL);
274 #define set_convert_var(X) do { if(mode != MODE_CONVERT) {\
275 fprintf(stderr, "please use option --%s after --convert option\n",\
276 long_options[option_index].name);\
283 parse_command_line(int argc, char **argv)
285 int mode = MODE_CONT;
286 char *query_string = NULL;
287 char *informat = "abook",
294 int option_index = 0;
306 static struct option long_options[] = {
307 { "help", 0, 0, 'h' },
308 { "add-email", 0, 0, OPT_ADD_EMAIL },
309 { "add-email-quiet", 0, 0, OPT_ADD_EMAIL_QUIET },
310 { "datafile", 1, 0, 'f' },
311 { "mutt-query", 1, 0, OPT_MUTT_QUERY },
312 { "config", 1, 0, 'C' },
313 { "convert", 0, 0, OPT_CONVERT },
314 { "informat", 1, 0, OPT_INFORMAT },
315 { "outformat", 1, 0, OPT_OUTFORMAT },
316 { "infile", 1, 0, OPT_INFILE },
317 { "outfile", 1, 0, OPT_OUTFILE },
318 { "formats", 0, 0, OPT_FORMATS },
322 c = getopt_long(argc, argv, "hC:",
323 long_options, &option_index);
333 change_mode(&mode, MODE_ADD_EMAIL);
335 case OPT_ADD_EMAIL_QUIET:
336 change_mode(&mode, MODE_ADD_EMAIL_QUIET);
339 set_filename(&datafile, optarg);
340 alternative_datafile = TRUE;
343 query_string = optarg;
344 change_mode(&mode, MODE_QUERY);
347 set_filename(&rcfile, optarg);
348 alternative_rcfile = TRUE;
351 change_mode(&mode, MODE_CONVERT);
354 set_convert_var(informat);
357 set_convert_var(outformat);
360 set_convert_var(infile);
363 set_convert_var(outfile);
374 fprintf(stderr, "%s: unrecognized arguments on command line\n",
382 case MODE_ADD_EMAIL_QUIET:
385 mutt_query(query_string);
387 convert(informat, infile, outformat, outfile);
395 puts (PACKAGE " v " VERSION "\n");
396 puts (" -h --help show usage");
397 puts (" -C --config <file> use an alternative configuration file");
398 puts (" --datafile <file> use an alternative addressbook file");
399 puts (" --mutt-query <string> make a query for mutt");
400 puts (" --add-email "
401 "read an e-mail message from stdin and\n"
403 "add the sender to the addressbook");
404 puts (" --add-email-quiet "
405 "same as --add-email but doesn't\n"
408 puts (" --convert convert address book files");
409 puts (" options to use with --convert:");
410 puts (" --informat <format> format for input file");
411 puts (" (default: abook)");
412 puts (" --infile <file> source file");
413 puts (" (default: stdin)");
414 puts (" --outformat <format> format for output file");
415 puts (" (default: text)");
416 puts (" --outfile <file> destination file");
417 puts (" (default: stdout)");
418 puts (" --formats list available formats");
425 extern list_item *database;
428 quit_mutt_query(int status)
437 muttq_print_item(FILE *file, int item)
439 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
442 split_emailstr(item, emails);
444 for(i = 0; i < (opt_get_bool(BOOL_MUTT_RETURN_ALL_EMAILS) ?
445 MAX_EMAILS : 1) ; i++)
447 fprintf(file, "%s\t%s\t%s\n", emails[i],
448 database[item][NAME],
449 database[item][NOTES] == NULL ? " " :
450 database[item][NOTES]
455 mutt_query(char *str)
459 if( str == NULL || !strcasecmp(str, "all") ) {
460 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
461 printf("All items\n");
462 db_enumerate_items(e)
463 muttq_print_item(stdout, e.item);
465 int search_fields[] = {NAME, EMAIL, NICK, -1};
467 if( (i = find_item(str, 0, search_fields)) < 0 ) {
468 printf("Not found\n");
469 quit_mutt_query(EXIT_FAILURE);
473 muttq_print_item(stdout, i);
474 i = find_item(str, i + 1, search_fields);
478 quit_mutt_query(EXIT_SUCCESS);
488 if( load_database(datafile) ) {
489 printf("Cannot open database\n");
490 quit_mutt_query(EXIT_FAILURE);
497 make_mailstr(int item)
499 char email[MAX_EMAIL_LEN];
501 char *name = mkstr("\"%s\"", database[item][NAME]);
503 get_first_email(email, item);
505 ret = *database[item][EMAIL] ?
506 mkstr("%s <%s>", name, email) :
515 print_stderr(int item)
517 fprintf (stderr, "%c", '\n');
519 if( is_valid_item(item) )
520 muttq_print_item(stderr, item);
522 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
523 db_enumerate_items(e) {
524 muttq_print_item(stderr, e.item);
531 launch_mutt(int item)
533 char *cmd = NULL, *mailstr = NULL;
534 char *mutt_command = opt_get_str(STR_MUTT_COMMAND);
536 if(mutt_command == NULL || !*mutt_command)
539 if( is_valid_item(item) )
540 mailstr = make_mailstr(item);
542 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
544 db_enumerate_items(e) {
547 strconcat(tmp, ",", make_mailstr(e.item), NULL):
548 strconcat(make_mailstr(e.item), NULL);
553 cmd = strconcat(mutt_command, " \'", mailstr, "\'", NULL);
556 fprintf(stderr, "cmd: %s\n", cmd);
562 * we need to make sure that curses settings are correct
568 launch_wwwbrowser(int item)
572 if( !is_valid_item(item) )
575 if( database[item][URL] )
576 cmd = mkstr("%s '%s'",
577 opt_get_str(STR_WWW_COMMAND),
578 safe_str(database[item][URL]));
588 * we need to make sure that curses settings are correct
594 abook_fopen (const char *path, const char *mode)
599 stat_ok = (stat(path, &s) != -1);
601 if(strchr(mode, 'r'))
602 return (stat_ok && S_ISREG(s.st_mode)) ?
603 fopen(path, mode) : NULL;
605 return (stat_ok && S_ISDIR(s.st_mode)) ?
606 NULL : fopen(path, mode);
610 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
614 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
615 fprintf(stderr, "too few argumets to make conversion\n");
616 fprintf(stderr, "try --help\n");
620 if( !strcasecmp(srcformat, dstformat) ) {
621 printf( "input and output formats are the same\n"
631 switch(import_file(srcformat, srcfile)) {
634 "input format %s not supported\n", srcformat);
638 fprintf(stderr, "cannot read file %s\n", srcfile);
644 switch(export_file(dstformat, dstfile)) {
647 "output format %s not supported\n",
653 "cannot write file %s\n", dstfile);
664 * --add-email handling
667 static int add_email_count = 0;
672 if(add_email_count > 0) {
673 if(save_database() < 0) {
674 fprintf(stderr, "cannot open %s\n", datafile);
677 printf("%d item(s) added to %s\n", add_email_count, datafile);
679 puts("Valid sender address not found");
686 quit_add_email_sig(int signal)
695 atexit(free_filenames);
696 check_abook_directory();
702 * we don't actually care if loading fails or not
704 load_database(datafile);
706 atexit(close_database);
708 signal(SIGINT, quit_add_email_sig);
712 add_email_add_item(int quiet, char *name, char *email)
716 if(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
717 int search_fields[] = { EMAIL, -1 };
718 if(find_item(email, 0, search_fields) >= 0) {
720 printf("Address %s already in addressbook\n",
727 FILE *in = fopen("/dev/tty", "r");
730 fprintf(stderr, "cannot open /dev/tty\n"
731 "you may want to use --add-email-quiet\n");
736 printf("Add ``%s <%s>'' to %s ? (y/n)\n",
741 if(c == 'n' || c == 'N') {
745 } while(c != 'y' && c != 'Y');
749 memset(item, 0, sizeof(item));
750 item[NAME] = strdup(name);
751 item[EMAIL] = strdup(email);
752 add_item2database(item);
761 char *name = NULL, *email = NULL;
764 if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode) ) {
765 fprintf(stderr, "stdin is a directory or cannot stat stdin\n");
772 line = getaline(stdin);
773 if(line && !strncasecmp("From:", line, 5) ) {
774 getname(line, &name, &email);
775 add_email_count += add_email_add_item(quiet,
781 } while( !feof(stdin) );
787 * end of --add-email handling