--- /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 86A9E431FBC\r
+ for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:42:33 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.099\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.099 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_NONE=-0.0001] 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 UUxSU7q4fmU7 for <notmuch@notmuchmail.org>;\r
+ Tue, 2 Apr 2013 15:42:30 -0700 (PDT)\r
+Received: from mail-pd0-f177.google.com (mail-pd0-f177.google.com\r
+ [209.85.192.177]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 27364431FC2\r
+ for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:42:27 -0700 (PDT)\r
+Received: by mail-pd0-f177.google.com with SMTP id u11so486031pdi.22\r
+ for <notmuch@notmuchmail.org>; Tue, 02 Apr 2013 15:42:26 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
+ :references:mime-version:content-type:content-transfer-encoding;\r
+ bh=zRs7R1aE5gXB3AGJHSoTAdxGp1TZIs9MfQ8M+Dfl5ME=;\r
+ b=iQ3l+DDrH43HhMZQjDL9eg6+diKY7377MnHRyjXW8tDkB6NKTaQQWlAAINC//TRlhZ\r
+ UpYcZKAHZ2/a4gORlxLyMRnd2B4fm3grSXH4Z9AYqHpwd28szV29JC/BqIr3/r8MD6mp\r
+ sWCpCSoovzrQgRmmoSCZKteJ+Ng7R4H6yLpaIv56YeRAN/zxezU6JVmW3LmxygDoku5+\r
+ bU35+5b4ke9wR7ARA+0DoxubGHe8DN9VHwWrucM4g6K2oYxSz1MWViI267qxT9wQoekU\r
+ eSzGGENrNftUgFcVYl3jg5ywkFhlRVftjgpVXTH1yCvORQDwTesznPihHM6FZu78gG4L\r
+ KaEw==\r
+X-Received: by 10.68.170.98 with SMTP id al2mr27124662pbc.87.1364942546332;\r
+ Tue, 02 Apr 2013 15:42:26 -0700 (PDT)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215])\r
+ by mx.google.com with ESMTPS id gn2sm3257803pbb.22.2013.04.02.15.42.22\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Tue, 02 Apr 2013 15:42:25 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 03/12] cli: add insert command\r
+Date: Wed, 3 Apr 2013 09:41:48 +1100\r
+Message-Id: <1364942517-6982-4-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1364942517-6982-1-git-send-email-novalazy@gmail.com>\r
+References: <1364942517-6982-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: Tue, 02 Apr 2013 22:42:33 -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 | 336 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ notmuch.c | 3 +\r
+ 4 files changed, 343 insertions(+)\r
+ create mode 100644 notmuch-insert.c\r
+\r
+diff --git a/Makefile.local b/Makefile.local\r
+index c274f07..bb2381d 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 45749a6..da332f3 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -180,6 +180,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..19b1cf9\r
+--- /dev/null\r
++++ b/notmuch-insert.c\r
+@@ -0,0 +1,336 @@\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 Maildir 'tmp' directory.\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
++static notmuch_bool_t\r
++copy_stdin (int fdin, int fdout)\r
++{\r
++ char buf[4096];\r
++ char *p;\r
++ ssize_t remain;\r
++ ssize_t written;\r
++\r
++ while (! interrupted) {\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
++ 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
++ } while (remain > 0);\r
++ }\r
++\r
++ return ! interrupted;\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 notmuch_bool_t\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 FALSE;\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
++ status = tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);\r
++ }\r
++\r
++ notmuch_message_destroy (message);\r
++\r
++ return (status == NOTMUCH_STATUS_SUCCESS);\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
++ cleanup_path = newpath;\r
++\r
++ if (! add_file_to_database (notmuch, newpath, tag_ops)) {\r
++ /* XXX add an option to keep the file in maildir? */\r
++ goto FAIL;\r
++ }\r
++\r
++ if (! sync_dir (newdir))\r
++ goto FAIL;\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 f51a84f..1a44f11 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -48,6 +48,9 @@ static command_t commands[] = {\r
+ { "new", notmuch_new_command, FALSE,\r
+ "[options...]",\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
+ "[options...] <search-terms> [...]",\r
+ "Search for messages matching the given search terms." },\r
+-- \r
+1.7.12.1\r
+\r