--- /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 091CF431FAE\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:47:52 -0700 (PDT)\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 XgAVDnsJh2uN for <notmuch@notmuchmail.org>;\r
+ Fri, 21 Jun 2013 22:47:46 -0700 (PDT)\r
+Received: from mail-pb0-f43.google.com (mail-pb0-f43.google.com\r
+ [209.85.160.43]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 5B2C2431FCB\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:47:31 -0700 (PDT)\r
+Received: by mail-pb0-f43.google.com with SMTP id md12so8731192pbc.2\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:47:30 -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=Eo7Q8asaeQD403v1NJdvjFXsrRJKoAQwhAoXEsO8oYo=;\r
+ b=jj+HY/NxLRn51hp7vFVnDY+G8L60IO1+LN+xTlbafsw3x+V+K+du34UAkR5oR00f7b\r
+ PsUj8Mnp0jptAjm+HNsNbas25Qp578HEjTrRIDu8hmR3n2lW4z1KTVkiN4NHFGHVNnRq\r
+ Q9QGebJ0IiU/TxSjXX6OrvSltezOs5xtz9rrTzqqH2SpoOWSwyfCsJe3PDa1wQXB2D34\r
+ aMWVl6uv5c39mhNcoXMO/57BP0g9YuDODK6e/HDwAUUlLH4tT6nkvEQ7hoQPWB1UZCpD\r
+ Q5j+inn1MhY505DG6y3GhV/u6CgGeChTFz+tsCVbloSuaBfH1Fy8066gGajKTFd5Er2c\r
+ SOtQ==\r
+X-Received: by 10.68.252.67 with SMTP id zq3mr15486676pbc.66.1371880050653;\r
+ Fri, 21 Jun 2013 22:47:30 -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 vb8sm7907732pbc.11.2013.06.21.22.47.28\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Fri, 21 Jun 2013 22:47:29 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v6 07/12] insert: add --folder option\r
+Date: Sat, 22 Jun 2013 15:46:43 +1000\r
+Message-Id: <1371880008-18312-8-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1371880008-18312-1-git-send-email-novalazy@gmail.com>\r
+References: <1371880008-18312-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: Sat, 22 Jun 2013 05:47:52 -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 1495234..a6134a8 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
+@@ -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