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;
54 char custom_format[FORMAT_STRING_LEN] = "{nick} ({name}): {mobile}";
55 struct abook_output_item_filter selected_item_filter;
57 bool alternative_datafile = FALSE;
58 bool alternative_rcfile = FALSE;
66 assert(datafile != NULL);
68 if( (f = fopen(datafile, "a")) == NULL)
77 check_abook_directory()
82 assert(!is_ui_initialized());
84 if(alternative_datafile)
87 dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
90 if(stat(dir, &s) == -1) {
96 if(mkdir(dir, 0700) == -1) {
97 printf(_("Cannot create directory %s\n"), dir);
102 } else if(!S_ISDIR(s.st_mode)) {
103 printf(_("%s is not a directory\n"), dir);
112 xmalloc_error_handler(int err)
115 * We don't try to save addressbook here because we don't know
116 * if it's fully loaded to memory.
118 if(is_ui_initialized())
121 fprintf(stderr, _("Memory allocation failure: %s\n"), strerror(err));
129 check_abook_directory();
131 if(load_opts(rcfile) > 0) {
132 printf(_("Press enter to continue...\n"));
135 init_default_views();
137 signal(SIGTERM, quit_abook_sig);
144 umask(DEFAULT_UMASK);
146 if(!datafile_writeable()) {
147 char *s = strdup_printf(_("File %s is not writeable"), datafile);
151 if(load_database(datafile) || !statusline_ask_boolean(
152 _("If you continue all changes will "
153 "be lost. Do you want to continue?"), FALSE)) {
155 /*close_database();*/
160 load_database(datafile);
166 quit_abook(int save_db)
169 if(opt_get_bool(BOOL_AUTOSAVE))
171 else if(statusline_ask_boolean(_("Save database"), TRUE))
173 } else if(!statusline_ask_boolean(_("Quit without saving"), FALSE))
185 quit_abook_sig(int i)
187 quit_abook(QUIT_SAVE);
191 main(int argc, char **argv)
193 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
194 setlocale(LC_MESSAGES, "");
195 setlocale(LC_TIME, "");
196 setlocale(LC_CTYPE, "");
197 setlocale(LC_COLLATE, "");
199 bindtextdomain(PACKAGE, LOCALEDIR);
202 xmalloc_set_error_handler(xmalloc_error_handler);
204 prepare_database_internals();
206 parse_command_line(argc, argv);
212 quit_abook(QUIT_SAVE);
230 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
231 fprintf(stderr,_("%s is not a valid HOME directory\n"), getenv("HOME") );
236 datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
240 rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
243 atexit(free_filenames);
253 MODE_ADD_EMAIL_QUIET,
259 change_mode(int *current, int mode)
261 if(*current != MODE_CONT) {
262 fprintf(stderr, _("Cannot combine options --mutt-query, "
264 "--add-email or --add-email-quiet\n"));
272 set_filename(char **var, char *path)
277 assert(*var == NULL); /* or else we probably leak memory */
278 assert(path != NULL);
281 *var = xstrdup(path);
287 *var = strconcat(cwd, "/", path, NULL);
292 #define set_convert_var(X) do { if(mode != MODE_CONVERT) {\
293 fprintf(stderr, _("please use option --%s after --convert option\n"),\
294 long_options[option_index].name);\
301 parse_command_line(int argc, char **argv)
303 int mode = MODE_CONT;
304 char *query_string = NULL;
305 char *informat = "abook",
310 selected_item_filter = select_output_item_filter("muttq");
313 int option_index = 0;
326 static struct option long_options[] = {
327 { "help", 0, 0, 'h' },
328 { "add-email", 0, 0, OPT_ADD_EMAIL },
329 { "add-email-quiet", 0, 0, OPT_ADD_EMAIL_QUIET },
330 { "datafile", 1, 0, 'f' },
331 { "mutt-query", 1, 0, OPT_MUTT_QUERY },
332 { "config", 1, 0, 'C' },
333 { "convert", 0, 0, OPT_CONVERT },
334 { "informat", 1, 0, OPT_INFORMAT },
335 { "outformat", 1, 0, OPT_OUTFORMAT },
336 { "outformatstr", 1, 0, OPT_OUTFORMAT_STR },
337 { "infile", 1, 0, OPT_INFILE },
338 { "outfile", 1, 0, OPT_OUTFILE },
339 { "formats", 0, 0, OPT_FORMATS },
343 c = getopt_long(argc, argv, "hC:f:",
344 long_options, &option_index);
354 change_mode(&mode, MODE_ADD_EMAIL);
356 case OPT_ADD_EMAIL_QUIET:
357 change_mode(&mode, MODE_ADD_EMAIL_QUIET);
360 set_filename(&datafile, optarg);
361 alternative_datafile = TRUE;
364 query_string = optarg;
365 change_mode(&mode, MODE_QUERY);
368 set_filename(&rcfile, optarg);
369 alternative_rcfile = TRUE;
372 change_mode(&mode, MODE_CONVERT);
375 set_convert_var(informat);
378 if(mode != MODE_CONVERT && mode != MODE_QUERY) {
380 _("please use option --outformat after --convert or --mutt-query option\n"));
383 // ascii-name is stored, it's used to traverse
384 // e_filters[] in MODE_CONVERT (see export_file())
386 // but in case a query-compatible filter is requested
387 // try to guess right now which one it is, from u_filters[]
388 selected_item_filter = select_output_item_filter(outformat);
390 case OPT_OUTFORMAT_STR:
391 strncpy(custom_format, optarg, FORMAT_STRING_LEN);
392 custom_format[FORMAT_STRING_LEN - 1] = 0;
395 set_convert_var(infile);
398 set_convert_var(outfile);
408 // if the output format requested does not allow filtered querying
409 // (not in u_filter[]) and --convert has not been specified; bailout
410 if(! selected_item_filter.func && mode != MODE_CONVERT) {
411 printf("output format %s not supported or incompatible with --mutt-query\n", outformat);
414 if(! selected_item_filter.func)
415 selected_item_filter = select_output_item_filter("muttq");
416 else if (! strcmp(outformat, "custom")) {
417 if(! *custom_format) {
418 fprintf(stderr, _("Invalid custom format string\n"));
423 fprintf(stderr, _("%s: unrecognized arguments on command line\n"),
431 case MODE_ADD_EMAIL_QUIET:
434 mutt_query(query_string);
436 convert(informat, infile, outformat, outfile);
444 puts (PACKAGE " v" VERSION "\n");
445 puts (_(" -h --help show usage"));
446 puts (_(" -C --config <file> use an alternative configuration file"));
447 puts (_(" -f --datafile <file> use an alternative addressbook file"));
448 puts (_(" --mutt-query <string> make a query for mutt"));
449 puts (_(" --add-email "
450 "read an e-mail message from stdin and\n"
452 "add the sender to the addressbook"));
453 puts (_(" --add-email-quiet "
454 "same as --add-email but doesn't\n"
455 " require to confirm adding"));
457 puts (_(" --convert convert address book files"));
458 puts (_(" options to use with --convert:"));
459 puts (_(" --informat <format> format for input file"));
460 puts (_(" (default: abook)"));
461 puts (_(" --infile <file> source file"));
462 puts (_(" (default: stdin)"));
463 puts (_(" --outformat <format> format for output file"));
464 puts (_(" (default: text)"));
465 puts (_(" --outfile <file> destination file"));
466 puts (_(" (default: stdout)"));
467 puts (_(" --outformatstr <str> format to use for \"custom\" --outformat"));
468 puts (_(" (default: \"{nick} ({name}): {mobile}\")"));
469 puts (_(" --formats list available formats"));
478 quit_mutt_query(int status)
487 mutt_query(char *str)
491 if( str == NULL || !strcasecmp(str, "all") ) {
492 export_file("muttq", "-");
494 int search_fields[] = {NAME, EMAIL, NICK, -1};
496 if( (i = find_item(str, 0, search_fields)) < 0 ) {
497 printf("Not found\n");
498 quit_mutt_query(EXIT_FAILURE);
500 // mutt expects a leading line containing
501 // a message about the query.
502 // Others output filter supporting query (vcard, custom)
504 if(!strcmp(selected_item_filter.filtname, "muttq"))
507 e_write_item(stdout, i, selected_item_filter.func);
508 i = find_item(str, i + 1, search_fields);
512 quit_mutt_query(EXIT_SUCCESS);
522 if( load_database(datafile) ) {
523 printf(_("Cannot open database\n"));
524 quit_mutt_query(EXIT_FAILURE);
531 make_mailstr(int item)
533 char email[MAX_EMAIL_LEN];
535 char *name = strdup_printf("\"%s\"", db_name_get(item));
537 get_first_email(email, item);
540 strdup_printf("%s <%s>", name, email) :
549 print_stderr(int item)
551 fprintf (stderr, "%c", '\n');
553 if( is_valid_item(item) )
554 muttq_print_item(stderr, item);
556 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
557 db_enumerate_items(e) {
558 muttq_print_item(stderr, e.item);
565 launch_mutt(int item)
567 char *cmd = NULL, *mailstr = NULL;
568 char *mutt_command = opt_get_str(STR_MUTT_COMMAND);
570 if(mutt_command == NULL || !*mutt_command)
573 if( is_valid_item(item) )
574 mailstr = make_mailstr(item);
576 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
578 db_enumerate_items(e) {
581 strconcat(tmp, ",", make_mailstr(e.item), NULL):
582 strconcat(make_mailstr(e.item), NULL);
587 cmd = strconcat(mutt_command, " \'", mailstr, "\'", NULL);
590 fprintf(stderr, "cmd: %s\n", cmd);
596 * we need to make sure that curses settings are correct
602 launch_wwwbrowser(int item)
606 if( !is_valid_item(item) )
609 if(db_fget(item, URL))
610 cmd = strdup_printf("%s '%s'",
611 opt_get_str(STR_WWW_COMMAND),
612 safe_str(db_fget(item, URL)));
622 * we need to make sure that curses settings are correct
628 abook_fopen (const char *path, const char *mode)
633 stat_ok = (stat(path, &s) != -1);
635 if(strchr(mode, 'r'))
636 return (stat_ok && S_ISREG(s.st_mode)) ?
637 fopen(path, mode) : NULL;
639 return (stat_ok && S_ISDIR(s.st_mode)) ?
640 NULL : fopen(path, mode);
644 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
648 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
649 fprintf(stderr, _("too few arguments to make conversion\n"));
650 fprintf(stderr, _("try --help\n"));
654 if( !strcasecmp(srcformat, dstformat) ) {
655 printf( _("input and output formats are the same\n"
664 init_standard_fields();
666 switch(import_file(srcformat, srcfile)) {
669 _("input format %s not supported\n"), srcformat);
673 fprintf(stderr, _("cannot read file %s\n"), srcfile);
679 switch(export_file(dstformat, dstfile)) {
682 _("output format %s not supported\n"),
688 _("cannot write file %s\n"), dstfile);
699 * --add-email handling
702 static int add_email_count = 0, add_email_found = 0;
707 if(add_email_count > 0) {
708 if(save_database() < 0) {
709 fprintf(stderr, _("cannot open %s\n"), datafile);
712 printf(_("%d item(s) added to %s\n"), add_email_count, datafile);
713 } else if (add_email_found == 0) {
714 puts(_("Valid sender address not found"));
721 quit_add_email_sig(int signal)
730 check_abook_directory();
733 init_standard_fields();
737 * we don't actually care if loading fails or not
739 load_database(datafile);
741 atexit(close_database);
743 signal(SIGINT, quit_add_email_sig);
747 add_email_add_item(int quiet, char *name, char *email)
751 if(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
752 int search_fields[] = { EMAIL, -1 };
753 if(find_item(email, 0, search_fields) >= 0) {
755 printf(_("Address %s already in addressbook\n"),
762 FILE *in = fopen("/dev/tty", "r");
765 fprintf(stderr, _("cannot open /dev/tty\n"
766 "you may want to use --add-email-quiet\n"));
771 printf(_("Add \"%s <%s>\" to %s? (%c/%c)\n"),
775 *S_("keybinding for yes|y"),
776 *S_("keybinding for no|n"));
777 c = tolower(getc(in));
778 if(c == *S_("keybinding for no|n")) {
782 } while(c != *S_("keybinding for yes|y"));
786 item = item_create();
787 item_fput(item, NAME, xstrdup(name));
788 item_fput(item, EMAIL, xstrdup(email));
789 add_item2database(item);
799 char *name = NULL, *email = NULL;
802 if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode) ) {
803 fprintf(stderr, _("stdin is a directory or cannot stat stdin\n"));
810 line = getaline(stdin);
811 if(line && !strncasecmp("From:", line, 5) ) {
813 getname(line, &name, &email);
814 add_email_count += add_email_add_item(quiet,
820 } while( !feof(stdin) );
826 * end of --add-email handling