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);
276 std::string term = *i;
277 if (strncmp (term.c_str(), prefix, prefix_len))
280 value = talloc_strdup (message, term.c_str() + 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)
418 /* Fetch header from the appropriate xapian value field if
420 if (strcasecmp (header, "from") == 0)
421 value = message->doc.get_value (NOTMUCH_VALUE_FROM);
422 else if (strcasecmp (header, "subject") == 0)
423 value = message->doc.get_value (NOTMUCH_VALUE_SUBJECT);
424 else if (strcasecmp (header, "message-id") == 0)
425 value = message->doc.get_value (NOTMUCH_VALUE_MESSAGE_ID);
428 return talloc_strdup (message, value.c_str ());
430 } catch (Xapian::Error &error) {
431 fprintf (stderr, "A Xapian exception occurred when reading header: %s\n",
432 error.get_msg().c_str());
433 message->notmuch->exception_reported = TRUE;
437 /* Otherwise fall back to parsing the file */
438 _notmuch_message_ensure_message_file (message);
439 if (message->message_file == NULL)
442 return notmuch_message_file_get_header (message->message_file, header);
445 /* Return the message ID from the In-Reply-To header of 'message'.
447 * Returns an empty string ("") if 'message' has no In-Reply-To
450 * Returns NULL if any error occurs.
453 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
455 if (!message->in_reply_to)
456 _notmuch_message_ensure_metadata (message);
457 return message->in_reply_to;
461 notmuch_message_get_thread_id (notmuch_message_t *message)
463 if (!message->thread_id)
464 _notmuch_message_ensure_metadata (message);
465 if (!message->thread_id)
466 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
468 return message->thread_id;
472 _notmuch_message_add_reply (notmuch_message_t *message,
473 notmuch_message_t *reply)
475 _notmuch_message_list_add_message (message->replies, reply);
479 notmuch_message_get_replies (notmuch_message_t *message)
481 return _notmuch_messages_create (message->replies);
484 /* Add an additional 'filename' for 'message'.
486 * This change will not be reflected in the database until the next
487 * call to _notmuch_message_sync. */
489 _notmuch_message_add_filename (notmuch_message_t *message,
490 const char *filename)
492 const char *relative, *directory;
493 notmuch_status_t status;
494 void *local = talloc_new (message);
497 if (filename == NULL)
498 INTERNAL_ERROR ("Message filename cannot be NULL.");
500 relative = _notmuch_database_relative_path (message->notmuch, filename);
502 status = _notmuch_database_split_path (local, relative, &directory, NULL);
506 status = _notmuch_database_filename_to_direntry (
507 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
511 /* New file-direntry allows navigating to this message with
512 * notmuch_directory_get_child_files() . */
513 _notmuch_message_add_term (message, "file-direntry", direntry);
515 /* New terms allow user to search with folder: specification. */
516 _notmuch_message_gen_terms (message, "folder", directory);
520 return NOTMUCH_STATUS_SUCCESS;
523 /* Remove a particular 'filename' from 'message'.
525 * This change will not be reflected in the database until the next
526 * call to _notmuch_message_sync.
528 * If this message still has other filenames, returns
529 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
531 * Note: This function does not remove a document from the database,
532 * even if the specified filename is the only filename for this
533 * message. For that functionality, see
534 * _notmuch_database_remove_message. */
536 _notmuch_message_remove_filename (notmuch_message_t *message,
537 const char *filename)
539 const char *direntry_prefix = _find_prefix ("file-direntry");
540 int direntry_prefix_len = strlen (direntry_prefix);
541 const char *folder_prefix = _find_prefix ("folder");
542 int folder_prefix_len = strlen (folder_prefix);
543 void *local = talloc_new (message);
544 char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);
545 int zfolder_prefix_len = strlen (zfolder_prefix);
547 notmuch_private_status_t private_status;
548 notmuch_status_t status;
549 Xapian::TermIterator i, last;
551 status = _notmuch_database_filename_to_direntry (
552 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
553 if (status || !direntry)
556 /* Unlink this file from its parent directory. */
557 private_status = _notmuch_message_remove_term (message,
558 "file-direntry", direntry);
559 status = COERCE_STATUS (private_status,
560 "Unexpected error from _notmuch_message_remove_term");
564 /* Re-synchronize "folder:" terms for this message. This requires:
565 * 1. removing all "folder:" terms
566 * 2. removing all "folder:" stemmed terms
567 * 3. adding back terms for all remaining filenames of the message. */
569 /* 1. removing all "folder:" terms */
571 i = message->doc.termlist_begin ();
572 i.skip_to (folder_prefix);
574 /* Terminate loop when no terms remain with desired prefix. */
575 if (i == message->doc.termlist_end () ||
576 strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))
582 message->doc.remove_term ((*i));
583 } catch (const Xapian::InvalidArgumentError) {
584 /* Ignore failure to remove non-existent term. */
588 /* 2. removing all "folder:" stemmed terms */
590 i = message->doc.termlist_begin ();
591 i.skip_to (zfolder_prefix);
593 /* Terminate loop when no terms remain with desired prefix. */
594 if (i == message->doc.termlist_end () ||
595 strncmp ((*i).c_str (), zfolder_prefix, zfolder_prefix_len))
601 message->doc.remove_term ((*i));
602 } catch (const Xapian::InvalidArgumentError) {
603 /* Ignore failure to remove non-existent term. */
607 /* 3. adding back terms for all remaining filenames of the message. */
608 i = message->doc.termlist_begin ();
609 i.skip_to (direntry_prefix);
611 for (; i != message->doc.termlist_end (); i++) {
612 unsigned int directory_id;
613 const char *direntry, *directory;
616 /* Terminate loop at first term without desired prefix. */
617 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
620 /* Indicate that there are filenames remaining. */
621 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
623 direntry = (*i).c_str ();
624 direntry += direntry_prefix_len;
626 directory_id = strtol (direntry, &colon, 10);
628 if (colon == NULL || *colon != ':')
629 INTERNAL_ERROR ("malformed direntry");
631 directory = _notmuch_database_get_directory_path (local,
634 if (strlen (directory))
635 _notmuch_message_gen_terms (message, "folder", directory);
644 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
646 return talloc_strdup (message, message->doc.get_data ().c_str ());
650 _notmuch_message_clear_data (notmuch_message_t *message)
652 message->doc.set_data ("");
656 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
658 notmuch_string_node_t *node;
660 if (message->filename_list)
663 if (!message->filename_term_list)
664 _notmuch_message_ensure_metadata (message);
666 message->filename_list = _notmuch_string_list_create (message);
667 node = message->filename_term_list->head;
670 /* A message document created by an old version of notmuch
671 * (prior to rename support) will have the filename in the
672 * data of the document rather than as a file-direntry term.
674 * It would be nice to do the upgrade of the document directly
675 * here, but the database is likely open in read-only mode. */
678 data = message->doc.get_data ().c_str ();
681 INTERNAL_ERROR ("message with no filename");
683 _notmuch_string_list_append (message->filename_list, data);
688 for (; node; node = node->next) {
689 void *local = talloc_new (message);
690 const char *db_path, *directory, *basename, *filename;
691 char *colon, *direntry = NULL;
692 unsigned int directory_id;
694 direntry = node->string;
696 directory_id = strtol (direntry, &colon, 10);
698 if (colon == NULL || *colon != ':')
699 INTERNAL_ERROR ("malformed direntry");
701 basename = colon + 1;
705 db_path = notmuch_database_get_path (message->notmuch);
707 directory = _notmuch_database_get_directory_path (local,
711 if (strlen (directory))
712 filename = talloc_asprintf (message, "%s/%s/%s",
713 db_path, directory, basename);
715 filename = talloc_asprintf (message, "%s/%s",
718 _notmuch_string_list_append (message->filename_list, filename);
723 talloc_free (message->filename_term_list);
724 message->filename_term_list = NULL;
728 notmuch_message_get_filename (notmuch_message_t *message)
730 _notmuch_message_ensure_filename_list (message);
732 if (message->filename_list == NULL)
735 if (message->filename_list->head == NULL ||
736 message->filename_list->head->string == NULL)
738 INTERNAL_ERROR ("message with no filename");
741 return message->filename_list->head->string;
744 notmuch_filenames_t *
745 notmuch_message_get_filenames (notmuch_message_t *message)
747 _notmuch_message_ensure_filename_list (message);
749 return _notmuch_filenames_create (message, message->filename_list);
753 notmuch_message_get_flag (notmuch_message_t *message,
754 notmuch_message_flag_t flag)
756 return message->flags & (1 << flag);
760 notmuch_message_set_flag (notmuch_message_t *message,
761 notmuch_message_flag_t flag, notmuch_bool_t enable)
764 message->flags |= (1 << flag);
766 message->flags &= ~(1 << flag);
770 notmuch_message_get_date (notmuch_message_t *message)
775 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
776 } catch (Xapian::Error &error) {
777 fprintf (stderr, "A Xapian exception occurred when reading date: %s\n",
778 error.get_msg().c_str());
779 message->notmuch->exception_reported = TRUE;
783 return Xapian::sortable_unserialise (value);
787 notmuch_message_get_tags (notmuch_message_t *message)
789 notmuch_tags_t *tags;
791 if (!message->tag_list)
792 _notmuch_message_ensure_metadata (message);
794 tags = _notmuch_tags_create (message, message->tag_list);
795 /* _notmuch_tags_create steals the reference to the tag_list, but
796 * in this case it's still used by the message, so we add an
797 * *additional* talloc reference to the list. As a result, it's
798 * possible to modify the message tags (which talloc_unlink's the
799 * current list from the message) while still iterating because
800 * the iterator will keep the current list alive. */
801 if (!talloc_reference (message, message->tag_list))
808 notmuch_message_get_author (notmuch_message_t *message)
810 return message->author;
814 notmuch_message_set_author (notmuch_message_t *message,
818 talloc_free(message->author);
819 message->author = talloc_strdup(message, author);
824 _notmuch_message_set_header_values (notmuch_message_t *message,
831 /* GMime really doesn't want to see a NULL date, so protect its
833 if (date == NULL || *date == '\0')
836 time_value = g_mime_utils_header_decode_date (date, NULL);
838 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
839 Xapian::sortable_serialise (time_value));
840 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
841 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
844 /* Synchronize changes made to message->doc out into the database. */
846 _notmuch_message_sync (notmuch_message_t *message)
848 Xapian::WritableDatabase *db;
850 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
853 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
854 db->replace_document (message->doc_id, message->doc);
857 /* Delete a message document from the database. */
859 _notmuch_message_delete (notmuch_message_t *message)
861 notmuch_status_t status;
862 Xapian::WritableDatabase *db;
864 status = _notmuch_database_ensure_writable (message->notmuch);
868 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
869 db->delete_document (message->doc_id);
870 return NOTMUCH_STATUS_SUCCESS;
873 /* Ensure that 'message' is not holding any file object open. Future
874 * calls to various functions will still automatically open the
875 * message file as needed.
878 _notmuch_message_close (notmuch_message_t *message)
880 if (message->message_file) {
881 notmuch_message_file_close (message->message_file);
882 message->message_file = NULL;
886 /* Add a name:value term to 'message', (the actual term will be
887 * encoded by prefixing the value with a short prefix). See
888 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
889 * names to prefix values.
891 * This change will not be reflected in the database until the next
892 * call to _notmuch_message_sync. */
893 notmuch_private_status_t
894 _notmuch_message_add_term (notmuch_message_t *message,
895 const char *prefix_name,
902 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
904 term = talloc_asprintf (message, "%s%s",
905 _find_prefix (prefix_name), value);
907 if (strlen (term) > NOTMUCH_TERM_MAX)
908 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
910 message->doc.add_term (term, 0);
914 _notmuch_message_invalidate_metadata (message, prefix_name);
916 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
919 /* Parse 'text' and add a term to 'message' for each parsed word. Each
920 * term will be added both prefixed (if prefix_name is not NULL) and
921 * also non-prefixed). */
922 notmuch_private_status_t
923 _notmuch_message_gen_terms (notmuch_message_t *message,
924 const char *prefix_name,
927 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
930 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
932 term_gen->set_document (message->doc);
933 term_gen->set_termpos (message->termpos);
936 const char *prefix = _find_prefix (prefix_name);
938 term_gen->index_text (text, 1, prefix);
939 message->termpos = term_gen->get_termpos ();
942 term_gen->index_text (text);
944 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
947 /* Remove a name:value term from 'message', (the actual term will be
948 * encoded by prefixing the value with a short prefix). See
949 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
950 * names to prefix values.
952 * This change will not be reflected in the database until the next
953 * call to _notmuch_message_sync. */
954 notmuch_private_status_t
955 _notmuch_message_remove_term (notmuch_message_t *message,
956 const char *prefix_name,
962 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
964 term = talloc_asprintf (message, "%s%s",
965 _find_prefix (prefix_name), value);
967 if (strlen (term) > NOTMUCH_TERM_MAX)
968 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
971 message->doc.remove_term (term);
972 } catch (const Xapian::InvalidArgumentError) {
973 /* We'll let the philosopher's try to wrestle with the
974 * question of whether failing to remove that which was not
975 * there in the first place is failure. For us, we'll silently
976 * consider it all good. */
981 _notmuch_message_invalidate_metadata (message, prefix_name);
983 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
987 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
989 notmuch_private_status_t private_status;
990 notmuch_status_t status;
992 status = _notmuch_database_ensure_writable (message->notmuch);
997 return NOTMUCH_STATUS_NULL_POINTER;
999 if (strlen (tag) > NOTMUCH_TAG_MAX)
1000 return NOTMUCH_STATUS_TAG_TOO_LONG;
1002 private_status = _notmuch_message_add_term (message, "tag", tag);
1003 if (private_status) {
1004 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1008 if (! message->frozen)
1009 _notmuch_message_sync (message);
1011 return NOTMUCH_STATUS_SUCCESS;
1015 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1017 notmuch_private_status_t private_status;
1018 notmuch_status_t status;
1020 status = _notmuch_database_ensure_writable (message->notmuch);
1025 return NOTMUCH_STATUS_NULL_POINTER;
1027 if (strlen (tag) > NOTMUCH_TAG_MAX)
1028 return NOTMUCH_STATUS_TAG_TOO_LONG;
1030 private_status = _notmuch_message_remove_term (message, "tag", tag);
1031 if (private_status) {
1032 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1036 if (! message->frozen)
1037 _notmuch_message_sync (message);
1039 return NOTMUCH_STATUS_SUCCESS;
1042 /* Is the given filename within a maildir directory?
1044 * Specifically, is the final directory component of 'filename' either
1045 * "cur" or "new". If so, return a pointer to that final directory
1046 * component within 'filename'. If not, return NULL.
1048 * A non-NULL return value is guaranteed to be a valid string pointer
1049 * pointing to the characters "new/" or "cur/", (but not
1053 _filename_is_in_maildir (const char *filename)
1055 const char *slash, *dir = NULL;
1057 /* Find the last '/' separating directory from filename. */
1058 slash = strrchr (filename, '/');
1062 /* Jump back 4 characters to where the previous '/' will be if the
1063 * directory is named "cur" or "new". */
1064 if (slash - filename < 4)
1074 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1075 STRNCMP_LITERAL (dir, "new/") == 0)
1084 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1087 notmuch_status_t status;
1088 notmuch_filenames_t *filenames;
1089 const char *filename, *dir;
1090 char *combined_flags = talloc_strdup (message, "");
1092 int seen_maildir_info = 0;
1094 for (filenames = notmuch_message_get_filenames (message);
1095 notmuch_filenames_valid (filenames);
1096 notmuch_filenames_move_to_next (filenames))
1098 filename = notmuch_filenames_get (filenames);
1099 dir = _filename_is_in_maildir (filename);
1104 flags = strstr (filename, ":2,");
1106 seen_maildir_info = 1;
1108 combined_flags = talloc_strdup_append (combined_flags, flags);
1109 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1110 /* Messages are delivered to new/ with no "info" part, but
1111 * they effectively have default maildir flags. According
1112 * to the spec, we should ignore the info part for
1113 * messages in new/, but some MUAs (mutt) can set maildir
1114 * flags on messages in new/, so we're liberal in what we
1116 seen_maildir_info = 1;
1120 /* If none of the filenames have any maildir info field (not even
1121 * an empty info with no flags set) then there's no information to
1122 * go on, so do nothing. */
1123 if (! seen_maildir_info)
1124 return NOTMUCH_STATUS_SUCCESS;
1126 status = notmuch_message_freeze (message);
1130 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1131 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1133 flag2tag[i].inverse)
1135 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1137 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1142 status = notmuch_message_thaw (message);
1144 talloc_free (combined_flags);
1149 /* From the set of tags on 'message' and the flag2tag table, compute a
1150 * set of maildir-flag actions to be taken, (flags that should be
1151 * either set or cleared).
1153 * The result is returned as two talloced strings: to_set, and to_clear
1156 _get_maildir_flag_actions (notmuch_message_t *message,
1158 char **to_clear_ret)
1160 char *to_set, *to_clear;
1161 notmuch_tags_t *tags;
1165 to_set = talloc_strdup (message, "");
1166 to_clear = talloc_strdup (message, "");
1168 /* First, find flags for all set tags. */
1169 for (tags = notmuch_message_get_tags (message);
1170 notmuch_tags_valid (tags);
1171 notmuch_tags_move_to_next (tags))
1173 tag = notmuch_tags_get (tags);
1175 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1176 if (strcmp (tag, flag2tag[i].tag) == 0) {
1177 if (flag2tag[i].inverse)
1178 to_clear = talloc_asprintf_append (to_clear,
1182 to_set = talloc_asprintf_append (to_set,
1189 /* Then, find the flags for all tags not present. */
1190 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1191 if (flag2tag[i].inverse) {
1192 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1193 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1195 if (strchr (to_set, flag2tag[i].flag) == NULL)
1196 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1200 *to_set_ret = to_set;
1201 *to_clear_ret = to_clear;
1204 /* Given 'filename' and a set of maildir flags to set and to clear,
1205 * compute the new maildir filename.
1207 * If the existing filename is in the directory "new", the new
1208 * filename will be in the directory "cur", except for the case when
1209 * no flags are changed and the existing filename does not contain
1210 * maildir info (starting with ",2:").
1212 * After a sequence of ":2," in the filename, any subsequent
1213 * single-character flags will be added or removed according to the
1214 * characters in flags_to_set and flags_to_clear. Any existing flags
1215 * not mentioned in either string will remain. The final list of flags
1216 * will be in ASCII order.
1218 * If the original flags seem invalid, (repeated characters or
1219 * non-ASCII ordering of flags), this function will return NULL
1220 * (meaning that renaming would not be safe and should not occur).
1223 _new_maildir_filename (void *ctx,
1224 const char *filename,
1225 const char *flags_to_set,
1226 const char *flags_to_clear)
1228 const char *info, *flags;
1229 unsigned int flag, last_flag;
1230 char *filename_new, *dir;
1232 int flags_in_map = 0;
1233 notmuch_bool_t flags_changed = FALSE;
1237 memset (flag_map, 0, sizeof (flag_map));
1239 info = strstr (filename, ":2,");
1242 info = filename + strlen(filename);
1244 /* Loop through existing flags in filename. */
1245 for (flags = info + 3, last_flag = 0;
1247 last_flag = flag, flags++)
1251 /* Original flags not in ASCII order. Abort. */
1252 if (flag < last_flag)
1255 /* Non-ASCII flag. Abort. */
1256 if (flag > sizeof(flag_map) - 1)
1259 /* Repeated flag value. Abort. */
1268 /* Then set and clear our flags from tags. */
1269 for (flags = flags_to_set; *flags; flags++) {
1271 if (flag_map[flag] == 0) {
1274 flags_changed = TRUE;
1278 for (flags = flags_to_clear; *flags; flags++) {
1280 if (flag_map[flag]) {
1283 flags_changed = TRUE;
1287 /* Messages in new/ without maildir info can be kept in new/ if no
1288 * flags have changed. */
1289 dir = (char *) _filename_is_in_maildir (filename);
1290 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1291 return talloc_strdup (ctx, filename);
1293 filename_new = (char *) talloc_size (ctx,
1295 strlen (":2,") + flags_in_map + 1);
1296 if (unlikely (filename_new == NULL))
1299 strncpy (filename_new, filename, info - filename);
1300 filename_new[info - filename] = '\0';
1302 strcat (filename_new, ":2,");
1304 s = filename_new + strlen (filename_new);
1305 for (i = 0; i < sizeof (flag_map); i++)
1314 /* If message is in new/ move it under cur/. */
1315 dir = (char *) _filename_is_in_maildir (filename_new);
1316 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1317 memcpy (dir, "cur/", 4);
1319 return filename_new;
1323 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1325 notmuch_filenames_t *filenames;
1326 const char *filename;
1328 char *to_set, *to_clear;
1329 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1331 _get_maildir_flag_actions (message, &to_set, &to_clear);
1333 for (filenames = notmuch_message_get_filenames (message);
1334 notmuch_filenames_valid (filenames);
1335 notmuch_filenames_move_to_next (filenames))
1337 filename = notmuch_filenames_get (filenames);
1339 if (! _filename_is_in_maildir (filename))
1342 filename_new = _new_maildir_filename (message, filename,
1344 if (filename_new == NULL)
1347 if (strcmp (filename, filename_new)) {
1349 notmuch_status_t new_status;
1351 err = rename (filename, filename_new);
1355 new_status = _notmuch_message_remove_filename (message,
1357 /* Hold on to only the first error. */
1358 if (! status && new_status
1359 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1360 status = new_status;
1364 new_status = _notmuch_message_add_filename (message,
1366 /* Hold on to only the first error. */
1367 if (! status && new_status) {
1368 status = new_status;
1372 _notmuch_message_sync (message);
1375 talloc_free (filename_new);
1378 talloc_free (to_set);
1379 talloc_free (to_clear);
1381 return NOTMUCH_STATUS_SUCCESS;
1385 notmuch_message_remove_all_tags (notmuch_message_t *message)
1387 notmuch_private_status_t private_status;
1388 notmuch_status_t status;
1389 notmuch_tags_t *tags;
1392 status = _notmuch_database_ensure_writable (message->notmuch);
1396 for (tags = notmuch_message_get_tags (message);
1397 notmuch_tags_valid (tags);
1398 notmuch_tags_move_to_next (tags))
1400 tag = notmuch_tags_get (tags);
1402 private_status = _notmuch_message_remove_term (message, "tag", tag);
1403 if (private_status) {
1404 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1409 if (! message->frozen)
1410 _notmuch_message_sync (message);
1413 return NOTMUCH_STATUS_SUCCESS;
1417 notmuch_message_freeze (notmuch_message_t *message)
1419 notmuch_status_t status;
1421 status = _notmuch_database_ensure_writable (message->notmuch);
1427 return NOTMUCH_STATUS_SUCCESS;
1431 notmuch_message_thaw (notmuch_message_t *message)
1433 notmuch_status_t status;
1435 status = _notmuch_database_ensure_writable (message->notmuch);
1439 if (message->frozen > 0) {
1441 if (message->frozen == 0)
1442 _notmuch_message_sync (message);
1443 return NOTMUCH_STATUS_SUCCESS;
1445 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1450 notmuch_message_destroy (notmuch_message_t *message)
1452 talloc_free (message);