X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=abook.c;h=74ade409606f089acae4cf9d0299f16fb08d6b66;hb=d4cd6aada86c2d1c3e046b91a271184e659716fa;hp=b79fcae0b2fad72537bcb29bd681f7f0dd9bdfdd;hpb=a79562b49189a27165ca9ffb38c08aa9e687f97e;p=pkg%2Fabook.git diff --git a/abook.c b/abook.c index b79fcae..74ade40 100644 --- a/abook.c +++ b/abook.c @@ -1,7 +1,7 @@ /* * $Id$ * - * by JH + * by JH * * Copyright (C) Jaakko Heinonen */ @@ -26,6 +26,7 @@ #include "edit.h" #include "misc.h" #include "options.h" +#include "getname.h" static void init_abook(); static void set_filenames(); @@ -37,6 +38,7 @@ static void init_mutt_query(); static void quit_mutt_query(); static void convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile); +static void add_email(int); char *datafile = NULL; char *rcfile = NULL; @@ -147,12 +149,10 @@ parse_command_line(int argc, char **argv) if( !strcmp(argv[i], "--help") ) { show_usage(); exit(1); - } else - if( !strcmp(argv[i], "--mutt-query") ) + } else if( !strcmp(argv[i], "--mutt-query") ) { mutt_query(argv[i + 1]); - else - if( !strcmp(argv[i], "--datafile") ) { - if (argv[i+1]) { + } else if( !strcmp(argv[i], "--datafile") ) { + if (argc > i + 1 ) { if (argv[i+1][0] != '/') { char *cwd = my_getcwd(); datafile = strconcat(cwd, "/", argv[i+1], NULL); @@ -165,18 +165,21 @@ parse_command_line(int argc, char **argv) show_usage(); exit(1); } - } else - if( !strcmp(argv[i], "--convert") ) { + } else if( !strcmp(argv[i], "--convert") ) { if( argc < 5 || argc > 6 ) { fprintf(stderr, "incorrect number of argumets to make conversion\n"); fprintf(stderr, "try %s --help\n", argv[0]); exit(1); } - if( argv[i+4] ) + if( argc > i + 4 ) convert(argv[i+1], argv[i+2], argv[i+3], argv[i+4]); else convert(argv[i+1], argv[i+2], argv[i+3], "-"); + } else if( !strcmp(argv[i], "--add-email") ) { + add_email(0); + } else if( !strcmp(argv[i], "--add-email-quiet") ) { + add_email(1); } else { printf("option %s not recognized\n", argv[i]); printf("try %s --help\n", argv[0]); @@ -194,7 +197,14 @@ show_usage() puts (" --datafile use an alternative addressbook file"); puts (" --mutt-query make a query for mutt"); puts (" --convert " - " "); + " "); + puts (" --add-email " + "read an e-mail message from stdin and\n" + " " + "add the sender to the addressbook"); + puts (" --add-email-quiet " + "same as --add-email but doesn't\n" + " confirm adding"); putchar('\n'); puts ("available formats for --convert option:"); print_filters(); @@ -206,7 +216,7 @@ show_usage() extern list_item *database; static void -muttq_print_item(int item) +muttq_print_item(FILE *file, int item) { char emails[MAX_EMAILS][MAX_EMAIL_LEN]; int i; @@ -216,7 +226,7 @@ muttq_print_item(int item) for(i = 0; i < (options_get_int("mutt_return_all_emails") ? MAX_EMAILS : 1) ; i++) if( *emails[i] ) - printf("%s\t%s\t%s\n", emails[i], + fprintf(file, "%s\t%s\t%s\n", emails[i], database[item][NAME], database[item][NOTES] == NULL ? " " : database[item][NOTES] @@ -232,7 +242,7 @@ mutt_query(char *str) struct db_enumerator e = init_db_enumerator(ENUM_ALL); printf("All items\n"); db_enumerate_items(e) - muttq_print_item(e.item); + muttq_print_item(stdout, e.item); } else { int search_fields[] = {NAME, EMAIL, NICK, -1}; int i; @@ -242,7 +252,7 @@ mutt_query(char *str) } putchar('\n'); while(i >= 0) { - muttq_print_item(i); + muttq_print_item(stdout, i); i = find_item(str, i+1, search_fields); } } @@ -291,6 +301,22 @@ make_mailstr(int item) return ret; } +void +print_stderr(int item) +{ + fprintf (stderr, "%c", '\n'); + + if( is_valid_item(item) ) + muttq_print_item(stderr, item); + else { + struct db_enumerator e = init_db_enumerator(ENUM_SELECTED); + db_enumerate_items(e) { + muttq_print_item(stderr, e.item); + } + } + +} + void launch_mutt(int item) { @@ -417,11 +443,13 @@ convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile) strlower(srcformat); strlower(dstformat); +#ifndef DEBUG if( !strcmp(srcformat, dstformat) ) { printf( "input and output formats are the same\n" "exiting...\n"); exit(1); } +#endif set_filenames(); init_options(); @@ -453,4 +481,106 @@ convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile) exit(ret); } +/* + * --add-email handling + */ + +static int add_email_count = 0; + +static void +quit_add_email() +{ + if(add_email_count > 0) { + if(save_database() < 0) { + fprintf(stderr, "cannot open %s\n", datafile); + exit(1); + } + printf("%d item(s) added to %s\n", add_email_count, datafile); + } else { + puts("Valid sender address not found"); + } + + exit(0); +} + +static void +init_add_email() +{ + set_filenames(); + atexit(free_filenames); + init_options(); + atexit(close_config); + + /* + * we don't actually care if loading fails or not + */ + load_database(datafile); + atexit(close_database); + + signal(SIGINT, quit_add_email); +} + +static int +add_email_add_item(int quiet, char *name, char *email) +{ + list_item item; + + if(!quiet) { + FILE *in = fopen("/dev/tty", "r"); + char c; + if(!in) { + fprintf(stderr, "cannot open /dev/tty\n" + "you may want to use --add-email-quiet\n"); + exit(1); + } + printf("Add ``%s <%s>'' to %s ? (y/n)\n", + name, + email, + datafile + ); + do { + c = fgetc(in); + if(c == 'n' || c == 'N') { + fclose(in); + return 0; + } + } while(c != 'y' && c != 'Y'); + fclose(in); + } + + memset(item, 0, sizeof(item)); + item[NAME] = strdup(name); + item[EMAIL] = strdup(email); + add_item2database(item); + + return 1; +} + +static void +add_email(int quiet) +{ + char *line; + char *name = NULL, *email = NULL; + + init_add_email(); + + do { + line = getaline(stdin); + if(line && !strncasecmp("From:", line, 5) ) { + getname(line, &name, &email); + my_free(line); + add_email_count += add_email_add_item(quiet, + name, email); + my_free(name); + my_free(email); + } + my_free(line); + } while( !feof(stdin) ); + + quit_add_email(); +} + +/* + * end of --add-email handling + */