[PATCH v6 07/12] insert: add --folder option
[notmuch-archives.git] / 24 / c85f18b28a58a79007c3f873f2ffc7e46ec6ab
1 Return-Path: <novalazy@gmail.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 091CF431FAE\r
6         for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:47:52 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.799\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
13         FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id XgAVDnsJh2uN for <notmuch@notmuchmail.org>;\r
17         Fri, 21 Jun 2013 22:47:46 -0700 (PDT)\r
18 Received: from mail-pb0-f43.google.com (mail-pb0-f43.google.com\r
19         [209.85.160.43]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 5B2C2431FCB\r
22         for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:47:31 -0700 (PDT)\r
23 Received: by mail-pb0-f43.google.com with SMTP id md12so8731192pbc.2\r
24         for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:47:30 -0700 (PDT)\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
26         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
27         bh=Eo7Q8asaeQD403v1NJdvjFXsrRJKoAQwhAoXEsO8oYo=;\r
28         b=jj+HY/NxLRn51hp7vFVnDY+G8L60IO1+LN+xTlbafsw3x+V+K+du34UAkR5oR00f7b\r
29         PsUj8Mnp0jptAjm+HNsNbas25Qp578HEjTrRIDu8hmR3n2lW4z1KTVkiN4NHFGHVNnRq\r
30         Q9QGebJ0IiU/TxSjXX6OrvSltezOs5xtz9rrTzqqH2SpoOWSwyfCsJe3PDa1wQXB2D34\r
31         aMWVl6uv5c39mhNcoXMO/57BP0g9YuDODK6e/HDwAUUlLH4tT6nkvEQ7hoQPWB1UZCpD\r
32         Q5j+inn1MhY505DG6y3GhV/u6CgGeChTFz+tsCVbloSuaBfH1Fy8066gGajKTFd5Er2c\r
33         SOtQ==\r
34 X-Received: by 10.68.252.67 with SMTP id zq3mr15486676pbc.66.1371880050653;\r
35         Fri, 21 Jun 2013 22:47:30 -0700 (PDT)\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.\r
37         [220.233.42.215])\r
38         by mx.google.com with ESMTPSA id vb8sm7907732pbc.11.2013.06.21.22.47.28\r
39         for <multiple recipients>\r
40         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
41         Fri, 21 Jun 2013 22:47:29 -0700 (PDT)\r
42 From: Peter Wang <novalazy@gmail.com>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH v6 07/12] insert: add --folder option\r
45 Date: Sat, 22 Jun 2013 15:46:43 +1000\r
46 Message-Id: <1371880008-18312-8-git-send-email-novalazy@gmail.com>\r
47 X-Mailer: git-send-email 1.7.12.1\r
48 In-Reply-To: <1371880008-18312-1-git-send-email-novalazy@gmail.com>\r
49 References: <1371880008-18312-1-git-send-email-novalazy@gmail.com>\r
50 X-BeenThere: notmuch@notmuchmail.org\r
51 X-Mailman-Version: 2.1.13\r
52 Precedence: list\r
53 List-Id: "Use and development of the notmuch mail system."\r
54         <notmuch.notmuchmail.org>\r
55 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
57 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
58 List-Post: <mailto:notmuch@notmuchmail.org>\r
59 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
60 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
62 X-List-Received-Date: Sat, 22 Jun 2013 05:47:52 -0000\r
63 \r
64 Allow the new message to be inserted into a folder within the Maildir\r
65 hierarchy instead of the top-level folder.\r
66 ---\r
67  notmuch-insert.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--\r
68  1 file changed, 44 insertions(+), 2 deletions(-)\r
69 \r
70 diff --git a/notmuch-insert.c b/notmuch-insert.c\r
71 index 1495234..a6134a8 100644\r
72 --- a/notmuch-insert.c\r
73 +++ b/notmuch-insert.c\r
74 @@ -83,6 +83,23 @@ sync_dir (const char *dir)\r
75      return ret;\r
76  }\r
77  \r
78 +/* Check the specified folder name does not contain a directory\r
79 + * component ".." to prevent writes outside of the Maildir hierarchy. */\r
80 +static notmuch_bool_t\r
81 +check_folder_name (const char *folder)\r
82 +{\r
83 +    const char *p = folder;\r
84 +\r
85 +    for (;;) {\r
86 +       if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/'))\r
87 +           return FALSE;\r
88 +       p = strchr (p, '/');\r
89 +       if (!p)\r
90 +           return TRUE;\r
91 +       p++;\r
92 +    }\r
93 +}\r
94 +\r
95  /* Open a unique file in the Maildir 'tmp' directory.\r
96   * Returns the file descriptor on success, or -1 on failure.\r
97   * On success, file paths for the message in the 'tmp' and 'new'\r
98 @@ -286,11 +303,24 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
99      size_t new_tags_length;\r
100      tag_op_list_t *tag_ops;\r
101      char *query_string = NULL;\r
102 +    const char *folder = NULL;\r
103      const char *maildir;\r
104 -    int opt_index = 1;\r
105 +    int opt_index;\r
106      unsigned int i;\r
107      notmuch_bool_t ret;\r
108  \r
109 +    notmuch_opt_desc_t options[] = {\r
110 +       { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
111 +       { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
112 +    };\r
113 +\r
114 +    opt_index = parse_arguments (argc, argv, options, 1);\r
115 +\r
116 +    if (opt_index < 0) {\r
117 +       /* diagnostics already printed */\r
118 +       return 1;\r
119 +    }\r
120 +\r
121      db_path = notmuch_config_get_database_path (config);\r
122      new_tags = notmuch_config_get_new_tags (config, &new_tags_length);\r
123  \r
124 @@ -313,7 +343,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
125         return 1;\r
126      }\r
127  \r
128 -    maildir = db_path;\r
129 +    if (folder == NULL) {\r
130 +       maildir = db_path;\r
131 +    } else {\r
132 +       if (! check_folder_name (folder)) {\r
133 +           fprintf (stderr, "Error: bad folder name: %s\n", folder);\r
134 +           return 1;\r
135 +       }\r
136 +       maildir = talloc_asprintf (config, "%s/%s", db_path, folder);\r
137 +       if (! maildir) {\r
138 +           fprintf (stderr, "Out of memory\n");\r
139 +           return 1;\r
140 +       }\r
141 +    }\r
142  \r
143      /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
144       * from standard input may be interrupted. */\r
145 -- \r
146 1.7.12.1\r
147 \r