]> git.deb.at Git - pkg/abook.git/blob - abook.c
mutt-query (2/3): moved mutt full database querying to mutt_query_export_database...
[pkg/abook.git] / abook.c
1 /*
2  * $Id$
3  *
4  * by JH <jheinonen@users.sourceforge.net>
5  *
6  * Copyright (C) Jaakko Heinonen
7  */
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <ctype.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #ifdef HAVE_CONFIG_H
19 #       include "config.h"
20 #endif
21 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
22 #       include <locale.h>
23 #endif
24 #include <assert.h>
25 #include "abook.h"
26 #include "gettext.h"
27 #include "ui.h"
28 #include "database.h"
29 #include "list.h"
30 #include "filter.h"
31 #include "edit.h"
32 #include "misc.h"
33 #include "options.h"
34 #include "getname.h"
35 #include "getopt.h"
36 #include "views.h"
37 #include "xmalloc.h"
38
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);
49
50 char *datafile = NULL;
51 static char *rcfile = NULL;
52
53 bool alternative_datafile = FALSE;
54 bool alternative_rcfile = FALSE;
55
56
57 static int
58 datafile_writeable()
59 {
60         FILE *f;
61
62         assert(datafile != NULL);
63
64         if( (f = fopen(datafile, "a")) == NULL)
65                 return FALSE;
66
67         fclose(f);
68
69         return TRUE;
70 }
71
72 static void
73 check_abook_directory()
74 {
75         struct stat s;
76         char *dir;
77
78         assert(!is_ui_initialized());
79
80         if(alternative_datafile)
81                 return;
82
83         dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
84         assert(dir != NULL);
85
86         if(stat(dir, &s) == -1) {
87                 if(errno != ENOENT) {
88                         perror(dir);
89                         free(dir);
90                         exit(EXIT_FAILURE);
91                 }
92                 if(mkdir(dir, 0700) == -1) {
93                         printf(_("Cannot create directory %s\n"), dir);
94                         perror(dir);
95                         free(dir);
96                         exit(EXIT_FAILURE);
97                 }
98         } else if(!S_ISDIR(s.st_mode)) {
99                 printf(_("%s is not a directory\n"), dir);
100                 free(dir);
101                 exit(EXIT_FAILURE);
102         }
103
104         free(dir);
105 }
106
107 static void
108 xmalloc_error_handler(int err)
109 {
110         /*
111          * We don't try to save addressbook here because we don't know
112          * if it's fully loaded to memory.
113          */
114         if(is_ui_initialized())
115                 close_ui();
116
117         fprintf(stderr, _("Memory allocation failure: %s\n"), strerror(err));
118         exit(EXIT_FAILURE);
119 }
120
121 static void
122 init_abook()
123 {
124         set_filenames();
125         check_abook_directory();
126         init_opts();
127         if(load_opts(rcfile) > 0) {
128                 printf(_("Press enter to continue...\n"));
129                 fgetc(stdin);
130         }
131         init_default_views();
132
133         signal(SIGTERM, quit_abook_sig);
134
135         init_index();
136
137         if(init_ui())
138                 exit(EXIT_FAILURE);
139
140         umask(DEFAULT_UMASK);
141
142         if(!datafile_writeable()) {
143                 char *s = strdup_printf(_("File %s is not writeable"), datafile);
144                 refresh_screen();
145                 statusline_msg(s);
146                 free(s);
147                 if(load_database(datafile) || !statusline_ask_boolean(
148                                         _("If you continue all changes will "
149                                 "be lost. Do you want to continue?"), FALSE)) {
150                         free_opts();
151                         /*close_database();*/
152                         close_ui();
153                         exit(EXIT_FAILURE);
154                 }
155         } else
156                 load_database(datafile);
157
158         refresh_screen();
159 }
160
161 void
162 quit_abook(int save_db)
163 {
164         if(save_db)  {
165                 if(opt_get_bool(BOOL_AUTOSAVE))
166                         save_database();
167                 else if(statusline_ask_boolean(_("Save database"), TRUE))
168                         save_database();
169         } else if(!statusline_ask_boolean(_("Quit without saving"), FALSE))
170                 return;
171
172         free_opts();
173         close_database();
174
175         close_ui();
176
177         exit(EXIT_SUCCESS);
178 }
179
180 static void
181 quit_abook_sig(int i)
182 {
183         quit_abook(QUIT_SAVE);
184 }
185
186 int
187 main(int argc, char **argv)
188 {
189 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
190         setlocale(LC_MESSAGES, "");
191         setlocale(LC_TIME, "");
192         setlocale(LC_CTYPE, "");
193         setlocale(LC_COLLATE, "");
194 #endif
195         bindtextdomain(PACKAGE, LOCALEDIR);
196         textdomain(PACKAGE);
197
198         xmalloc_set_error_handler(xmalloc_error_handler);
199
200         prepare_database_internals();
201
202         parse_command_line(argc, argv);
203
204         init_abook();
205
206         get_commands();
207
208         quit_abook(QUIT_SAVE);
209
210         return 0;
211 }
212
213 static void
214 free_filenames()
215 {
216         xfree(rcfile);
217         xfree(datafile);
218 }
219
220
221 static void
222 set_filenames()
223 {
224         struct stat s;
225
226         if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
227                 fprintf(stderr,_("%s is not a valid HOME directory\n"), getenv("HOME") );
228                 exit(EXIT_FAILURE);
229         }
230
231         if(!datafile)
232                 datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
233                                 DATAFILE, NULL);
234
235         if(!rcfile)
236                 rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
237                                 RCFILE, NULL);
238
239         atexit(free_filenames);
240 }
241
242 /*
243  * CLI
244  */
245
246 enum {
247         MODE_CONT,
248         MODE_ADD_EMAIL,
249         MODE_ADD_EMAIL_QUIET,
250         MODE_QUERY,
251         MODE_CONVERT
252 };
253
254 static void
255 change_mode(int *current, int mode)
256 {
257         if(*current != MODE_CONT) {
258                 fprintf(stderr, _("Cannot combine options --mutt-query, "
259                                 "--convert, "
260                                 "--add-email or --add-email-quiet\n"));
261                 exit(EXIT_FAILURE);
262         }
263
264         *current = mode;
265 }
266
267 void
268 set_filename(char **var, char *path)
269 {
270         char *cwd;
271
272         assert(var != NULL);
273         assert(*var == NULL); /* or else we probably leak memory */
274         assert(path != NULL);
275
276         if(*path == '/') {
277                 *var = xstrdup(path);
278                 return;
279         }
280
281         cwd = my_getcwd();
282
283         *var = strconcat(cwd, "/", path, NULL);
284
285         free(cwd);
286 }
287
288 #define set_convert_var(X) do { if(mode != MODE_CONVERT) {\
289         fprintf(stderr, _("please use option --%s after --convert option\n"),\
290                         long_options[option_index].name);\
291                 exit(EXIT_FAILURE);\
292         } else\
293                 X = optarg;\
294         } while(0)
295
296 static void
297 parse_command_line(int argc, char **argv)
298 {
299         int mode = MODE_CONT;
300         char *query_string = NULL;
301         char *informat = "abook",
302                 *outformat = "text",
303                 *infile = "-",
304                 *outfile = "-";
305         int c;
306
307         for(;;) {
308                 int option_index = 0;
309                 enum {
310                         OPT_ADD_EMAIL,
311                         OPT_ADD_EMAIL_QUIET,
312                         OPT_MUTT_QUERY,
313                         OPT_CONVERT,
314                         OPT_INFORMAT,
315                         OPT_OUTFORMAT,
316                         OPT_INFILE,
317                         OPT_OUTFILE,
318                         OPT_FORMATS
319                 };
320                 static struct option long_options[] = {
321                         { "help", 0, 0, 'h' },
322                         { "add-email", 0, 0, OPT_ADD_EMAIL },
323                         { "add-email-quiet", 0, 0, OPT_ADD_EMAIL_QUIET },
324                         { "datafile", 1, 0, 'f' },
325                         { "mutt-query", 1, 0, OPT_MUTT_QUERY },
326                         { "config", 1, 0, 'C' },
327                         { "convert", 0, 0, OPT_CONVERT },
328                         { "informat", 1, 0, OPT_INFORMAT },
329                         { "outformat", 1, 0, OPT_OUTFORMAT },
330                         { "infile", 1, 0, OPT_INFILE },
331                         { "outfile", 1, 0, OPT_OUTFILE },
332                         { "formats", 0, 0, OPT_FORMATS },
333                         { 0, 0, 0, 0 }
334                 };
335
336                 c = getopt_long(argc, argv, "hC:",
337                                 long_options, &option_index);
338
339                 if(c == -1)
340                         break;
341
342                 switch(c) {
343                         case 'h':
344                                 show_usage();
345                                 exit(EXIT_SUCCESS);
346                         case OPT_ADD_EMAIL:
347                                 change_mode(&mode, MODE_ADD_EMAIL);
348                                 break;
349                         case OPT_ADD_EMAIL_QUIET:
350                                 change_mode(&mode, MODE_ADD_EMAIL_QUIET);
351                                 break;
352                         case 'f':
353                                 set_filename(&datafile, optarg);
354                                 alternative_datafile = TRUE;
355                                 break;
356                         case OPT_MUTT_QUERY:
357                                 query_string = optarg;
358                                 change_mode(&mode, MODE_QUERY);
359                                 break;
360                         case 'C':
361                                 set_filename(&rcfile, optarg);
362                                 alternative_rcfile = TRUE;
363                                 break;
364                         case OPT_CONVERT:
365                                 change_mode(&mode, MODE_CONVERT);
366                                 break;
367                         case OPT_INFORMAT:
368                                 set_convert_var(informat);
369                                 break;
370                         case OPT_OUTFORMAT:
371                                 set_convert_var(outformat);
372                                 break;
373                         case OPT_INFILE:
374                                 set_convert_var(infile);
375                                 break;
376                         case OPT_OUTFILE:
377                                 set_convert_var(outfile);
378                                 break;
379                         case OPT_FORMATS:
380                                 print_filters();
381                                 exit(EXIT_SUCCESS);
382                         default:
383                                 exit(EXIT_FAILURE);
384                 }
385         }
386
387         if(optind < argc) {
388                 fprintf(stderr, _("%s: unrecognized arguments on command line\n"),
389                                 argv[0]);
390                 exit(EXIT_FAILURE);
391         }
392
393         switch(mode) {
394                 case MODE_ADD_EMAIL:
395                         add_email(0);
396                 case MODE_ADD_EMAIL_QUIET:
397                         add_email(1);
398                 case MODE_QUERY:
399                         mutt_query(query_string);
400                 case MODE_CONVERT:
401                         convert(informat, infile, outformat, outfile);
402         }
403 }
404
405
406 static void
407 show_usage()
408 {
409         puts    (PACKAGE " v " VERSION "\n");
410         puts    (_("     -h     --help                          show usage"));
411         puts    (_("     -C     --config        <file>          use an alternative configuration file"));
412         puts    (_("    --datafile      <file>          use an alternative addressbook file"));
413         puts    (_("    --mutt-query    <string>        make a query for mutt"));
414         puts    (_("    --add-email                     "
415                         "read an e-mail message from stdin and\n"
416                 "                                       "
417                 "add the sender to the addressbook"));
418         puts    (_("    --add-email-quiet               "
419                 "same as --add-email but doesn't\n"
420                 "                                       require to confirm adding"));
421         putchar('\n');
422         puts    (_("    --convert                       convert address book files"));
423         puts    (_("    options to use with --convert:"));
424         puts    (_("    --informat      <format>        format for input file"));
425         puts    (_("                                    (default: abook)"));
426         puts    (_("    --infile        <file>          source file"));
427         puts    (_("                                    (default: stdin)"));
428         puts    (_("    --outformat     <format>        format for output file"));
429         puts    (_("                                    (default: text)"));
430         puts    (_("    --outfile       <file>          destination file"));
431         puts    (_("                                    (default: stdout)"));
432         puts    (_("    --formats                       list available formats"));
433 }
434
435 /*
436  * end of CLI
437  */
438
439
440 static void
441 quit_mutt_query(int status)
442 {
443         close_database();
444         free_opts();
445
446         exit(status);
447 }
448
449 static void
450 mutt_query(char *str)
451 {
452         init_mutt_query();
453
454         if( str == NULL || !strcasecmp(str, "all") ) {
455                 export_file("muttq", "-");
456         } else {
457                 int search_fields[] = {NAME, EMAIL, NICK, -1};
458                 int i;
459                 if( (i = find_item(str, 0, search_fields)) < 0 ) {
460                         printf("Not found\n");
461                         quit_mutt_query(EXIT_FAILURE);
462                 }
463                 putchar('\n');
464                 while(i >= 0) {
465                         muttq_print_item(stdout, i);
466                         i = find_item(str, i + 1, search_fields);
467                 }
468         }
469
470         quit_mutt_query(EXIT_SUCCESS);
471 }
472
473 static void
474 init_mutt_query()
475 {
476         set_filenames();
477         init_opts();
478         load_opts(rcfile);
479
480         if( load_database(datafile) ) {
481                 printf(_("Cannot open database\n"));
482                 quit_mutt_query(EXIT_FAILURE);
483                 exit(EXIT_FAILURE);
484         }
485 }
486
487
488 static char *
489 make_mailstr(int item)
490 {
491         char email[MAX_EMAIL_LEN];
492         char *ret;
493         char *name = strdup_printf("\"%s\"", db_name_get(item));
494
495         get_first_email(email, item);
496
497         ret = *email ?
498                 strdup_printf("%s <%s>", name, email) :
499                 xstrdup(name);
500
501         free(name);
502
503         return ret;
504 }
505
506 void
507 print_stderr(int item)
508 {
509         fprintf (stderr, "%c", '\n');
510
511         if( is_valid_item(item) )
512                 muttq_print_item(stderr, item);
513         else {
514                 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
515                 db_enumerate_items(e) {
516                         muttq_print_item(stderr, e.item);
517                 }
518         }
519
520 }
521
522 void
523 launch_mutt(int item)
524 {
525         char *cmd = NULL, *mailstr = NULL;
526         char *mutt_command = opt_get_str(STR_MUTT_COMMAND);
527
528         if(mutt_command == NULL || !*mutt_command)
529                 return;
530
531         if( is_valid_item(item) )
532                 mailstr = make_mailstr(item);
533         else {
534                 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
535                 char *tmp = NULL;
536                 db_enumerate_items(e) {
537                         tmp = mailstr;
538                         mailstr = tmp ?
539                                 strconcat(tmp, ",", make_mailstr(e.item), NULL):
540                                 strconcat(make_mailstr(e.item), NULL);
541                         free(tmp);
542                 }
543         }
544
545         cmd = strconcat(mutt_command, " \'", mailstr, "\'", NULL);
546         free(mailstr);
547 #ifdef DEBUG
548         fprintf(stderr, "cmd: %s\n", cmd);
549 #endif
550         system(cmd);
551         free(cmd);
552
553         /*
554          * we need to make sure that curses settings are correct
555          */
556         ui_init_curses();
557 }
558
559 void
560 launch_wwwbrowser(int item)
561 {
562         char *cmd = NULL;
563
564         if( !is_valid_item(item) )
565                 return;
566
567         if(db_fget(item, URL))
568                 cmd = strdup_printf("%s '%s'",
569                                 opt_get_str(STR_WWW_COMMAND),
570                                 safe_str(db_fget(item, URL)));
571         else
572                 return;
573
574         if ( cmd )
575                 system(cmd);
576
577         free(cmd);
578
579         /*
580          * we need to make sure that curses settings are correct
581          */
582         ui_init_curses();
583 }
584
585 FILE *
586 abook_fopen (const char *path, const char *mode)
587 {
588         struct stat s;
589         bool stat_ok;
590
591         stat_ok = (stat(path, &s) != -1);
592
593         if(strchr(mode, 'r'))
594                 return (stat_ok && S_ISREG(s.st_mode)) ?
595                         fopen(path, mode) : NULL;
596         else
597                 return (stat_ok && S_ISDIR(s.st_mode)) ?
598                         NULL : fopen(path, mode);
599 }
600
601 static void
602 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
603 {
604         int ret=0;
605
606         if( !srcformat || !srcfile || !dstformat || !dstfile ) {
607                 fprintf(stderr, _("too few arguments to make conversion\n"));
608                 fprintf(stderr, _("try --help\n"));
609         }
610
611 #ifndef DEBUG
612         if( !strcasecmp(srcformat, dstformat) ) {
613                 printf( _("input and output formats are the same\n"
614                         "exiting...\n"));
615                 exit(EXIT_FAILURE);
616         }
617 #endif
618
619         set_filenames();
620         init_opts();
621         load_opts(rcfile);
622         init_standard_fields();
623
624         switch(import_file(srcformat, srcfile)) {
625                 case -1:
626                         fprintf(stderr,
627                                 _("input format %s not supported\n"), srcformat);
628                         ret = 1;
629                         break;
630                 case 1:
631                         fprintf(stderr, _("cannot read file %s\n"), srcfile);
632                         ret = 1;
633                         break;
634         }
635
636         if(!ret)
637                 switch(export_file(dstformat, dstfile)) {
638                         case -1:
639                                 fprintf(stderr,
640                                         _("output format %s not supported\n"),
641                                         dstformat);
642                                 ret = 1;
643                                 break;
644                         case 1:
645                                 fprintf(stderr,
646                                         _("cannot write file %s\n"), dstfile);
647                                 ret = 1;
648                                 break;
649                 }
650
651         close_database();
652         free_opts();
653         exit(ret);
654 }
655
656 /*
657  * --add-email handling
658  */
659
660 static int add_email_count = 0;
661
662 static void
663 quit_add_email()
664 {
665         if(add_email_count > 0) {
666                 if(save_database() < 0) {
667                         fprintf(stderr, _("cannot open %s\n"), datafile);
668                         exit(EXIT_FAILURE);
669                 }
670                 printf(_("%d item(s) added to %s\n"), add_email_count, datafile);
671         } else {
672                 puts(_("Valid sender address not found"));
673         }
674
675         exit(EXIT_SUCCESS);
676 }
677
678 static void
679 quit_add_email_sig(int signal)
680 {
681         quit_add_email();
682 }
683
684 static void
685 init_add_email()
686 {
687         set_filenames();
688         check_abook_directory();
689         init_opts();
690         load_opts(rcfile);
691         init_standard_fields();
692         atexit(free_opts);
693
694         /*
695          * we don't actually care if loading fails or not
696          */
697         load_database(datafile);
698
699         atexit(close_database);
700
701         signal(SIGINT, quit_add_email_sig);
702 }
703
704 static int
705 add_email_add_item(int quiet, char *name, char *email)
706 {
707         list_item item;
708
709         if(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
710                 int search_fields[] = { EMAIL, -1 };
711                 if(find_item(email, 0, search_fields) >= 0) {
712                         if(!quiet)
713                                 printf(_("Address %s already in addressbook\n"),
714                                                 email);
715                         return 0;
716                 }
717         }
718
719         if(!quiet) {
720                 FILE *in = fopen("/dev/tty", "r");
721                 char c;
722                 if(!in) {
723                         fprintf(stderr, _("cannot open /dev/tty\n"
724                                 "you may want to use --add-email-quiet\n"));
725                         exit(EXIT_FAILURE);
726                 }
727
728                 do {
729                         printf(_("Add \"%s <%s>\" to %s? (%c/%c)\n"),
730                                         name,
731                                         email,
732                                         datafile,
733                                         *S_("keybinding for yes|y"),
734                                         *S_("keybinding for no|n"));
735                         c = tolower(getc(in));
736                         if(c == *S_("keybinding for no|n")) {
737                                 fclose(in);
738                                 return 0;
739                         }
740                 } while(c != *S_("keybinding for yes|y"));
741                 fclose(in);
742         }
743
744         item = item_create();
745         item_fput(item, NAME, xstrdup(name));
746         item_fput(item, EMAIL, xstrdup(email));
747         add_item2database(item);
748         item_free(&item);
749
750         return 1;
751 }
752
753 static void
754 add_email(int quiet)
755 {
756         char *line;
757         char *name = NULL, *email = NULL;
758         struct stat s;
759
760         if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode) ) {
761                 fprintf(stderr, _("stdin is a directory or cannot stat stdin\n"));
762                 exit(EXIT_FAILURE);
763         }
764
765         init_add_email();
766
767         do {
768                 line = getaline(stdin);
769                 if(line && !strncasecmp("From:", line, 5) ) {
770                         getname(line, &name, &email);
771                         add_email_count += add_email_add_item(quiet,
772                                         name, email);
773                         xfree(name);
774                         xfree(email);
775                 }
776                 xfree(line);
777         } while( !feof(stdin) );
778
779         quit_add_email();
780 }
781
782 /*
783  * end of --add-email handling
784  */