Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / fb / 1f79875ae2eec62936a281cd318d0f1584b05d
1 Return-Path: <wsh@resox.2x.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 7E5DC40D170\r
6         for <notmuch@notmuchmail.org>; Sun, 31 Oct 2010 14:49:19 -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 9aq3mIMnoTO7 for <notmuch@notmuchmail.org>;\r
16         Sun, 31 Oct 2010 14:49:04 -0700 (PDT)\r
17 Received: from smtp.nextra.cz (smtp.nextra.cz [212.65.193.3])\r
18         by olra.theworths.org (Postfix) with ESMTP id 9327840D16F\r
19         for <notmuch@notmuchmail.org>; Sun, 31 Oct 2010 14:48:02 -0700 (PDT)\r
20 Received: from resox.2x.cz (unknown [213.29.198.144])\r
21         by smtp.nextra.cz (Postfix) with ESMTP id 5A7E388CF8;\r
22         Sun, 31 Oct 2010 22:30:16 +0100 (CET)\r
23 Received: from wsh by resox.2x.cz with local (Exim 4.72)\r
24         (envelope-from <wsh@resox.2x.cz>)\r
25         id 1PCfTa-0004wi-42; Sun, 31 Oct 2010 22:30:06 +0100\r
26 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [PATCH v4 2/4] Maildir synchronization\r
29 Date: Sun, 31 Oct 2010 22:29:16 +0100\r
30 Message-Id: <1288560558-18915-3-git-send-email-sojkam1@fel.cvut.cz>\r
31 X-Mailer: git-send-email 1.7.1\r
32 In-Reply-To: <87tyk3vpxd.fsf@wsheee.2x.cz>\r
33 References: <87tyk3vpxd.fsf@wsheee.2x.cz>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.13\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38         <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
40         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
45         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sun, 31 Oct 2010 21:49:19 -0000\r
47 \r
48 This patch allows bi-directional synchronization between maildir\r
49 flags and certain tags. The flag-to-tag mapping is defined by flag2tag\r
50 array.\r
51 \r
52 The synchronization works this way:\r
53 \r
54 1) Whenever notmuch new is executed, the following happens:\r
55    o New messages are tagged with configured new_tags.\r
56    o For new or renamed messages with maildir info present in the file\r
57      name, the tags defined in flag2tag are either added or removed\r
58      depending on the flags from the file name.\r
59 \r
60 2) Whenever notmuch tag (or notmuch restore) is executed, a new set of\r
61    flags based on the tags is constructed for every message and a new\r
62    file name is prepared based on the old file name but with the new\r
63    flags. If the flags differs and the old message was in 'new'\r
64    directory then this is replaced with 'cur' in the new file name. If\r
65    the new and old file names differ, the file is renamed and notmuch\r
66    database is updated accordingly.\r
67 \r
68    The rename happens before the database is updated. In case of crash\r
69    between rename and database update, the next run of notmuch new\r
70    brings the database in sync with the mail store again.\r
71 \r
72 There is currently one known issue: Viewing/storing of attachments of\r
73 unread messages doesn't work. The reason is that when you view the\r
74 message its unread tag is removed which leads to rename of the file,\r
75 but Emacs still uses the original name to access the attachment.\r
76 \r
77 Workaround: close the message and open it again.\r
78 ---\r
79  lib/database.cc       |    7 ++\r
80  lib/message.cc        |  226 +++++++++++++++++++++++++++++++++++++++++++++++++\r
81  lib/notmuch-private.h |    4 +\r
82  lib/notmuch.h         |    7 ++\r
83  notmuch-new.c         |    3 +-\r
84  5 files changed, 246 insertions(+), 1 deletions(-)\r
85 \r
86 diff --git a/lib/database.cc b/lib/database.cc\r
87 index 9a4f715..9652013 100644\r
88 --- a/lib/database.cc\r
89 +++ b/lib/database.cc\r
90 @@ -1643,6 +1643,13 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
91  \r
92         _notmuch_message_add_filename (message, filename);\r
93  \r
94 +       /* This is a new message or it has a new filename and as such,\r
95 +        * its tags in database either do not exists or might be out\r
96 +        * of date. We assign the tags later in notmuch new, but until\r
97 +        * then we should not synchronize the tags back to the maildir\r
98 +        * flags (if notmuch is configured to do so). */\r
99 +       notmuch_message_set_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID, TRUE);\r
100 +\r
101         /* Is this a newly created message object? */\r
102         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
103             _notmuch_message_add_term (message, "type", "mail");\r
104 diff --git a/lib/message.cc b/lib/message.cc\r
105 index 71f5619..bb96242 100644\r
106 --- a/lib/message.cc\r
107 +++ b/lib/message.cc\r
108 @@ -43,6 +43,24 @@ struct _notmuch_message {\r
109      Xapian::Document doc;\r
110  };\r
111  \r
112 +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
113 +\r
114 +struct maildir_flag_tag {\r
115 +    char flag;\r
116 +    const char *tag;\r
117 +    bool inverse;\r
118 +};\r
119 +\r
120 +/* ASCII ordered table of Maildir flags and associated tags */\r
121 +struct maildir_flag_tag flag2tag[] = {\r
122 +    { 'D', "draft",   false},\r
123 +    { 'F', "flagged", false},\r
124 +    { 'P', "passed",  false},\r
125 +    { 'R', "replied", false},\r
126 +    { 'S', "unread",  true },\r
127 +    { 'T', "deleted", false},\r
128 +};\r
129 +\r
130  /* We end up having to call the destructor explicitly because we had\r
131   * to use "placement new" in order to initialize C++ objects within a\r
132   * block that we allocated with talloc. So C++ is making talloc\r
133 @@ -595,15 +613,29 @@ _notmuch_message_set_date (notmuch_message_t *message,\r
134                             Xapian::sortable_serialise (time_value));\r
135  }\r
136  \r
137 +static notmuch_private_status_t\r
138 +_notmuch_message_tags_to_maildir (notmuch_message_t *message);\r
139 +\r
140  /* Synchronize changes made to message->doc out into the database. */\r
141  void\r
142  _notmuch_message_sync (notmuch_message_t *message)\r
143  {\r
144      Xapian::WritableDatabase *db;\r
145 +    notmuch_private_status_t status;\r
146  \r
147      if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
148         return;\r
149  \r
150 +    if (// todo_sync_enabled &&\r
151 +       !notmuch_message_get_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID)) {\r
152 +       status = _notmuch_message_tags_to_maildir (message);\r
153 +       if (status != NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
154 +           fprintf (stderr, "Error: Cannot sync tags to maildir (%s)\n",\r
155 +                    notmuch_status_to_string ((notmuch_status_t)status));\r
156 +           /* Exit to avoid unsynchronized mailstore. */\r
157 +           exit(1);\r
158 +       }\r
159 +    }\r
160      db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);\r
161      db->replace_document (message->doc_id, message->doc);\r
162  }\r
163 @@ -715,6 +747,44 @@ _notmuch_message_remove_term (notmuch_message_t *message,\r
164      return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
165  }\r
166  \r
167 +/* Change the message filename stored in the database.\r
168 + *\r
169 + * This change will not be reflected in the database until the next\r
170 + * call to _notmuch_message_sync.\r
171 + */\r
172 +notmuch_private_status_t\r
173 +_notmuch_message_rename (notmuch_message_t *message,\r
174 +                        const char *new_filename)\r
175 +{\r
176 +    void *local = talloc_new (message);\r
177 +    char *direntry;\r
178 +    Xapian::PostingIterator i, end;\r
179 +    Xapian::Document document;\r
180 +    notmuch_private_status_t pstatus;\r
181 +    notmuch_status_t status;\r
182 +    const char *old_filename;\r
183 +\r
184 +    old_filename = notmuch_message_get_filename(message);\r
185 +    old_filename = talloc_reference(local, old_filename);\r
186 +    if (unlikely(!old_filename))\r
187 +       return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
188 +\r
189 +    status = _notmuch_message_add_filename (message, new_filename);\r
190 +    if (status)\r
191 +       return (notmuch_private_status_t)status;\r
192 +\r
193 +    status = _notmuch_database_filename_to_direntry (local, message->notmuch,\r
194 +                                                    old_filename, &direntry);\r
195 +    if (status)\r
196 +       return (notmuch_private_status_t)status;\r
197 +\r
198 +    pstatus = _notmuch_message_remove_term (message, "file-direntry", direntry);\r
199 +\r
200 +    talloc_free (local);\r
201 +\r
202 +    return pstatus;\r
203 +}\r
204 +\r
205  notmuch_status_t\r
206  notmuch_message_add_tag (notmuch_message_t *message, const char *tag)\r
207  {\r
208 @@ -772,6 +842,162 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)\r
209  }\r
210  \r
211  notmuch_status_t\r
212 +notmuch_message_maildir_to_tags (notmuch_message_t *message, const char *filename)\r
213 +{\r
214 +    const char *flags, *p;\r
215 +    char f;\r
216 +    bool valid, unread;\r
217 +    unsigned i;\r
218 +    notmuch_status_t status;\r
219 +\r
220 +    flags = strstr (filename, ":2,");\r
221 +    if (!flags)\r
222 +       return NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
223 +    flags += 3;\r
224 +\r
225 +    /*  Check that the letters are valid Maildir flags */\r
226 +    f = 0;\r
227 +    valid = true;\r
228 +    for (p=flags; valid && *p; p++) {\r
229 +       switch (*p) {\r
230 +       case 'P':\r
231 +       case 'R':\r
232 +       case 'S':\r
233 +       case 'T':\r
234 +       case 'D':\r
235 +       case 'F':\r
236 +           if (*p > f) f=*p;\r
237 +           else valid = false;\r
238 +       break;\r
239 +       default:\r
240 +           valid = false;\r
241 +       }\r
242 +    }\r
243 +    if (!valid) {\r
244 +       fprintf (stderr, "Warning: Invalid maildir flags in filename %s\n", filename);\r
245 +       return NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
246 +    }\r
247 +\r
248 +    status = notmuch_message_freeze (message);\r
249 +    if (status)\r
250 +       return status;\r
251 +    unread = true;\r
252 +    for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
253 +       if ((strchr (flags, flag2tag[i].flag) != NULL) ^ flag2tag[i].inverse) {\r
254 +           status = notmuch_message_add_tag (message, flag2tag[i].tag);\r
255 +       } else {\r
256 +           status = notmuch_message_remove_tag (message, flag2tag[i].tag);\r
257 +       }\r
258 +       if (status)\r
259 +           return status;\r
260 +    }\r
261 +    status = notmuch_message_thaw (message);\r
262 +\r
263 +    /* From now on, we can synchronize the tags from the database to\r
264 +     * the mailstore. */\r
265 +    notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID, FALSE);\r
266 +    return status;\r
267 +}\r
268 +\r
269 +static void\r
270 +maildir_get_new_flags(notmuch_message_t *message, char *flags)\r
271 +{\r
272 +    notmuch_tags_t *tags;\r
273 +    const char *tag;\r
274 +    unsigned i;\r
275 +    char *p;\r
276 +\r
277 +    for (i = 0; i < ARRAY_SIZE(flag2tag); i++)\r
278 +       flags[i] = flag2tag[i].inverse ? flag2tag[i].flag : '\0';\r
279 +\r
280 +    for (tags = notmuch_message_get_tags (message);\r
281 +        notmuch_tags_valid (tags);\r
282 +        notmuch_tags_move_to_next (tags))\r
283 +    {\r
284 +       tag = notmuch_tags_get (tags);\r
285 +       for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
286 +           if (strcmp(tag, flag2tag[i].tag) == 0)\r
287 +               flags[i] = flag2tag[i].inverse ? '\0' : flag2tag[i].flag;\r
288 +       }\r
289 +    }\r
290 +\r
291 +    p = flags;\r
292 +    for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
293 +       if (flags[i])\r
294 +           *p++ = flags[i];\r
295 +    }\r
296 +    *p = '\0';\r
297 +}\r
298 +\r
299 +static char *\r
300 +maildir_get_subdir (char *filename)\r
301 +{\r
302 +    char *p, *subdir = NULL;\r
303 +\r
304 +    p = filename + strlen (filename) - 1;\r
305 +    while (p > filename + 3 && *p != '/')\r
306 +       p--;\r
307 +    if (*p == '/') {\r
308 +       subdir = p - 3;\r
309 +       if (subdir > filename && *(subdir - 1) != '/')\r
310 +           subdir = NULL;\r
311 +    }\r
312 +    return subdir;\r
313 +}\r
314 +\r
315 +/* Rename the message file so that maildir flags corresponds to the\r
316 + * tags and, if aplicable, move the message from new/ to cur/. */\r
317 +static notmuch_private_status_t\r
318 +_notmuch_message_tags_to_maildir (notmuch_message_t *message)\r
319 +{\r
320 +    char flags[ARRAY_SIZE(flag2tag)+1];\r
321 +    const char *filename, *p;\r
322 +    char *filename_new, *subdir = NULL;\r
323 +    int ret;\r
324 +\r
325 +    maildir_get_new_flags (message, flags);\r
326 +\r
327 +    filename = notmuch_message_get_filename (message);\r
328 +    /* TODO: Iterate over all file names. */\r
329 +    p = strstr(filename, ":2,");\r
330 +    if ((p && strcmp (p+3, flags) == 0) ||\r
331 +       (!p && flags[0] == '\0')) {\r
332 +       // Return if flags are not to be changed - this suppresses\r
333 +       // moving the message from new/ to cur/ during initial\r
334 +       // tagging.\r
335 +       return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
336 +    }\r
337 +    if (!p)\r
338 +       p = filename + strlen(filename);\r
339 +\r
340 +    filename_new = (char*)talloc_size(message, (p-filename) + 3 + sizeof(flags));\r
341 +    if (unlikely (filename_new == NULL))\r
342 +       return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
343 +    memcpy(filename_new, filename, p-filename);\r
344 +    filename_new[p-filename] = '\0';\r
345 +\r
346 +    /* If message is in new/ move it under cur/. */\r
347 +    subdir = maildir_get_subdir (filename_new);\r
348 +    if (subdir && memcmp (subdir, "new/", 4) == 0)\r
349 +       memcpy (subdir, "cur/", 4);\r
350 +\r
351 +    strcpy (filename_new+(p-filename), ":2,");\r
352 +    strcpy (filename_new+(p-filename)+3, flags);\r
353 +\r
354 +    if (strcmp (filename, filename_new) != 0) {\r
355 +       ret = rename (filename, filename_new);\r
356 +       if (ret == -1) {\r
357 +           perror (talloc_asprintf (message, "rename of %s to %s failed",\r
358 +                                    filename, filename_new));\r
359 +           exit (1);\r
360 +       }\r
361 +       return _notmuch_message_rename (message, filename_new);\r
362 +       /* _notmuch_message_sync is our caller. Do not call it here. */\r
363 +    }\r
364 +    return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
365 +}\r
366 +\r
367 +notmuch_status_t\r
368  notmuch_message_remove_all_tags (notmuch_message_t *message)\r
369  {\r
370      notmuch_private_status_t private_status;\r
371 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
372 index 5a0cf92..1dc0a20 100644\r
373 --- a/lib/notmuch-private.h\r
374 +++ b/lib/notmuch-private.h\r
375 @@ -259,6 +259,10 @@ notmuch_status_t\r
376  _notmuch_message_add_filename (notmuch_message_t *message,\r
377                                const char *filename);\r
378  \r
379 +notmuch_private_status_t\r
380 +_notmuch_message_rename (notmuch_message_t *message,\r
381 +                        const char *new_filename);\r
382 +\r
383  void\r
384  _notmuch_message_ensure_thread_id (notmuch_message_t *message);\r
385  \r
386 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
387 index 61c68d6..fe01e73 100644\r
388 --- a/lib/notmuch.h\r
389 +++ b/lib/notmuch.h\r
390 @@ -780,6 +780,7 @@ notmuch_message_get_filename (notmuch_message_t *message);\r
391  /* Message flags */\r
392  typedef enum _notmuch_message_flag {\r
393      NOTMUCH_MESSAGE_FLAG_MATCH,\r
394 +    NOTMUCH_MESSAGE_FLAG_TAGS_INVALID,\r
395  } notmuch_message_flag_t;\r
396  \r
397  /* Get a value of a flag for the email corresponding to 'message'. */\r
398 @@ -896,6 +897,12 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);\r
399  notmuch_status_t\r
400  notmuch_message_remove_all_tags (notmuch_message_t *message);\r
401  \r
402 +/* Add or remove tags based on the maildir flags in the file name.\r
403 + */\r
404 +notmuch_status_t\r
405 +notmuch_message_maildir_to_tags (notmuch_message_t *message,\r
406 +                                const char *filename);\r
407 +\r
408  /* Freeze the current state of 'message' within the database.\r
409   *\r
410   * This means that changes to the message state, (via\r
411 diff --git a/notmuch-new.c b/notmuch-new.c\r
412 index 8818728..ed3f944 100644\r
413 --- a/notmuch-new.c\r
414 +++ b/notmuch-new.c\r
415 @@ -410,10 +410,11 @@ add_files_recursive (notmuch_database_t *notmuch,\r
416             state->added_messages++;\r
417             for (tag=state->new_tags; *tag != NULL; tag++)\r
418                 notmuch_message_add_tag (message, *tag);\r
419 +           notmuch_message_maildir_to_tags (message, next);\r
420             break;\r
421         /* Non-fatal issues (go on to next file) */\r
422         case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
423 -           /* Stay silent on this one. */\r
424 +           notmuch_message_maildir_to_tags (message, next);\r
425             break;\r
426         case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
427             fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
428 -- \r
429 1.7.1\r
430 \r