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 visible _notmuch_message {
29 notmuch_database_t *notmuch;
35 notmuch_string_list_t *tag_list;
36 notmuch_string_list_t *filename_term_list;
37 notmuch_string_list_t *filename_list;
39 notmuch_message_file_t *message_file;
40 notmuch_message_list_t *replies;
44 Xapian::termcount termpos;
47 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
49 struct maildir_flag_tag {
55 /* ASCII ordered table of Maildir flags and associated tags */
56 static struct maildir_flag_tag flag2tag[] = {
57 { 'D', "draft", false},
58 { 'F', "flagged", false},
59 { 'P', "passed", false},
60 { 'R', "replied", false},
61 { 'S', "unread", true }
64 /* We end up having to call the destructor explicitly because we had
65 * to use "placement new" in order to initialize C++ objects within a
66 * block that we allocated with talloc. So C++ is making talloc
67 * slightly less simple to use, (we wouldn't need
68 * talloc_set_destructor at all otherwise).
71 _notmuch_message_destructor (notmuch_message_t *message)
73 message->doc.~Document ();
78 static notmuch_message_t *
79 _notmuch_message_create_for_document (const void *talloc_owner,
80 notmuch_database_t *notmuch,
83 notmuch_private_status_t *status)
85 notmuch_message_t *message;
88 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
90 message = talloc (talloc_owner, notmuch_message_t);
91 if (unlikely (message == NULL)) {
93 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
97 message->notmuch = notmuch;
98 message->doc_id = doc_id;
103 /* Each of these will be lazily created as needed. */
104 message->message_id = NULL;
105 message->thread_id = NULL;
106 message->in_reply_to = NULL;
107 message->tag_list = NULL;
108 message->filename_term_list = NULL;
109 message->filename_list = NULL;
110 message->message_file = NULL;
111 message->author = NULL;
113 message->replies = _notmuch_message_list_create (message);
114 if (unlikely (message->replies == NULL)) {
116 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
120 /* This is C++'s creepy "placement new", which is really just an
121 * ugly way to call a constructor for a pre-allocated object. So
122 * it's really not an error to not be checking for OUT_OF_MEMORY
123 * here, since this "new" isn't actually allocating memory. This
124 * is language-design comedy of the wrong kind. */
126 new (&message->doc) Xapian::Document;
128 talloc_set_destructor (message, _notmuch_message_destructor);
131 message->termpos = 0;
136 /* Create a new notmuch_message_t object for an existing document in
139 * Here, 'talloc owner' is an optional talloc context to which the new
140 * message will belong. This allows for the caller to not bother
141 * calling notmuch_message_destroy on the message, and know that all
142 * memory will be reclaimed when 'talloc_owner' is freed. The caller
143 * still can call notmuch_message_destroy when finished with the
144 * message if desired.
146 * The 'talloc_owner' argument can also be NULL, in which case the
147 * caller *is* responsible for calling notmuch_message_destroy.
149 * If no document exists in the database with document ID of 'doc_id'
150 * then this function returns NULL and optionally sets *status to
151 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
153 * This function can also fail to due lack of available memory,
154 * returning NULL and optionally setting *status to
155 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
157 * The caller can pass NULL for status if uninterested in
158 * distinguishing these two cases.
161 _notmuch_message_create (const void *talloc_owner,
162 notmuch_database_t *notmuch,
164 notmuch_private_status_t *status)
166 Xapian::Document doc;
169 doc = notmuch->xapian_db->get_document (doc_id);
170 } catch (const Xapian::DocNotFoundError &error) {
172 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
176 return _notmuch_message_create_for_document (talloc_owner, notmuch,
177 doc_id, doc, status);
180 /* Create a new notmuch_message_t object for a specific message ID,
181 * (which may or may not already exist in the database).
183 * The 'notmuch' database will be the talloc owner of the returned
186 * This function returns a valid notmuch_message_t whether or not
187 * there is already a document in the database with the given message
188 * ID. These two cases can be distinguished by the value of *status:
191 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
193 * There is already a document with message ID 'message_id' in the
194 * database. The returned message can be used to query/modify the
196 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
198 * No document with 'message_id' exists in the database. The
199 * returned message contains a newly created document (not yet
200 * added to the database) and a document ID that is known not to
201 * exist in the database. The caller can modify the message, and a
202 * call to _notmuch_message_sync will add * the document to the
205 * If an error occurs, this function will return NULL and *status
206 * will be set as appropriate. (The status pointer argument must
210 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
211 const char *message_id,
212 notmuch_private_status_t *status_ret)
214 notmuch_message_t *message;
215 Xapian::Document doc;
219 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
221 message = notmuch_database_find_message (notmuch, message_id);
223 return talloc_steal (notmuch, message);
225 term = talloc_asprintf (NULL, "%s%s",
226 _find_prefix ("id"), message_id);
228 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
232 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
233 INTERNAL_ERROR ("Failure to ensure database is writable.");
236 doc.add_term (term, 0);
239 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
241 doc_id = _notmuch_database_generate_doc_id (notmuch);
242 } catch (const Xapian::Error &error) {
243 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
244 error.get_msg().c_str());
245 notmuch->exception_reported = TRUE;
246 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
250 message = _notmuch_message_create_for_document (notmuch, notmuch,
251 doc_id, doc, status_ret);
253 /* We want to inform the caller that we had to create a new
255 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
256 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
262 _notmuch_message_get_term (notmuch_message_t *message,
263 Xapian::TermIterator &i, Xapian::TermIterator &end,
266 int prefix_len = strlen (prefix);
267 const char *term = NULL;
273 term = (*i).c_str ();
275 if (!term || strncmp (term, prefix, prefix_len))
278 value = talloc_strdup (message, term + prefix_len);
280 #if DEBUG_DATABASE_SANITY
283 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
284 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
285 message->doc_id, prefix, value,
286 (*i).c_str () + prefix_len);
294 _notmuch_message_ensure_metadata (notmuch_message_t *message)
296 Xapian::TermIterator i, end;
297 const char *thread_prefix = _find_prefix ("thread"),
298 *tag_prefix = _find_prefix ("tag"),
299 *id_prefix = _find_prefix ("id"),
300 *filename_prefix = _find_prefix ("file-direntry"),
301 *replyto_prefix = _find_prefix ("replyto");
303 /* We do this all in a single pass because Xapian decompresses the
304 * term list every time you iterate over it. Thus, while this is
305 * slightly more costly than looking up individual fields if only
306 * one field of the message object is actually used, it's a huge
307 * win as more fields are used. */
309 i = message->doc.termlist_begin ();
310 end = message->doc.termlist_end ();
313 if (!message->thread_id)
315 _notmuch_message_get_term (message, i, end, thread_prefix);
318 assert (strcmp (thread_prefix, tag_prefix) < 0);
319 if (!message->tag_list) {
321 _notmuch_database_get_terms_with_prefix (message, i, end,
323 _notmuch_string_list_sort (message->tag_list);
327 assert (strcmp (tag_prefix, id_prefix) < 0);
328 if (!message->message_id)
329 message->message_id =
330 _notmuch_message_get_term (message, i, end, id_prefix);
332 /* Get filename list. Here we get only the terms. We lazily
333 * expand them to full file names when needed in
334 * _notmuch_message_ensure_filename_list. */
335 assert (strcmp (id_prefix, filename_prefix) < 0);
336 if (!message->filename_term_list && !message->filename_list)
337 message->filename_term_list =
338 _notmuch_database_get_terms_with_prefix (message, i, end,
342 assert (strcmp (filename_prefix, replyto_prefix) < 0);
343 if (!message->in_reply_to)
344 message->in_reply_to =
345 _notmuch_message_get_term (message, i, end, replyto_prefix);
346 /* It's perfectly valid for a message to have no In-Reply-To
347 * header. For these cases, we return an empty string. */
348 if (!message->in_reply_to)
349 message->in_reply_to = talloc_strdup (message, "");
353 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
354 const char *prefix_name)
356 if (strcmp ("thread", prefix_name) == 0) {
357 talloc_free (message->thread_id);
358 message->thread_id = NULL;
361 if (strcmp ("tag", prefix_name) == 0) {
362 talloc_unlink (message, message->tag_list);
363 message->tag_list = NULL;
366 if (strcmp ("file-direntry", prefix_name) == 0) {
367 talloc_free (message->filename_term_list);
368 talloc_free (message->filename_list);
369 message->filename_term_list = message->filename_list = NULL;
372 if (strcmp ("replyto", prefix_name) == 0) {
373 talloc_free (message->in_reply_to);
374 message->in_reply_to = NULL;
379 _notmuch_message_get_doc_id (notmuch_message_t *message)
381 return message->doc_id;
385 notmuch_message_get_message_id (notmuch_message_t *message)
387 if (!message->message_id)
388 _notmuch_message_ensure_metadata (message);
389 if (!message->message_id)
390 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
392 return message->message_id;
396 _notmuch_message_ensure_message_file (notmuch_message_t *message)
398 const char *filename;
400 if (message->message_file)
403 filename = notmuch_message_get_filename (message);
404 if (unlikely (filename == NULL))
407 message->message_file = _notmuch_message_file_open_ctx (message, filename);
411 notmuch_message_get_header (notmuch_message_t *message, const char *header)
413 _notmuch_message_ensure_message_file (message);
414 if (message->message_file == NULL)
417 return notmuch_message_file_get_header (message->message_file, header);
420 /* Return the message ID from the In-Reply-To header of 'message'.
422 * Returns an empty string ("") if 'message' has no In-Reply-To
425 * Returns NULL if any error occurs.
428 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
430 if (!message->in_reply_to)
431 _notmuch_message_ensure_metadata (message);
432 return message->in_reply_to;
436 notmuch_message_get_thread_id (notmuch_message_t *message)
438 if (!message->thread_id)
439 _notmuch_message_ensure_metadata (message);
440 if (!message->thread_id)
441 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
443 return message->thread_id;
447 _notmuch_message_add_reply (notmuch_message_t *message,
448 notmuch_message_node_t *reply)
450 _notmuch_message_list_append (message->replies, reply);
454 notmuch_message_get_replies (notmuch_message_t *message)
456 return _notmuch_messages_create (message->replies);
459 /* Add an additional 'filename' for 'message'.
461 * This change will not be reflected in the database until the next
462 * call to _notmuch_message_sync. */
464 _notmuch_message_add_filename (notmuch_message_t *message,
465 const char *filename)
467 const char *relative, *directory;
468 notmuch_status_t status;
469 void *local = talloc_new (message);
472 if (filename == NULL)
473 INTERNAL_ERROR ("Message filename cannot be NULL.");
475 relative = _notmuch_database_relative_path (message->notmuch, filename);
477 status = _notmuch_database_split_path (local, relative, &directory, NULL);
481 status = _notmuch_database_filename_to_direntry (local,
483 filename, &direntry);
487 /* New file-direntry allows navigating to this message with
488 * notmuch_directory_get_child_files() . */
489 _notmuch_message_add_term (message, "file-direntry", direntry);
491 /* New terms allow user to search with folder: specification. */
492 _notmuch_message_gen_terms (message, "folder", directory);
496 return NOTMUCH_STATUS_SUCCESS;
499 /* Remove a particular 'filename' from 'message'.
501 * This change will not be reflected in the database until the next
502 * call to _notmuch_message_sync.
504 * If this message still has other filenames, returns
505 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
507 * Note: This function does not remove a document from the database,
508 * even if the specified filename is the only filename for this
509 * message. For that functionality, see
510 * _notmuch_database_remove_message. */
512 _notmuch_message_remove_filename (notmuch_message_t *message,
513 const char *filename)
515 const char *direntry_prefix = _find_prefix ("file-direntry");
516 int direntry_prefix_len = strlen (direntry_prefix);
517 const char *folder_prefix = _find_prefix ("folder");
518 int folder_prefix_len = strlen (folder_prefix);
519 void *local = talloc_new (message);
520 char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);
521 int zfolder_prefix_len = strlen (zfolder_prefix);
523 notmuch_private_status_t private_status;
524 notmuch_status_t status;
525 Xapian::TermIterator i, last;
527 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
528 filename, &direntry);
532 /* Unlink this file from its parent directory. */
533 private_status = _notmuch_message_remove_term (message,
534 "file-direntry", direntry);
535 status = COERCE_STATUS (private_status,
536 "Unexpected error from _notmuch_message_remove_term");
540 /* Re-synchronize "folder:" terms for this message. This requires:
541 * 1. removing all "folder:" terms
542 * 2. removing all "folder:" stemmed terms
543 * 3. adding back terms for all remaining filenames of the message. */
545 /* 1. removing all "folder:" terms */
547 i = message->doc.termlist_begin ();
548 i.skip_to (folder_prefix);
550 /* Terminate loop when no terms remain with desired prefix. */
551 if (i == message->doc.termlist_end () ||
552 strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))
558 message->doc.remove_term ((*i));
559 } catch (const Xapian::InvalidArgumentError) {
560 /* Ignore failure to remove non-existent term. */
564 /* 2. removing all "folder:" stemmed terms */
566 i = message->doc.termlist_begin ();
567 i.skip_to (zfolder_prefix);
569 /* Terminate loop when no terms remain with desired prefix. */
570 if (i == message->doc.termlist_end () ||
571 strncmp ((*i).c_str (), zfolder_prefix, zfolder_prefix_len))
577 message->doc.remove_term ((*i));
578 } catch (const Xapian::InvalidArgumentError) {
579 /* Ignore failure to remove non-existent term. */
583 /* 3. adding back terms for all remaining filenames of the message. */
584 i = message->doc.termlist_begin ();
585 i.skip_to (direntry_prefix);
587 for (; i != message->doc.termlist_end (); i++) {
588 unsigned int directory_id;
589 const char *direntry, *directory;
592 /* Terminate loop at first term without desired prefix. */
593 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
596 /* Indicate that there are filenames remaining. */
597 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
599 direntry = (*i).c_str ();
600 direntry += direntry_prefix_len;
602 directory_id = strtol (direntry, &colon, 10);
604 if (colon == NULL || *colon != ':')
605 INTERNAL_ERROR ("malformed direntry");
607 directory = _notmuch_database_get_directory_path (local,
610 if (strlen (directory))
611 _notmuch_message_gen_terms (message, "folder", directory);
620 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
622 return talloc_strdup (message, message->doc.get_data ().c_str ());
626 _notmuch_message_clear_data (notmuch_message_t *message)
628 message->doc.set_data ("");
632 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
634 notmuch_string_node_t *node;
636 if (message->filename_list)
639 if (!message->filename_term_list)
640 _notmuch_message_ensure_metadata (message);
642 message->filename_list = _notmuch_string_list_create (message);
643 node = message->filename_term_list->head;
646 /* A message document created by an old version of notmuch
647 * (prior to rename support) will have the filename in the
648 * data of the document rather than as a file-direntry term.
650 * It would be nice to do the upgrade of the document directly
651 * here, but the database is likely open in read-only mode. */
654 data = message->doc.get_data ().c_str ();
657 INTERNAL_ERROR ("message with no filename");
659 _notmuch_string_list_append (message->filename_list, data);
664 for (; node; node = node->next) {
665 void *local = talloc_new (message);
666 const char *db_path, *directory, *basename, *filename;
667 char *colon, *direntry = NULL;
668 unsigned int directory_id;
670 direntry = node->string;
672 directory_id = strtol (direntry, &colon, 10);
674 if (colon == NULL || *colon != ':')
675 INTERNAL_ERROR ("malformed direntry");
677 basename = colon + 1;
681 db_path = notmuch_database_get_path (message->notmuch);
683 directory = _notmuch_database_get_directory_path (local,
687 if (strlen (directory))
688 filename = talloc_asprintf (message, "%s/%s/%s",
689 db_path, directory, basename);
691 filename = talloc_asprintf (message, "%s/%s",
694 _notmuch_string_list_append (message->filename_list, filename);
699 talloc_free (message->filename_term_list);
700 message->filename_term_list = NULL;
704 notmuch_message_get_filename (notmuch_message_t *message)
706 _notmuch_message_ensure_filename_list (message);
708 if (message->filename_list == NULL)
711 if (message->filename_list->head == NULL ||
712 message->filename_list->head->string == NULL)
714 INTERNAL_ERROR ("message with no filename");
717 return message->filename_list->head->string;
720 notmuch_filenames_t *
721 notmuch_message_get_filenames (notmuch_message_t *message)
723 _notmuch_message_ensure_filename_list (message);
725 return _notmuch_filenames_create (message, message->filename_list);
729 notmuch_message_get_flag (notmuch_message_t *message,
730 notmuch_message_flag_t flag)
732 return message->flags & (1 << flag);
736 notmuch_message_set_flag (notmuch_message_t *message,
737 notmuch_message_flag_t flag, notmuch_bool_t enable)
740 message->flags |= (1 << flag);
742 message->flags &= ~(1 << flag);
746 notmuch_message_get_date (notmuch_message_t *message)
751 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
752 } catch (Xapian::Error &error) {
753 INTERNAL_ERROR ("Failed to read timestamp value from document.");
757 return Xapian::sortable_unserialise (value);
761 notmuch_message_get_tags (notmuch_message_t *message)
763 notmuch_tags_t *tags;
765 if (!message->tag_list)
766 _notmuch_message_ensure_metadata (message);
768 tags = _notmuch_tags_create (message, message->tag_list);
769 /* _notmuch_tags_create steals the reference to the tag_list, but
770 * in this case it's still used by the message, so we add an
771 * *additional* talloc reference to the list. As a result, it's
772 * possible to modify the message tags (which talloc_unlink's the
773 * current list from the message) while still iterating because
774 * the iterator will keep the current list alive. */
775 talloc_reference (message, message->tag_list);
780 notmuch_message_get_author (notmuch_message_t *message)
782 return message->author;
786 notmuch_message_set_author (notmuch_message_t *message,
790 talloc_free(message->author);
791 message->author = talloc_strdup(message, author);
796 _notmuch_message_set_date (notmuch_message_t *message,
801 /* GMime really doesn't want to see a NULL date, so protect its
803 if (date == NULL || *date == '\0')
806 time_value = g_mime_utils_header_decode_date (date, NULL);
808 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
809 Xapian::sortable_serialise (time_value));
812 /* Synchronize changes made to message->doc out into the database. */
814 _notmuch_message_sync (notmuch_message_t *message)
816 Xapian::WritableDatabase *db;
818 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
821 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
822 db->replace_document (message->doc_id, message->doc);
825 /* Delete a message document from the database. */
827 _notmuch_message_delete (notmuch_message_t *message)
829 notmuch_status_t status;
830 Xapian::WritableDatabase *db;
832 status = _notmuch_database_ensure_writable (message->notmuch);
836 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
837 db->delete_document (message->doc_id);
838 return NOTMUCH_STATUS_SUCCESS;
841 /* Ensure that 'message' is not holding any file object open. Future
842 * calls to various functions will still automatically open the
843 * message file as needed.
846 _notmuch_message_close (notmuch_message_t *message)
848 if (message->message_file) {
849 notmuch_message_file_close (message->message_file);
850 message->message_file = NULL;
854 /* Add a name:value term to 'message', (the actual term will be
855 * encoded by prefixing the value with a short prefix). See
856 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
857 * names to prefix values.
859 * This change will not be reflected in the database until the next
860 * call to _notmuch_message_sync. */
861 notmuch_private_status_t
862 _notmuch_message_add_term (notmuch_message_t *message,
863 const char *prefix_name,
870 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
872 term = talloc_asprintf (message, "%s%s",
873 _find_prefix (prefix_name), value);
875 if (strlen (term) > NOTMUCH_TERM_MAX)
876 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
878 message->doc.add_term (term, 0);
882 _notmuch_message_invalidate_metadata (message, prefix_name);
884 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
887 /* Parse 'text' and add a term to 'message' for each parsed word. Each
888 * term will be added both prefixed (if prefix_name is not NULL) and
889 * also non-prefixed). */
890 notmuch_private_status_t
891 _notmuch_message_gen_terms (notmuch_message_t *message,
892 const char *prefix_name,
895 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
898 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
900 term_gen->set_document (message->doc);
901 term_gen->set_termpos (message->termpos);
904 const char *prefix = _find_prefix (prefix_name);
906 term_gen->index_text (text, 1, prefix);
907 message->termpos = term_gen->get_termpos ();
910 term_gen->index_text (text);
912 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
915 /* Remove a name:value term from 'message', (the actual term will be
916 * encoded by prefixing the value with a short prefix). See
917 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
918 * names to prefix values.
920 * This change will not be reflected in the database until the next
921 * call to _notmuch_message_sync. */
922 notmuch_private_status_t
923 _notmuch_message_remove_term (notmuch_message_t *message,
924 const char *prefix_name,
930 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
932 term = talloc_asprintf (message, "%s%s",
933 _find_prefix (prefix_name), value);
935 if (strlen (term) > NOTMUCH_TERM_MAX)
936 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
939 message->doc.remove_term (term);
940 } catch (const Xapian::InvalidArgumentError) {
941 /* We'll let the philosopher's try to wrestle with the
942 * question of whether failing to remove that which was not
943 * there in the first place is failure. For us, we'll silently
944 * consider it all good. */
949 _notmuch_message_invalidate_metadata (message, prefix_name);
951 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
955 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
957 notmuch_private_status_t private_status;
958 notmuch_status_t status;
960 status = _notmuch_database_ensure_writable (message->notmuch);
965 return NOTMUCH_STATUS_NULL_POINTER;
967 if (strlen (tag) > NOTMUCH_TAG_MAX)
968 return NOTMUCH_STATUS_TAG_TOO_LONG;
970 private_status = _notmuch_message_add_term (message, "tag", tag);
971 if (private_status) {
972 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
976 if (! message->frozen)
977 _notmuch_message_sync (message);
979 return NOTMUCH_STATUS_SUCCESS;
983 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
985 notmuch_private_status_t private_status;
986 notmuch_status_t status;
988 status = _notmuch_database_ensure_writable (message->notmuch);
993 return NOTMUCH_STATUS_NULL_POINTER;
995 if (strlen (tag) > NOTMUCH_TAG_MAX)
996 return NOTMUCH_STATUS_TAG_TOO_LONG;
998 private_status = _notmuch_message_remove_term (message, "tag", tag);
999 if (private_status) {
1000 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1004 if (! message->frozen)
1005 _notmuch_message_sync (message);
1007 return NOTMUCH_STATUS_SUCCESS;
1011 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1014 notmuch_status_t status;
1015 notmuch_filenames_t *filenames;
1016 const char *filename;
1017 char *combined_flags = talloc_strdup (message, "");
1019 int seen_maildir_info = 0;
1021 for (filenames = notmuch_message_get_filenames (message);
1022 notmuch_filenames_valid (filenames);
1023 notmuch_filenames_move_to_next (filenames))
1025 filename = notmuch_filenames_get (filenames);
1027 flags = strstr (filename, ":2,");
1031 seen_maildir_info = 1;
1034 combined_flags = talloc_strdup_append (combined_flags, flags);
1037 /* If none of the filenames have any maildir info field (not even
1038 * an empty info with no flags set) then there's no information to
1039 * go on, so do nothing. */
1040 if (! seen_maildir_info)
1041 return NOTMUCH_STATUS_SUCCESS;
1043 status = notmuch_message_freeze (message);
1047 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1048 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1050 flag2tag[i].inverse)
1052 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1054 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1059 status = notmuch_message_thaw (message);
1061 talloc_free (combined_flags);
1066 /* Is the given filename within a maildir directory?
1068 * Specifically, is the final directory component of 'filename' either
1069 * "cur" or "new". If so, return a pointer to that final directory
1070 * component within 'filename'. If not, return NULL.
1072 * A non-NULL return value is guaranteed to be a valid string pointer
1073 * pointing to the characters "new/" or "cur/", (but not
1077 _filename_is_in_maildir (const char *filename)
1079 const char *slash, *dir = NULL;
1081 /* Find the last '/' separating directory from filename. */
1082 slash = strrchr (filename, '/');
1086 /* Jump back 4 characters to where the previous '/' will be if the
1087 * directory is named "cur" or "new". */
1088 if (slash - filename < 4)
1098 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1099 STRNCMP_LITERAL (dir, "new/") == 0)
1107 /* From the set of tags on 'message' and the flag2tag table, compute a
1108 * set of maildir-flag actions to be taken, (flags that should be
1109 * either set or cleared).
1111 * The result is returned as two talloced strings: to_set, and to_clear
1114 _get_maildir_flag_actions (notmuch_message_t *message,
1116 char **to_clear_ret)
1118 char *to_set, *to_clear;
1119 notmuch_tags_t *tags;
1123 to_set = talloc_strdup (message, "");
1124 to_clear = talloc_strdup (message, "");
1126 /* First, find flags for all set tags. */
1127 for (tags = notmuch_message_get_tags (message);
1128 notmuch_tags_valid (tags);
1129 notmuch_tags_move_to_next (tags))
1131 tag = notmuch_tags_get (tags);
1133 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1134 if (strcmp (tag, flag2tag[i].tag) == 0) {
1135 if (flag2tag[i].inverse)
1136 to_clear = talloc_asprintf_append (to_clear,
1140 to_set = talloc_asprintf_append (to_set,
1147 /* Then, find the flags for all tags not present. */
1148 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1149 if (flag2tag[i].inverse) {
1150 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1151 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1153 if (strchr (to_set, flag2tag[i].flag) == NULL)
1154 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1158 *to_set_ret = to_set;
1159 *to_clear_ret = to_clear;
1162 /* Given 'filename' and a set of maildir flags to set and to clear,
1163 * compute the new maildir filename.
1165 * If the existing filename is in the directory "new", the new
1166 * filename will be in the directory "cur".
1168 * After a sequence of ":2," in the filename, any subsequent
1169 * single-character flags will be added or removed according to the
1170 * characters in flags_to_set and flags_to_clear. Any existing flags
1171 * not mentioned in either string will remain. The final list of flags
1172 * will be in ASCII order.
1174 * If the original flags seem invalid, (repeated characters or
1175 * non-ASCII ordering of flags), this function will return NULL
1176 * (meaning that renaming would not be safe and should not occur).
1179 _new_maildir_filename (void *ctx,
1180 const char *filename,
1181 const char *flags_to_set,
1182 const char *flags_to_clear)
1184 const char *info, *flags;
1185 unsigned int flag, last_flag;
1186 char *filename_new, *dir;
1188 int flags_in_map = 0;
1192 memset (flag_map, 0, sizeof (flag_map));
1194 info = strstr (filename, ":2,");
1197 info = filename + strlen(filename);
1201 /* Loop through existing flags in filename. */
1202 for (flags = info + 3, last_flag = 0;
1204 last_flag = flag, flags++)
1208 /* Original flags not in ASCII order. Abort. */
1209 if (flag < last_flag)
1212 /* Non-ASCII flag. Abort. */
1213 if (flag > sizeof(flag_map) - 1)
1216 /* Repeated flag value. Abort. */
1225 /* Then set and clear our flags from tags. */
1226 for (flags = flags_to_set; *flags; flags++) {
1228 if (flag_map[flag] == 0) {
1234 for (flags = flags_to_clear; *flags; flags++) {
1236 if (flag_map[flag]) {
1242 filename_new = (char *) talloc_size (ctx,
1244 strlen (":2,") + flags_in_map + 1);
1245 if (unlikely (filename_new == NULL))
1248 strncpy (filename_new, filename, info - filename);
1249 filename_new[info - filename] = '\0';
1251 strcat (filename_new, ":2,");
1253 s = filename_new + strlen (filename_new);
1254 for (i = 0; i < sizeof (flag_map); i++)
1263 /* If message is in new/ move it under cur/. */
1264 dir = (char *) _filename_is_in_maildir (filename_new);
1265 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1266 memcpy (dir, "cur/", 4);
1268 return filename_new;
1272 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1274 notmuch_filenames_t *filenames;
1275 const char *filename;
1277 char *to_set, *to_clear;
1278 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1280 _get_maildir_flag_actions (message, &to_set, &to_clear);
1282 for (filenames = notmuch_message_get_filenames (message);
1283 notmuch_filenames_valid (filenames);
1284 notmuch_filenames_move_to_next (filenames))
1286 filename = notmuch_filenames_get (filenames);
1288 if (! _filename_is_in_maildir (filename))
1291 filename_new = _new_maildir_filename (message, filename,
1293 if (filename_new == NULL)
1296 if (strcmp (filename, filename_new)) {
1298 notmuch_status_t new_status;
1300 err = rename (filename, filename_new);
1304 new_status = _notmuch_message_remove_filename (message,
1306 /* Hold on to only the first error. */
1307 if (! status && new_status
1308 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1309 status = new_status;
1313 new_status = _notmuch_message_add_filename (message,
1315 /* Hold on to only the first error. */
1316 if (! status && new_status) {
1317 status = new_status;
1321 _notmuch_message_sync (message);
1324 talloc_free (filename_new);
1327 talloc_free (to_set);
1328 talloc_free (to_clear);
1330 return NOTMUCH_STATUS_SUCCESS;
1334 notmuch_message_remove_all_tags (notmuch_message_t *message)
1336 notmuch_private_status_t private_status;
1337 notmuch_status_t status;
1338 notmuch_tags_t *tags;
1341 status = _notmuch_database_ensure_writable (message->notmuch);
1345 for (tags = notmuch_message_get_tags (message);
1346 notmuch_tags_valid (tags);
1347 notmuch_tags_move_to_next (tags))
1349 tag = notmuch_tags_get (tags);
1351 private_status = _notmuch_message_remove_term (message, "tag", tag);
1352 if (private_status) {
1353 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1358 if (! message->frozen)
1359 _notmuch_message_sync (message);
1362 return NOTMUCH_STATUS_SUCCESS;
1366 notmuch_message_freeze (notmuch_message_t *message)
1368 notmuch_status_t status;
1370 status = _notmuch_database_ensure_writable (message->notmuch);
1376 return NOTMUCH_STATUS_SUCCESS;
1380 notmuch_message_thaw (notmuch_message_t *message)
1382 notmuch_status_t status;
1384 status = _notmuch_database_ensure_writable (message->notmuch);
1388 if (message->frozen > 0) {
1390 if (message->frozen == 0)
1391 _notmuch_message_sync (message);
1392 return NOTMUCH_STATUS_SUCCESS;
1394 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1399 notmuch_message_destroy (notmuch_message_t *message)
1401 talloc_free (message);