4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
18 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
31 static void init_abook();
32 static void set_filenames();
33 static void free_filenames();
34 static void parse_command_line(int argc, char **argv);
35 static void show_usage();
36 static void mutt_query(char *str);
37 static void init_mutt_query();
38 static void quit_mutt_query();
39 static void convert(char *srcformat, char *srcfile,
40 char *dstformat, char *dstfile);
41 static void add_email(int);
43 char *datafile = NULL;
52 signal(SIGKILL, quit_abook);
53 signal(SIGTERM, quit_abook);
61 * this is very ugly for now
63 /*if( options_get_int("datafile", "autosave") )*/
65 if( load_database(datafile) == 2 ) {
66 char *tmp = strconcat(getenv("HOME"),
69 if( safe_strcmp(tmp, datafile) ) {
71 statusline_msg("Sorry, the specified file does "
72 "not appear to be a valid abook addressbook");
73 statusline_msg("Will open default addressbook...");
76 load_database(datafile);
87 if( options_get_int("autosave") )
89 else if( statusline_ask_boolean("Save database", TRUE) )
101 main(int argc, char **argv)
103 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
104 setlocale(LC_ALL, "" );
107 parse_command_line(argc, argv);
123 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
124 fprintf(stderr,"%s is not a valid HOME directory\n", getenv("HOME") );
129 datafile = strconcat(getenv("HOME"), "/" DATAFILE, NULL);
131 rcfile = strconcat(getenv("HOME"), "/" RCFILE, NULL);
133 atexit(free_filenames);
144 parse_command_line(int argc, char **argv)
148 for( i = 1; i < argc; i++ ) {
149 if( !strcmp(argv[i], "--help") ) {
152 } else if( !strcmp(argv[i], "--mutt-query") ) {
153 mutt_query(argv[i + 1]);
154 } else if( !strcmp(argv[i], "--datafile") ) {
156 if (argv[i+1][0] != '/') {
157 char *cwd = my_getcwd();
158 datafile = strconcat(cwd, "/", argv[i+1], NULL);
161 datafile = strdup(argv[i+1]);
168 } else if( !strcmp(argv[i], "--convert") ) {
169 if( argc < 5 || argc > 6 ) {
170 fprintf(stderr, "incorrect number of argumets to make conversion\n");
171 fprintf(stderr, "try %s --help\n", argv[0]);
175 convert(argv[i+1], argv[i+2],
176 argv[i+3], argv[i+4]);
178 convert(argv[i+1], argv[i+2], argv[i+3], "-");
179 } else if( !strcmp(argv[i], "--add-email") ) {
181 } else if( !strcmp(argv[i], "--add-email-quiet") ) {
184 printf("option %s not recognized\n", argv[i]);
185 printf("try %s --help\n", argv[0]);
195 puts (PACKAGE " v " VERSION "\n");
196 puts (" --help show usage");
197 puts (" --datafile <filename> use an alternative addressbook file");
198 puts (" --mutt-query <string> make a query for mutt");
199 puts (" --convert <inputformat> <inputfile> "
200 "<outputformat> <outputfile>");
201 puts (" --add-email "
202 "read an e-mail message from stdin and\n"
204 "add the sender to the addressbook");
205 puts (" --add-email-quiet "
206 "same as --add-email but doesn't\n"
209 puts ("available formats for --convert option:");
212 puts ("\nWarning: this version compiled with DEBUG flag ON");
216 extern list_item *database;
219 muttq_print_item(FILE *file, int item)
221 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
224 split_emailstr(item, emails);
226 for(i = 0; i < (options_get_int("mutt_return_all_emails") ?
227 MAX_EMAILS : 1) ; i++)
229 fprintf(file, "%s\t%s\t%s\n", emails[i],
230 database[item][NAME],
231 database[item][NOTES] == NULL ? " " :
232 database[item][NOTES]
237 mutt_query(char *str)
241 if( str == NULL || !strcasecmp(str, "all") ) {
242 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
243 printf("All items\n");
244 db_enumerate_items(e)
245 muttq_print_item(stdout, e.item);
247 int search_fields[] = {NAME, EMAIL, NICK, -1};
249 if( (i = find_item(str, 0, search_fields)) < 0 ) {
250 printf("Not found\n");
255 muttq_print_item(stdout, i);
256 i = find_item(str, i+1, search_fields);
269 if( load_database(datafile) ) {
270 printf("Cannot open database\n");
277 quit_mutt_query(int status)
287 make_mailstr(int item)
289 char email[MAX_EMAIL_LEN];
291 char *name = mkstr("\"%s\"", database[item][NAME]);
293 get_first_email(email, item);
295 ret = *database[item][EMAIL] ?
296 mkstr("%s <%s>", name, email) :
305 print_stderr(int item)
307 fprintf (stderr, "%c", '\n');
309 if( is_valid_item(item) )
310 muttq_print_item(stderr, item);
312 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
313 db_enumerate_items(e) {
314 muttq_print_item(stderr, e.item);
321 launch_mutt(int item)
323 char *cmd = NULL, *mailstr = NULL;
324 char *mutt_command = options_get_str("mutt_command");
326 if(mutt_command == NULL || !*mutt_command)
329 if( is_valid_item(item) )
330 mailstr = make_mailstr(item);
332 struct db_enumerator e = init_db_enumerator(ENUM_SELECTED);
334 db_enumerate_items(e) {
337 strconcat(tmp, ",", make_mailstr(e.item), NULL):
338 strconcat(make_mailstr(e.item), NULL);
343 cmd = strconcat(mutt_command, " \'", mailstr,
347 fprintf(stderr, "cmd: %s\n", cmd);
353 * we need to make sure that curses settings are correct
359 launch_wwwbrowser(int item)
363 if( !is_valid_item(item) )
366 if( database[item][URL] )
367 cmd = mkstr("%s '%s'",
368 options_get_str("www_command"),
369 safe_str(database[item][URL]));
379 * we need to make sure that curses settings are correct
385 abook_malloc(size_t size)
389 if ( (ptr = malloc(size)) == NULL ) {
390 if( is_ui_initialized() )
392 perror("malloc() failed");
400 abook_realloc(void *ptr, size_t size)
402 ptr = realloc(ptr, size);
408 if( is_ui_initialized() )
410 perror("realloc() failed");
418 abook_fopen (const char *path, const char *mode)
422 if( ! strchr(mode, 'r') )
423 return fopen(path, mode);
425 if ( (stat(path, &s)) == -1 )
428 return S_ISREG(s.st_mode) ? fopen(path, mode) : NULL;
434 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
438 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
439 fprintf(stderr, "too few argumets to make conversion\n");
440 fprintf(stderr, "try --help\n");
447 if( !strcmp(srcformat, dstformat) ) {
448 printf( "input and output formats are the same\n"
457 switch( import(srcformat, srcfile) ) {
460 "input format %s not supported\n", srcformat);
463 fprintf(stderr, "cannot read file %s\n", srcfile);
468 switch( export(dstformat, dstfile) ) {
471 "output format %s not supported\n",
477 "cannot write file %s\n", dstfile);
488 * --add-email handling
491 static int add_email_count = 0;
496 if(add_email_count > 0) {
497 if(save_database() < 0) {
498 fprintf(stderr, "cannot open %s\n", datafile);
501 printf("%d item(s) added to %s\n", add_email_count, datafile);
503 puts("Valid sender address not found");
510 quit_add_email_sig(int signal)
519 atexit(free_filenames);
521 atexit(close_config);
524 * we don't actually care if loading fails or not
526 load_database(datafile);
528 atexit(close_database);
530 signal(SIGINT, quit_add_email_sig);
534 add_email_add_item(int quiet, char *name, char *email)
539 FILE *in = fopen("/dev/tty", "r");
542 fprintf(stderr, "cannot open /dev/tty\n"
543 "you may want to use --add-email-quiet\n");
546 printf("Add ``%s <%s>'' to %s ? (y/n)\n",
553 if(c == 'n' || c == 'N') {
557 } while(c != 'y' && c != 'Y');
561 memset(item, 0, sizeof(item));
562 item[NAME] = strdup(name);
563 item[EMAIL] = strdup(email);
564 add_item2database(item);
573 char *name = NULL, *email = NULL;
578 line = getaline(stdin);
579 if(line && !strncasecmp("From:", line, 5) ) {
580 getname(line, &name, &email);
581 add_email_count += add_email_add_item(quiet,
587 } while( !feof(stdin) );
593 * end of --add-email handling