1 /* message.cc - Results of message-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 #include <gmime/gmime.h>
28 struct _notmuch_message {
29 notmuch_database_t *notmuch;
35 notmuch_string_list_t *filename_list;
37 notmuch_message_file_t *message_file;
38 notmuch_message_list_t *replies;
42 Xapian::termcount termpos;
45 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
47 struct maildir_flag_tag {
53 /* ASCII ordered table of Maildir flags and associated tags */
54 static struct maildir_flag_tag flag2tag[] = {
55 { 'D', "draft", false},
56 { 'F', "flagged", false},
57 { 'P', "passed", false},
58 { 'R', "replied", false},
59 { 'S', "unread", true }
62 /* We end up having to call the destructor explicitly because we had
63 * to use "placement new" in order to initialize C++ objects within a
64 * block that we allocated with talloc. So C++ is making talloc
65 * slightly less simple to use, (we wouldn't need
66 * talloc_set_destructor at all otherwise).
69 _notmuch_message_destructor (notmuch_message_t *message)
71 message->doc.~Document ();
76 static notmuch_message_t *
77 _notmuch_message_create_for_document (const void *talloc_owner,
78 notmuch_database_t *notmuch,
81 notmuch_private_status_t *status)
83 notmuch_message_t *message;
86 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
88 message = talloc (talloc_owner, notmuch_message_t);
89 if (unlikely (message == NULL)) {
91 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
95 message->notmuch = notmuch;
96 message->doc_id = doc_id;
101 /* Each of these will be lazily created as needed. */
102 message->message_id = NULL;
103 message->thread_id = NULL;
104 message->in_reply_to = NULL;
105 message->filename_list = NULL;
106 message->message_file = NULL;
107 message->author = NULL;
109 message->replies = _notmuch_message_list_create (message);
110 if (unlikely (message->replies == NULL)) {
112 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
116 /* This is C++'s creepy "placement new", which is really just an
117 * ugly way to call a constructor for a pre-allocated object. So
118 * it's really not an error to not be checking for OUT_OF_MEMORY
119 * here, since this "new" isn't actually allocating memory. This
120 * is language-design comedy of the wrong kind. */
122 new (&message->doc) Xapian::Document;
124 talloc_set_destructor (message, _notmuch_message_destructor);
127 message->termpos = 0;
132 /* Create a new notmuch_message_t object for an existing document in
135 * Here, 'talloc owner' is an optional talloc context to which the new
136 * message will belong. This allows for the caller to not bother
137 * calling notmuch_message_destroy on the message, and know that all
138 * memory will be reclaimed when 'talloc_owner' is freed. The caller
139 * still can call notmuch_message_destroy when finished with the
140 * message if desired.
142 * The 'talloc_owner' argument can also be NULL, in which case the
143 * caller *is* responsible for calling notmuch_message_destroy.
145 * If no document exists in the database with document ID of 'doc_id'
146 * then this function returns NULL and optionally sets *status to
147 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
149 * This function can also fail to due lack of available memory,
150 * returning NULL and optionally setting *status to
151 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
153 * The caller can pass NULL for status if uninterested in
154 * distinguishing these two cases.
157 _notmuch_message_create (const void *talloc_owner,
158 notmuch_database_t *notmuch,
160 notmuch_private_status_t *status)
162 Xapian::Document doc;
165 doc = notmuch->xapian_db->get_document (doc_id);
166 } catch (const Xapian::DocNotFoundError &error) {
168 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
172 return _notmuch_message_create_for_document (talloc_owner, notmuch,
173 doc_id, doc, status);
176 /* Create a new notmuch_message_t object for a specific message ID,
177 * (which may or may not already exist in the database).
179 * The 'notmuch' database will be the talloc owner of the returned
182 * This function returns a valid notmuch_message_t whether or not
183 * there is already a document in the database with the given message
184 * ID. These two cases can be distinguished by the value of *status:
187 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
189 * There is already a document with message ID 'message_id' in the
190 * database. The returned message can be used to query/modify the
192 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
194 * No document with 'message_id' exists in the database. The
195 * returned message contains a newly created document (not yet
196 * added to the database) and a document ID that is known not to
197 * exist in the database. The caller can modify the message, and a
198 * call to _notmuch_message_sync will add * the document to the
201 * If an error occurs, this function will return NULL and *status
202 * will be set as appropriate. (The status pointer argument must
206 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
207 const char *message_id,
208 notmuch_private_status_t *status_ret)
210 notmuch_message_t *message;
211 Xapian::Document doc;
212 Xapian::WritableDatabase *db;
216 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
218 message = notmuch_database_find_message (notmuch, message_id);
220 return talloc_steal (notmuch, message);
222 term = talloc_asprintf (NULL, "%s%s",
223 _find_prefix ("id"), message_id);
225 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
229 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
230 INTERNAL_ERROR ("Failure to ensure database is writable.");
232 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
234 doc.add_term (term, 0);
237 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
239 doc_id = _notmuch_database_generate_doc_id (notmuch);
240 } catch (const Xapian::Error &error) {
241 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
242 error.get_msg().c_str());
243 notmuch->exception_reported = TRUE;
244 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
248 message = _notmuch_message_create_for_document (notmuch, notmuch,
249 doc_id, doc, status_ret);
251 /* We want to inform the caller that we had to create a new
253 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
254 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
260 _notmuch_message_get_term (notmuch_message_t *message,
261 Xapian::TermIterator &i, Xapian::TermIterator &end,
264 int prefix_len = strlen (prefix);
265 const char *term = NULL;
271 term = (*i).c_str ();
273 if (!term || strncmp (term, prefix, prefix_len))
276 value = talloc_strdup (message, term + prefix_len);
278 #if DEBUG_DATABASE_SANITY
281 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
282 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
283 message->doc_id, prefix, value,
284 (*i).c_str () + prefix_len);
292 _notmuch_message_ensure_metadata (notmuch_message_t *message)
294 Xapian::TermIterator i, end;
295 const char *thread_prefix = _find_prefix ("thread"),
296 *id_prefix = _find_prefix ("id"),
297 *replyto_prefix = _find_prefix ("replyto");
299 /* We do this all in a single pass because Xapian decompresses the
300 * term list every time you iterate over it. Thus, while this is
301 * slightly more costly than looking up individual fields if only
302 * one field of the message object is actually used, it's a huge
303 * win as more fields are used. */
305 i = message->doc.termlist_begin ();
306 end = message->doc.termlist_end ();
309 if (!message->thread_id)
311 _notmuch_message_get_term (message, i, end, thread_prefix);
314 assert (strcmp (thread_prefix, id_prefix) < 0);
315 if (!message->message_id)
316 message->message_id =
317 _notmuch_message_get_term (message, i, end, id_prefix);
320 assert (strcmp (id_prefix, replyto_prefix) < 0);
321 if (!message->in_reply_to)
322 message->in_reply_to =
323 _notmuch_message_get_term (message, i, end, replyto_prefix);
324 /* It's perfectly valid for a message to have no In-Reply-To
325 * header. For these cases, we return an empty string. */
326 if (!message->in_reply_to)
327 message->in_reply_to = talloc_strdup (message, "");
331 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
332 const char *prefix_name)
334 if (strcmp ("thread", prefix_name) == 0) {
335 talloc_free (message->thread_id);
336 message->thread_id = NULL;
339 if (strcmp ("replyto", prefix_name) == 0) {
340 talloc_free (message->in_reply_to);
341 message->in_reply_to = NULL;
346 _notmuch_message_get_doc_id (notmuch_message_t *message)
348 return message->doc_id;
352 notmuch_message_get_message_id (notmuch_message_t *message)
354 if (!message->message_id)
355 _notmuch_message_ensure_metadata (message);
356 if (!message->message_id)
357 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
359 return message->message_id;
363 _notmuch_message_ensure_message_file (notmuch_message_t *message)
365 const char *filename;
367 if (message->message_file)
370 filename = notmuch_message_get_filename (message);
371 if (unlikely (filename == NULL))
374 message->message_file = _notmuch_message_file_open_ctx (message, filename);
378 notmuch_message_get_header (notmuch_message_t *message, const char *header)
380 _notmuch_message_ensure_message_file (message);
381 if (message->message_file == NULL)
384 return notmuch_message_file_get_header (message->message_file, header);
387 /* Return the message ID from the In-Reply-To header of 'message'.
389 * Returns an empty string ("") if 'message' has no In-Reply-To
392 * Returns NULL if any error occurs.
395 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
397 if (!message->in_reply_to)
398 _notmuch_message_ensure_metadata (message);
399 return message->in_reply_to;
403 notmuch_message_get_thread_id (notmuch_message_t *message)
405 if (!message->thread_id)
406 _notmuch_message_ensure_metadata (message);
407 if (!message->thread_id)
408 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
410 return message->thread_id;
414 _notmuch_message_add_reply (notmuch_message_t *message,
415 notmuch_message_node_t *reply)
417 _notmuch_message_list_append (message->replies, reply);
421 notmuch_message_get_replies (notmuch_message_t *message)
423 return _notmuch_messages_create (message->replies);
426 /* Add an additional 'filename' for 'message'.
428 * This change will not be reflected in the database until the next
429 * call to _notmuch_message_sync. */
431 _notmuch_message_add_filename (notmuch_message_t *message,
432 const char *filename)
434 const char *relative, *directory;
435 notmuch_status_t status;
436 void *local = talloc_new (message);
439 if (filename == NULL)
440 INTERNAL_ERROR ("Message filename cannot be NULL.");
442 if (message->filename_list) {
443 talloc_free (message->filename_list);
444 message->filename_list = NULL;
447 relative = _notmuch_database_relative_path (message->notmuch, filename);
449 status = _notmuch_database_split_path (local, relative, &directory, NULL);
453 status = _notmuch_database_filename_to_direntry (local,
455 filename, &direntry);
459 /* New file-direntry allows navigating to this message with
460 * notmuch_directory_get_child_files() . */
461 _notmuch_message_add_term (message, "file-direntry", direntry);
463 /* New terms allow user to search with folder: specification. */
464 _notmuch_message_gen_terms (message, "folder", directory);
468 return NOTMUCH_STATUS_SUCCESS;
471 /* Remove a particular 'filename' from 'message'.
473 * This change will not be reflected in the database until the next
474 * call to _notmuch_message_sync.
476 * Note: This function does not remove a document from the database,
477 * even if the specified filename is the only filename for this
478 * message. For that functionality, see
479 * _notmuch_database_remove_message. */
481 _notmuch_message_remove_filename (notmuch_message_t *message,
482 const char *filename)
484 const char *direntry_prefix = _find_prefix ("file-direntry");
485 int direntry_prefix_len = strlen (direntry_prefix);
486 const char *folder_prefix = _find_prefix ("folder");
487 int folder_prefix_len = strlen (folder_prefix);
488 void *local = talloc_new (message);
490 notmuch_private_status_t private_status;
491 notmuch_status_t status;
492 Xapian::TermIterator i, last;
494 if (message->filename_list) {
495 talloc_free (message->filename_list);
496 message->filename_list = NULL;
499 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
500 filename, &direntry);
504 /* Unlink this file from its parent directory. */
505 private_status = _notmuch_message_remove_term (message,
506 "file-direntry", direntry);
507 status = COERCE_STATUS (private_status,
508 "Unexpected error from _notmuch_message_remove_term");
510 /* Re-synchronize "folder:" terms for this message. This requires
511 * first removing all "folder:" terms, then adding back terms for
512 * all remaining filenames of the message. */
514 i = message->doc.termlist_begin ();
515 i.skip_to (folder_prefix);
517 /* Terminate loop when no terms remain with desired prefix. */
518 if (i == message->doc.termlist_end () ||
519 strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))
525 message->doc.remove_term ((*i));
526 } catch (const Xapian::InvalidArgumentError) {
527 /* Ignore failure to remove non-existent term. */
531 i = message->doc.termlist_begin ();
532 i.skip_to (direntry_prefix);
534 for (; i != message->doc.termlist_end (); i++) {
535 unsigned int directory_id;
536 const char *direntry, *directory;
539 /* Terminate loop at first term without desired prefix. */
540 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
543 direntry = (*i).c_str ();
544 direntry += direntry_prefix_len;
546 directory_id = strtol (direntry, &colon, 10);
548 if (colon == NULL || *colon != ':')
549 INTERNAL_ERROR ("malformed direntry");
551 directory = _notmuch_database_get_directory_path (local,
554 if (strlen (directory))
555 _notmuch_message_gen_terms (message, "folder", directory);
564 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
566 return talloc_strdup (message, message->doc.get_data ().c_str ());
570 _notmuch_message_clear_data (notmuch_message_t *message)
572 message->doc.set_data ("");
576 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
578 const char *prefix = _find_prefix ("file-direntry");
579 int prefix_len = strlen (prefix);
580 Xapian::TermIterator i;
582 if (message->filename_list)
585 message->filename_list = _notmuch_string_list_create (message);
587 i = message->doc.termlist_begin ();
590 if (i == message->doc.termlist_end () ||
591 strncmp ((*i).c_str (), prefix, prefix_len))
593 /* A message document created by an old version of notmuch
594 * (prior to rename support) will have the filename in the
595 * data of the document rather than as a file-direntry term.
597 * It would be nice to do the upgrade of the document directly
598 * here, but the database is likely open in read-only mode. */
601 data = message->doc.get_data ().c_str ();
604 INTERNAL_ERROR ("message with no filename");
606 _notmuch_string_list_append (message->filename_list, data);
611 for (; i != message->doc.termlist_end (); i++) {
612 void *local = talloc_new (message);
613 const char *db_path, *directory, *basename, *filename;
614 char *colon, *direntry = NULL;
615 unsigned int directory_id;
617 /* Terminate loop at first term without desired prefix. */
618 if (strncmp ((*i).c_str (), prefix, prefix_len))
621 direntry = talloc_strdup (local, (*i).c_str ());
623 direntry += prefix_len;
625 directory_id = strtol (direntry, &colon, 10);
627 if (colon == NULL || *colon != ':')
628 INTERNAL_ERROR ("malformed direntry");
630 basename = colon + 1;
634 db_path = notmuch_database_get_path (message->notmuch);
636 directory = _notmuch_database_get_directory_path (local,
640 if (strlen (directory))
641 filename = talloc_asprintf (message, "%s/%s/%s",
642 db_path, directory, basename);
644 filename = talloc_asprintf (message, "%s/%s",
647 _notmuch_string_list_append (message->filename_list, filename);
654 notmuch_message_get_filename (notmuch_message_t *message)
656 _notmuch_message_ensure_filename_list (message);
658 if (message->filename_list == NULL)
661 if (message->filename_list->head == NULL ||
662 message->filename_list->head->string == NULL)
664 INTERNAL_ERROR ("message with no filename");
667 return message->filename_list->head->string;
670 notmuch_filenames_t *
671 notmuch_message_get_filenames (notmuch_message_t *message)
673 _notmuch_message_ensure_filename_list (message);
675 return _notmuch_filenames_create (message, message->filename_list);
679 notmuch_message_get_flag (notmuch_message_t *message,
680 notmuch_message_flag_t flag)
682 return message->flags & (1 << flag);
686 notmuch_message_set_flag (notmuch_message_t *message,
687 notmuch_message_flag_t flag, notmuch_bool_t enable)
690 message->flags |= (1 << flag);
692 message->flags &= ~(1 << flag);
696 notmuch_message_get_date (notmuch_message_t *message)
701 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
702 } catch (Xapian::Error &error) {
703 INTERNAL_ERROR ("Failed to read timestamp value from document.");
707 return Xapian::sortable_unserialise (value);
711 notmuch_message_get_tags (notmuch_message_t *message)
713 Xapian::TermIterator i, end;
714 i = message->doc.termlist_begin();
715 end = message->doc.termlist_end();
716 return _notmuch_convert_tags(message, i, end);
720 notmuch_message_get_author (notmuch_message_t *message)
722 return message->author;
726 notmuch_message_set_author (notmuch_message_t *message,
730 talloc_free(message->author);
731 message->author = talloc_strdup(message, author);
736 _notmuch_message_set_date (notmuch_message_t *message,
741 /* GMime really doesn't want to see a NULL date, so protect its
743 if (date == NULL || *date == '\0')
746 time_value = g_mime_utils_header_decode_date (date, NULL);
748 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
749 Xapian::sortable_serialise (time_value));
752 /* Synchronize changes made to message->doc out into the database. */
754 _notmuch_message_sync (notmuch_message_t *message)
756 Xapian::WritableDatabase *db;
758 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
761 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
762 db->replace_document (message->doc_id, message->doc);
765 /* Ensure that 'message' is not holding any file object open. Future
766 * calls to various functions will still automatically open the
767 * message file as needed.
770 _notmuch_message_close (notmuch_message_t *message)
772 if (message->message_file) {
773 notmuch_message_file_close (message->message_file);
774 message->message_file = NULL;
778 /* Add a name:value term to 'message', (the actual term will be
779 * encoded by prefixing the value with a short prefix). See
780 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
781 * names to prefix values.
783 * This change will not be reflected in the database until the next
784 * call to _notmuch_message_sync. */
785 notmuch_private_status_t
786 _notmuch_message_add_term (notmuch_message_t *message,
787 const char *prefix_name,
794 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
796 term = talloc_asprintf (message, "%s%s",
797 _find_prefix (prefix_name), value);
799 if (strlen (term) > NOTMUCH_TERM_MAX)
800 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
802 message->doc.add_term (term, 0);
806 _notmuch_message_invalidate_metadata (message, prefix_name);
808 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
811 /* Parse 'text' and add a term to 'message' for each parsed word. Each
812 * term will be added both prefixed (if prefix_name is not NULL) and
813 * also unprefixed). */
814 notmuch_private_status_t
815 _notmuch_message_gen_terms (notmuch_message_t *message,
816 const char *prefix_name,
819 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
822 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
824 term_gen->set_document (message->doc);
825 term_gen->set_termpos (message->termpos);
828 const char *prefix = _find_prefix (prefix_name);
830 term_gen->index_text (text, 1, prefix);
831 message->termpos = term_gen->get_termpos ();
834 term_gen->index_text (text);
836 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
839 /* Remove a name:value term from 'message', (the actual term will be
840 * encoded by prefixing the value with a short prefix). See
841 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
842 * names to prefix values.
844 * This change will not be reflected in the database until the next
845 * call to _notmuch_message_sync. */
846 notmuch_private_status_t
847 _notmuch_message_remove_term (notmuch_message_t *message,
848 const char *prefix_name,
854 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
856 term = talloc_asprintf (message, "%s%s",
857 _find_prefix (prefix_name), value);
859 if (strlen (term) > NOTMUCH_TERM_MAX)
860 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
863 message->doc.remove_term (term);
864 } catch (const Xapian::InvalidArgumentError) {
865 /* We'll let the philosopher's try to wrestle with the
866 * question of whether failing to remove that which was not
867 * there in the first place is failure. For us, we'll silently
868 * consider it all good. */
873 _notmuch_message_invalidate_metadata (message, prefix_name);
875 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
879 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
881 notmuch_private_status_t private_status;
882 notmuch_status_t status;
884 status = _notmuch_database_ensure_writable (message->notmuch);
889 return NOTMUCH_STATUS_NULL_POINTER;
891 if (strlen (tag) > NOTMUCH_TAG_MAX)
892 return NOTMUCH_STATUS_TAG_TOO_LONG;
894 private_status = _notmuch_message_add_term (message, "tag", tag);
895 if (private_status) {
896 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
900 if (! message->frozen)
901 _notmuch_message_sync (message);
903 return NOTMUCH_STATUS_SUCCESS;
907 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
909 notmuch_private_status_t private_status;
910 notmuch_status_t status;
912 status = _notmuch_database_ensure_writable (message->notmuch);
917 return NOTMUCH_STATUS_NULL_POINTER;
919 if (strlen (tag) > NOTMUCH_TAG_MAX)
920 return NOTMUCH_STATUS_TAG_TOO_LONG;
922 private_status = _notmuch_message_remove_term (message, "tag", tag);
923 if (private_status) {
924 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
928 if (! message->frozen)
929 _notmuch_message_sync (message);
931 return NOTMUCH_STATUS_SUCCESS;
935 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
938 notmuch_status_t status;
939 notmuch_filenames_t *filenames;
940 const char *filename;
941 char *combined_flags = talloc_strdup (message, "");
943 int seen_maildir_info = 0;
945 for (filenames = notmuch_message_get_filenames (message);
946 notmuch_filenames_valid (filenames);
947 notmuch_filenames_move_to_next (filenames))
949 filename = notmuch_filenames_get (filenames);
951 flags = strstr (filename, ":2,");
955 seen_maildir_info = 1;
958 combined_flags = talloc_strdup_append (combined_flags, flags);
961 /* If none of the filenames have any maildir info field (not even
962 * an empty info with no flags set) then there's no information to
963 * go on, so do nothing. */
964 if (! seen_maildir_info)
965 return NOTMUCH_STATUS_SUCCESS;
967 status = notmuch_message_freeze (message);
971 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
972 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
976 status = notmuch_message_add_tag (message, flag2tag[i].tag);
978 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
983 status = notmuch_message_thaw (message);
985 talloc_free (combined_flags);
990 /* Is the given filename within a maildir directory?
992 * Specifically, is the final directory component of 'filename' either
993 * "cur" or "new". If so, return a pointer to that final directory
994 * component within 'filename'. If not, return NULL.
996 * A non-NULL return value is guaranteed to be a valid string pointer
997 * pointing to the characters "new/" or "cur/", (but not
1001 _filename_is_in_maildir (const char *filename)
1003 const char *slash, *dir = NULL;
1005 /* Find the last '/' separating directory from filename. */
1006 slash = strrchr (filename, '/');
1010 /* Jump back 4 characters to where the previous '/' will be if the
1011 * directory is named "cur" or "new". */
1012 if (slash - filename < 4)
1022 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1023 STRNCMP_LITERAL (dir, "new/") == 0)
1031 /* From the set of tags on 'message' and the flag2tag table, compute a
1032 * set of maildir-flag actions to be taken, (flags that should be
1033 * either set or cleared).
1035 * The result is returned as two talloced strings: to_set, and to_clear
1038 _get_maildir_flag_actions (notmuch_message_t *message,
1040 char **to_clear_ret)
1042 char *to_set, *to_clear;
1043 notmuch_tags_t *tags;
1047 to_set = talloc_strdup (message, "");
1048 to_clear = talloc_strdup (message, "");
1050 /* First, find flags for all set tags. */
1051 for (tags = notmuch_message_get_tags (message);
1052 notmuch_tags_valid (tags);
1053 notmuch_tags_move_to_next (tags))
1055 tag = notmuch_tags_get (tags);
1057 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1058 if (strcmp (tag, flag2tag[i].tag) == 0) {
1059 if (flag2tag[i].inverse)
1060 to_clear = talloc_asprintf_append (to_clear,
1064 to_set = talloc_asprintf_append (to_set,
1071 /* Then, find the flags for all tags not present. */
1072 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1073 if (flag2tag[i].inverse) {
1074 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1075 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1077 if (strchr (to_set, flag2tag[i].flag) == NULL)
1078 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1082 *to_set_ret = to_set;
1083 *to_clear_ret = to_clear;
1086 /* Given 'filename' and a set of maildir flags to set and to clear,
1087 * compute the new maildir filename.
1089 * If the existing filename is in the directory "new", the new
1090 * filename will be in the directory "cur".
1092 * After a sequence of ":2," in the filename, any subsequent
1093 * single-character flags will be added or removed according to the
1094 * characters in flags_to_set and flags_to_clear. Any existing flags
1095 * not mentioned in either string will remain. The final list of flags
1096 * will be in ASCII order.
1098 * If the original flags seem invalid, (repeated characters or
1099 * non-ASCII ordering of flags), this function will return NULL
1100 * (meaning that renaming would not be safe and should not occur).
1103 _new_maildir_filename (void *ctx,
1104 const char *filename,
1105 const char *flags_to_set,
1106 const char *flags_to_clear)
1108 const char *info, *flags;
1109 unsigned int flag, last_flag;
1110 char *filename_new, *dir;
1112 int flags_in_map = 0;
1116 memset (flag_map, 0, sizeof (flag_map));
1118 info = strstr (filename, ":2,");
1121 info = filename + strlen(filename);
1125 /* Loop through existing flags in filename. */
1126 for (flags = info + 3, last_flag = 0;
1128 last_flag = flag, flags++)
1132 /* Original flags not in ASCII order. Abort. */
1133 if (flag < last_flag)
1136 /* Non-ASCII flag. Abort. */
1137 if (flag > sizeof(flag_map) - 1)
1140 /* Repeated flag value. Abort. */
1149 /* Then set and clear our flags from tags. */
1150 for (flags = flags_to_set; *flags; flags++) {
1152 if (flag_map[flag] == 0) {
1158 for (flags = flags_to_clear; *flags; flags++) {
1160 if (flag_map[flag]) {
1166 filename_new = (char *) talloc_size (ctx,
1168 strlen (":2,") + flags_in_map + 1);
1169 if (unlikely (filename_new == NULL))
1172 strncpy (filename_new, filename, info - filename);
1173 filename_new[info - filename] = '\0';
1175 strcat (filename_new, ":2,");
1177 s = filename_new + strlen (filename_new);
1178 for (i = 0; i < sizeof (flag_map); i++)
1187 /* If message is in new/ move it under cur/. */
1188 dir = (char *) _filename_is_in_maildir (filename_new);
1189 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1190 memcpy (dir, "cur/", 4);
1192 return filename_new;
1196 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1198 notmuch_filenames_t *filenames;
1199 const char *filename;
1201 char *to_set, *to_clear;
1202 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1204 _get_maildir_flag_actions (message, &to_set, &to_clear);
1206 for (filenames = notmuch_message_get_filenames (message);
1207 notmuch_filenames_valid (filenames);
1208 notmuch_filenames_move_to_next (filenames))
1210 filename = notmuch_filenames_get (filenames);
1212 if (! _filename_is_in_maildir (filename))
1215 filename_new = _new_maildir_filename (message, filename,
1217 if (filename_new == NULL)
1220 if (strcmp (filename, filename_new)) {
1222 notmuch_status_t new_status;
1224 err = rename (filename, filename_new);
1228 new_status = _notmuch_message_remove_filename (message,
1230 /* Hold on to only the first error. */
1231 if (! status && new_status) {
1232 status = new_status;
1236 new_status = _notmuch_message_add_filename (message,
1238 /* Hold on to only the first error. */
1239 if (! status && new_status) {
1240 status = new_status;
1244 _notmuch_message_sync (message);
1247 talloc_free (filename_new);
1250 talloc_free (to_set);
1251 talloc_free (to_clear);
1253 return NOTMUCH_STATUS_SUCCESS;
1257 notmuch_message_remove_all_tags (notmuch_message_t *message)
1259 notmuch_private_status_t private_status;
1260 notmuch_status_t status;
1261 notmuch_tags_t *tags;
1264 status = _notmuch_database_ensure_writable (message->notmuch);
1268 for (tags = notmuch_message_get_tags (message);
1269 notmuch_tags_valid (tags);
1270 notmuch_tags_move_to_next (tags))
1272 tag = notmuch_tags_get (tags);
1274 private_status = _notmuch_message_remove_term (message, "tag", tag);
1275 if (private_status) {
1276 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1281 if (! message->frozen)
1282 _notmuch_message_sync (message);
1284 return NOTMUCH_STATUS_SUCCESS;
1288 notmuch_message_freeze (notmuch_message_t *message)
1290 notmuch_status_t status;
1292 status = _notmuch_database_ensure_writable (message->notmuch);
1298 return NOTMUCH_STATUS_SUCCESS;
1302 notmuch_message_thaw (notmuch_message_t *message)
1304 notmuch_status_t status;
1306 status = _notmuch_database_ensure_writable (message->notmuch);
1310 if (message->frozen > 0) {
1312 if (message->frozen == 0)
1313 _notmuch_message_sync (message);
1314 return NOTMUCH_STATUS_SUCCESS;
1316 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1321 notmuch_message_destroy (notmuch_message_t *message)
1323 talloc_free (message);