--- /dev/null
+Return-Path: <novalazy@gmail.com>\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 18DF7429E30\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:34 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 Pxfk0fmgzURE for <notmuch@notmuchmail.org>;\r
+ Sat, 22 Jun 2013 21:24:24 -0700 (PDT)\r
+Received: from mail-pb0-f47.google.com (mail-pb0-f47.google.com\r
+ [209.85.160.47]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 7F007431FBD\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:11 -0700 (PDT)\r
+Received: by mail-pb0-f47.google.com with SMTP id rr13so9474791pbb.6\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:10 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
+ :mime-version:content-type:content-transfer-encoding;\r
+ bh=UCqEGTYOHAP7NjtDAsNmvHf6yp1XQ2I5tdxfDHe3djk=;\r
+ b=rmgMud4K9jEVQ/kcIQvnm5Z/+mEVckIfMmk7ZSzCCRTX+Q0si417jmy+th6uVsxR91\r
+ yyvlz+Q8qV9TUGF6JVSXxOTbeUrMCNXzgxtpz3lZbqDwcG54llk1YOgbqQJC+z3nIsj2\r
+ /DByAXXh9scYVoEqeWTz6CYda8JcEFvcEOzs0cy2SeitFt6+F5ucAI2V7QlgNvaNRyUg\r
+ H1f4dttWtEbHJOLgQ92e7q5Eyre+9k4AyB+iRs9eMQdGmk2RoFUaTuLNgsYshMxCcjDp\r
+ xOVheog0L325tFqlxEygmF1A1JLrjGCJfM2JKRIDZbCw/84aRBU220hKK77sUSxVex16\r
+ UpHQ==\r
+X-Received: by 10.66.164.232 with SMTP id yt8mr22407973pab.21.1371961450776;\r
+ Sat, 22 Jun 2013 21:24:10 -0700 (PDT)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215]) by mx.google.com with ESMTPSA id\r
+ pb5sm11840797pbc.29.2013.06.22.21.24.08 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 22 Jun 2013 21:24:10 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7 03/12] cli: add insert command\r
+Date: Sun, 23 Jun 2013 14:23:56 +1000\r
+Message-Id: <1371961445-15182-4-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1371961445-15182-1-git-send-email-novalazy@gmail.com>\r
+References: <1371961445-15182-1-git-send-email-novalazy@gmail.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\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: Sun, 23 Jun 2013 04:24:34 -0000\r
+\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
+ notmuch.c | 2 +\r
+ 4 files changed, 341 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 = \\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
+ \r
+ int\r
++notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]);\r
++\r
++int\r
+ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);\r
+ \r
+ int\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+new file mode 100644\r
+index 0000000..591189f\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 © 2013 Peter Wang\r
++ *\r
++ * Based in part on notmuch-deliver\r
++ * Copyright © 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[] = "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 = 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) == -1) {\r
++ strncpy (hostname, "unknown", len);\r
++ }\r
++ hostname[len - 1] = '\0';\r
++\r
++ for (p = hostname; *p != '\0'; p++) {\r
++ if (*p == '/' || *p == ':')\r
++ *p = '_';\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 = open (dir, O_RDONLY);\r
++ if (fd == -1) {\r
++ fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno));\r
++ return FALSE;\r
++ }\r
++ ret = (fsync (fd) == 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 'tmp' sub-directory of dir.\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 = -1;\r
++\r
++ /* We follow the Dovecot file name generation algorithm. */\r
++ pid = getpid ();\r
++ safe_gethostname (hostname, sizeof (hostname));\r
++ do {\r
++ gettimeofday (&tv, NULL);\r
++ filename = 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 = 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 = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);\r
++ } while (fd == -1 && errno == EEXIST);\r
++\r
++ if (fd == -1) {\r
++ fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno));\r
++ return -1;\r
++ }\r
++\r
++ *newdir = talloc_asprintf (ctx, "%s/new", dir);\r
++ *newpath = 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 = TRUE;\r
++\r
++ while (! interrupted) {\r
++ ssize_t remain;\r
++ char buf[4096];\r
++ char *p;\r
++\r
++ remain = read (fdin, buf, sizeof (buf));\r
++ if (remain == 0)\r
++ break;\r
++ if (remain < 0) {\r
++ if (errno == EINTR)\r
++ continue;\r
++ fprintf (stderr, "Error: reading from standard input: %s\n",\r
++ strerror (errno));\r
++ return FALSE;\r
++ }\r
++\r
++ p = buf;\r
++ do {\r
++ ssize_t written = write (fdout, p, remain);\r
++ if (written < 0 && errno == EINTR)\r
++ continue;\r
++ if (written <= 0) {\r
++ fprintf (stderr, "Error: writing to temporary file: %s",\r
++ strerror (errno));\r
++ return FALSE;\r
++ }\r
++ p += written;\r
++ remain -= written;\r
++ empty = 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
++ notmuch_message_t *message;\r
++ notmuch_status_t status;\r
++\r
++ status = 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 == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
++ /* Don't change tags of an existing message. */\r
++ status = notmuch_message_tags_to_maildir_flags (message);\r
++ if (status != 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 = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);\r
++ if (fdout < 0)\r
++ return FALSE;\r
++\r
++ cleanup_path = tmppath;\r
++\r
++ if (! copy_stdin (fdin, fdout))\r
++ goto FAIL;\r
++\r
++ if (fsync (fdout) != 0) {\r
++ fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
++ goto FAIL;\r
++ }\r
++\r
++ close (fdout);\r
++ fdout = -1;\r
++\r
++ /* Atomically move the new message file from the Maildir 'tmp' directory\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_delivery\r
++ */\r
++ if (rename (tmppath, newpath) != 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 >= 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 = NULL;\r
++ const char *maildir;\r
++ int opt_index = 1;\r
++ unsigned int i;\r
++ notmuch_bool_t ret;\r
++\r
++ db_path = notmuch_config_get_database_path (config);\r
++ new_tags = notmuch_config_get_new_tags (config, &new_tags_length);\r
++\r
++ tag_ops = tag_op_list_create (config);\r
++ if (tag_ops == NULL) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return 1;\r
++ }\r
++ for (i = 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_index,\r
++ &query_string, tag_ops))\r
++ return 1;\r
++\r
++ if (*query_string != '\0') {\r
++ fprintf (stderr, "Error: unexpected query string: %s\n", query_string);\r
++ return 1;\r
++ }\r
++\r
++ maildir = db_path;\r
++\r
++ /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
++ * from standard input may be interrupted. */\r
++ memset (&action, 0, sizeof (struct sigaction));\r
++ action.sa_handler = handle_sigint;\r
++ sigemptyset (&action.sa_mask);\r
++ action.sa_flags = 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 = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_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..d11f214 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -44,6 +44,8 @@ static command_t commands[] = {\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
++ "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
+-- \r
+1.7.12.1\r
+\r