4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
19 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
34 static void init_abook();
35 static void quit_abook_sig(int i);
36 static void set_filenames();
37 static void parse_command_line(int argc, char **argv);
38 static void show_usage();
39 static void mutt_query(char *str);
40 static void init_mutt_query();
41 static void convert(char *srcformat, char *srcfile,
42 char *dstformat, char *dstfile);
43 static void add_email(int);
45 char *datafile = NULL;
48 bool alternative_datafile = FALSE;
49 bool alternative_rcfile = FALSE;
56 assert(datafile != NULL);
58 if( (f = fopen(datafile, "a")) == NULL)
67 check_abook_directory()
72 assert(!is_ui_initialized());
74 if(alternative_datafile)
77 dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
80 if(stat(dir, &s) == -1) {
86 if(mkdir(dir, 0700) == -1) {
87 printf("Cannot create directory %s\n", dir);
92 } else if(!S_ISDIR(s.st_mode)) {
93 printf("%s is not a directory\n", dir);
105 check_abook_directory();
107 if(load_opts(rcfile) > 0) {
108 printf("Press enter to continue...\n");
112 signal(SIGKILL, quit_abook_sig);
113 signal(SIGTERM, quit_abook_sig);
118 umask(DEFAULT_UMASK);
120 if(!datafile_writeable()) {
121 char *s = mkstr("File %s is not writeable", datafile);
125 if(load_database(datafile) || !statusline_ask_boolean(
126 "If you continue all changes will "
127 "be lost. Do you want to continue?", FALSE)) {
129 /*close_database();*/
134 load_database(datafile);
142 if( opt_get_bool(BOOL_AUTOSAVE) )
144 else if( statusline_ask_boolean("Save database", TRUE) )
156 quit_abook_sig(int i)
162 main(int argc, char **argv)
164 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
165 setlocale(LC_ALL, "" );
168 parse_command_line(argc, argv);
192 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
193 fprintf(stderr,"%s is not a valid HOME directory\n", getenv("HOME") );
198 datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
202 rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
205 atexit(free_filenames);
215 MODE_ADD_EMAIL_QUIET,
221 change_mode(int *current, int mode)
223 if(*current != MODE_CONT) {
224 fprintf(stderr, "Cannot combine options --mutt-query, "
226 "--add-email or --add-email-quiet\n");
234 set_filename(char **var, char *path)
239 assert(*var == NULL); /* or else we probably leak memory */
240 assert(path != NULL);
249 *var = strconcat(cwd, "/", path, NULL);
254 #define set_convert_var(X) do { if(mode != MODE_CONVERT) {\
255 fprintf(stderr, "please use option --%s after --convert option\n",\
256 long_options[option_index].name);\
263 parse_command_line(int argc, char **argv)
265 int mode = MODE_CONT;
266 char *query_string = NULL;
267 char *informat = "abook",
274 int option_index = 0;
286 static struct option long_options[] = {
287 { "help", 0, 0, 'h' },
288 { "add-email", 0, 0, OPT_ADD_EMAIL },
289 { "add-email-quiet", 0, 0, OPT_ADD_EMAIL_QUIET },
290 { "datafile", 1, 0, 'f' },
291 { "mutt-query", 1, 0, OPT_MUTT_QUERY },
292 { "config", 1, 0, 'C' },
293 { "convert", 0, 0, OPT_CONVERT },
294 { "informat", 1, 0, OPT_INFORMAT },
295 { "outformat", 1, 0, OPT_OUTFORMAT },
296 { "infile", 1, 0, OPT_INFILE },
297 { "outfile", 1, 0, OPT_OUTFILE },
298 { "formats", 0, 0, OPT_FORMATS },
302 c = getopt_long(argc, argv, "hC:",
303 long_options, &option_index);
313 change_mode(&mode, MODE_ADD_EMAIL);
315 case OPT_ADD_EMAIL_QUIET:
316 change_mode(&mode, MODE_ADD_EMAIL_QUIET);
319 set_filename(&datafile, optarg);
320 alternative_datafile = TRUE;
323 query_string = optarg;
324 change_mode(&mode, MODE_QUERY);
327 set_filename(&rcfile, optarg);
328 alternative_rcfile = TRUE;
331 change_mode(&mode, MODE_CONVERT);
334 set_convert_var(informat);
337 set_convert_var(outformat);
340 set_convert_var(infile);
343 set_convert_var(outfile);
354 fprintf(stderr, "%s: unrecognized arguments on command line\n",
362 case MODE_ADD_EMAIL_QUIET:
365 mutt_query(query_string);
367 convert(informat, infile, outformat, outfile);
375 puts (PACKAGE " v " VERSION "\n");
376 puts (" -h --help show usage");
377 puts (" -C --config <file> use an alternative configuration file");
378 puts (" --datafile <file> use an alternative addressbook file");
379 puts (" --mutt-query <string> make a query for mutt");
380 puts (" --add-email "
381 "read an e-mail message from stdin and\n"
383 "add the sender to the addressbook");
384 puts (" --add-email-quiet "
385 "same as --add-email but doesn't\n"
388 puts (" --convert convert address book files");
389 puts (" options to use with --convert:");
390 puts (" --informat <format> format for input file");
391 puts (" (default: abook)");
392 puts (" --infile <file> source file");
393 puts (" (default: stdin)");
394 puts (" --outformat <format> format for output file");
395 puts (" (default: text)");
396 puts (" --outfile <file> destination file");
397 puts (" (default: stdout)");
398 puts (" --formats list available formats");
405 extern list_item *database;
408 quit_mutt_query(int status)
417 muttq_print_item(FILE *file, int item)
419 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
422 split_emailstr(item, emails);
424 for(i = 0; i < (opt_get_bool(BOOL_MUTT_RETURN_ALL_EMAILS) ?
425 MAX_EMAILS : 1) ; i++)
427 fprintf(file, "%s\t%s\t%s\n", emails[i],
428 database[item][NAME],
429 database[item][NOTES] == NULL ? " " :
430 database[item][NOTES]
435 mutt_query(char *str)
439 if( str == NULL || !strcasecmp(str, "all") ) {
440 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
441 printf("All items\n");
442 db_enumerate_items(e)
443 muttq_print_item(stdout, e.item);
445 int search_fields[] = {NAME, EMAIL, NICK, -1};
447 if( (i = find_item(str, 0, search_fields)) < 0 ) {
448 printf("Not found\n");
453 muttq_print_item(stdout, i);
454 i = find_item(str, i+1, search_fields);
468 if( load_database(datafile) ) {
469 printf("Cannot open database\n");
477 make_mailstr(int item)
479 char email[MAX_EMAIL_LEN];
481 char *name = mkstr("\"%s\"", database[item][NAME]);
483 get_first_email(email, item);
485 ret = *database[item][EMAIL] ?
486 mkstr("%s <%s>", name, email) :
495 print_stderr(int item)
497 fprintf (stderr, "%c", '\n');
499 if( is_valid_item(item) )
500 muttq_print_item(stderr, item);
502 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
503 db_enumerate_items(e) {
504 muttq_print_item(stderr, e.item);
511 launch_mutt(int item)
513 char *cmd = NULL, *mailstr = NULL;
514 char *mutt_command = opt_get_str(STR_MUTT_COMMAND);
516 if(mutt_command == NULL || !*mutt_command)
519 if( is_valid_item(item) )
520 mailstr = make_mailstr(item);
522 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
524 db_enumerate_items(e) {
527 strconcat(tmp, ",", make_mailstr(e.item), NULL):
528 strconcat(make_mailstr(e.item), NULL);
533 cmd = strconcat(mutt_command, " \'", mailstr,
537 fprintf(stderr, "cmd: %s\n", cmd);
543 * we need to make sure that curses settings are correct
549 launch_wwwbrowser(int item)
553 if( !is_valid_item(item) )
556 if( database[item][URL] )
557 cmd = mkstr("%s '%s'",
558 opt_get_str(STR_WWW_COMMAND),
559 safe_str(database[item][URL]));
569 * we need to make sure that curses settings are correct
575 abook_malloc(size_t size)
579 if ( (ptr = malloc(size)) == NULL ) {
580 if( is_ui_initialized() )
582 perror("malloc() failed");
590 abook_realloc(void *ptr, size_t size)
592 ptr = realloc(ptr, size);
598 if( is_ui_initialized() )
600 perror("realloc() failed");
608 abook_fopen (const char *path, const char *mode)
613 stat_ok = (stat(path, &s) != -1);
615 if(strchr(mode, 'r'))
616 return (stat_ok && S_ISREG(s.st_mode)) ?
617 fopen(path, mode) : NULL;
619 return (stat_ok && S_ISDIR(s.st_mode)) ?
620 NULL : fopen(path, mode);
624 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
628 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
629 fprintf(stderr, "too few argumets to make conversion\n");
630 fprintf(stderr, "try --help\n");
634 if( !strcasecmp(srcformat, dstformat) ) {
635 printf( "input and output formats are the same\n"
645 switch( import_file(srcformat, srcfile) ) {
648 "input format %s not supported\n", srcformat);
652 fprintf(stderr, "cannot read file %s\n", srcfile);
658 switch( export_file(dstformat, dstfile) ) {
661 "output format %s not supported\n",
667 "cannot write file %s\n", dstfile);
678 * --add-email handling
681 static int add_email_count = 0;
686 if(add_email_count > 0) {
687 if(save_database() < 0) {
688 fprintf(stderr, "cannot open %s\n", datafile);
691 printf("%d item(s) added to %s\n", add_email_count, datafile);
693 puts("Valid sender address not found");
700 quit_add_email_sig(int signal)
709 atexit(free_filenames);
715 * we don't actually care if loading fails or not
717 load_database(datafile);
719 atexit(close_database);
721 signal(SIGINT, quit_add_email_sig);
725 add_email_add_item(int quiet, char *name, char *email)
729 if(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
730 int search_fields[] = { EMAIL, -1 };
731 if(find_item(email, 0, search_fields) >= 0) {
733 printf("Address %s already in addressbook\n",
740 FILE *in = fopen("/dev/tty", "r");
743 fprintf(stderr, "cannot open /dev/tty\n"
744 "you may want to use --add-email-quiet\n");
747 printf("Add ``%s <%s>'' to %s ? (y/n)\n",
754 if(c == 'n' || c == 'N') {
758 } while(c != 'y' && c != 'Y');
762 memset(item, 0, sizeof(item));
763 item[NAME] = strdup(name);
764 item[EMAIL] = strdup(email);
765 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