[PATCH v4 03/12] cli: add insert command
authorPeter Wang <novalazy@gmail.com>
Thu, 24 Jan 2013 12:07:59 +0000 (23:07 +1100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:21 +0000 (09:53 -0800)
43/167b470170fb0076beede42b85200c4a9a3bc0 [new file with mode: 0644]

diff --git a/43/167b470170fb0076beede42b85200c4a9a3bc0 b/43/167b470170fb0076beede42b85200c4a9a3bc0
new file mode 100644 (file)
index 0000000..e22da66
--- /dev/null
@@ -0,0 +1,473 @@
+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 4A9E1431FDA\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:09:38 -0800 (PST)\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 1ogO2gYrHdnC for <notmuch@notmuchmail.org>;\r
+       Thu, 24 Jan 2013 04:09:34 -0800 (PST)\r
+Received: from mail-da0-f48.google.com (mail-da0-f48.google.com\r
+       [209.85.210.48]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 52BFB431FDB\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:09:30 -0800 (PST)\r
+Received: by mail-da0-f48.google.com with SMTP id k18so4242643dae.21\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:09:29 -0800 (PST)\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=MXqGwY8d1zepC3hxsBpykWuFWGygQ9eNuz2WI3R4Efw=;\r
+       b=bneo3r8406MFmssekRC9Gr02BlCCyS2qvnyjMAM/lOYP5AcE+v7Y9MfL4CXQ6NiwaN\r
+       IJesIvD04cnUNUdQl1GXm8Dy0fJWYgp7Yc29nrUIvkeNecdgbQR7lqTozoHLEZPbIuRa\r
+       9vdKXWes6HZG+eIUeTmBQxx7dbSp/xNBNMGC+F1Ek3D4S++vVWD2rJH7jZMsXVSgujlN\r
+       YgNM64lEeTc/Vkq1oEs1c4Ju2awO/uu12um1uBvfNe/SSx5wlsX7mbWqNOpf/3PVX9z6\r
+       kKBk8H8383IIuPrgDgowNpPNqeGRCYmkaJuWqLJq7tCUzI4qkv4NrATK9H3D77Sl2Aph\r
+       WVmA==\r
+X-Received: by 10.66.88.164 with SMTP id bh4mr4069139pab.41.1359029369550;\r
+       Thu, 24 Jan 2013 04:09:29 -0800 (PST)\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 i5sm15604556pax.13.2013.01.24.04.09.27\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Thu, 24 Jan 2013 04:09:29 -0800 (PST)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 03/12] cli: add insert command\r
+Date: Thu, 24 Jan 2013 23:07:59 +1100\r
+Message-Id: <1359029288-12132-4-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1359029288-12132-1-git-send-email-novalazy@gmail.com>\r
+References: <1359029288-12132-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: Thu, 24 Jan 2013 12:09:38 -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 | 341 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ notmuch.c        |   3 +\r
+ 4 files changed, 348 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 5f28836..af7d094 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -175,6 +175,9 @@ int\r
+ notmuch_dump_command (void *ctx, int argc, char *argv[]);\r
\r
+ int\r
++notmuch_insert_command (void *ctx, int argc, char *argv[]);\r
++\r
++int\r
+ notmuch_new_command (void *ctx, 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..31c1152\r
+--- /dev/null\r
++++ b/notmuch-insert.c\r
+@@ -0,0 +1,341 @@\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
++    ret = TRUE;\r
++    fd = open (dir, O_RDONLY);\r
++    if (fd == -1) {\r
++      fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno));\r
++      ret = FALSE;\r
++    }\r
++    if (ret && fsync (fd) != 0) {\r
++      fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno));\r
++      ret = FALSE;\r
++    }\r
++    if (fd != -1)\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) ? TRUE : FALSE;\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
++\r
++    fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);\r
++    if (fdout < 0) {\r
++      return FALSE;\r
++    }\r
++\r
++    if (! copy_stdin (fdin, fdout)) {\r
++      close (fdout);\r
++      unlink (tmppath);\r
++      return FALSE;\r
++    }\r
++\r
++    if (fsync (fdout) != 0) {\r
++      fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
++      close (fdout);\r
++      unlink (tmppath);\r
++      return FALSE;\r
++    }\r
++\r
++    close (fdout);\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
++      unlink (tmppath);\r
++      return FALSE;\r
++    }\r
++\r
++    if (! add_file_to_database (notmuch, newpath, tag_ops)) {\r
++      /* XXX add an option to keep the file in maildir? */\r
++      unlink (newpath);\r
++      return FALSE;\r
++    }\r
++\r
++    if (! sync_dir (newdir)) {\r
++      unlink (newpath);\r
++      return FALSE;\r
++    }\r
++\r
++    return TRUE;\r
++}\r
++\r
++int\r
++notmuch_insert_command (void *ctx, int argc, char *argv[])\r
++{\r
++    notmuch_config_t *config;\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
++    config = notmuch_config_open (ctx, NULL, NULL);\r
++    if (config == NULL)\r
++      return 1;\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 (ctx);\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 (ctx, 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, &notmuch))\r
++      return 1;\r
++\r
++    ret = insert_message (ctx, 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 4fc0973..1c3b893 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -53,6 +53,9 @@ static command_t commands[] = {\r
+     { "new", notmuch_new_command,\r
+       "[options...]",\r
+       "Find and import new messages to the notmuch database." },\r
++    { "insert", notmuch_insert_command,\r
++      "[options...] [--] [+<tag>|-<tag> ...] < message",\r
++      "Add a new message into the maildir and notmuch database." },\r
+     { "search", notmuch_search_command,\r
+       "[options...] <search-terms> [...]",\r
+       "Search for messages matching the given search terms." },\r
+-- \r
+1.7.12.1\r
+\r