--- /dev/null
+Return-Path: <m.walters@qmul.ac.uk>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 43442431FB6\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 00:49:10 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+ tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+ NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id zTzfilsm46Rd for <notmuch@notmuchmail.org>;\r
+ Sat, 22 Jun 2013 00:49:02 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 8907B431FAE\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 00:49:01 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+ by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UqIZ9-0007eH-MF; Sat, 22 Jun 2013 08:49:00 +0100\r
+Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UqIZ8-0001Du-VH; Sat, 22 Jun 2013 08:48:59 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Peter Wang <novalazy@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v6 03/12] cli: add insert command\r
+In-Reply-To: <1371880008-18312-4-git-send-email-novalazy@gmail.com>\r
+References: <1371880008-18312-1-git-send-email-novalazy@gmail.com>\r
+ <1371880008-18312-4-git-send-email-novalazy@gmail.com>\r
+User-Agent: Notmuch/0.15.2+171~ge2f30a2 (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Sat, 22 Jun 2013 08:48:56 +0100\r
+Message-ID: <87hagqvb4n.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+X-Sender-Host-Address: 93.97.24.31\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: 1d3c093fd17cca711a7face32803b846 (of first 20000 bytes)\r
+X-SpamAssassin-Score: -0.0\r
+X-SpamAssassin-SpamBar: /\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+ determine if it is\r
+ spam. We require at least 5.0 points to mark a message as spam.\r
+ This message scored -0.0 points.\r
+ Summary of the scoring: \r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+ * -0.0 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 22 Jun 2013 07:49:10 -0000\r
+\r
+On Sat, 22 Jun 2013, Peter Wang <novalazy@gmail.com> wrote:\r
+> The notmuch insert command reads a message from standard input,\r
+> writes it to a Maildir folder, and then incorporates the message into\r
+> the notmuch database. Essentially it moves the functionality of\r
+> notmuch-deliver into notmuch.\r
+>\r
+> Though it could be used as an alternative to notmuch new, the reason\r
+> I want this is to allow my notmuch frontend to add postponed or sent\r
+> messages to the mail store and notmuch database, without resorting to\r
+> another tool (e.g. notmuch-deliver) nor directly modifying the maildir.\r
+> ---\r
+> Makefile.local | 1 +\r
+> notmuch-client.h | 3 +\r
+> notmuch-insert.c | 335 +++++++++++++++++++++++++++++++++++++++++++++++++=\r
+++++++\r
+> notmuch.c | 3 +\r
+> 4 files changed, 342 insertions(+)\r
+> create mode 100644 notmuch-insert.c\r
+>\r
+> diff --git a/Makefile.local b/Makefile.local\r
+> index 644623f..84043fe 100644\r
+> --- a/Makefile.local\r
+> +++ b/Makefile.local\r
+> @@ -261,6 +261,7 @@ notmuch_client_srcs =3D \\r
+> notmuch-config.c \\r
+> notmuch-count.c \\r
+> notmuch-dump.c \\r
+> + notmuch-insert.c \\r
+> notmuch-new.c \\r
+> notmuch-reply.c \\r
+> notmuch-restore.c \\r
+> diff --git a/notmuch-client.h b/notmuch-client.h\r
+> index 4a3c7ac..dfe81e6 100644\r
+> --- a/notmuch-client.h\r
+> +++ b/notmuch-client.h\r
+> @@ -181,6 +181,9 @@ int\r
+> notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]);\r
+>=20=20\r
+> int\r
+> +notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]=\r
+);\r
+> +\r
+> +int\r
+> notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);\r
+>=20=20\r
+> int\r
+> diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+> new file mode 100644\r
+> index 0000000..1495234\r
+> --- /dev/null\r
+> +++ b/notmuch-insert.c\r
+> @@ -0,0 +1,335 @@\r
+> +/* notmuch - Not much of an email program, (just index and search)\r
+> + *\r
+> + * Copyright =C2=A9 2013 Peter Wang\r
+> + *\r
+> + * Based in part on notmuch-deliver\r
+> + * Copyright =C2=A9 2010 Ali Polatel\r
+> + *\r
+> + * This program is free software: you can redistribute it and/or modify\r
+> + * it under the terms of the GNU General Public License as published by\r
+> + * the Free Software Foundation, either version 3 of the License, or\r
+> + * (at your option) any later version.\r
+> + *\r
+> + * This program is distributed in the hope that it will be useful,\r
+> + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+> + * GNU General Public License for more details.\r
+> + *\r
+> + * You should have received a copy of the GNU General Public License\r
+> + * along with this program. If not, see http://www.gnu.org/licenses/ .\r
+> + *\r
+> + * Author: Peter Wang <novalazy@gmail.com>\r
+> + */\r
+> +\r
+> +#include "notmuch-client.h"\r
+> +#include "tag-util.h"\r
+> +\r
+> +#include <sys/types.h>\r
+> +#include <sys/stat.h>\r
+> +#include <fcntl.h>\r
+> +\r
+> +static volatile sig_atomic_t interrupted;\r
+> +\r
+> +static void\r
+> +handle_sigint (unused (int sig))\r
+> +{\r
+> + static char msg[] =3D "Stopping... \n";\r
+> +\r
+> + /* This write is "opportunistic", so it's okay to ignore the\r
+> + * result. It is not required for correctness, and if it does\r
+> + * fail or produce a short write, we want to get out of the signal\r
+> + * handler as quickly as possible, not retry it. */\r
+> + IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));\r
+> + interrupted =3D 1;\r
+> +}\r
+> +\r
+> +/* Like gethostname but guarantees that a null-terminated hostname is\r
+> + * returned, even if it has to make one up. Invalid characters are\r
+> + * substituted such that the hostname can be used within a filename.\r
+> + */\r
+> +static void\r
+> +safe_gethostname (char *hostname, size_t len)\r
+> +{\r
+> + char *p;\r
+> +\r
+> + if (gethostname (hostname, len) =3D=3D -1) {\r
+> + strncpy (hostname, "unknown", len);\r
+> + }\r
+> + hostname[len - 1] =3D '\0';\r
+> +\r
+> + for (p =3D hostname; *p !=3D '\0'; p++) {\r
+> + if (*p =3D=3D '/' || *p =3D=3D ':')\r
+> + *p =3D '_';\r
+> + }\r
+> +}\r
+> +\r
+> +/* Call fsync() on a directory path. */\r
+> +static notmuch_bool_t\r
+> +sync_dir (const char *dir)\r
+> +{\r
+> + notmuch_bool_t ret;\r
+> + int fd;\r
+> +\r
+> + fd =3D open (dir, O_RDONLY);\r
+> + if (fd =3D=3D -1) {\r
+> + fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno));\r
+> + return FALSE;\r
+> + }\r
+> + ret =3D (fsync (fd) =3D=3D 0);\r
+> + if (! ret) {\r
+> + fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno));\r
+> + }\r
+> + close (fd);\r
+> + return ret;\r
+> +}\r
+> +\r
+> +/* Open a unique file in the Maildir 'tmp' directory.\r
+\r
+perhaps add "in the folder dir" or similar?\r
+\r
+> + * Returns the file descriptor on success, or -1 on failure.\r
+> + * On success, file paths for the message in the 'tmp' and 'new'\r
+> + * directories are returned via tmppath and newpath,\r
+> + * and the path of the 'new' directory itself in newdir. */\r
+> +static int\r
+> +maildir_open_tmp_file (void *ctx, const char *dir,\r
+> + char **tmppath, char **newpath, char **newdir)\r
+> +{\r
+> + pid_t pid;\r
+> + char hostname[256];\r
+> + struct timeval tv;\r
+> + char *filename;\r
+> + int fd =3D -1;\r
+> +\r
+> + /* We follow the Dovecot file name generation algorithm. */\r
+> + pid =3D getpid ();\r
+> + safe_gethostname (hostname, sizeof (hostname));\r
+> + do {\r
+> + gettimeofday (&tv, NULL);\r
+> + filename =3D talloc_asprintf (ctx, "%ld.M%ldP%d.%s",\r
+> + tv.tv_sec, tv.tv_usec, pid, hostname);\r
+> + if (! filename) {\r
+> + fprintf (stderr, "Out of memory\n");\r
+> + return -1;\r
+> + }\r
+> +\r
+> + *tmppath =3D talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);\r
+> + if (! *tmppath) {\r
+> + fprintf (stderr, "Out of memory\n");\r
+> + return -1;\r
+> + }\r
+> +\r
+> + fd =3D open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);\r
+> + } while (fd =3D=3D -1 && errno =3D=3D EEXIST);\r
+> +\r
+> + if (fd =3D=3D -1) {\r
+> + fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno));\r
+> + return -1;\r
+> + }\r
+> +\r
+> + *newdir =3D talloc_asprintf (ctx, "%s/new", dir);\r
+> + *newpath =3D talloc_asprintf (ctx, "%s/new/%s", dir, filename);\r
+> + if (! *newdir || ! *newpath) {\r
+> + fprintf (stderr, "Out of memory\n");\r
+> + close (fd);\r
+> + unlink (*tmppath);\r
+> + return -1;\r
+> + }\r
+> +\r
+> + talloc_free (filename);\r
+> +\r
+> + return fd;\r
+> +}\r
+> +\r
+> +/* Copy the contents of standard input (fdin) into fdout.\r
+> + * Returns TRUE if a non-empty file was written successfully.\r
+> + * Otherwise, return FALSE. */\r
+> +static notmuch_bool_t\r
+> +copy_stdin (int fdin, int fdout)\r
+> +{\r
+> + notmuch_bool_t empty =3D TRUE;\r
+> +\r
+> + while (! interrupted) {\r
+> + ssize_t remain;\r
+> + char buf[4096];\r
+> + char *p;\r
+> +\r
+> + remain =3D read (fdin, buf, sizeof (buf));\r
+> + if (remain =3D=3D 0)\r
+> + break;\r
+> + if (remain < 0) {\r
+> + if (errno =3D=3D EINTR)\r
+> + continue;\r
+> + fprintf (stderr, "Error: reading from standard input: %s\n",\r
+> + strerror (errno));\r
+> + return FALSE;\r
+> + }\r
+> +\r
+> + p =3D buf;\r
+> + do {\r
+> + ssize_t written =3D write (fdout, p, remain);\r
+> + if (written < 0 && errno =3D=3D EINTR)\r
+> + continue;\r
+> + if (written <=3D 0) {\r
+> + fprintf (stderr, "Error: writing to temporary file: %s",\r
+> + strerror (errno));\r
+> + return FALSE;\r
+> + }\r
+> + p +=3D written;\r
+> + remain -=3D written;\r
+> + empty =3D FALSE;\r
+> + } while (remain > 0);\r
+> + }\r
+> +\r
+> + return (!interrupted && !empty);\r
+> +}\r
+> +\r
+> +/* Add the specified message file to the notmuch database, applying tags.\r
+> + * The file is renamed to encode notmuch tags as maildir flags. */\r
+> +static void\r
+> +add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+> + tag_op_list_t *tag_ops)\r
+> +{\r
+\r
+I think I would like the option to have the command fail if it doesn't\r
+get added to the database. If, for example, the command is used to\r
+postpone a message it may be very difficult/impossible to find it\r
+without the postponed tag that I asked for.\r
+\r
+Could we have a --fail-on-index-fail cli option which says which\r
+behaviour we want? It is a little ugly but I think different users will\r
+want different behaviour and there is a risk of dataloss in either case.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+\r
+> + notmuch_message_t *message;\r
+> + notmuch_status_t status;\r
+> +\r
+> + status =3D notmuch_database_add_message (notmuch, path, &message);\r
+> + switch (status) {\r
+> + case NOTMUCH_STATUS_SUCCESS:\r
+> + case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+> + break;\r
+> + default:\r
+> + case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
+> + case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
+> + case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
+> + case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
+> + case NOTMUCH_STATUS_FILE_ERROR:\r
+> + case NOTMUCH_STATUS_NULL_POINTER:\r
+> + case NOTMUCH_STATUS_TAG_TOO_LONG:\r
+> + case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
+> + case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
+> + case NOTMUCH_STATUS_LAST_STATUS:\r
+> + fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",\r
+> + path, notmuch_status_to_string (status));\r
+> + return;\r
+> + }\r
+> +\r
+> + if (status =3D=3D NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
+> + /* Don't change tags of an existing message. */\r
+> + status =3D notmuch_message_tags_to_maildir_flags (message);\r
+> + if (status !=3D NOTMUCH_STATUS_SUCCESS)\r
+> + fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
+> + } else {\r
+> + tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);\r
+> + }\r
+> +\r
+> + notmuch_message_destroy (message);\r
+> +}\r
+> +\r
+> +static notmuch_bool_t\r
+> +insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,\r
+> + const char *dir, tag_op_list_t *tag_ops)\r
+> +{\r
+> + char *tmppath;\r
+> + char *newpath;\r
+> + char *newdir;\r
+> + int fdout;\r
+> + char *cleanup_path;\r
+> +\r
+> + fdout =3D maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newd=\r
+ir);\r
+> + if (fdout < 0)\r
+> + return FALSE;\r
+> +\r
+> + cleanup_path =3D tmppath;\r
+> +\r
+> + if (! copy_stdin (fdin, fdout))\r
+> + goto FAIL;\r
+> +\r
+> + if (fsync (fdout) !=3D 0) {\r
+> + fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
+> + goto FAIL;\r
+> + }\r
+> +\r
+> + close (fdout);\r
+> + fdout =3D -1;\r
+> +\r
+> + /* Atomically move the new message file from the Maildir 'tmp' direc=\r
+tory\r
+> + * to the 'new' directory. We follow the Dovecot recommendation to\r
+> + * simply use rename() instead of link() and unlink().\r
+> + * See also: http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_deli=\r
+very\r
+> + */\r
+> + if (rename (tmppath, newpath) !=3D 0) {\r
+> + fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));\r
+> + goto FAIL;\r
+> + }\r
+> +\r
+> + if (! sync_dir (newdir))\r
+> + goto FAIL;\r
+> +\r
+> + /* Even if adding the message to the notmuch database fails,\r
+> + * the message is on disk and we consider the delivery completed. */\r
+> + add_file_to_database (notmuch, newpath, tag_ops);\r
+> +\r
+> + return TRUE;\r
+> +\r
+> + FAIL:\r
+> + if (fdout >=3D 0)\r
+> + close (fdout);\r
+> + unlink (cleanup_path);\r
+> + return FALSE;\r
+> +}\r
+> +\r
+> +int\r
+> +notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+> +{\r
+> + notmuch_database_t *notmuch;\r
+> + struct sigaction action;\r
+> + const char *db_path;\r
+> + const char **new_tags;\r
+> + size_t new_tags_length;\r
+> + tag_op_list_t *tag_ops;\r
+> + char *query_string =3D NULL;\r
+> + const char *maildir;\r
+> + int opt_index =3D 1;\r
+> + unsigned int i;\r
+> + notmuch_bool_t ret;\r
+> +\r
+> + db_path =3D notmuch_config_get_database_path (config);\r
+> + new_tags =3D notmuch_config_get_new_tags (config, &new_tags_length);\r
+> +\r
+> + tag_ops =3D tag_op_list_create (config);\r
+> + if (tag_ops =3D=3D NULL) {\r
+> + fprintf (stderr, "Out of memory.\n");\r
+> + return 1;\r
+> + }\r
+> + for (i =3D 0; i < new_tags_length; i++) {\r
+> + if (tag_op_list_append (tag_ops, new_tags[i], FALSE))\r
+> + return 1;\r
+> + }\r
+> +\r
+> + if (parse_tag_command_line (config, argc - opt_index, argv + opt_ind=\r
+ex,\r
+> + &query_string, tag_ops))\r
+> + return 1;\r
+> +\r
+> + if (*query_string !=3D '\0') {\r
+> + fprintf (stderr, "Error: unexpected query string: %s\n", query_string);\r
+> + return 1;\r
+> + }\r
+> +\r
+> + maildir =3D db_path;\r
+> +\r
+> + /* Setup our handler for SIGINT. We do not set SA_RESTART so that co=\r
+pying\r
+> + * from standard input may be interrupted. */\r
+> + memset (&action, 0, sizeof (struct sigaction));\r
+> + action.sa_handler =3D handle_sigint;\r
+> + sigemptyset (&action.sa_mask);\r
+> + action.sa_flags =3D 0;\r
+> + sigaction (SIGINT, &action, NULL);\r
+> +\r
+> + if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+> + NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))\r
+> + return 1;\r
+> +\r
+> + ret =3D insert_message (config, notmuch, STDIN_FILENO, maildir, tag_=\r
+ops);\r
+> +\r
+> + notmuch_database_destroy (notmuch);\r
+> +\r
+> + return (ret) ? 0 : 1;\r
+> +}\r
+> diff --git a/notmuch.c b/notmuch.c\r
+> index 45a73ce..cf5bf95 100644\r
+> --- a/notmuch.c\r
+> +++ b/notmuch.c\r
+> @@ -44,6 +44,9 @@ static command_t commands[] =3D {\r
+> "Interactively setup notmuch for first use." },\r
+> { "new", notmuch_new_command, FALSE,\r
+> "Find and import new messages to the notmuch database." },\r
+> + { "insert", notmuch_insert_command, FALSE,\r
+> + "[options...] [--] [+<tag>|-<tag> ...] < message",\r
+> + "Add a new message into the maildir and notmuch database." },\r
+> { "search", notmuch_search_command, FALSE,\r
+> "Search for messages matching the given search terms." },\r
+> { "show", notmuch_show_command, FALSE,\r
+> --=20\r
+> 1.7.12.1\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r