[PATCH] WIP: remove all non-prefixed-terms (and stemmed versions)
[notmuch-archives.git] / 0a / 09f1b9b362a04af42ba8ed697f72b3d8b48ad7
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 73164431FD5\r
6         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:50 -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 ugtun4eivJZn for <notmuch@notmuchmail.org>;\r
16         Sat, 24 Nov 2012 13:20:47 -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 43FF5431E62\r
21         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:34 -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 1TcN9N-0006ST-3y; Sat, 24 Nov 2012 17:20:33 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1TcN9H-0008DN-Lr; Sat, 24 Nov 2012 17:20:27 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [Patch v2 13/17] notmuch-restore: add support for input format\r
34         'batch-tag'\r
35 Date: Sat, 24 Nov 2012 17:20:13 -0400\r
36 Message-Id: <1353792017-31459-14-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <1353792017-31459-1-git-send-email-david@tethera.net>\r
39 References: <1353792017-31459-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: Sat, 24 Nov 2012 21:20:50 -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 |  199 +++++++++++++++++++++++++++++++++--------------------\r
63  1 file changed, 125 insertions(+), 74 deletions(-)\r
64 \r
65 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
66 index f03dcac..22fcd2d 100644\r
67 --- a/notmuch-restore.c\r
68 +++ b/notmuch-restore.c\r
69 @@ -19,18 +19,22 @@\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 +#include "string-util.h"\r
76 +\r
77 +static volatile sig_atomic_t interrupted;\r
78 +static regex_t regex;\r
79  \r
80  static int\r
81 -tag_message (notmuch_database_t *notmuch, const char *message_id,\r
82 -            char *file_tags, notmuch_bool_t remove_all,\r
83 -            notmuch_bool_t synchronize_flags)\r
84 +tag_message (unused (void *ctx),\r
85 +            notmuch_database_t *notmuch,\r
86 +            const char *message_id,\r
87 +            tag_op_list_t *tag_ops,\r
88 +            tag_op_flag_t flags)\r
89  {\r
90      notmuch_status_t status;\r
91 -    notmuch_tags_t *db_tags;\r
92 -    char *db_tags_str;\r
93      notmuch_message_t *message = NULL;\r
94 -    const char *tag;\r
95 -    char *next;\r
96      int ret = 0;\r
97  \r
98      status = notmuch_database_find_message (notmuch, message_id, &message);\r
99 @@ -44,55 +48,63 @@ tag_message (notmuch_database_t *notmuch, const char *message_id,\r
100  \r
101      /* In order to detect missing messages, this check/optimization is\r
102       * intentionally done *after* first finding the message. */\r
103 -    if (! remove_all && (file_tags == NULL || *file_tags == '\0'))\r
104 -       goto DONE;\r
105 -\r
106 -    db_tags_str = NULL;\r
107 -    for (db_tags = notmuch_message_get_tags (message);\r
108 -        notmuch_tags_valid (db_tags);\r
109 -        notmuch_tags_move_to_next (db_tags)) {\r
110 -       tag = notmuch_tags_get (db_tags);\r
111 -\r
112 -       if (db_tags_str)\r
113 -           db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
114 -       else\r
115 -           db_tags_str = talloc_strdup (message, tag);\r
116 -    }\r
117 +    if ( (flags & TAG_FLAG_REMOVE_ALL) || (tag_op_list_size (tag_ops)))\r
118 +       tag_op_list_apply (message, tag_ops, flags);\r
119  \r
120 -    if (((file_tags == NULL || *file_tags == '\0') &&\r
121 -        (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
122 -       (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
123 -       goto DONE;\r
124 +    if (message)\r
125 +       notmuch_message_destroy (message);\r
126  \r
127 -    notmuch_message_freeze (message);\r
128 +    return ret;\r
129 +}\r
130  \r
131 -    if (remove_all)\r
132 -       notmuch_message_remove_all_tags (message);\r
133 +static int\r
134 +parse_sup_line (void *ctx, char *line,\r
135 +               char **query_str, tag_op_list_t *tag_ops)\r
136 +{\r
137  \r
138 -    next = file_tags;\r
139 -    while (next) {\r
140 -       tag = strsep (&next, " ");\r
141 -       if (*tag == '\0')\r
142 -           continue;\r
143 -       status = notmuch_message_add_tag (message, tag);\r
144 -       if (status) {\r
145 -           fprintf (stderr, "Error applying tag %s to message %s:\n",\r
146 -                    tag, message_id);\r
147 -           fprintf (stderr, "%s\n", notmuch_status_to_string (status));\r
148 -           ret = 1;\r
149 -       }\r
150 +    regmatch_t match[3];\r
151 +    char *file_tags;\r
152 +    int rerr;\r
153 +\r
154 +    tag_op_list_reset (tag_ops);\r
155 +\r
156 +    chomp_newline (line);\r
157 +\r
158 +    /* Silently ignore blank lines */\r
159 +    if (line[0] == '\0') {\r
160 +       return 1;\r
161 +    }\r
162 +\r
163 +    rerr = xregexec (&regex, line, 3, match, 0);\r
164 +    if (rerr == REG_NOMATCH) {\r
165 +       fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
166 +                line);\r
167 +       return 1;\r
168      }\r
169  \r
170 -    notmuch_message_thaw (message);\r
171 +    *query_str = talloc_strndup (ctx, line + match[1].rm_so,\r
172 +                                match[1].rm_eo - match[1].rm_so);\r
173 +    file_tags = talloc_strndup (ctx, line + match[2].rm_so,\r
174 +                               match[2].rm_eo - match[2].rm_so);\r
175  \r
176 -    if (synchronize_flags)\r
177 -       notmuch_message_tags_to_maildir_flags (message);\r
178 +    char *tok = file_tags;\r
179 +    size_t tok_len = 0;\r
180  \r
181 -  DONE:\r
182 -    if (message)\r
183 -       notmuch_message_destroy (message);\r
184 +    tag_op_list_reset (tag_ops);\r
185 +\r
186 +    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
187 +\r
188 +       if (*(tok + tok_len) != '\0') {\r
189 +           *(tok + tok_len) = '\0';\r
190 +           tok_len++;\r
191 +       }\r
192 +\r
193 +       if (tag_op_list_append (ctx, tag_ops, tok, FALSE))\r
194 +           return -1;\r
195 +    }\r
196 +\r
197 +    return 0;\r
198  \r
199 -    return ret;\r
200  }\r
201  \r
202  int\r
203 @@ -100,16 +112,19 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
204  {\r
205      notmuch_config_t *config;\r
206      notmuch_database_t *notmuch;\r
207 -    notmuch_bool_t synchronize_flags;\r
208      notmuch_bool_t accumulate = FALSE;\r
209 +    tag_op_flag_t flags = 0;\r
210 +    tag_op_list_t *tag_ops;\r
211 +\r
212      char *input_file_name = NULL;\r
213      FILE *input = stdin;\r
214      char *line = NULL;\r
215      size_t line_size;\r
216      ssize_t line_len;\r
217 -    regex_t regex;\r
218 -    int rerr;\r
219 +\r
220 +    int ret = 0;\r
221      int opt_index;\r
222 +    int input_format = DUMP_FORMAT_AUTO;\r
223  \r
224      config = notmuch_config_open (ctx, NULL, NULL);\r
225      if (config == NULL)\r
226 @@ -119,9 +134,15 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
227                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
228         return 1;\r
229  \r
230 -    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
231 +    if (notmuch_config_get_maildir_synchronize_flags (config))\r
232 +       flags |= TAG_FLAG_MAILDIR_SYNC;\r
233  \r
234      notmuch_opt_desc_t options[] = {\r
235 +       { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',\r
236 +         (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },\r
237 +                                 { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
238 +                                 { "sup", DUMP_FORMAT_SUP },\r
239 +                                 { 0, 0 } } },\r
240         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
241         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
242         { 0, 0, 0, 0, 0 }\r
243 @@ -134,6 +155,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
244         return 1;\r
245      }\r
246  \r
247 +    if (! accumulate)\r
248 +       flags |= TAG_FLAG_REMOVE_ALL;\r
249 +\r
250      if (input_file_name) {\r
251         input = fopen (input_file_name, "r");\r
252         if (input == NULL) {\r
253 @@ -154,35 +178,61 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
254       * non-space characters for the message-id, then one or more\r
255       * spaces, then a list of space-separated tags as a sequence of\r
256       * characters within literal '(' and ')'. */\r
257 -    if ( xregcomp (&regex,\r
258 -                  "^([^ ]+) \\(([^)]*)\\)$",\r
259 -                  REG_EXTENDED) )\r
260 -       INTERNAL_ERROR ("compile time constant regex failed.");\r
261 +    char *p;\r
262  \r
263 -    while ((line_len = getline (&line, &line_size, input)) != -1) {\r
264 -       regmatch_t match[3];\r
265 -       char *message_id, *file_tags;\r
266 +    line_len = getline (&line, &line_size, input);\r
267 +    if (line_len == 0)\r
268 +       return 0;\r
269  \r
270 -       chomp_newline (line);\r
271 +    for (p = line; *p; p++) {\r
272 +       if (*p == '(')\r
273 +           input_format = DUMP_FORMAT_SUP;\r
274 +    }\r
275  \r
276 -       rerr = xregexec (&regex, line, 3, match, 0);\r
277 -       if (rerr == REG_NOMATCH) {\r
278 -           fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
279 -                    line);\r
280 -           continue;\r
281 +    if (input_format == DUMP_FORMAT_AUTO)\r
282 +       input_format = DUMP_FORMAT_BATCH_TAG;\r
283 +\r
284 +    if (input_format == DUMP_FORMAT_SUP)\r
285 +       if ( xregcomp (&regex,\r
286 +                      "^([^ ]+) \\(([^)]*)\\)$",\r
287 +                      REG_EXTENDED) )\r
288 +           INTERNAL_ERROR ("compile time constant regex failed.");\r
289 +\r
290 +    tag_ops = tag_op_list_create (ctx);\r
291 +    if (tag_ops == NULL) {\r
292 +       fprintf (stderr, "Out of memory.\n");\r
293 +       return 1;\r
294 +    }\r
295 +\r
296 +    do {\r
297 +       char *query_string;\r
298 +\r
299 +       if (input_format == DUMP_FORMAT_SUP) {\r
300 +           ret =  parse_sup_line (ctx, line, &query_string, tag_ops);\r
301 +       } else {\r
302 +           ret =  parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS,\r
303 +                                  &query_string, tag_ops);\r
304 +\r
305 +           if (ret == 0) {\r
306 +               if ( strncmp ("id:", query_string, 3) != 0) {\r
307 +                   fprintf (stderr, "Unsupported query: %s\n", query_string);\r
308 +                   continue;\r
309 +               }\r
310 +               /* delete id: from front of string; tag_message expects a\r
311 +                * raw message-id */\r
312 +               query_string = query_string + 3;\r
313 +           }\r
314         }\r
315  \r
316 -       message_id = xstrndup (line + match[1].rm_so,\r
317 -                              match[1].rm_eo - match[1].rm_so);\r
318 -       file_tags = xstrndup (line + match[2].rm_so,\r
319 -                             match[2].rm_eo - match[2].rm_so);\r
320 +       if (ret > 0)\r
321 +           continue;\r
322  \r
323 -       tag_message (notmuch, message_id, file_tags, ! accumulate,\r
324 -                    synchronize_flags);\r
325 +       if (ret < 0 || tag_message (ctx, notmuch, query_string,\r
326 +                                   tag_ops, flags))\r
327 +           break;\r
328 +\r
329 +    }  while ((line_len = getline (&line, &line_size, input)) != -1);\r
330  \r
331 -       free (message_id);\r
332 -       free (file_tags);\r
333 -    }\r
334  \r
335      regfree (&regex);\r
336  \r
337 @@ -190,8 +240,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
338         free (line);\r
339  \r
340      notmuch_database_destroy (notmuch);\r
341 +\r
342      if (input != stdin)\r
343         fclose (input);\r
344  \r
345 -    return 0;\r
346 +    return ret;\r
347  }\r
348 -- \r
349 1.7.10.4\r
350 \r