--- /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 C2C55429E35\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:52 -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 XdtX7ZoVhYNC for <notmuch@notmuchmail.org>;\r
+ Sat, 22 Jun 2013 21:24:46 -0700 (PDT)\r
+Received: from mail-pd0-f180.google.com (mail-pd0-f180.google.com\r
+ [209.85.192.180]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 756EE431FAE\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:32 -0700 (PDT)\r
+Received: by mail-pd0-f180.google.com with SMTP id 10so193959pdi.11\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:31 -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
+ bh=TdXdXI6sKbuEWN5l8tT+T9wOym3gupUFWXjcwz1c4AA=;\r
+ b=PI8q6xWpUq46+LANj2AA8l4EpOZk5BIfhbHKlIk1pdrKhOSDy8LOO/1NWHR+WJb5mQ\r
+ q2hB4/wpzqyoUF+rx28k2DBM3aQP+W6WTFw6ahidbr8pfXALmSl4Fa+hiMY4G7V3r50f\r
+ v5lmCgMnnOwi975qh1aRdzD0VoT4ga7bbtVoQVeaaf/50e4jv7kNPxpBWDHnd/VfdLCg\r
+ q8IaRrlBO0vaKplJy1jJc9paWUWxLT6rMmeAU3XiiccdxhrclyVVJpIutb9B25m/i3EG\r
+ kzDHoBy4LQdnagWQCDpM7g8Fjym/I0gqF1hCn9eFoFWOM3fThfCbw8d361Jv3TdhF1WU\r
+ ZKmg==\r
+X-Received: by 10.67.21.229 with SMTP id hn5mr22609201pad.135.1371961471623;\r
+ Sat, 22 Jun 2013 21:24:31 -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 ESMTPSA id kc8sm5959550pbc.18.2013.06.22.21.24.29\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 22 Jun 2013 21:24:30 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7 07/12] insert: add --folder option\r
+Date: Sun, 23 Jun 2013 14:24:00 +1000\r
+Message-Id: <1371961445-15182-8-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
+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:53 -0000\r
+\r
+Allow the new message to be inserted into a folder within the Maildir\r
+hierarchy instead of the top-level folder.\r
+---\r
+ notmuch-insert.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--\r
+ 1 file changed, 44 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 591189f..5a4b3ea 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -83,6 +83,23 @@ sync_dir (const char *dir)\r
+ return ret;\r
+ }\r
+ \r
++/* Check the specified folder name does not contain a directory\r
++ * component ".." to prevent writes outside of the Maildir hierarchy. */\r
++static notmuch_bool_t\r
++check_folder_name (const char *folder)\r
++{\r
++ const char *p = folder;\r
++\r
++ for (;;) {\r
++ if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/'))\r
++ return FALSE;\r
++ p = strchr (p, '/');\r
++ if (!p)\r
++ return TRUE;\r
++ p++;\r
++ }\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
+@@ -286,11 +303,24 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ size_t new_tags_length;\r
+ tag_op_list_t *tag_ops;\r
+ char *query_string = NULL;\r
++ const char *folder = NULL;\r
+ const char *maildir;\r
+- int opt_index = 1;\r
++ int opt_index;\r
+ unsigned int i;\r
+ notmuch_bool_t ret;\r
+ \r
++ notmuch_opt_desc_t options[] = {\r
++ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
++ { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
++ };\r
++\r
++ opt_index = parse_arguments (argc, argv, options, 1);\r
++\r
++ if (opt_index < 0) {\r
++ /* diagnostics already printed */\r
++ return 1;\r
++ }\r
++\r
+ db_path = notmuch_config_get_database_path (config);\r
+ new_tags = notmuch_config_get_new_tags (config, &new_tags_length);\r
+ \r
+@@ -313,7 +343,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return 1;\r
+ }\r
+ \r
+- maildir = db_path;\r
++ if (folder == NULL) {\r
++ maildir = db_path;\r
++ } else {\r
++ if (! check_folder_name (folder)) {\r
++ fprintf (stderr, "Error: bad folder name: %s\n", folder);\r
++ return 1;\r
++ }\r
++ maildir = talloc_asprintf (config, "%s/%s", db_path, folder);\r
++ if (! maildir) {\r
++ fprintf (stderr, "Out of memory\n");\r
++ return 1;\r
++ }\r
++ }\r
+ \r
+ /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
+ * from standard input may be interrupted. */\r
+-- \r
+1.7.12.1\r
+\r