+/*
+ * --add-email handling
+ */
+
+static int add_email_count = 0, add_email_found = 0;
+
+static void
+quit_add_email()
+{
+ if(add_email_count > 0) {
+ if(save_database() < 0) {
+ fprintf(stderr, _("cannot open %s\n"), datafile);
+ exit(EXIT_FAILURE);
+ }
+ printf(_("%d item(s) added to %s\n"), add_email_count, datafile);
+ } else if (add_email_found == 0) {
+ puts(_("Valid sender address not found"));
+ }
+
+ exit(EXIT_SUCCESS);
+}
+
+static void
+quit_add_email_sig(int signal)
+{
+ quit_add_email();
+}
+
+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);
+ }