X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=abook.c;h=c7efa0992a9a96bf49e9ee253e6d51f8b16a8303;hb=00afcdbf62dbf2233c2e09fc9b0abe305a6988e7;hp=dadf41e4383bdf491ac66a7889d4e1ded50d5791;hpb=12a57d9405d7910566d5ff888d743d29b0716554;p=pkg%2Fabook.git diff --git a/abook.c b/abook.c index dadf41e..c7efa09 100644 --- a/abook.c +++ b/abook.c @@ -1,9 +1,9 @@ /* * $Id$ * - * by JH + * by JH * - * Copyright (C) 1999, 2000 Jaakko Heinonen + * Copyright (C) Jaakko Heinonen */ #include @@ -12,108 +12,50 @@ #include #include #include -#include "abook_curses.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) # include #endif -#ifdef HAVE_TERMIOS_H -# include -#else -# ifdef HAVE_LINUX_TERMIOS_H -# include -# endif -#endif -#ifdef HAVE_SYS_IOCTL_H -# include -#endif #include "abook.h" +#include "ui.h" #include "database.h" #include "list.h" #include "filter.h" #include "edit.h" #include "misc.h" -#include "help.h" #include "options.h" -#include "estr.h" +#include "getname.h" static void init_abook(); static void set_filenames(); static void free_filenames(); -static void display_help(char **tbl); -static void get_commands(); static void parse_command_line(int argc, char **argv); static void show_usage(); static void mutt_query(char *str); static void init_mutt_query(); static void quit_mutt_query(); -static void launch_mutt(); -static void launch_lynx(); -static void win_changed(int dummy); -static void open_datafile(); -#ifdef SIGWINCH -static void resize_abook(); -#endif static void convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile); - -int should_resize = FALSE; -int can_resize = FALSE; +static void add_email(int); char *datafile = NULL; char *rcfile = NULL; -WINDOW *top = NULL, *bottom = NULL; - -static void -init_windows() -{ - top = newwin(LIST_TOP - 1, COLS, 0, 0); - - bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0); -} - -static void -free_windows() -{ - delwin(top); - delwin(bottom); -} - static void init_abook() { set_filenames(); init_options(); - initscr(); cbreak(); noecho(); - nonl(); - intrflush(stdscr, FALSE); - keypad(stdscr, TRUE); -#ifdef DEBUG - fprintf(stderr, "init_abook():\n"); - fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES); -#endif - if( LINES < MIN_LINES || COLS < MIN_COLS ) { - clear(); refresh(); endwin(); - fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES); - fprintf(stderr, "Terminal is too small. Minium terminal size " - "for abook is " - "%dx%d\n", MIN_COLS, MIN_LINES); - exit(1); - } - umask(DEFAULT_UMASK); -#ifdef SIGWINCH - signal(SIGWINCH, win_changed); -#endif - signal(SIGINT, quit_abook); signal(SIGKILL, quit_abook); signal(SIGTERM, quit_abook); - init_list(); - init_windows(); + if( init_ui() ) + exit(1); + + umask(DEFAULT_UMASK); /* * this is very ugly for now @@ -144,23 +86,14 @@ quit_abook() { if( options_get_int("autosave") ) save_database(); - else { - statusline_addstr("Save database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': - save_database(); - default: break; - } - } + else if( statusline_ask_boolean("Save database", TRUE) ) + save_database(); + close_config(); close_database(); - close_list(); - free_windows(); - clear(); - refresh(); - endwin(); + close_ui(); + exit(0); } @@ -207,299 +140,6 @@ free_filenames() my_free(datafile); } -void -headerline(char *str) -{ - werase(top); - - mvwhline(top, 1, 0, ACS_HLINE, COLS); - - mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str); - - refresh(); - wrefresh(top); -} - - -void -refresh_screen() -{ -#ifdef SIGWINCH - if( should_resize ) { - resize_abook(); - return; - } -#endif - clear(); - - refresh_statusline(); - headerline(MAIN_HELPLINE); - list_headerline(); - - refresh_list(); -} - -#ifdef DEBUG -extern int curitem; -extern list_item *database; -static void -dump_item() -{ - int i; - - fprintf(stderr,"sizeof(list_item) = %d\n", sizeof(list_item)); - fprintf(stderr,"--- dumping item %d ---\n", curitem); - - for(i=0; i= 0) { - selected[curitem] = !selected[curitem]; - print_number_of_items(); - refresh_list(); - } - break; - case '+': select_all(); - refresh_list(); - break; - case '-': select_none(); - refresh_list(); - break; - case '*': invert_selection(); - refresh_list(); - break; - case 'A': move_curitem(MOVE_ITEM_UP); - break; - case 'Z': move_curitem(MOVE_ITEM_DOWN); - break; - - case 'm': launch_mutt(); break; - - case 'p': print_database(); break; - - case 'u': launch_lynx(); break; -#ifdef DEBUG - case 'D': dump_item(); -#endif - } - } -} - - -static void -display_help(char **tbl) -{ - int i, j = 3; - - erase(); - headerline("help"); - refresh_statusline(); - - for( i = 0; tbl[i] != NULL; i++) { - mvaddstr(j++, 0, tbl[i]); - if( ( !( (i+1) % (LINES-7) ) ) || - (tbl[i+1] == NULL) ) { - refresh(); - statusline_msg("Press any key to continue..."); - erase(); - refresh_statusline(); - headerline("help"); - j = 3; - } - } - refresh_screen(); -} - -void -display_editor_help(WINDOW *w) -{ - int i; - - werase(w); - - headerline("editor help"); - - for( i = 0; editorhelp[i] != NULL; i++) { - waddstr(w, editorhelp[i]); - if( ( !( (i+1) % (LINES-8) ) ) || - (editorhelp[i+1] == NULL) ) { - refresh(); - wrefresh(w); - statusline_msg("Press any key to continue..."); - wclear(w); - } - } -} - - -void -statusline_msg(char *msg) -{ - clear_statusline(); - statusline_addstr(msg); - getch(); -#ifdef DEBUG - fprintf(stderr, "statusline_msg(\"%s\")\n", msg); -#endif - clear_statusline(); -} - -void -statusline_addstr(char *str) -{ - mvwaddstr(bottom, 1, 0, str); - refresh(); - wrefresh(bottom); -} - -/* - * function statusline_getnstr - * - * parameters: - * (char *str) - * if n >= 0 str is a pointer which points a place where to store - * the string, else str is ingnored - * (int n) - * the maximum length of the string - * If n < 0 function will allocate needed space for the string. - * Value 0 is not allowed for n. - * (int use_filesel) - * if this value is nonzero the fileselector is enabled - * - * returns (char *) - * If n < 0 a pointer to a newly allocated string is returned. - * If n > 0 a nonzero value is returned if user has typed a valid - * string. If not NULL value is returned. Never really use the - * _pointer_ if n > 0. - * - */ - -char * -statusline_getnstr(char *str, int n, int use_filesel) -{ - char *buf; - int y, x; - - getyx(bottom, y, x); - wmove(bottom, 1, x); - - buf = wenter_string(bottom, n, - (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP); - - if(n < 0) - return buf; - - if(buf == NULL) - str[0] = 0; - else - strncpy(str, buf, n); - - str[n-1] = 0; - - free(buf); - - return buf; -} - -void -refresh_statusline() -{ - werase(bottom); - - mvwhline(bottom, 0, 0, ACS_HLINE, COLS); - mvwhline(bottom, 2, 0, ACS_HLINE, COLS); - - refresh(); - wrefresh(bottom); -} - - -char * -ask_filename(char *prompt, int flags) -{ - char *buf = NULL; - - clear_statusline(); - - statusline_addstr(prompt); - buf = statusline_getnstr(NULL, -1, flags); - - clear_statusline(); - - return buf; -} - -void -clear_statusline() -{ - wmove(bottom, 1, 0); - wclrtoeol(bottom); - wrefresh(bottom); - refresh(); -} - static void parse_command_line(int argc, char **argv) { @@ -509,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); @@ -527,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]); @@ -556,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(); @@ -566,10 +214,9 @@ show_usage() } extern list_item *database; -extern int items; static void -muttq_print_item(int item) +muttq_print_item(FILE *file, int item) { char emails[MAX_EMAILS][MAX_EMAIL_LEN]; int i; @@ -579,80 +226,35 @@ 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] ); } -static int -mutt_query_name(char *str) -{ - int i, j; - char *tmp; - - for(i = 0, j = 0 ; i < items; i++) { - tmp = strdup(database[i][NAME]); - if( strstr( strupper(tmp), strupper(str) ) != NULL ) { - if( !j ) - putchar('\n'); - muttq_print_item(i); - j++; - } - free(tmp); - } - - return j; -} - -static int -mutt_query_email(char *str) -{ - int i, j, k; - char *tmp, emails[MAX_EMAILS][MAX_EMAIL_LEN]; - - for(i = 0, j = 0; i < items; i++) { - split_emailstr(i, emails); - for(k = 0; k < MAX_EMAILS; k++) { - if( *emails[k] ) { - tmp = strdup( emails[k] ); - if( strstr( strupper(tmp), strupper(str) ) != NULL ) { - if( !j ) - putchar('\n'); - j++; - if( options_get_int("mutt_return_all_emails") ) { - muttq_print_item(i); - free(tmp); - break; - } else - printf("%s\t%s\n", emails[k], - database[i][NAME]); - } - free(tmp); - } - } - } - - return j; -} - static void mutt_query(char *str) { - int i; - init_mutt_query(); if( str == NULL || !strcasecmp(str, "all") ) { + struct db_enumerator e = init_db_enumerator(ENUM_ALL); printf("All items\n"); - for(i = 0; i < items; i++) - muttq_print_item(i); + db_enumerate_items(e) + muttq_print_item(stdout, e.item); } else { - if( !mutt_query_name(str) && !mutt_query_email(str) ) { + int search_fields[] = {NAME, EMAIL, NICK, -1}; + int i; + if( (i = find_item(str, 0, search_fields)) < 0 ) { printf("Not found\n"); quit_mutt_query(1); } + putchar('\n'); + while(i >= 0) { + muttq_print_item(stdout, i); + i = find_item(str, i+1, search_fields); + } } quit_mutt_query(0); @@ -681,58 +283,90 @@ quit_mutt_query(int status) } -static void -launch_mutt() +static char * +make_mailstr(int item) { - int i; char email[MAX_EMAIL_LEN]; - char *cmd; - char *tmp = options_get_str("mutt_command"); + char *ret; + char *name = mkstr("\"%s\"", database[item][NAME]); - if(curitem < 0) - return; + get_first_email(email, item); + + ret = *database[item][EMAIL] ? + mkstr("%s <%s>", name, email) : + strdup(name); + + free(name); + + 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) +{ + char *cmd = NULL, *mailstr = NULL; + char *mutt_command = options_get_str("mutt_command"); - cmd = strconcat(tmp, " '", NULL ); + if(mutt_command == NULL || !*mutt_command) + return; - for(i=0; i < items; i++) { - if( ! selected[i] && i != curitem ) - continue; - get_first_email(email, i); - tmp = mkstr("%s \"%s\"", cmd, database[i][NAME]); - my_free(cmd); - if( *database[i][EMAIL] ) { - cmd = mkstr("%s <%s>", tmp, email); + if( is_valid_item(item) ) + mailstr = make_mailstr(item); + else { + struct db_enumerator e = init_db_enumerator(ENUM_SELECTED); + char *tmp = NULL; + db_enumerate_items(e) { + tmp = mailstr; + mailstr = tmp ? + strconcat(tmp, ",", make_mailstr(e.item), NULL): + strconcat(make_mailstr(e.item), NULL); free(tmp); - tmp = cmd; } - cmd = strconcat(tmp, " ", NULL); - free(tmp); } - tmp = mkstr("%s%c", cmd, '\''); - free(cmd); - cmd = tmp; + cmd = strconcat(mutt_command, " \'", mailstr, + "\'", NULL); + free(mailstr); #ifdef DEBUG fprintf(stderr, "cmd: %s\n", cmd); #endif system(cmd); - free(cmd); - refresh_screen(); + + /* + * we need to make sure that curses settings are correct + */ + ui_init_curses(); } -static void -launch_lynx() +void +launch_wwwbrowser(int item) { char *cmd = NULL; - if(curitem < 0) + if( !is_valid_item(item) ) return; - if( database[curitem][URL] ) + if( database[item][URL] ) cmd = mkstr("%s '%s'", options_get_str("www_command"), - safe_str(database[curitem][URL])); + safe_str(database[item][URL])); else return; @@ -740,7 +374,11 @@ launch_lynx() system(cmd); free(cmd); - refresh_screen(); + + /* + * we need to make sure that curses settings are correct + */ + ui_init_curses(); } void * @@ -749,7 +387,7 @@ abook_malloc(size_t size) void *ptr; if ( (ptr = malloc(size)) == NULL ) { - if(top) /* determinate if init_abook has been called */ + if( is_ui_initialized() ) quit_abook(); perror("malloc() failed"); exit(1); @@ -767,7 +405,7 @@ abook_realloc(void *ptr, size_t size) return NULL; if( ptr == NULL ) { - if(top) /* determinate if init_abook has been called */ + if( is_ui_initialized() ) quit_abook(); perror("realloc() failed"); exit(1); @@ -791,49 +429,6 @@ abook_fopen (const char *path, const char *mode) } -static void -win_changed(int i) -{ - if( can_resize ) - resize_abook(); - else - should_resize = TRUE; -} - -#ifdef SIGWINCH -static void -resize_abook() -{ -#ifdef TIOCGWINSZ - struct winsize winsz; - - ioctl (0, TIOCGWINSZ, &winsz); -#ifdef DEBUG - if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) { - fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row); - } -#endif - - if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) { -#ifdef HAVE_RESIZETERM - resizeterm(winsz.ws_row, winsz.ws_col); -#else - COLS = winsz.ws_col; - LINES = winsz.ws_row; -#endif - } - - should_resize = FALSE; - close_list(); /* we need to recreate windows */ - init_list(); - free_windows(); - init_windows(); - refresh_screen(); - refresh(); -#endif /* TIOCGWINSZ */ -} -#endif /* SIGWINCH */ - static void convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile) @@ -848,33 +443,40 @@ 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(); switch( import(srcformat, srcfile) ) { case -1: - printf("input format %s not supported\n", srcformat); + fprintf(stderr, + "input format %s not supported\n", srcformat); ret = 1; + break; case 1: - printf("cannot read file %s\n", srcfile); + fprintf(stderr, "cannot read file %s\n", srcfile); ret = 1; + break; } if(!ret) switch( export(dstformat, dstfile) ) { case -1: - printf("output format %s not supported\n", - dstformat); + fprintf(stderr, + "output format %s not supported\n", + dstformat); ret = 1; break; case 1: - printf("cannot write file %s\n", dstfile); + fprintf(stderr, + "cannot write file %s\n", dstfile); ret = 1; break; } @@ -884,43 +486,111 @@ convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile) exit(ret); } +/* + * --add-email handling + */ + +static int add_email_count = 0; static void -open_datafile() +quit_add_email() { - char *filename; + 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"); + } - filename = ask_filename("File to open: ", 1); + exit(0); +} - if( !filename ) { - refresh_screen(); - return; - } +static void +quit_add_email_sig(int signal) +{ + quit_add_email(); +} - if( options_get_int("autosave") ) - save_database(); - else { - statusline_addstr("Save current database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': - save_database(); - default: break; +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_sig); +} + +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); } - close_database(); + memset(item, 0, sizeof(item)); + item[NAME] = strdup(name); + item[EMAIL] = strdup(email); + add_item2database(item); - load_database(filename); + return 1; +} - if( items == 0 ) { - statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook"); - load_database(datafile); - } else { - free(datafile); - datafile = strdup(filename); - } +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); + add_email_count += add_email_add_item(quiet, + name, email); + my_free(name); + my_free(email); + } + my_free(line); + } while( !feof(stdin) ); - refresh_screen(); - free(filename); + quit_add_email(); } + +/* + * end of --add-email handling + */