From 92a55b40de2fac0dbc977b481a7667abc4e4a0f0 Mon Sep 17 00:00:00 2001 From: Peter Wang Date: Sat, 22 Jun 2013 15:46:43 +1000 Subject: [PATCH] [PATCH v6 07/12] insert: add --folder option --- 24/c85f18b28a58a79007c3f873f2ffc7e46ec6ab | 147 ++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 24/c85f18b28a58a79007c3f873f2ffc7e46ec6ab diff --git a/24/c85f18b28a58a79007c3f873f2ffc7e46ec6ab b/24/c85f18b28a58a79007c3f873f2ffc7e46ec6ab new file mode 100644 index 000000000..9205938cf --- /dev/null +++ b/24/c85f18b28a58a79007c3f873f2ffc7e46ec6ab @@ -0,0 +1,147 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 091CF431FAE + for ; Fri, 21 Jun 2013 22:47:52 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.799 +X-Spam-Level: +X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id XgAVDnsJh2uN for ; + Fri, 21 Jun 2013 22:47:46 -0700 (PDT) +Received: from mail-pb0-f43.google.com (mail-pb0-f43.google.com + [209.85.160.43]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 5B2C2431FCB + for ; Fri, 21 Jun 2013 22:47:31 -0700 (PDT) +Received: by mail-pb0-f43.google.com with SMTP id md12so8731192pbc.2 + for ; Fri, 21 Jun 2013 22:47:30 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; + bh=Eo7Q8asaeQD403v1NJdvjFXsrRJKoAQwhAoXEsO8oYo=; + b=jj+HY/NxLRn51hp7vFVnDY+G8L60IO1+LN+xTlbafsw3x+V+K+du34UAkR5oR00f7b + PsUj8Mnp0jptAjm+HNsNbas25Qp578HEjTrRIDu8hmR3n2lW4z1KTVkiN4NHFGHVNnRq + Q9QGebJ0IiU/TxSjXX6OrvSltezOs5xtz9rrTzqqH2SpoOWSwyfCsJe3PDa1wQXB2D34 + aMWVl6uv5c39mhNcoXMO/57BP0g9YuDODK6e/HDwAUUlLH4tT6nkvEQ7hoQPWB1UZCpD + Q5j+inn1MhY505DG6y3GhV/u6CgGeChTFz+tsCVbloSuaBfH1Fy8066gGajKTFd5Er2c + SOtQ== +X-Received: by 10.68.252.67 with SMTP id zq3mr15486676pbc.66.1371880050653; + Fri, 21 Jun 2013 22:47:30 -0700 (PDT) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPSA id vb8sm7907732pbc.11.2013.06.21.22.47.28 + for + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Fri, 21 Jun 2013 22:47:29 -0700 (PDT) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v6 07/12] insert: add --folder option +Date: Sat, 22 Jun 2013 15:46:43 +1000 +Message-Id: <1371880008-18312-8-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1371880008-18312-1-git-send-email-novalazy@gmail.com> +References: <1371880008-18312-1-git-send-email-novalazy@gmail.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 22 Jun 2013 05:47:52 -0000 + +Allow the new message to be inserted into a folder within the Maildir +hierarchy instead of the top-level folder. +--- + notmuch-insert.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- + 1 file changed, 44 insertions(+), 2 deletions(-) + +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 1495234..a6134a8 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -83,6 +83,23 @@ sync_dir (const char *dir) + return ret; + } + ++/* Check the specified folder name does not contain a directory ++ * component ".." to prevent writes outside of the Maildir hierarchy. */ ++static notmuch_bool_t ++check_folder_name (const char *folder) ++{ ++ const char *p = folder; ++ ++ for (;;) { ++ if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/')) ++ return FALSE; ++ p = strchr (p, '/'); ++ if (!p) ++ return TRUE; ++ p++; ++ } ++} ++ + /* Open a unique file in the Maildir 'tmp' directory. + * Returns the file descriptor on success, or -1 on failure. + * On success, file paths for the message in the 'tmp' and 'new' +@@ -286,11 +303,24 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + size_t new_tags_length; + tag_op_list_t *tag_ops; + char *query_string = NULL; ++ const char *folder = NULL; + const char *maildir; +- int opt_index = 1; ++ int opt_index; + unsigned int i; + notmuch_bool_t ret; + ++ notmuch_opt_desc_t options[] = { ++ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, ++ { NOTMUCH_OPT_END, 0, 0, 0, 0 } ++ }; ++ ++ opt_index = parse_arguments (argc, argv, options, 1); ++ ++ if (opt_index < 0) { ++ /* diagnostics already printed */ ++ return 1; ++ } ++ + db_path = notmuch_config_get_database_path (config); + new_tags = notmuch_config_get_new_tags (config, &new_tags_length); + +@@ -313,7 +343,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + return 1; + } + +- maildir = db_path; ++ if (folder == NULL) { ++ maildir = db_path; ++ } else { ++ if (! check_folder_name (folder)) { ++ fprintf (stderr, "Error: bad folder name: %s\n", folder); ++ return 1; ++ } ++ maildir = talloc_asprintf (config, "%s/%s", db_path, folder); ++ if (! maildir) { ++ fprintf (stderr, "Out of memory\n"); ++ return 1; ++ } ++ } + + /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying + * from standard input may be interrupted. */ +-- +1.7.12.1 + -- 2.26.2