--- /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 529E0429E29\r
+ for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:51:07 -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 AunlE-0+pQqC for <notmuch@notmuchmail.org>;\r
+ Sat, 19 Jan 2013 16:51:06 -0800 (PST)\r
+Received: from mail-pb0-f48.google.com (mail-pb0-f48.google.com\r
+ [209.85.160.48]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 8C7AE431FDE\r
+ for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:50:58 -0800 (PST)\r
+Received: by mail-pb0-f48.google.com with SMTP id wy12so1317990pbc.7\r
+ for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:50:57 -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; bh=Bzx58UkTThN5g0PYx2nrUbbkWjWhx6/7oCLg5+dn2T8=;\r
+ b=dO5F4futU6W1Yot/SOIm2WBgWgx80vld61YLjWhvAE/zMJTfp/y34FJw05uq8pdkzg\r
+ B6c4hJivfgn7eGVK6TauqW7sec6rKzN/FrMkqBgdROd5tToITNM9Kb+DCRfikI54G0aS\r
+ 8L7w8C80/u0OqFCFeAlonYpj/s9P+Zy7I1PaUnPmmM83wD+SUWNF3iEs3bGMIstrdqma\r
+ WynF51cT8W1hF3M8Lac7UebEFhHGmJjJAdZDLfPnzwgKaVhtYv1tjXdXoe3ycnJBB3JL\r
+ BVveJ0RJNaAgipsFuhHDt7Z2x9QmV5+EzKqjWoStR0MQUiRJXT97i779l5yPEWyYcejx\r
+ LYxg==\r
+X-Received: by 10.68.190.227 with SMTP id gt3mr18619897pbc.5.1358643057683;\r
+ Sat, 19 Jan 2013 16:50:57 -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 oi3sm5811059pbb.1.2013.01.19.16.50.55\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 19 Jan 2013 16:50:57 -0800 (PST)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 05/20] insert: add new message to database\r
+Date: Sun, 20 Jan 2013 11:49:49 +1100\r
+Message-Id: <1358643004-14522-6-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com>\r
+References: <1358643004-14522-1-git-send-email-novalazy@gmail.com>\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, 20 Jan 2013 00:51:08 -0000\r
+\r
+Add the new message to the notmuch database, renaming the file to encode\r
+notmuch tags as maildir flags.\r
+---\r
+ notmuch-insert.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-\r
+ 1 file changed, 48 insertions(+), 1 deletion(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index c0289d9..498421d 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -153,6 +153,44 @@ copy_stdin (int fdin, int fdout)\r
+ return TRUE;\r
+ }\r
+ \r
++/* Add the specified message file to the notmuch database.\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
++{\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
++ break;\r
++ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
++ fprintf (stderr, "Warning: duplicate message.\n");\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
++ notmuch_message_tags_to_maildir_flags (message);\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)\r
+@@ -173,8 +211,17 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,\r
+ }\r
+ if (!ret) {\r
+ unlink (tmppath);\r
++ return FALSE;\r
+ }\r
+- return ret;\r
++\r
++ ret = add_file_to_database (notmuch, newpath);\r
++ if (!ret) {\r
++ /* XXX maybe there should be an option to keep the file in maildir? */\r
++ unlink (newpath);\r
++ return FALSE;\r
++ }\r
++\r
++ return TRUE;\r
+ }\r
+ \r
+ int\r
+-- \r
+1.7.12.1\r
+\r