--- /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 76EA2431FAF\r
+ for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:42:59 -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 2-63gk4Sy69I for <notmuch@notmuchmail.org>;\r
+ Tue, 2 Apr 2013 15:42:58 -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 345E2431FB6\r
+ for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:42:51 -0700 (PDT)\r
+Received: by mail-pd0-f180.google.com with SMTP id q11so488555pdj.39\r
+ for <notmuch@notmuchmail.org>; Tue, 02 Apr 2013 15:42:50 -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; bh=jV//+uBvocbFAKz2inXvoDkxaR0m4vsBVApXE8uXQxc=;\r
+ b=nIjDz9IFgbJzDGbE+x5//bJwtXdL9ed0GGuu2FsGCCgKoBnEqq/DcIens3izPJsEk5\r
+ lSAjujXHxyfjYXmb1mXl6JEhzpm8yTWpcQUhnVS1oAeFH0DrzkDJsPJlbRRhsRxWdn5g\r
+ e2PC6GyBD8SV8hlQCBkQj4DcMQJMWH+pYZOvZhHhO0LD7/q7u6dTA33X5xHTUpvpwQ6W\r
+ o6y8NmKkbPJEUtkiKEWnrE3e4fj0aacf8WQhrc6KDaFk2/qQxJPPciAMUzhmKx4LTMhB\r
+ x/aa2zR2H+KF/rs+YUYH6kgIy4NyxTa3x3XiYNhShC5EO0pqyzz+8UM46AoG4moORyiM\r
+ wZrQ==\r
+X-Received: by 10.68.253.68 with SMTP id zy4mr26830147pbc.38.1364942570501;\r
+ Tue, 02 Apr 2013 15:42:50 -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 sl9sm3272125pbc.17.2013.04.02.15.42.48\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Tue, 02 Apr 2013 15:42:49 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 07/12] insert: add --folder option\r
+Date: Wed, 3 Apr 2013 09:41:52 +1100\r
+Message-Id: <1364942517-6982-8-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
+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:43:00 -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 | 47 +++++++++++++++++++++++++++++++++++++++++++++--\r
+ 1 file changed, 45 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 19b1cf9..778ac04 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 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
+@@ -287,11 +304,25 @@ 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
++ fprintf (stderr, "Error: bad argument to notmuch insert: %s\n",\r
++ argv[-opt_index]);\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
+@@ -314,7 +345,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