Reply all - issue
[notmuch-archives.git] / d5 / 91414e0144d2261d3c549e60e4fb112a626bca
1 Return-Path: <bremner@tethera.net>\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 D3121431FAF\r
6         for <notmuch@notmuchmail.org>; Sun, 18 Nov 2012 11:05:35 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id zFLsV02a6pqQ for <notmuch@notmuchmail.org>;\r
16         Sun, 18 Nov 2012 11:05:33 -0800 (PST)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 8B410431FDB\r
21         for <notmuch@notmuchmail.org>; Sun, 18 Nov 2012 11:05:19 -0800 (PST)\r
22 Received: from fctnnbsc30w-156034089108.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([156.34.89.108] helo=zancas.localnet)\r
24         by tesseract.cs.unb.ca with esmtpsa\r
25         (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
26         (envelope-from <bremner@tethera.net>)\r
27         id 1TaABC-00028B-KP; Sun, 18 Nov 2012 15:05:18 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1TaAB7-0001J0-2y; Sun, 18 Nov 2012 15:05:13 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH 11/16] notmuch-restore: add support for input format\r
34         'batch-tag'\r
35 Date: Sun, 18 Nov 2012 15:04:53 -0400\r
36 Message-Id: <1353265498-3839-12-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <1353265498-3839-1-git-send-email-david@tethera.net>\r
39 References: <1353265498-3839-1-git-send-email-david@tethera.net>\r
40 X-Spam_bar: -\r
41 Cc: David Bremner <bremner@debian.org>\r
42 X-BeenThere: notmuch@notmuchmail.org\r
43 X-Mailman-Version: 2.1.13\r
44 Precedence: list\r
45 List-Id: "Use and development of the notmuch mail system."\r
46         <notmuch.notmuchmail.org>\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
48         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
50 List-Post: <mailto:notmuch@notmuchmail.org>\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
53         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
54 X-List-Received-Date: Sun, 18 Nov 2012 19:05:36 -0000\r
55 \r
56 From: David Bremner <bremner@debian.org>\r
57 \r
58 This is the same as the batch input for notmuch tag, except by default\r
59 it removes all tags before modifying a given message id and only "id:"\r
60 is supported.\r
61 ---\r
62  notmuch-restore.c |  180 +++++++++++++++++++++++++++++++----------------------\r
63  1 file changed, 105 insertions(+), 75 deletions(-)\r
64 \r
65 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
66 index f03dcac..1776e99 100644\r
67 --- a/notmuch-restore.c\r
68 +++ b/notmuch-restore.c\r
69 @@ -19,18 +19,21 @@\r
70   */\r
71  \r
72  #include "notmuch-client.h"\r
73 +#include "dump-restore-private.h"\r
74 +#include "tag-util.h"\r
75 +\r
76 +static volatile sig_atomic_t interrupted;\r
77 +static regex_t regex;\r
78  \r
79  static int\r
80 -tag_message (notmuch_database_t *notmuch, const char *message_id,\r
81 -            char *file_tags, notmuch_bool_t remove_all,\r
82 -            notmuch_bool_t synchronize_flags)\r
83 +tag_message (unused (void *ctx),\r
84 +            notmuch_database_t *notmuch,\r
85 +            const char *message_id,\r
86 +            tag_op_list_t *tag_ops,\r
87 +            tag_op_flag_t flags)\r
88  {\r
89      notmuch_status_t status;\r
90 -    notmuch_tags_t *db_tags;\r
91 -    char *db_tags_str;\r
92      notmuch_message_t *message = NULL;\r
93 -    const char *tag;\r
94 -    char *next;\r
95      int ret = 0;\r
96  \r
97      status = notmuch_database_find_message (notmuch, message_id, &message);\r
98 @@ -44,55 +47,46 @@ tag_message (notmuch_database_t *notmuch, const char *message_id,\r
99  \r
100      /* In order to detect missing messages, this check/optimization is\r
101       * intentionally done *after* first finding the message. */\r
102 -    if (! remove_all && (file_tags == NULL || *file_tags == '\0'))\r
103 -       goto DONE;\r
104 -\r
105 -    db_tags_str = NULL;\r
106 -    for (db_tags = notmuch_message_get_tags (message);\r
107 -        notmuch_tags_valid (db_tags);\r
108 -        notmuch_tags_move_to_next (db_tags)) {\r
109 -       tag = notmuch_tags_get (db_tags);\r
110 -\r
111 -       if (db_tags_str)\r
112 -           db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
113 -       else\r
114 -           db_tags_str = talloc_strdup (message, tag);\r
115 -    }\r
116 +    if ( (flags & TAG_FLAG_REMOVE_ALL) || ! (tag_op_list_empty (tag_ops)))\r
117 +       tag_op_list_apply (message, tag_ops, flags);\r
118  \r
119 -    if (((file_tags == NULL || *file_tags == '\0') &&\r
120 -        (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
121 -       (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
122 -       goto DONE;\r
123 +    if (message)\r
124 +       notmuch_message_destroy (message);\r
125  \r
126 -    notmuch_message_freeze (message);\r
127 +    return ret;\r
128 +}\r
129  \r
130 -    if (remove_all)\r
131 -       notmuch_message_remove_all_tags (message);\r
132 +static int\r
133 +parse_sup_line (void *ctx, char *line,\r
134 +               char **query_str, tag_op_list_t *tag_ops)\r
135 +{\r
136  \r
137 -    next = file_tags;\r
138 -    while (next) {\r
139 -       tag = strsep (&next, " ");\r
140 -       if (*tag == '\0')\r
141 -           continue;\r
142 -       status = notmuch_message_add_tag (message, tag);\r
143 -       if (status) {\r
144 -           fprintf (stderr, "Error applying tag %s to message %s:\n",\r
145 -                    tag, message_id);\r
146 -           fprintf (stderr, "%s\n", notmuch_status_to_string (status));\r
147 -           ret = 1;\r
148 -       }\r
149 -    }\r
150 +    regmatch_t match[3];\r
151 +    char *file_tags;\r
152 +    int rerr;\r
153  \r
154 -    notmuch_message_thaw (message);\r
155 +    tag_op_list_reset (tag_ops);\r
156  \r
157 -    if (synchronize_flags)\r
158 -       notmuch_message_tags_to_maildir_flags (message);\r
159 +    chomp_newline (line);\r
160  \r
161 -  DONE:\r
162 -    if (message)\r
163 -       notmuch_message_destroy (message);\r
164 +    /* Silently ignore blank lines */\r
165 +    if (line[0] == '\0') {\r
166 +       return 1;\r
167 +    }\r
168  \r
169 -    return ret;\r
170 +    rerr = xregexec (&regex, line, 3, match, 0);\r
171 +    if (rerr == REG_NOMATCH) {\r
172 +       fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
173 +                line);\r
174 +       return 1;\r
175 +    }\r
176 +\r
177 +    *query_str = talloc_strndup (ctx, line + match[1].rm_so,\r
178 +                                match[1].rm_eo - match[1].rm_so);\r
179 +    file_tags = talloc_strndup (ctx, line + match[2].rm_so,\r
180 +                               match[2].rm_eo - match[2].rm_so);\r
181 +\r
182 +    return tag_op_list_from_string (ctx, file_tags, FALSE, tag_ops);\r
183  }\r
184  \r
185  int\r
186 @@ -100,16 +94,19 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
187  {\r
188      notmuch_config_t *config;\r
189      notmuch_database_t *notmuch;\r
190 -    notmuch_bool_t synchronize_flags;\r
191      notmuch_bool_t accumulate = FALSE;\r
192 +    tag_op_flag_t flags = 0;\r
193 +    tag_op_list_t *tag_ops;\r
194 +\r
195      char *input_file_name = NULL;\r
196      FILE *input = stdin;\r
197      char *line = NULL;\r
198      size_t line_size;\r
199      ssize_t line_len;\r
200 -    regex_t regex;\r
201 -    int rerr;\r
202 +\r
203 +    int ret = 0;\r
204      int opt_index;\r
205 +    int input_format = DUMP_FORMAT_AUTO;\r
206  \r
207      config = notmuch_config_open (ctx, NULL, NULL);\r
208      if (config == NULL)\r
209 @@ -119,9 +116,15 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
210                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
211         return 1;\r
212  \r
213 -    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
214 +    if (notmuch_config_get_maildir_synchronize_flags (config))\r
215 +       flags |= TAG_FLAG_MAILDIR_SYNC;\r
216  \r
217      notmuch_opt_desc_t options[] = {\r
218 +       { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',\r
219 +         (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },\r
220 +                                 { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
221 +                                 { "sup", DUMP_FORMAT_SUP },\r
222 +                                 { 0, 0 } } },\r
223         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
224         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
225         { 0, 0, 0, 0, 0 }\r
226 @@ -134,6 +137,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
227         return 1;\r
228      }\r
229  \r
230 +    if (! accumulate)\r
231 +       flags |= TAG_FLAG_REMOVE_ALL;\r
232 +\r
233      if (input_file_name) {\r
234         input = fopen (input_file_name, "r");\r
235         if (input == NULL) {\r
236 @@ -154,35 +160,58 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
237       * non-space characters for the message-id, then one or more\r
238       * spaces, then a list of space-separated tags as a sequence of\r
239       * characters within literal '(' and ')'. */\r
240 -    if ( xregcomp (&regex,\r
241 -                  "^([^ ]+) \\(([^)]*)\\)$",\r
242 -                  REG_EXTENDED) )\r
243 -       INTERNAL_ERROR ("compile time constant regex failed.");\r
244 +    char *p;\r
245  \r
246 -    while ((line_len = getline (&line, &line_size, input)) != -1) {\r
247 -       regmatch_t match[3];\r
248 -       char *message_id, *file_tags;\r
249 +    line_len = getline (&line, &line_size, input);\r
250 +    if (line_len == 0)\r
251 +       return 0;\r
252  \r
253 -       chomp_newline (line);\r
254 +    for (p = line; *p; p++) {\r
255 +       if (*p == '(')\r
256 +           input_format = DUMP_FORMAT_SUP;\r
257 +    }\r
258  \r
259 -       rerr = xregexec (&regex, line, 3, match, 0);\r
260 -       if (rerr == REG_NOMATCH) {\r
261 -           fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
262 -                    line);\r
263 -           continue;\r
264 +    if (input_format == DUMP_FORMAT_AUTO)\r
265 +       input_format = DUMP_FORMAT_BATCH_TAG;\r
266 +\r
267 +    if (input_format == DUMP_FORMAT_SUP)\r
268 +       if ( xregcomp (&regex,\r
269 +                      "^([^ ]+) \\(([^)]*)\\)$",\r
270 +                      REG_EXTENDED) )\r
271 +           INTERNAL_ERROR ("compile time constant regex failed.");\r
272 +\r
273 +    tag_ops = tag_op_list_create (ctx);\r
274 +    if (tag_ops == NULL) {\r
275 +       fprintf (stderr, "Out of memory.\n");\r
276 +       return 1;\r
277 +    }\r
278 +\r
279 +    do {\r
280 +       char *query_string;\r
281 +\r
282 +       if (input_format == DUMP_FORMAT_SUP) {\r
283 +           ret =  parse_sup_line (ctx, line, &query_string, tag_ops);\r
284 +       } else {\r
285 +           ret =  parse_tag_line (ctx, line, &query_string, tag_ops);\r
286 +\r
287 +           if (ret == 0) {\r
288 +               if ( strncmp ("id:", query_string, 3) != 0) {\r
289 +                   fprintf (stderr, "Unsupported query: %s\n", query_string);\r
290 +                   continue;\r
291 +               }\r
292 +               query_string = query_string + 3;\r
293 +           }\r
294         }\r
295  \r
296 -       message_id = xstrndup (line + match[1].rm_so,\r
297 -                              match[1].rm_eo - match[1].rm_so);\r
298 -       file_tags = xstrndup (line + match[2].rm_so,\r
299 -                             match[2].rm_eo - match[2].rm_so);\r
300 +       if (ret > 0)\r
301 +           continue;\r
302  \r
303 -       tag_message (notmuch, message_id, file_tags, ! accumulate,\r
304 -                    synchronize_flags);\r
305 +       if (ret < 0 || tag_message (ctx, notmuch, query_string,\r
306 +                                   tag_ops, flags))\r
307 +           break;\r
308 +\r
309 +    }  while ((line_len = getline (&line, &line_size, input)) != -1);\r
310  \r
311 -       free (message_id);\r
312 -       free (file_tags);\r
313 -    }\r
314  \r
315      regfree (&regex);\r
316  \r
317 @@ -190,8 +219,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
318         free (line);\r
319  \r
320      notmuch_database_destroy (notmuch);\r
321 +\r
322      if (input != stdin)\r
323         fclose (input);\r
324  \r
325 -    return 0;\r
326 +    return ret;\r
327  }\r
328 -- \r
329 1.7.10.4\r
330 \r