Re: [PATCH v3 01/20] cli: add stub for insert command
authorJani Nikula <jani@nikula.org>
Tue, 22 Jan 2013 21:45:43 +0000 (23:45 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:18 +0000 (09:53 -0800)
48/fd4cb04bb77df2feb3df9be98dac017e3cfd3b [new file with mode: 0644]

diff --git a/48/fd4cb04bb77df2feb3df9be98dac017e3cfd3b b/48/fd4cb04bb77df2feb3df9be98dac017e3cfd3b
new file mode 100644 (file)
index 0000000..dd4ff97
--- /dev/null
@@ -0,0 +1,543 @@
+Return-Path: <jani@nikula.org>\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 6FA95431FB6\r
+       for <notmuch@notmuchmail.org>; Tue, 22 Jan 2013 13:45:50 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[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 UtWJbfZMCSWn for <notmuch@notmuchmail.org>;\r
+       Tue, 22 Jan 2013 13:45:49 -0800 (PST)\r
+Received: from mail-la0-f54.google.com (mail-la0-f54.google.com\r
+       [209.85.215.54]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 944B2431FAF\r
+       for <notmuch@notmuchmail.org>; Tue, 22 Jan 2013 13:45:48 -0800 (PST)\r
+Received: by mail-la0-f54.google.com with SMTP id gw10so7085995lab.41\r
+       for <notmuch@notmuchmail.org>; Tue, 22 Jan 2013 13:45:47 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=x-received:from:to:subject:in-reply-to:references:user-agent:date\r
+       :message-id:mime-version:content-type:content-transfer-encoding\r
+       :x-gm-message-state;\r
+       bh=+O/d8PJKF9Oye9+3pmLLre214k7HdRAgWA9ZxFTcfZs=;\r
+       b=X68Xuy0zoUo8wecnYSBIVZQwL7EwEYAnVxJDQsXKsh8fdC3DwneFcgSJljHsawW5AF\r
+       aQKRDMJs+t1koQ/AAjbQZOAJu1LAG1UetBgViuqmv4zNi20PJKdE1o+obrtxjiMBY1c5\r
+       /T2hUe7FY+ZKgOSfYb2/4SQMjWzergDBL0K28vrJERXGCZj0kHxSUq5gMTnWSqZ7ygdp\r
+       AdHeEvvYQjPhVO4SYdmCb7Vl+WWOwZbzlmSLBFDKKBNlupTmkcGe4aDCv6fDbyLVvqBZ\r
+       KINQi3+v0lIfG+YCzW6ns9qJqPMkKdlnPZM87J8fbZmImQ4wi77P8d2yCAyAkQbDvFeT\r
+       Y5sg==\r
+X-Received: by 10.112.23.34 with SMTP id j2mr9813037lbf.118.1358891145572;\r
+       Tue, 22 Jan 2013 13:45:45 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id ft8sm7511229lab.9.2013.01.22.13.45.43\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Tue, 22 Jan 2013 13:45:44 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Peter Wang <novalazy@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v3 01/20] cli: add stub for insert command\r
+In-Reply-To: <1358643004-14522-2-git-send-email-novalazy@gmail.com>\r
+References: <1358643004-14522-1-git-send-email-novalazy@gmail.com>\r
+       <1358643004-14522-2-git-send-email-novalazy@gmail.com>\r
+User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/24.2.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Tue, 22 Jan 2013 23:45:43 +0200\r
+Message-ID: <87vcaoj3i0.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+X-Gm-Message-State:\r
+ ALoCoQkJd7fmbnhlqI0o+eYsHjLUH6tK3udqP0ZQgTwJ+I38rbLOueoL52URBy6v2sC4V4vjLHS8\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, 22 Jan 2013 21:45:50 -0000\r
+\r
+\r
+Hi Peter -\r
+\r
+On Sun, 20 Jan 2013, Peter Wang <novalazy@gmail.com> wrote:\r
+> The notmuch insert command should read a message from standard input\r
+> and deliver it to a Maildir folder, and then incorporate the message\r
+> into the notmuch database.  Essentially it moves the functionality of\r
+> notmuch-deliver into notmuch.\r
+\r
+Bikeshedding first, I'd prefer "notmuch deliver" for the command too...\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
+This review is based on the following patches squashed together:\r
+\r
+       cli: add stub for insert command\r
+       insert: open Maildir tmp file\r
+       insert: copy stdin to Maildir tmp file\r
+       insert: move file from Maildir tmp to new\r
+       insert: add new message to database\r
+       insert: apply default tags to new message\r
+       insert: parse and apply command-line tag operations\r
+       insert: fsync after writing tmp file\r
+       insert: trap SIGINT and clean up\r
+       insert: add copyright line from notmuch-deliver\r
+\r
+It's much easier for me to grasp the big picture this way.\r
+\r
+> ---\r
+>  Makefile.local   |    1 +\r
+>  notmuch-client.h |    3 +\r
+>  notmuch-insert.c |  316 ++++++++++++++++++++++++++++++++++++++++++++++++=\r
+++++++\r
+>  notmuch.c        |    3 +\r
+>  4 files changed, 323 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 =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 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
+>=20=20\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
+>=20=20\r
+>  int\r
+> diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+> new file mode 100644\r
+> index 0000000..0e74be0\r
+> --- /dev/null\r
+> +++ b/notmuch-insert.c\r
+> @@ -0,0 +1,316 @@\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.\r
+> + * Returns true unless hostname contains a slash. */\r
+> +static notmuch_bool_t\r
+> +safe_gethostname (char *hostname, size_t len)\r
+> +{\r
+> +    if (gethostname (hostname, len) =3D=3D -1) {\r
+> +    strncpy (hostname, "unknown", len);\r
+> +    }\r
+> +    hostname[len - 1] =3D '\0';\r
+> +\r
+> +    return (strchr (hostname, '/') =3D=3D NULL);\r
+\r
+You could just replace all chars you don't accept with something you\r
+do. Add ':' to the list of unacceptable chars.\r
+\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
+> +static int\r
+> +maildir_open_tmp_file (void *ctx, const char *dir,\r
+> +                   char **tmppath, char **newpath)\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
+\r
+See also http://cr.yp.to/proto/maildir.html\r
+\r
+> +    pid =3D getpid ();\r
+> +    if (! safe_gethostname (hostname, sizeof (hostname))) {\r
+> +    fprintf (stderr, "Error: invalid host name.\n");\r
+> +    return -1;\r
+> +    }\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
+> +    *newpath =3D talloc_asprintf (ctx, "%s/new/%s", dir, filename);\r
+> +    if (! *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
+Nitpick, in theory the do-while loop above could allocate a bunch of\r
+filenames and paths that you do not free (they're freed as part of the\r
+context).\r
+\r
+> +\r
+> +    return fd;\r
+> +}\r
+> +\r
+> +/* Atomically move the new message file from the Maildir 'tmp' directory\r
+> + * to the 'new' directory.\r
+> + *\r
+> + * We follow the Dovecot recommendation to simply use rename()\r
+> + * instead of link() and unlink().  See also:\r
+> + * http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery\r
+> + */\r
+> +static notmuch_bool_t\r
+> +maildir_move_tmp_to_new (const char *tmppath, const char *newpath)\r
+> +{\r
+> +    if (rename (tmppath, newpath) !=3D 0) {\r
+> +    fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));\r
+> +    return FALSE;\r
+> +    }\r
+> +\r
+> +    return TRUE;\r
+> +}\r
+\r
+IMO you could just use rename() inline in the caller, without a wrapper.\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
+The comment and the function name imply the function has something to do\r
+with stdin, while it only cares about file descriptors.\r
+\r
+> +{\r
+> +    char buf[4096];\r
+> +    char *p;\r
+> +    ssize_t remain;\r
+> +    ssize_t written;\r
+> +\r
+> +    while (! interrupted) {\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
+> +        written =3D write (fdout, p, remain);\r
+> +        if (written =3D=3D 0)\r
+> +            return FALSE;\r
+\r
+No error message?\r
+\r
+> +        if (written < 0) {\r
+> +            if (errno =3D=3D EINTR)\r
+> +                continue;\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
+> +    } 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 =3D notmuch_database_add_message (notmuch, path, &message);\r
+> +    switch (status) {\r
+> +    case NOTMUCH_STATUS_SUCCESS:\r
+> +    break;\r
+> +    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+> +    fprintf (stderr, "Warning: duplicate message.\n");\r
+\r
+This is not uncommon. Why the warning?\r
+\r
+Also, notmuch new does not apply new.tags in this case. Are you sure we\r
+want to do that here? (You get mail, you read and archive it, you get\r
+the dupe, it pops up unread in your inbox again.)\r
+\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
+> +    tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);\r
+\r
+Check return value.\r
+\r
+> +\r
+> +    notmuch_message_destroy (message);\r
+> +\r
+> +    return TRUE;\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
+> +    int fdout;\r
+> +    notmuch_bool_t ret;\r
+> +\r
+> +    fdout =3D maildir_open_tmp_file (ctx, dir, &tmppath, &newpath);\r
+> +    if (fdout < 0) {\r
+> +    return FALSE;\r
+> +    }\r
+> +    ret =3D copy_stdin (fdin, fdout);\r
+> +    if (ret && fsync (fdout) !=3D 0) {\r
+\r
+Keep ret check and fsync separate.\r
+\r
+On some file systems you need to fsync the directory after adding a new\r
+file as well.\r
+\r
+> +    fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
+> +    ret =3D FALSE;\r
+> +    }\r
+> +    close (fdout);\r
+> +    if (ret) {\r
+> +    ret =3D maildir_move_tmp_to_new (tmppath, newpath);\r
+> +    }\r
+> +    if (!ret) {\r
+> +    unlink (tmppath);\r
+> +    return FALSE;\r
+> +    }\r
+\r
+I think you should clean up all of the error paths above. It's not\r
+obvious at a glance what happens. Typically it should be just blocks\r
+like this repeated:\r
+\r
+       ret =3D foo();\r
+       if (ret) {\r
+           /* error handling */\r
+       }\r
+\r
+> +\r
+> +    ret =3D add_file_to_database (notmuch, newpath, tag_ops);\r
+> +    if (!ret) {\r
+> +    /* XXX maybe there should be an option to keep the file in maildir? */\r
+\r
+Yes, in the future.\r
+\r
+I might like it better if you separated writing the file to maildir and\r
+adding the file to the database in the top level\r
+notmuch_insert_command() function. The delivery function could return a\r
+char * to the filename so it could be passed to\r
+add_file_to_database(). And you wouldn't need to pass database or tag\r
+ops to the maildir writing function, keeping things clearly separated.\r
+\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 =3D NULL;\r
+> +    char *maildir;\r
+> +    int opt_index =3D 1;\r
+> +    unsigned int i;\r
+> +    notmuch_bool_t ret;\r
+> +\r
+> +    config =3D notmuch_config_open (ctx, NULL, NULL);\r
+> +    if (config =3D=3D NULL)\r
+> +    return 1;\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 (ctx);\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 (ctx, argc - opt_index, argv + opt_index,\r
+> +                            &query_string, tag_ops))\r
+> +    return 1;\r
+\r
+Never mind about my earlier comment about changing\r
+parse_tag_command_line(). It's probably better to allow this to override\r
+new.tags.\r
+\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 talloc_asprintf (ctx, "%s", db_path);\r
+> +    if (! maildir) {\r
+> +    fprintf (stderr, "Out of memory\n");\r
+> +    return 1;\r
+> +    }\r
+\r
+There's no need to talloc maildir; it's the same as db_path.\r
+\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, &notmuch))\r
+> +    return 1;\r
+> +\r
+> +    ret =3D insert_message (ctx, notmuch, STDIN_FILENO, maildir, tag_ops=\r
+);\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[] =3D {\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
+> --=20\r
+> 1.7.10.4\r