+static void
+init_add_email()
+{
+ set_filenames();
+ check_abook_directory();
+ init_opts();
+ load_opts(rcfile);
+ init_standard_fields();
+ atexit(free_opts);
+
+ /*
+ * 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(opt_get_bool(BOOL_ADD_EMAIL_PREVENT_DUPLICATES)) {
+ int search_fields[] = { EMAIL, -1 };
+ if(find_item(email, 0, search_fields) >= 0) {
+ if(!quiet)
+ printf(_("Address %s already in addressbook\n"),
+ email);
+ return 0;
+ }
+ }
+
+ 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(EXIT_FAILURE);
+ }
+
+ do {
+ printf(_("Add \"%s <%s>\" to %s? (%c/%c)\n"),
+ name,
+ email,
+ datafile,
+ *S_("keybinding for yes|y"),
+ *S_("keybinding for no|n"));
+ c = tolower(getc(in));
+ if(c == *S_("keybinding for no|n")) {
+ fclose(in);
+ return 0;
+ }
+ } while(c != *S_("keybinding for yes|y"));
+ fclose(in);
+ }
+
+ item = item_create();
+ item_fput(item, NAME, xstrdup(name));
+ item_fput(item, EMAIL, xstrdup(email));
+ add_item2database(item);
+ item_free(&item);
+
+ return 1;
+}
+
+static void
+add_email(int quiet)
+{
+ char *line;
+ char *name = NULL, *email = NULL;
+ struct stat s;
+
+ if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode) ) {
+ fprintf(stderr, _("stdin is a directory or cannot stat stdin\n"));
+ exit(EXIT_FAILURE);
+ }
+
+ init_add_email();
+
+ do {
+ line = getaline(stdin);
+ if(line && !strncasecmp("From:", line, 5) ) {
+ add_email_found++;
+ getname(line, &name, &email);
+ add_email_count += add_email_add_item(quiet,
+ name, email);
+ xfree(name);
+ xfree(email);
+ }
+ xfree(line);
+ } while( !feof(stdin) );
+
+ quit_add_email();
+}
+
+/*
+ * end of --add-email handling
+ */