From 6c3dbb81c13b554603e30438c3dfa8cb8e2c0e0b Mon Sep 17 00:00:00 2001 From: Peter Wang Date: Thu, 24 Jan 2013 23:08:03 +1100 Subject: [PATCH] [PATCH v4 07/12] insert: add --folder option --- 0a/3a3c335d4bd9382a9985365b2221825303cdc6 | 147 ++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 0a/3a3c335d4bd9382a9985365b2221825303cdc6 diff --git a/0a/3a3c335d4bd9382a9985365b2221825303cdc6 b/0a/3a3c335d4bd9382a9985365b2221825303cdc6 new file mode 100644 index 000000000..c04c82117 --- /dev/null +++ b/0a/3a3c335d4bd9382a9985365b2221825303cdc6 @@ -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 C9835431FAF + for ; Thu, 24 Jan 2013 04:10:05 -0800 (PST) +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 e6OqYqgOBJu9 for ; + Thu, 24 Jan 2013 04:10:04 -0800 (PST) +Received: from mail-da0-f47.google.com (mail-da0-f47.google.com + [209.85.210.47]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 38F23431FD2 + for ; Thu, 24 Jan 2013 04:09:50 -0800 (PST) +Received: by mail-da0-f47.google.com with SMTP id s35so4226763dak.34 + for ; Thu, 24 Jan 2013 04:09:49 -0800 (PST) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to + :references; bh=0gqOSG5ReP1lmZQUKsuerhTRTl7TyEHFiACf41gWRLA=; + b=rVwQTS3slb7L4aRBUdEjUpEAJDjrO0DUrfZrTDK6x7GlI4tNt5eI2eLRRKmKG5DLc3 + 7yuLGyr8WsQeHIKyxklDvVJLN5fbSOkEPF+R9Ceb+cLW4m+Dm0Z8fF8pdSUoo06Z81v2 + paC5YKpEM//phDUo5TOr1pE1bP3ktvGlg9sgo6Pj3ZHRQk5EBlN5S92fDFjT3LfHuxgl + tnqjcvMR7lyY5TlnHMl1pe52aiKI1Moc81cVnHU6c5dWTr8cHBVW0C72r0uXMWEpNboo + xCdkmJmkf/rpoFYnA++4MSrJcZqsiINW5c1hu2Z7A0nqCZiwZS6CyAeYtx5z+udOA6Tg + yqaA== +X-Received: by 10.68.248.70 with SMTP id yk6mr4051044pbc.160.1359029389452; + Thu, 24 Jan 2013 04:09:49 -0800 (PST) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPS id rs7sm14790445pbc.2.2013.01.24.04.09.47 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Thu, 24 Jan 2013 04:09:48 -0800 (PST) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v4 07/12] insert: add --folder option +Date: Thu, 24 Jan 2013 23:08:03 +1100 +Message-Id: <1359029288-12132-8-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1359029288-12132-1-git-send-email-novalazy@gmail.com> +References: <1359029288-12132-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: Thu, 24 Jan 2013 12:10:06 -0000 + +Allow the new message to be inserted into a folder within the Maildir +hierarchy instead of the top-level folder. +--- + notmuch-insert.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- + 1 file changed, 45 insertions(+), 2 deletions(-) + +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 31c1152..bc848ff 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -85,6 +85,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' +@@ -288,11 +305,25 @@ notmuch_insert_command (void *ctx, 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) { ++ fprintf (stderr, "Error: bad argument to notmuch insert: %s\n", ++ argv[-opt_index]); ++ return 1; ++ } ++ + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; +@@ -319,7 +350,19 @@ notmuch_insert_command (void *ctx, 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 (ctx, "%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