Re: [PATCH] Fix typo in Message.maildir_flags_to_tags
[notmuch-archives.git] / a1 / 951760dee96872dfb437a442e67b57b033b8f6
1 Return-Path: <sojkam1@fel.cvut.cz>\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 D77A7418C25\r
6         for <notmuch@notmuchmail.org>; Fri, 26 Mar 2010 14:43:15 -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: -1.9\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.9 tagged_above=-999 required=5\r
12         tests=[BAYES_00=-1.9] autolearn=ham\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 KN8TwVbTqRFj for <notmuch@notmuchmail.org>;\r
16         Fri, 26 Mar 2010 14:43:13 -0700 (PDT)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id 94BFD4196F3\r
19         for <notmuch@notmuchmail.org>; Fri, 26 Mar 2010 14:43:12 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.4])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id D576819F33C7;\r
22         Fri, 26 Mar 2010 22:43:11 +0100 (CET)\r
23 X-Virus-Scanned: IMAP AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new,\r
26         port 10044)\r
27         with ESMTP id U4mJt9X7Huei; Fri, 26 Mar 2010 22:43:10 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 3EDCA19F3404;\r
30         Fri, 26 Mar 2010 22:43:10 +0100 (CET)\r
31 Received: from steelpick.2x.cz (r5da224.net.upc.cz [86.49.116.224])\r
32         (Authenticated sender: sojkam1)\r
33         by imap.feld.cvut.cz (Postfix) with ESMTPSA id 083A015C062;\r
34         Fri, 26 Mar 2010 22:43:10 +0100 (CET)\r
35 Received: from wsh by steelpick.2x.cz with local (Exim 4.71)\r
36         (envelope-from <sojkam1@fel.cvut.cz>)\r
37         id 1NvHJ7-0006iC-Ap; Fri, 26 Mar 2010 22:43:09 +0100\r
38 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
39 To: notmuch@notmuchmail.org\r
40 Date: Fri, 26 Mar 2010 22:42:56 +0100\r
41 Message-Id: <1269639777-25765-3-git-send-email-sojkam1@fel.cvut.cz>\r
42 X-Mailer: git-send-email 1.7.0.2\r
43 In-Reply-To: <87tys292w7.fsf@steelpick.2x.cz>\r
44 References: <87tys292w7.fsf@steelpick.2x.cz>\r
45 Subject: [notmuch] [PATCH v2 3/4] Add maildir-based mailstore\r
46 X-BeenThere: notmuch@notmuchmail.org\r
47 X-Mailman-Version: 2.1.13\r
48 Precedence: list\r
49 List-Id: "Use and development of the notmuch mail system."\r
50         <notmuch.notmuchmail.org>\r
51 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
53 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
54 List-Post: <mailto:notmuch@notmuchmail.org>\r
55 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
56 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
57         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
58 X-List-Received-Date: Fri, 26 Mar 2010 21:43:16 -0000\r
59 \r
60 This mailstore allows bi-directional synchronization between maildir\r
61 flags and certain tags. The flag-to-tag mapping is defined by flag2tag\r
62 array.\r
63 \r
64 The synchronization works this way:\r
65 \r
66 1) Whenever notmuch new is executed, the following happens:\r
67    o New messages are tagged with inbox.\r
68    o New messages without maildir info in the file name (typically\r
69      files in new/) are tagged with unread.\r
70    o For new or renamed messages with maildir info present in the\r
71      file name, the tags defined in flag2tag are either added or\r
72      removed depending on the flags from the file name.\r
73 \r
74 2) Whenever notmuch tag is executed, a new set of flags based on the\r
75    tags is constructed for every message and a new file name is\r
76    prepared based on the old file name but with the new flags. If the\r
77    old message file was in 'new' directory then this is replaced with\r
78    'cur' in the new file name. If the new and old file names differ,\r
79    the file is renamed and notmuch database is updated accordingly.\r
80 \r
81    The rename happens before the database is updated. In case of crash\r
82    between rename and database update, the next run of notmuch new\r
83    brings the database in sync with the mail store again.\r
84 \r
85 This mailstore is enabled by putting the following to your\r
86 .notmuch-config:\r
87 \r
88 [mailstore]\r
89 type=maildir\r
90 \r
91 Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>\r
92 ---\r
93  lib/database.cc       |    7 ++\r
94  lib/mailstore-files.c |  203 ++++++++++++++++++++++++++++++++++++++++++++++++-\r
95  lib/mailstore.c       |    1 +\r
96  lib/message.cc        |   41 ++++++++++-\r
97  lib/notmuch-private.h |    4 +\r
98  lib/notmuch.h         |    1 +\r
99  6 files changed, 253 insertions(+), 4 deletions(-)\r
100 \r
101 diff --git a/lib/database.cc b/lib/database.cc\r
102 index 93c8d0f..33ef889 100644\r
103 --- a/lib/database.cc\r
104 +++ b/lib/database.cc\r
105 @@ -1471,6 +1471,13 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
106  \r
107         _notmuch_message_add_filename (message, filename);\r
108  \r
109 +       /* This is a new message or it has a new filename and as such,\r
110 +        * its tags in database either do not exists or might be out\r
111 +        * of date. Mailstore assigns the tags later in index_new(),\r
112 +        * but until then we should not synchronize the tags back to\r
113 +        * the mailstore. */\r
114 +       notmuch_message_set_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID, TRUE);\r
115 +\r
116         /* Is this a newly created message object? */\r
117         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
118             _notmuch_message_add_term (message, "type", "mail");\r
119 diff --git a/lib/mailstore-files.c b/lib/mailstore-files.c\r
120 index e4086ba..b4e6e42 100644\r
121 --- a/lib/mailstore-files.c\r
122 +++ b/lib/mailstore-files.c\r
123 @@ -24,6 +24,30 @@\r
124  #include "notmuch.h"\r
125  #include "mailstore-private.h"\r
126  #include <dirent.h>\r
127 +#include <stdbool.h>\r
128 +\r
129 +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
130 +\r
131 +struct mailstore_priv {\r
132 +    void (*tag_new)(notmuch_message_t *message, const char *filename);\r
133 +    int  (*tag_renamed)(notmuch_message_t *message, const char *filename);\r
134 +};\r
135 +\r
136 +struct maildir_flag_tag {\r
137 +    char flag;\r
138 +    const char *tag;\r
139 +    bool inverse;\r
140 +};\r
141 +\r
142 +/* ASCII ordered table of Maildir flags and assiciated tags */\r
143 +struct maildir_flag_tag flag2tag[] = {\r
144 +    { 'D', "draft",   false},\r
145 +    { 'F', "flagged", false},\r
146 +    { 'P', "passed",  false},\r
147 +    { 'R', "replied", false},\r
148 +    { 'S', "unread",  true },\r
149 +    { 'T', "delete",  false},\r
150 +};\r
151  \r
152  typedef struct _filename_node {\r
153      char *filename;\r
154 @@ -69,8 +93,9 @@ _filename_list_add (_filename_list_t *list,\r
155  }\r
156  \r
157  static void\r
158 -tag_inbox_and_unread (notmuch_message_t *message)\r
159 +tag_inbox_and_unread (notmuch_message_t *message, const char *filename)\r
160  {\r
161 +    (void)filename;\r
162      notmuch_message_add_tag (message, "inbox");\r
163      notmuch_message_add_tag (message, "unread");\r
164  }\r
165 @@ -116,6 +141,156 @@ _entries_resemble_maildir (struct dirent **entries, int count)\r
166      return 0;\r
167  }\r
168  \r
169 +static int\r
170 +tag_from_maildir_flags (notmuch_message_t *message, const char *filename)\r
171 +{\r
172 +    const char *flags, *p;\r
173 +    char f;\r
174 +    bool valid, unread;\r
175 +    unsigned i;\r
176 +\r
177 +    flags = strstr(filename, ":2,");\r
178 +    if (!flags)\r
179 +       return -1;\r
180 +    flags += 3;\r
181 +\r
182 +    /*  Check that the letters are valid Maildir flags */\r
183 +    f = 0;\r
184 +    valid = true;\r
185 +    for (p=flags; valid && *p; p++) {\r
186 +       switch (*p) {\r
187 +       case 'P':\r
188 +       case 'R':\r
189 +       case 'S':\r
190 +       case 'T':\r
191 +       case 'D':\r
192 +       case 'F':\r
193 +           if (*p > f) f=*p;\r
194 +           else valid = false;\r
195 +       break;\r
196 +       default:\r
197 +           valid = false;\r
198 +       }\r
199 +    }\r
200 +    if (!valid) {\r
201 +       fprintf (stderr, "Warning: Invalid maildir flags in filename %s\n", filename);\r
202 +       return -1;\r
203 +    }\r
204 +\r
205 +    notmuch_message_freeze(message);\r
206 +    unread = true;\r
207 +    for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
208 +       if ((strchr(flags, flag2tag[i].flag) != NULL) ^ flag2tag[i].inverse) {\r
209 +           notmuch_message_add_tag (message, flag2tag[i].tag);\r
210 +       } else {\r
211 +           notmuch_message_remove_tag (message, flag2tag[i].tag);\r
212 +       }\r
213 +    }\r
214 +    notmuch_message_thaw(message);\r
215 +\r
216 +    /* From now on, we can synchronize the tags from the database to\r
217 +     * the mailstore. */\r
218 +    notmuch_message_set_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID, FALSE);\r
219 +    return 0;\r
220 +}\r
221 +\r
222 +static void\r
223 +get_new_flags(notmuch_message_t *message, char *flags)\r
224 +{\r
225 +    notmuch_tags_t *tags;\r
226 +    const char *tag;\r
227 +    unsigned i;\r
228 +    char *p;\r
229 +\r
230 +    for (i = 0; i < ARRAY_SIZE(flag2tag); i++)\r
231 +       flags[i] = flag2tag[i].inverse ? flag2tag[i].flag : '\0';\r
232 +\r
233 +    for (tags = notmuch_message_get_tags (message);\r
234 +        notmuch_tags_valid (tags);\r
235 +        notmuch_tags_move_to_next (tags))\r
236 +    {\r
237 +       tag = notmuch_tags_get (tags);\r
238 +       for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
239 +           if (strcmp(tag, flag2tag[i].tag) == 0)\r
240 +               flags[i] = flag2tag[i].inverse ? '\0' : flag2tag[i].flag;\r
241 +       }\r
242 +    }\r
243 +\r
244 +    p = flags;\r
245 +    for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
246 +       if (flags[i])\r
247 +           *p++ = flags[i];\r
248 +    }\r
249 +    *p = '\0';\r
250 +}\r
251 +\r
252 +static char *\r
253 +get_subdir (char *filename)\r
254 +{\r
255 +    char *p, *subdir = NULL;\r
256 +\r
257 +    p = filename + strlen (filename) - 1;\r
258 +    while (p > filename + 3 && *p != '/')\r
259 +       p--;\r
260 +    if (*p == '/') {\r
261 +       subdir = p - 3;\r
262 +       if (subdir > filename && *(subdir - 1) != '/')\r
263 +           subdir = NULL;\r
264 +    }\r
265 +    return subdir;\r
266 +}\r
267 +\r
268 +/* Store maildir-related tags as maildir flags */\r
269 +static notmuch_private_status_t\r
270 +maildir_sync_tags (notmuch_mailstore_t *mailstore,\r
271 +                  notmuch_message_t *message)\r
272 +{\r
273 +    char flags[ARRAY_SIZE(flag2tag)+1];\r
274 +    const char *filename, *p;\r
275 +    char *filename_new, *subdir = NULL;\r
276 +    int ret;\r
277 +\r
278 +    (void)mailstore;\r
279 +    get_new_flags (message, flags);\r
280 +\r
281 +    filename = notmuch_message_get_filename (message);\r
282 +    p = strstr(filename, ":2,");\r
283 +    if (!p)\r
284 +       p = filename + strlen(filename);\r
285 +\r
286 +    filename_new = talloc_size(message, (p-filename) + 3 + sizeof(flags));\r
287 +    if (unlikely (filename_new == NULL))\r
288 +       return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
289 +    memcpy(filename_new, filename, p-filename);\r
290 +    filename_new[p-filename] = '\0';\r
291 +\r
292 +    /* If message is in new/ move it under cur/. */\r
293 +    subdir = get_subdir (filename_new);\r
294 +    if (subdir && memcmp (subdir, "new/", 4) == 0)\r
295 +       memcpy (subdir, "cur/", 4);\r
296 +\r
297 +    strcpy (filename_new+(p-filename), ":2,");\r
298 +    strcpy (filename_new+(p-filename)+3, flags);\r
299 +\r
300 +    if (strcmp (filename, filename_new) != 0) {\r
301 +       ret = rename (filename, filename_new);\r
302 +       if (ret == -1) {\r
303 +           perror (talloc_asprintf (message, "rename of %s to %s failed", filename, filename_new));\r
304 +           exit (1);\r
305 +       }\r
306 +       return _notmuch_message_rename (message, filename_new);\r
307 +       /* _notmuch_message_sync is our caller. Do not call it here. */\r
308 +    }\r
309 +    return NOTMUCH_STATUS_SUCCESS;\r
310 +}\r
311 +\r
312 +static void\r
313 +tag_inbox_and_maildir_flags (notmuch_message_t *message, const char *filename)\r
314 +{\r
315 +    notmuch_message_add_tag (message, "inbox");\r
316 +    if (tag_from_maildir_flags(message, filename) != 0)\r
317 +       notmuch_message_add_tag (message, "unread");\r
318 +}\r
319  \r
320  /* Examine 'path' recursively as follows:\r
321   *\r
322 @@ -171,6 +346,7 @@ add_files_recursive (notmuch_mailstore_t *mailstore,\r
323      struct stat st;\r
324      notmuch_bool_t is_maildir, new_directory;\r
325      _indexing_context_priv_t *priv = state->priv;\r
326 +    struct mailstore_priv *mailstore_priv = mailstore->priv;\r
327      notmuch_database_t *notmuch = mailstore->notmuch;\r
328  \r
329      if (stat (path, &st)) {\r
330 @@ -352,11 +528,12 @@ add_files_recursive (notmuch_mailstore_t *mailstore,\r
331         /* success */\r
332         case NOTMUCH_STATUS_SUCCESS:\r
333             state->added_messages++;\r
334 -           tag_inbox_and_unread (message);\r
335 +           mailstore_priv->tag_new (message, next);\r
336             break;\r
337         /* Non-fatal issues (go on to next file) */\r
338         case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
339 -           /* Stay silent on this one. */\r
340 +           if (mailstore_priv->tag_renamed)\r
341 +               mailstore_priv->tag_renamed (message, next);\r
342             break;\r
343         case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
344             fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
345 @@ -614,6 +791,10 @@ open_file(notmuch_mailstore_t *mailstore, const char *filename)\r
346      return fopen (abs_filename, "r");\r
347  }\r
348  \r
349 +struct mailstore_priv files_priv = {\r
350 +    .tag_new = tag_inbox_and_unread,\r
351 +};\r
352 +\r
353  /* Original notmuch mail store */\r
354  struct _notmuch_mailstore mailstore_files = {\r
355      .type = "files",\r
356 @@ -621,4 +802,20 @@ struct _notmuch_mailstore mailstore_files = {\r
357      .index_new = index_new,\r
358      .sync_tags = NULL,\r
359      .open_file = open_file,\r
360 +    .priv = &files_priv,\r
361 +};\r
362 +\r
363 +struct mailstore_priv maildir_priv = {\r
364 +    .tag_new = tag_inbox_and_maildir_flags,\r
365 +    .tag_renamed = tag_from_maildir_flags,\r
366 +};\r
367 +\r
368 +/* Similar to mailstore_files, but does bi-directional synchronization between certain tags and maildir flags */\r
369 +struct _notmuch_mailstore mailstore_maildir = {\r
370 +    .type = "maildir",\r
371 +    .count_files = count_files,\r
372 +    .index_new = index_new,\r
373 +    .sync_tags = maildir_sync_tags,\r
374 +    .open_file = open_file,\r
375 +    .priv = &maildir_priv,\r
376  };\r
377 diff --git a/lib/mailstore.c b/lib/mailstore.c\r
378 index cdac9a6..7dc55ad 100644\r
379 --- a/lib/mailstore.c\r
380 +++ b/lib/mailstore.c\r
381 @@ -25,6 +25,7 @@\r
382  \r
383  static notmuch_mailstore_t *available[] = {\r
384      &mailstore_files,\r
385 +    &mailstore_maildir,\r
386  };\r
387  \r
388  notmuch_mailstore_t *\r
389 diff --git a/lib/message.cc b/lib/message.cc\r
390 index c32ee7d..51208ba 100644\r
391 --- a/lib/message.cc\r
392 +++ b/lib/message.cc\r
393 @@ -561,7 +561,8 @@ _notmuch_message_sync (notmuch_message_t *message)\r
394      if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
395         return;\r
396  \r
397 -    if (message->notmuch->mailstore->sync_tags) {\r
398 +    if (message->notmuch->mailstore->sync_tags &&\r
399 +       !notmuch_message_get_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID)) {\r
400         status = message->notmuch->mailstore->sync_tags (message->notmuch->mailstore,\r
401                                                          message);\r
402         if (status != NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
403 @@ -683,6 +684,44 @@ _notmuch_message_remove_term (notmuch_message_t *message,\r
404      return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
405  }\r
406  \r
407 +/* Change the message filename stored in the database.\r
408 + *\r
409 + * This change will not be reflected in the database until the next\r
410 + * call to _notmuch_message_sync.\r
411 + */\r
412 +notmuch_private_status_t\r
413 +_notmuch_message_rename (notmuch_message_t *message,\r
414 +                        const char *new_filename)\r
415 +{\r
416 +    void *local = talloc_new (message);\r
417 +    char *direntry;\r
418 +    Xapian::PostingIterator i, end;\r
419 +    Xapian::Document document;\r
420 +    notmuch_private_status_t pstatus;\r
421 +    notmuch_status_t status;\r
422 +    const char *old_filename;\r
423 +\r
424 +    old_filename = notmuch_message_get_filename(message);\r
425 +    old_filename = talloc_reference(local, old_filename);\r
426 +    if (unlikely(!old_filename))\r
427 +       return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
428 +\r
429 +    status = _notmuch_message_add_filename (message, new_filename);\r
430 +    if (status)\r
431 +       return (notmuch_private_status_t)status;\r
432 +\r
433 +    status = _notmuch_database_filename_to_direntry (local, message->notmuch,\r
434 +                                                    old_filename, &direntry);\r
435 +    if (status)\r
436 +       return (notmuch_private_status_t)status;\r
437 +\r
438 +    pstatus = _notmuch_message_remove_term (message, "file-direntry", direntry);\r
439 +\r
440 +    talloc_free (local);\r
441 +\r
442 +    return pstatus;\r
443 +}\r
444 +\r
445  notmuch_status_t\r
446  notmuch_message_add_tag (notmuch_message_t *message, const char *tag)\r
447  {\r
448 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
449 index d52d84d..63e75ec 100644\r
450 --- a/lib/notmuch-private.h\r
451 +++ b/lib/notmuch-private.h\r
452 @@ -245,6 +245,10 @@ notmuch_status_t\r
453  _notmuch_message_add_filename (notmuch_message_t *message,\r
454                                const char *filename);\r
455  \r
456 +notmuch_private_status_t\r
457 +_notmuch_message_rename (notmuch_message_t *message,\r
458 +                        const char *new_filename);\r
459 +\r
460  void\r
461  _notmuch_message_ensure_thread_id (notmuch_message_t *message);\r
462  \r
463 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
464 index 8fa7b31..c554f6d 100644\r
465 --- a/lib/notmuch.h\r
466 +++ b/lib/notmuch.h\r
467 @@ -760,6 +760,7 @@ notmuch_message_get_filename (notmuch_message_t *message);\r
468  /* Message flags */\r
469  typedef enum _notmuch_message_flag {\r
470      NOTMUCH_MESSAGE_FLAG_MATCH,\r
471 +    NOTMUCH_MESSAGE_FLAG_TAGS_INVALID,\r
472  } notmuch_message_flag_t;\r
473  \r
474  /* Get a value of a flag for the email corresponding to 'message'. */\r
475 -- \r
476 1.7.0.2\r
477 \r