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 {
52 notmuch_bool_t inverse;
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_t) notmuch_database_find_message (notmuch,
223 return talloc_steal (notmuch, message);
224 else if (*status_ret)
227 term = talloc_asprintf (NULL, "%s%s",
228 _find_prefix ("id"), message_id);
230 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
234 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
235 INTERNAL_ERROR ("Failure to ensure database is writable.");
238 doc.add_term (term, 0);
241 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
243 doc_id = _notmuch_database_generate_doc_id (notmuch);
244 } catch (const Xapian::Error &error) {
245 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
246 error.get_msg().c_str());
247 notmuch->exception_reported = TRUE;
248 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
252 message = _notmuch_message_create_for_document (notmuch, notmuch,
253 doc_id, doc, status_ret);
255 /* We want to inform the caller that we had to create a new
257 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
258 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
264 _notmuch_message_get_term (notmuch_message_t *message,
265 Xapian::TermIterator &i, Xapian::TermIterator &end,
268 int prefix_len = strlen (prefix);
269 const char *term = NULL;
275 term = (*i).c_str ();
277 if (!term || strncmp (term, prefix, prefix_len))
280 value = talloc_strdup (message, term + prefix_len);
282 #if DEBUG_DATABASE_SANITY
285 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
286 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
287 message->doc_id, prefix, value,
288 (*i).c_str () + prefix_len);
296 _notmuch_message_ensure_metadata (notmuch_message_t *message)
298 Xapian::TermIterator i, end;
299 const char *thread_prefix = _find_prefix ("thread"),
300 *tag_prefix = _find_prefix ("tag"),
301 *id_prefix = _find_prefix ("id"),
302 *filename_prefix = _find_prefix ("file-direntry"),
303 *replyto_prefix = _find_prefix ("replyto");
305 /* We do this all in a single pass because Xapian decompresses the
306 * term list every time you iterate over it. Thus, while this is
307 * slightly more costly than looking up individual fields if only
308 * one field of the message object is actually used, it's a huge
309 * win as more fields are used. */
311 i = message->doc.termlist_begin ();
312 end = message->doc.termlist_end ();
315 if (!message->thread_id)
317 _notmuch_message_get_term (message, i, end, thread_prefix);
320 assert (strcmp (thread_prefix, tag_prefix) < 0);
321 if (!message->tag_list) {
323 _notmuch_database_get_terms_with_prefix (message, i, end,
325 _notmuch_string_list_sort (message->tag_list);
329 assert (strcmp (tag_prefix, id_prefix) < 0);
330 if (!message->message_id)
331 message->message_id =
332 _notmuch_message_get_term (message, i, end, id_prefix);
334 /* Get filename list. Here we get only the terms. We lazily
335 * expand them to full file names when needed in
336 * _notmuch_message_ensure_filename_list. */
337 assert (strcmp (id_prefix, filename_prefix) < 0);
338 if (!message->filename_term_list && !message->filename_list)
339 message->filename_term_list =
340 _notmuch_database_get_terms_with_prefix (message, i, end,
344 assert (strcmp (filename_prefix, replyto_prefix) < 0);
345 if (!message->in_reply_to)
346 message->in_reply_to =
347 _notmuch_message_get_term (message, i, end, replyto_prefix);
348 /* It's perfectly valid for a message to have no In-Reply-To
349 * header. For these cases, we return an empty string. */
350 if (!message->in_reply_to)
351 message->in_reply_to = talloc_strdup (message, "");
355 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
356 const char *prefix_name)
358 if (strcmp ("thread", prefix_name) == 0) {
359 talloc_free (message->thread_id);
360 message->thread_id = NULL;
363 if (strcmp ("tag", prefix_name) == 0) {
364 talloc_unlink (message, message->tag_list);
365 message->tag_list = NULL;
368 if (strcmp ("file-direntry", prefix_name) == 0) {
369 talloc_free (message->filename_term_list);
370 talloc_free (message->filename_list);
371 message->filename_term_list = message->filename_list = NULL;
374 if (strcmp ("replyto", prefix_name) == 0) {
375 talloc_free (message->in_reply_to);
376 message->in_reply_to = NULL;
381 _notmuch_message_get_doc_id (notmuch_message_t *message)
383 return message->doc_id;
387 notmuch_message_get_message_id (notmuch_message_t *message)
389 if (!message->message_id)
390 _notmuch_message_ensure_metadata (message);
391 if (!message->message_id)
392 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
394 return message->message_id;
398 _notmuch_message_ensure_message_file (notmuch_message_t *message)
400 const char *filename;
402 if (message->message_file)
405 filename = notmuch_message_get_filename (message);
406 if (unlikely (filename == NULL))
409 message->message_file = _notmuch_message_file_open_ctx (message, filename);
413 notmuch_message_get_header (notmuch_message_t *message, const char *header)
417 /* Fetch header from the appropriate xapian value field if
419 if (strcasecmp (header, "from") == 0)
420 value = message->doc.get_value (NOTMUCH_VALUE_FROM);
421 else if (strcasecmp (header, "subject") == 0)
422 value = message->doc.get_value (NOTMUCH_VALUE_SUBJECT);
423 else if (strcasecmp (header, "message-id") == 0)
424 value = message->doc.get_value (NOTMUCH_VALUE_MESSAGE_ID);
427 return talloc_strdup (message, value.c_str ());
429 /* Otherwise fall back to parsing the file */
430 _notmuch_message_ensure_message_file (message);
431 if (message->message_file == NULL)
434 return notmuch_message_file_get_header (message->message_file, header);
437 /* Return the message ID from the In-Reply-To header of 'message'.
439 * Returns an empty string ("") if 'message' has no In-Reply-To
442 * Returns NULL if any error occurs.
445 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
447 if (!message->in_reply_to)
448 _notmuch_message_ensure_metadata (message);
449 return message->in_reply_to;
453 notmuch_message_get_thread_id (notmuch_message_t *message)
455 if (!message->thread_id)
456 _notmuch_message_ensure_metadata (message);
457 if (!message->thread_id)
458 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
460 return message->thread_id;
464 _notmuch_message_add_reply (notmuch_message_t *message,
465 notmuch_message_node_t *reply)
467 _notmuch_message_list_append (message->replies, reply);
471 notmuch_message_get_replies (notmuch_message_t *message)
473 return _notmuch_messages_create (message->replies);
476 /* Add an additional 'filename' for 'message'.
478 * This change will not be reflected in the database until the next
479 * call to _notmuch_message_sync. */
481 _notmuch_message_add_filename (notmuch_message_t *message,
482 const char *filename)
484 const char *relative, *directory;
485 notmuch_status_t status;
486 void *local = talloc_new (message);
489 if (filename == NULL)
490 INTERNAL_ERROR ("Message filename cannot be NULL.");
492 relative = _notmuch_database_relative_path (message->notmuch, filename);
494 status = _notmuch_database_split_path (local, relative, &directory, NULL);
498 status = _notmuch_database_filename_to_direntry (local,
500 filename, &direntry);
504 /* New file-direntry allows navigating to this message with
505 * notmuch_directory_get_child_files() . */
506 _notmuch_message_add_term (message, "file-direntry", direntry);
508 /* New terms allow user to search with folder: specification. */
509 _notmuch_message_gen_terms (message, "folder", directory);
513 return NOTMUCH_STATUS_SUCCESS;
516 /* Remove a particular 'filename' from 'message'.
518 * This change will not be reflected in the database until the next
519 * call to _notmuch_message_sync.
521 * If this message still has other filenames, returns
522 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
524 * Note: This function does not remove a document from the database,
525 * even if the specified filename is the only filename for this
526 * message. For that functionality, see
527 * _notmuch_database_remove_message. */
529 _notmuch_message_remove_filename (notmuch_message_t *message,
530 const char *filename)
532 const char *direntry_prefix = _find_prefix ("file-direntry");
533 int direntry_prefix_len = strlen (direntry_prefix);
534 const char *folder_prefix = _find_prefix ("folder");
535 int folder_prefix_len = strlen (folder_prefix);
536 void *local = talloc_new (message);
537 char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);
538 int zfolder_prefix_len = strlen (zfolder_prefix);
540 notmuch_private_status_t private_status;
541 notmuch_status_t status;
542 Xapian::TermIterator i, last;
544 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
545 filename, &direntry);
549 /* Unlink this file from its parent directory. */
550 private_status = _notmuch_message_remove_term (message,
551 "file-direntry", direntry);
552 status = COERCE_STATUS (private_status,
553 "Unexpected error from _notmuch_message_remove_term");
557 /* Re-synchronize "folder:" terms for this message. This requires:
558 * 1. removing all "folder:" terms
559 * 2. removing all "folder:" stemmed terms
560 * 3. adding back terms for all remaining filenames of the message. */
562 /* 1. removing all "folder:" terms */
564 i = message->doc.termlist_begin ();
565 i.skip_to (folder_prefix);
567 /* Terminate loop when no terms remain with desired prefix. */
568 if (i == message->doc.termlist_end () ||
569 strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))
575 message->doc.remove_term ((*i));
576 } catch (const Xapian::InvalidArgumentError) {
577 /* Ignore failure to remove non-existent term. */
581 /* 2. removing all "folder:" stemmed terms */
583 i = message->doc.termlist_begin ();
584 i.skip_to (zfolder_prefix);
586 /* Terminate loop when no terms remain with desired prefix. */
587 if (i == message->doc.termlist_end () ||
588 strncmp ((*i).c_str (), zfolder_prefix, zfolder_prefix_len))
594 message->doc.remove_term ((*i));
595 } catch (const Xapian::InvalidArgumentError) {
596 /* Ignore failure to remove non-existent term. */
600 /* 3. adding back terms for all remaining filenames of the message. */
601 i = message->doc.termlist_begin ();
602 i.skip_to (direntry_prefix);
604 for (; i != message->doc.termlist_end (); i++) {
605 unsigned int directory_id;
606 const char *direntry, *directory;
609 /* Terminate loop at first term without desired prefix. */
610 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
613 /* Indicate that there are filenames remaining. */
614 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
616 direntry = (*i).c_str ();
617 direntry += direntry_prefix_len;
619 directory_id = strtol (direntry, &colon, 10);
621 if (colon == NULL || *colon != ':')
622 INTERNAL_ERROR ("malformed direntry");
624 directory = _notmuch_database_get_directory_path (local,
627 if (strlen (directory))
628 _notmuch_message_gen_terms (message, "folder", directory);
637 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
639 return talloc_strdup (message, message->doc.get_data ().c_str ());
643 _notmuch_message_clear_data (notmuch_message_t *message)
645 message->doc.set_data ("");
649 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
651 notmuch_string_node_t *node;
653 if (message->filename_list)
656 if (!message->filename_term_list)
657 _notmuch_message_ensure_metadata (message);
659 message->filename_list = _notmuch_string_list_create (message);
660 node = message->filename_term_list->head;
663 /* A message document created by an old version of notmuch
664 * (prior to rename support) will have the filename in the
665 * data of the document rather than as a file-direntry term.
667 * It would be nice to do the upgrade of the document directly
668 * here, but the database is likely open in read-only mode. */
671 data = message->doc.get_data ().c_str ();
674 INTERNAL_ERROR ("message with no filename");
676 _notmuch_string_list_append (message->filename_list, data);
681 for (; node; node = node->next) {
682 void *local = talloc_new (message);
683 const char *db_path, *directory, *basename, *filename;
684 char *colon, *direntry = NULL;
685 unsigned int directory_id;
687 direntry = node->string;
689 directory_id = strtol (direntry, &colon, 10);
691 if (colon == NULL || *colon != ':')
692 INTERNAL_ERROR ("malformed direntry");
694 basename = colon + 1;
698 db_path = notmuch_database_get_path (message->notmuch);
700 directory = _notmuch_database_get_directory_path (local,
704 if (strlen (directory))
705 filename = talloc_asprintf (message, "%s/%s/%s",
706 db_path, directory, basename);
708 filename = talloc_asprintf (message, "%s/%s",
711 _notmuch_string_list_append (message->filename_list, filename);
716 talloc_free (message->filename_term_list);
717 message->filename_term_list = NULL;
721 notmuch_message_get_filename (notmuch_message_t *message)
723 _notmuch_message_ensure_filename_list (message);
725 if (message->filename_list == NULL)
728 if (message->filename_list->head == NULL ||
729 message->filename_list->head->string == NULL)
731 INTERNAL_ERROR ("message with no filename");
734 return message->filename_list->head->string;
737 notmuch_filenames_t *
738 notmuch_message_get_filenames (notmuch_message_t *message)
740 _notmuch_message_ensure_filename_list (message);
742 return _notmuch_filenames_create (message, message->filename_list);
746 notmuch_message_get_flag (notmuch_message_t *message,
747 notmuch_message_flag_t flag)
749 return message->flags & (1 << flag);
753 notmuch_message_set_flag (notmuch_message_t *message,
754 notmuch_message_flag_t flag, notmuch_bool_t enable)
757 message->flags |= (1 << flag);
759 message->flags &= ~(1 << flag);
763 notmuch_message_get_date (notmuch_message_t *message)
768 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
769 } catch (Xapian::Error &error) {
770 INTERNAL_ERROR ("Failed to read timestamp value from document.");
774 return Xapian::sortable_unserialise (value);
778 notmuch_message_get_tags (notmuch_message_t *message)
780 notmuch_tags_t *tags;
782 if (!message->tag_list)
783 _notmuch_message_ensure_metadata (message);
785 tags = _notmuch_tags_create (message, message->tag_list);
786 /* _notmuch_tags_create steals the reference to the tag_list, but
787 * in this case it's still used by the message, so we add an
788 * *additional* talloc reference to the list. As a result, it's
789 * possible to modify the message tags (which talloc_unlink's the
790 * current list from the message) while still iterating because
791 * the iterator will keep the current list alive. */
792 talloc_reference (message, message->tag_list);
797 notmuch_message_get_author (notmuch_message_t *message)
799 return message->author;
803 notmuch_message_set_author (notmuch_message_t *message,
807 talloc_free(message->author);
808 message->author = talloc_strdup(message, author);
813 _notmuch_message_set_header_values (notmuch_message_t *message,
820 /* GMime really doesn't want to see a NULL date, so protect its
822 if (date == NULL || *date == '\0')
825 time_value = g_mime_utils_header_decode_date (date, NULL);
827 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
828 Xapian::sortable_serialise (time_value));
829 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
830 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
833 /* Synchronize changes made to message->doc out into the database. */
835 _notmuch_message_sync (notmuch_message_t *message)
837 Xapian::WritableDatabase *db;
839 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
842 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
843 db->replace_document (message->doc_id, message->doc);
846 /* Delete a message document from the database. */
848 _notmuch_message_delete (notmuch_message_t *message)
850 notmuch_status_t status;
851 Xapian::WritableDatabase *db;
853 status = _notmuch_database_ensure_writable (message->notmuch);
857 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
858 db->delete_document (message->doc_id);
859 return NOTMUCH_STATUS_SUCCESS;
862 /* Ensure that 'message' is not holding any file object open. Future
863 * calls to various functions will still automatically open the
864 * message file as needed.
867 _notmuch_message_close (notmuch_message_t *message)
869 if (message->message_file) {
870 notmuch_message_file_close (message->message_file);
871 message->message_file = NULL;
875 /* Add a name:value term to 'message', (the actual term will be
876 * encoded by prefixing the value with a short prefix). See
877 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
878 * names to prefix values.
880 * This change will not be reflected in the database until the next
881 * call to _notmuch_message_sync. */
882 notmuch_private_status_t
883 _notmuch_message_add_term (notmuch_message_t *message,
884 const char *prefix_name,
891 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
893 term = talloc_asprintf (message, "%s%s",
894 _find_prefix (prefix_name), value);
896 if (strlen (term) > NOTMUCH_TERM_MAX)
897 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
899 message->doc.add_term (term, 0);
903 _notmuch_message_invalidate_metadata (message, prefix_name);
905 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
908 /* Parse 'text' and add a term to 'message' for each parsed word. Each
909 * term will be added both prefixed (if prefix_name is not NULL) and
910 * also non-prefixed). */
911 notmuch_private_status_t
912 _notmuch_message_gen_terms (notmuch_message_t *message,
913 const char *prefix_name,
916 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
919 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
921 term_gen->set_document (message->doc);
922 term_gen->set_termpos (message->termpos);
925 const char *prefix = _find_prefix (prefix_name);
927 term_gen->index_text (text, 1, prefix);
928 message->termpos = term_gen->get_termpos ();
931 term_gen->index_text (text);
933 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
936 /* Remove a name:value term from 'message', (the actual term will be
937 * encoded by prefixing the value with a short prefix). See
938 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
939 * names to prefix values.
941 * This change will not be reflected in the database until the next
942 * call to _notmuch_message_sync. */
943 notmuch_private_status_t
944 _notmuch_message_remove_term (notmuch_message_t *message,
945 const char *prefix_name,
951 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
953 term = talloc_asprintf (message, "%s%s",
954 _find_prefix (prefix_name), value);
956 if (strlen (term) > NOTMUCH_TERM_MAX)
957 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
960 message->doc.remove_term (term);
961 } catch (const Xapian::InvalidArgumentError) {
962 /* We'll let the philosopher's try to wrestle with the
963 * question of whether failing to remove that which was not
964 * there in the first place is failure. For us, we'll silently
965 * consider it all good. */
970 _notmuch_message_invalidate_metadata (message, prefix_name);
972 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
976 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
978 notmuch_private_status_t private_status;
979 notmuch_status_t status;
981 status = _notmuch_database_ensure_writable (message->notmuch);
986 return NOTMUCH_STATUS_NULL_POINTER;
988 if (strlen (tag) > NOTMUCH_TAG_MAX)
989 return NOTMUCH_STATUS_TAG_TOO_LONG;
991 private_status = _notmuch_message_add_term (message, "tag", tag);
992 if (private_status) {
993 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
997 if (! message->frozen)
998 _notmuch_message_sync (message);
1000 return NOTMUCH_STATUS_SUCCESS;
1004 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1006 notmuch_private_status_t private_status;
1007 notmuch_status_t status;
1009 status = _notmuch_database_ensure_writable (message->notmuch);
1014 return NOTMUCH_STATUS_NULL_POINTER;
1016 if (strlen (tag) > NOTMUCH_TAG_MAX)
1017 return NOTMUCH_STATUS_TAG_TOO_LONG;
1019 private_status = _notmuch_message_remove_term (message, "tag", tag);
1020 if (private_status) {
1021 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1025 if (! message->frozen)
1026 _notmuch_message_sync (message);
1028 return NOTMUCH_STATUS_SUCCESS;
1032 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1035 notmuch_status_t status;
1036 notmuch_filenames_t *filenames;
1037 const char *filename;
1038 char *combined_flags = talloc_strdup (message, "");
1040 int seen_maildir_info = 0;
1042 for (filenames = notmuch_message_get_filenames (message);
1043 notmuch_filenames_valid (filenames);
1044 notmuch_filenames_move_to_next (filenames))
1046 filename = notmuch_filenames_get (filenames);
1048 flags = strstr (filename, ":2,");
1052 seen_maildir_info = 1;
1055 combined_flags = talloc_strdup_append (combined_flags, flags);
1058 /* If none of the filenames have any maildir info field (not even
1059 * an empty info with no flags set) then there's no information to
1060 * go on, so do nothing. */
1061 if (! seen_maildir_info)
1062 return NOTMUCH_STATUS_SUCCESS;
1064 status = notmuch_message_freeze (message);
1068 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1069 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1071 flag2tag[i].inverse)
1073 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1075 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1080 status = notmuch_message_thaw (message);
1082 talloc_free (combined_flags);
1087 /* Is the given filename within a maildir directory?
1089 * Specifically, is the final directory component of 'filename' either
1090 * "cur" or "new". If so, return a pointer to that final directory
1091 * component within 'filename'. If not, return NULL.
1093 * A non-NULL return value is guaranteed to be a valid string pointer
1094 * pointing to the characters "new/" or "cur/", (but not
1098 _filename_is_in_maildir (const char *filename)
1100 const char *slash, *dir = NULL;
1102 /* Find the last '/' separating directory from filename. */
1103 slash = strrchr (filename, '/');
1107 /* Jump back 4 characters to where the previous '/' will be if the
1108 * directory is named "cur" or "new". */
1109 if (slash - filename < 4)
1119 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1120 STRNCMP_LITERAL (dir, "new/") == 0)
1128 /* From the set of tags on 'message' and the flag2tag table, compute a
1129 * set of maildir-flag actions to be taken, (flags that should be
1130 * either set or cleared).
1132 * The result is returned as two talloced strings: to_set, and to_clear
1135 _get_maildir_flag_actions (notmuch_message_t *message,
1137 char **to_clear_ret)
1139 char *to_set, *to_clear;
1140 notmuch_tags_t *tags;
1144 to_set = talloc_strdup (message, "");
1145 to_clear = talloc_strdup (message, "");
1147 /* First, find flags for all set tags. */
1148 for (tags = notmuch_message_get_tags (message);
1149 notmuch_tags_valid (tags);
1150 notmuch_tags_move_to_next (tags))
1152 tag = notmuch_tags_get (tags);
1154 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1155 if (strcmp (tag, flag2tag[i].tag) == 0) {
1156 if (flag2tag[i].inverse)
1157 to_clear = talloc_asprintf_append (to_clear,
1161 to_set = talloc_asprintf_append (to_set,
1168 /* Then, find the flags for all tags not present. */
1169 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1170 if (flag2tag[i].inverse) {
1171 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1172 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1174 if (strchr (to_set, flag2tag[i].flag) == NULL)
1175 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1179 *to_set_ret = to_set;
1180 *to_clear_ret = to_clear;
1183 /* Given 'filename' and a set of maildir flags to set and to clear,
1184 * compute the new maildir filename.
1186 * If the existing filename is in the directory "new", the new
1187 * filename will be in the directory "cur".
1189 * After a sequence of ":2," in the filename, any subsequent
1190 * single-character flags will be added or removed according to the
1191 * characters in flags_to_set and flags_to_clear. Any existing flags
1192 * not mentioned in either string will remain. The final list of flags
1193 * will be in ASCII order.
1195 * If the original flags seem invalid, (repeated characters or
1196 * non-ASCII ordering of flags), this function will return NULL
1197 * (meaning that renaming would not be safe and should not occur).
1200 _new_maildir_filename (void *ctx,
1201 const char *filename,
1202 const char *flags_to_set,
1203 const char *flags_to_clear)
1205 const char *info, *flags;
1206 unsigned int flag, last_flag;
1207 char *filename_new, *dir;
1209 int flags_in_map = 0;
1213 memset (flag_map, 0, sizeof (flag_map));
1215 info = strstr (filename, ":2,");
1218 info = filename + strlen(filename);
1220 /* Loop through existing flags in filename. */
1221 for (flags = info + 3, last_flag = 0;
1223 last_flag = flag, flags++)
1227 /* Original flags not in ASCII order. Abort. */
1228 if (flag < last_flag)
1231 /* Non-ASCII flag. Abort. */
1232 if (flag > sizeof(flag_map) - 1)
1235 /* Repeated flag value. Abort. */
1244 /* Then set and clear our flags from tags. */
1245 for (flags = flags_to_set; *flags; flags++) {
1247 if (flag_map[flag] == 0) {
1253 for (flags = flags_to_clear; *flags; flags++) {
1255 if (flag_map[flag]) {
1261 filename_new = (char *) talloc_size (ctx,
1263 strlen (":2,") + flags_in_map + 1);
1264 if (unlikely (filename_new == NULL))
1267 strncpy (filename_new, filename, info - filename);
1268 filename_new[info - filename] = '\0';
1270 strcat (filename_new, ":2,");
1272 s = filename_new + strlen (filename_new);
1273 for (i = 0; i < sizeof (flag_map); i++)
1282 /* If message is in new/ move it under cur/. */
1283 dir = (char *) _filename_is_in_maildir (filename_new);
1284 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1285 memcpy (dir, "cur/", 4);
1287 return filename_new;
1291 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1293 notmuch_filenames_t *filenames;
1294 const char *filename;
1296 char *to_set, *to_clear;
1297 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1299 _get_maildir_flag_actions (message, &to_set, &to_clear);
1301 for (filenames = notmuch_message_get_filenames (message);
1302 notmuch_filenames_valid (filenames);
1303 notmuch_filenames_move_to_next (filenames))
1305 filename = notmuch_filenames_get (filenames);
1307 if (! _filename_is_in_maildir (filename))
1310 filename_new = _new_maildir_filename (message, filename,
1312 if (filename_new == NULL)
1315 if (strcmp (filename, filename_new)) {
1317 notmuch_status_t new_status;
1319 err = rename (filename, filename_new);
1323 new_status = _notmuch_message_remove_filename (message,
1325 /* Hold on to only the first error. */
1326 if (! status && new_status
1327 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1328 status = new_status;
1332 new_status = _notmuch_message_add_filename (message,
1334 /* Hold on to only the first error. */
1335 if (! status && new_status) {
1336 status = new_status;
1340 _notmuch_message_sync (message);
1343 talloc_free (filename_new);
1346 talloc_free (to_set);
1347 talloc_free (to_clear);
1349 return NOTMUCH_STATUS_SUCCESS;
1353 notmuch_message_remove_all_tags (notmuch_message_t *message)
1355 notmuch_private_status_t private_status;
1356 notmuch_status_t status;
1357 notmuch_tags_t *tags;
1360 status = _notmuch_database_ensure_writable (message->notmuch);
1364 for (tags = notmuch_message_get_tags (message);
1365 notmuch_tags_valid (tags);
1366 notmuch_tags_move_to_next (tags))
1368 tag = notmuch_tags_get (tags);
1370 private_status = _notmuch_message_remove_term (message, "tag", tag);
1371 if (private_status) {
1372 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1377 if (! message->frozen)
1378 _notmuch_message_sync (message);
1381 return NOTMUCH_STATUS_SUCCESS;
1385 notmuch_message_freeze (notmuch_message_t *message)
1387 notmuch_status_t status;
1389 status = _notmuch_database_ensure_writable (message->notmuch);
1395 return NOTMUCH_STATUS_SUCCESS;
1399 notmuch_message_thaw (notmuch_message_t *message)
1401 notmuch_status_t status;
1403 status = _notmuch_database_ensure_writable (message->notmuch);
1407 if (message->frozen > 0) {
1409 if (message->frozen == 0)
1410 _notmuch_message_sync (message);
1411 return NOTMUCH_STATUS_SUCCESS;
1413 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1418 notmuch_message_destroy (notmuch_message_t *message)
1420 talloc_free (message);