1 /* message.cc - Results of message-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 #include <gmime/gmime.h>
28 struct _notmuch_message {
29 notmuch_database_t *notmuch;
35 notmuch_filename_list_t *filename_list;
37 notmuch_message_file_t *message_file;
38 notmuch_message_list_t *replies;
44 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
46 struct maildir_flag_tag {
52 /* ASCII ordered table of Maildir flags and associated tags */
53 static struct maildir_flag_tag flag2tag[] = {
54 { 'D', "draft", false},
55 { 'F', "flagged", false},
56 { 'P', "passed", false},
57 { 'R', "replied", false},
58 { 'S', "unread", true }
61 /* We end up having to call the destructor explicitly because we had
62 * to use "placement new" in order to initialize C++ objects within a
63 * block that we allocated with talloc. So C++ is making talloc
64 * slightly less simple to use, (we wouldn't need
65 * talloc_set_destructor at all otherwise).
68 _notmuch_message_destructor (notmuch_message_t *message)
70 message->doc.~Document ();
75 static notmuch_message_t *
76 _notmuch_message_create_for_document (const void *talloc_owner,
77 notmuch_database_t *notmuch,
80 notmuch_private_status_t *status)
82 notmuch_message_t *message;
85 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
87 message = talloc (talloc_owner, notmuch_message_t);
88 if (unlikely (message == NULL)) {
90 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
94 message->notmuch = notmuch;
95 message->doc_id = doc_id;
100 /* Each of these will be lazily created as needed. */
101 message->message_id = NULL;
102 message->thread_id = NULL;
103 message->in_reply_to = NULL;
104 message->filename_list = NULL;
105 message->message_file = NULL;
106 message->author = NULL;
108 message->replies = _notmuch_message_list_create (message);
109 if (unlikely (message->replies == NULL)) {
111 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
115 /* This is C++'s creepy "placement new", which is really just an
116 * ugly way to call a constructor for a pre-allocated object. So
117 * it's really not an error to not be checking for OUT_OF_MEMORY
118 * here, since this "new" isn't actually allocating memory. This
119 * is language-design comedy of the wrong kind. */
121 new (&message->doc) Xapian::Document;
123 talloc_set_destructor (message, _notmuch_message_destructor);
130 /* Create a new notmuch_message_t object for an existing document in
133 * Here, 'talloc owner' is an optional talloc context to which the new
134 * message will belong. This allows for the caller to not bother
135 * calling notmuch_message_destroy on the message, and know that all
136 * memory will be reclaimed when 'talloc_owner' is freed. The caller
137 * still can call notmuch_message_destroy when finished with the
138 * message if desired.
140 * The 'talloc_owner' argument can also be NULL, in which case the
141 * caller *is* responsible for calling notmuch_message_destroy.
143 * If no document exists in the database with document ID of 'doc_id'
144 * then this function returns NULL and optionally sets *status to
145 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
147 * This function can also fail to due lack of available memory,
148 * returning NULL and optionally setting *status to
149 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
151 * The caller can pass NULL for status if uninterested in
152 * distinguishing these two cases.
155 _notmuch_message_create (const void *talloc_owner,
156 notmuch_database_t *notmuch,
158 notmuch_private_status_t *status)
160 Xapian::Document doc;
163 doc = notmuch->xapian_db->get_document (doc_id);
164 } catch (const Xapian::DocNotFoundError &error) {
166 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
170 return _notmuch_message_create_for_document (talloc_owner, notmuch,
171 doc_id, doc, status);
174 /* Create a new notmuch_message_t object for a specific message ID,
175 * (which may or may not already exist in the database).
177 * The 'notmuch' database will be the talloc owner of the returned
180 * This function returns a valid notmuch_message_t whether or not
181 * there is already a document in the database with the given message
182 * ID. These two cases can be distinguished by the value of *status:
185 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
187 * There is already a document with message ID 'message_id' in the
188 * database. The returned message can be used to query/modify the
190 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
192 * No document with 'message_id' exists in the database. The
193 * returned message contains a newly created document (not yet
194 * added to the database) and a document ID that is known not to
195 * exist in the database. The caller can modify the message, and a
196 * call to _notmuch_message_sync will add * the document to the
199 * If an error occurs, this function will return NULL and *status
200 * will be set as appropriate. (The status pointer argument must
204 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
205 const char *message_id,
206 notmuch_private_status_t *status_ret)
208 notmuch_message_t *message;
209 Xapian::Document doc;
210 Xapian::WritableDatabase *db;
214 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
216 message = notmuch_database_find_message (notmuch, message_id);
218 return talloc_steal (notmuch, message);
220 term = talloc_asprintf (NULL, "%s%s",
221 _find_prefix ("id"), message_id);
223 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
227 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
228 INTERNAL_ERROR ("Failure to ensure database is writable.");
230 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
232 doc.add_term (term, 0);
235 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
237 doc_id = _notmuch_database_generate_doc_id (notmuch);
238 } catch (const Xapian::Error &error) {
239 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
240 error.get_msg().c_str());
241 notmuch->exception_reported = TRUE;
242 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
246 message = _notmuch_message_create_for_document (notmuch, notmuch,
247 doc_id, doc, status_ret);
249 /* We want to inform the caller that we had to create a new
251 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
252 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
258 _notmuch_message_get_doc_id (notmuch_message_t *message)
260 return message->doc_id;
264 notmuch_message_get_message_id (notmuch_message_t *message)
266 Xapian::TermIterator i;
268 if (message->message_id)
269 return message->message_id;
271 i = message->doc.termlist_begin ();
272 i.skip_to (_find_prefix ("id"));
274 if (i == message->doc.termlist_end ())
275 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
278 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
280 #if DEBUG_DATABASE_SANITY
283 if (i != message->doc.termlist_end () &&
284 strncmp ((*i).c_str (), _find_prefix ("id"),
285 strlen (_find_prefix ("id"))) == 0)
287 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
292 return message->message_id;
296 _notmuch_message_ensure_message_file (notmuch_message_t *message)
298 const char *filename;
300 if (message->message_file)
303 filename = notmuch_message_get_filename (message);
304 if (unlikely (filename == NULL))
307 message->message_file = _notmuch_message_file_open_ctx (message, filename);
311 notmuch_message_get_header (notmuch_message_t *message, const char *header)
313 _notmuch_message_ensure_message_file (message);
314 if (message->message_file == NULL)
317 return notmuch_message_file_get_header (message->message_file, header);
320 /* Return the message ID from the In-Reply-To header of 'message'.
322 * Returns an empty string ("") if 'message' has no In-Reply-To
325 * Returns NULL if any error occurs.
328 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
330 const char *prefix = _find_prefix ("replyto");
331 int prefix_len = strlen (prefix);
332 Xapian::TermIterator i;
333 std::string in_reply_to;
335 if (message->in_reply_to)
336 return message->in_reply_to;
338 i = message->doc.termlist_begin ();
341 if (i != message->doc.termlist_end ())
344 /* It's perfectly valid for a message to have no In-Reply-To
345 * header. For these cases, we return an empty string. */
346 if (i == message->doc.termlist_end () ||
347 strncmp (in_reply_to.c_str (), prefix, prefix_len))
349 message->in_reply_to = talloc_strdup (message, "");
350 return message->in_reply_to;
353 message->in_reply_to = talloc_strdup (message,
354 in_reply_to.c_str () + prefix_len);
356 #if DEBUG_DATABASE_SANITY
361 if (i != message->doc.termlist_end () &&
362 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
364 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
365 notmuch_message_get_message_id (message),
366 message->in_reply_to,
367 (*i).c_str () + prefix_len);
371 return message->in_reply_to;
375 notmuch_message_get_thread_id (notmuch_message_t *message)
377 const char *prefix = _find_prefix ("thread");
378 Xapian::TermIterator i;
381 /* This code is written with the assumption that "thread" has a
382 * single-character prefix. */
383 assert (strlen (prefix) == 1);
385 if (message->thread_id)
386 return message->thread_id;
388 i = message->doc.termlist_begin ();
391 if (i != message->doc.termlist_end ())
394 if (i == message->doc.termlist_end () || id[0] != *prefix)
395 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
398 message->thread_id = talloc_strdup (message, id.c_str () + 1);
400 #if DEBUG_DATABASE_SANITY
404 if (i != message->doc.termlist_end () && id[0] == *prefix)
406 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
407 notmuch_message_get_message_id (message),
413 return message->thread_id;
417 _notmuch_message_add_reply (notmuch_message_t *message,
418 notmuch_message_node_t *reply)
420 _notmuch_message_list_append (message->replies, reply);
424 notmuch_message_get_replies (notmuch_message_t *message)
426 return _notmuch_messages_create (message->replies);
429 /* Add an additional 'filename' for 'message'.
431 * This change will not be reflected in the database until the next
432 * call to _notmuch_message_sync. */
434 _notmuch_message_add_filename (notmuch_message_t *message,
435 const char *filename)
437 const char *relative, *directory;
438 notmuch_status_t status;
439 void *local = talloc_new (message);
442 if (filename == NULL)
443 INTERNAL_ERROR ("Message filename cannot be NULL.");
445 if (message->filename_list) {
446 _notmuch_filename_list_destroy (message->filename_list);
447 message->filename_list = NULL;
450 relative = _notmuch_database_relative_path (message->notmuch, filename);
452 status = _notmuch_database_split_path (local, relative, &directory, NULL);
456 status = _notmuch_database_filename_to_direntry (local,
458 filename, &direntry);
462 /* New file-direntry allows navigating to this message with
463 * notmuch_directory_get_child_files() . */
464 _notmuch_message_add_term (message, "file-direntry", direntry);
466 /* New terms allow user to search with folder: specification. */
467 _notmuch_message_gen_terms (message, "folder", directory);
471 return NOTMUCH_STATUS_SUCCESS;
474 /* Remove a particular 'filename' from 'message'.
476 * This change will not be reflected in the database until the next
477 * call to _notmuch_message_sync.
479 * Note: This function does not remove a document from the database,
480 * even if the specified filename is the only filename for this
481 * message. For that functionality, see
482 * _notmuch_database_remove_message. */
484 _notmuch_message_remove_filename (notmuch_message_t *message,
485 const char *filename)
487 const char *direntry_prefix = _find_prefix ("file-direntry");
488 int direntry_prefix_len = strlen (direntry_prefix);
489 const char *folder_prefix = _find_prefix ("folder");
490 int folder_prefix_len = strlen (folder_prefix);
491 void *local = talloc_new (message);
493 notmuch_private_status_t private_status;
494 notmuch_status_t status;
495 Xapian::TermIterator i, last;
497 if (message->filename_list) {
498 _notmuch_filename_list_destroy (message->filename_list);
499 message->filename_list = NULL;
502 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
503 filename, &direntry);
507 /* Unlink this file from its parent directory. */
508 private_status = _notmuch_message_remove_term (message,
509 "file-direntry", direntry);
510 status = COERCE_STATUS (private_status,
511 "Unexpected error from _notmuch_message_remove_term");
513 /* Re-synchronize "folder:" terms for this message. This requires
514 * first removing all "folder:" terms, then adding back terms for
515 * all remaining filenames of the message. */
517 i = message->doc.termlist_begin ();
518 i.skip_to (folder_prefix);
520 /* Terminate loop when no terms remain with desired prefix. */
521 if (i == message->doc.termlist_end () ||
522 strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))
528 message->doc.remove_term ((*i));
529 } catch (const Xapian::InvalidArgumentError) {
530 /* Ignore failure to remove non-existent term. */
534 i = message->doc.termlist_begin ();
535 i.skip_to (direntry_prefix);
537 for (; i != message->doc.termlist_end (); i++) {
538 unsigned int directory_id;
539 const char *direntry, *directory;
542 /* Terminate loop at first term without desired prefix. */
543 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
546 direntry = (*i).c_str ();
547 direntry += direntry_prefix_len;
549 directory_id = strtol (direntry, &colon, 10);
551 if (colon == NULL || *colon != ':')
552 INTERNAL_ERROR ("malformed direntry");
554 directory = _notmuch_database_get_directory_path (local,
557 if (strlen (directory))
558 _notmuch_message_gen_terms (message, "folder", directory);
567 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
569 return talloc_strdup (message, message->doc.get_data ().c_str ());
573 _notmuch_message_clear_data (notmuch_message_t *message)
575 message->doc.set_data ("");
579 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
581 const char *prefix = _find_prefix ("file-direntry");
582 int prefix_len = strlen (prefix);
583 Xapian::TermIterator i;
585 if (message->filename_list)
588 message->filename_list = _notmuch_filename_list_create (message);
590 i = message->doc.termlist_begin ();
593 if (i == message->doc.termlist_end () ||
594 strncmp ((*i).c_str (), prefix, prefix_len))
596 /* A message document created by an old version of notmuch
597 * (prior to rename support) will have the filename in the
598 * data of the document rather than as a file-direntry term.
600 * It would be nice to do the upgrade of the document directly
601 * here, but the database is likely open in read-only mode. */
604 data = message->doc.get_data ().c_str ();
607 INTERNAL_ERROR ("message with no filename");
609 _notmuch_filename_list_add_filename (message->filename_list, data);
614 for (; i != message->doc.termlist_end (); i++) {
615 void *local = talloc_new (message);
616 const char *db_path, *directory, *basename, *filename;
617 char *colon, *direntry = NULL;
618 unsigned int directory_id;
620 /* Terminate loop at first term without desired prefix. */
621 if (strncmp ((*i).c_str (), prefix, prefix_len))
624 direntry = talloc_strdup (local, (*i).c_str ());
626 direntry += prefix_len;
628 directory_id = strtol (direntry, &colon, 10);
630 if (colon == NULL || *colon != ':')
631 INTERNAL_ERROR ("malformed direntry");
633 basename = colon + 1;
637 db_path = notmuch_database_get_path (message->notmuch);
639 directory = _notmuch_database_get_directory_path (local,
643 if (strlen (directory))
644 filename = talloc_asprintf (message, "%s/%s/%s",
645 db_path, directory, basename);
647 filename = talloc_asprintf (message, "%s/%s",
650 _notmuch_filename_list_add_filename (message->filename_list,
658 notmuch_message_get_filename (notmuch_message_t *message)
660 _notmuch_message_ensure_filename_list (message);
662 if (message->filename_list == NULL)
665 if (message->filename_list->head == NULL ||
666 message->filename_list->head->filename == NULL)
668 INTERNAL_ERROR ("message with no filename");
671 return message->filename_list->head->filename;
674 notmuch_filenames_t *
675 notmuch_message_get_filenames (notmuch_message_t *message)
677 _notmuch_message_ensure_filename_list (message);
679 return _notmuch_filenames_create (message, message->filename_list);
683 notmuch_message_get_flag (notmuch_message_t *message,
684 notmuch_message_flag_t flag)
686 return message->flags & (1 << flag);
690 notmuch_message_set_flag (notmuch_message_t *message,
691 notmuch_message_flag_t flag, notmuch_bool_t enable)
694 message->flags |= (1 << flag);
696 message->flags &= ~(1 << flag);
700 notmuch_message_get_date (notmuch_message_t *message)
705 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
706 } catch (Xapian::Error &error) {
707 INTERNAL_ERROR ("Failed to read timestamp value from document.");
711 return Xapian::sortable_unserialise (value);
715 notmuch_message_get_tags (notmuch_message_t *message)
717 Xapian::TermIterator i, end;
718 i = message->doc.termlist_begin();
719 end = message->doc.termlist_end();
720 return _notmuch_convert_tags(message, i, end);
724 notmuch_message_get_author (notmuch_message_t *message)
726 return message->author;
730 notmuch_message_set_author (notmuch_message_t *message,
734 talloc_free(message->author);
735 message->author = talloc_strdup(message, author);
740 _notmuch_message_set_date (notmuch_message_t *message,
745 /* GMime really doesn't want to see a NULL date, so protect its
747 if (date == NULL || *date == '\0')
750 time_value = g_mime_utils_header_decode_date (date, NULL);
752 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
753 Xapian::sortable_serialise (time_value));
756 /* Synchronize changes made to message->doc out into the database. */
758 _notmuch_message_sync (notmuch_message_t *message)
760 Xapian::WritableDatabase *db;
762 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
765 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
766 db->replace_document (message->doc_id, message->doc);
769 /* Ensure that 'message' is not holding any file object open. Future
770 * calls to various functions will still automatically open the
771 * message file as needed.
774 _notmuch_message_close (notmuch_message_t *message)
776 if (message->message_file) {
777 notmuch_message_file_close (message->message_file);
778 message->message_file = NULL;
782 /* Add a name:value term to 'message', (the actual term will be
783 * encoded by prefixing the value with a short prefix). See
784 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
785 * names to prefix values.
787 * This change will not be reflected in the database until the next
788 * call to _notmuch_message_sync. */
789 notmuch_private_status_t
790 _notmuch_message_add_term (notmuch_message_t *message,
791 const char *prefix_name,
798 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
800 term = talloc_asprintf (message, "%s%s",
801 _find_prefix (prefix_name), value);
803 if (strlen (term) > NOTMUCH_TERM_MAX)
804 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
806 message->doc.add_term (term, 0);
810 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
813 /* Parse 'text' and add a term to 'message' for each parsed word. Each
814 * term will be added both prefixed (if prefix_name is not NULL) and
815 * also unprefixed). */
816 notmuch_private_status_t
817 _notmuch_message_gen_terms (notmuch_message_t *message,
818 const char *prefix_name,
821 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
824 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
826 term_gen->set_document (message->doc);
829 const char *prefix = _find_prefix (prefix_name);
831 term_gen->index_text (text, 1, prefix);
834 term_gen->index_text (text);
836 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
839 /* Remove a name:value term from 'message', (the actual term will be
840 * encoded by prefixing the value with a short prefix). See
841 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
842 * names to prefix values.
844 * This change will not be reflected in the database until the next
845 * call to _notmuch_message_sync. */
846 notmuch_private_status_t
847 _notmuch_message_remove_term (notmuch_message_t *message,
848 const char *prefix_name,
854 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
856 term = talloc_asprintf (message, "%s%s",
857 _find_prefix (prefix_name), value);
859 if (strlen (term) > NOTMUCH_TERM_MAX)
860 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
863 message->doc.remove_term (term);
864 } catch (const Xapian::InvalidArgumentError) {
865 /* We'll let the philosopher's try to wrestle with the
866 * question of whether failing to remove that which was not
867 * there in the first place is failure. For us, we'll silently
868 * consider it all good. */
873 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
877 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
879 notmuch_private_status_t private_status;
880 notmuch_status_t status;
882 status = _notmuch_database_ensure_writable (message->notmuch);
887 return NOTMUCH_STATUS_NULL_POINTER;
889 if (strlen (tag) > NOTMUCH_TAG_MAX)
890 return NOTMUCH_STATUS_TAG_TOO_LONG;
892 private_status = _notmuch_message_add_term (message, "tag", tag);
893 if (private_status) {
894 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
898 if (! message->frozen)
899 _notmuch_message_sync (message);
901 return NOTMUCH_STATUS_SUCCESS;
905 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
907 notmuch_private_status_t private_status;
908 notmuch_status_t status;
910 status = _notmuch_database_ensure_writable (message->notmuch);
915 return NOTMUCH_STATUS_NULL_POINTER;
917 if (strlen (tag) > NOTMUCH_TAG_MAX)
918 return NOTMUCH_STATUS_TAG_TOO_LONG;
920 private_status = _notmuch_message_remove_term (message, "tag", tag);
921 if (private_status) {
922 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
926 if (! message->frozen)
927 _notmuch_message_sync (message);
929 return NOTMUCH_STATUS_SUCCESS;
933 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
936 notmuch_status_t status;
937 notmuch_filenames_t *filenames;
938 const char *filename;
939 char *combined_flags = talloc_strdup (message, "");
941 int seen_maildir_info = 0;
943 for (filenames = notmuch_message_get_filenames (message);
944 notmuch_filenames_valid (filenames);
945 notmuch_filenames_move_to_next (filenames))
947 filename = notmuch_filenames_get (filenames);
949 flags = strstr (filename, ":2,");
953 seen_maildir_info = 1;
956 combined_flags = talloc_strdup_append (combined_flags, flags);
959 /* If none of the filenames have any maildir info field (not even
960 * an empty info with no flags set) then there's no information to
961 * go on, so do nothing. */
962 if (! seen_maildir_info)
963 return NOTMUCH_STATUS_SUCCESS;
965 status = notmuch_message_freeze (message);
969 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
970 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
974 status = notmuch_message_add_tag (message, flag2tag[i].tag);
976 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
981 status = notmuch_message_thaw (message);
983 talloc_free (combined_flags);
988 /* Is the given filename within a maildir directory?
990 * Specifically, is the final directory component of 'filename' either
991 * "cur" or "new". If so, return a pointer to that final directory
992 * component within 'filename'. If not, return NULL.
994 * A non-NULL return value is guaranteed to be a valid string pointer
995 * pointing to the characters "new/" or "cur/", (but not
999 _filename_is_in_maildir (const char *filename)
1001 const char *slash, *dir = NULL;
1003 /* Find the last '/' separating directory from filename. */
1004 slash = strrchr (filename, '/');
1008 /* Jump back 4 characters to where the previous '/' will be if the
1009 * directory is named "cur" or "new". */
1010 if (slash - filename < 4)
1020 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1021 STRNCMP_LITERAL (dir, "new/") == 0)
1029 /* From the set of tags on 'message' and the flag2tag table, compute a
1030 * set of maildir-flag actions to be taken, (flags that should be
1031 * either set or cleared).
1033 * The result is returned as two talloced strings: to_set, and to_clear
1036 _get_maildir_flag_actions (notmuch_message_t *message,
1038 char **to_clear_ret)
1040 char *to_set, *to_clear;
1041 notmuch_tags_t *tags;
1045 to_set = talloc_strdup (message, "");
1046 to_clear = talloc_strdup (message, "");
1048 /* First, find flags for all set tags. */
1049 for (tags = notmuch_message_get_tags (message);
1050 notmuch_tags_valid (tags);
1051 notmuch_tags_move_to_next (tags))
1053 tag = notmuch_tags_get (tags);
1055 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1056 if (strcmp (tag, flag2tag[i].tag) == 0) {
1057 if (flag2tag[i].inverse)
1058 to_clear = talloc_asprintf_append (to_clear,
1062 to_set = talloc_asprintf_append (to_set,
1069 /* Then, find the flags for all tags not present. */
1070 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1071 if (flag2tag[i].inverse) {
1072 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1073 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1075 if (strchr (to_set, flag2tag[i].flag) == NULL)
1076 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1080 *to_set_ret = to_set;
1081 *to_clear_ret = to_clear;
1084 /* Given 'filename' and a set of maildir flags to set and to clear,
1085 * compute the new maildir filename.
1087 * If the existing filename is in the directory "new", the new
1088 * filename will be in the directory "cur".
1090 * After a sequence of ":2," in the filename, any subsequent
1091 * single-character flags will be added or removed according to the
1092 * characters in flags_to_set and flags_to_clear. Any existing flags
1093 * not mentioned in either string will remain. The final list of flags
1094 * will be in ASCII order.
1096 * If the original flags seem invalid, (repeated characters or
1097 * non-ASCII ordering of flags), this function will return NULL
1098 * (meaning that renaming would not be safe and should not occur).
1101 _new_maildir_filename (void *ctx,
1102 const char *filename,
1103 const char *flags_to_set,
1104 const char *flags_to_clear)
1106 const char *info, *flags;
1107 unsigned int flag, last_flag;
1108 char *filename_new, *dir;
1110 int flags_in_map = 0;
1114 memset (flag_map, 0, sizeof (flag_map));
1116 info = strstr (filename, ":2,");
1119 info = filename + strlen(filename);
1123 /* Loop through existing flags in filename. */
1124 for (flags = info + 3, last_flag = 0;
1126 last_flag = flag, flags++)
1130 /* Original flags not in ASCII order. Abort. */
1131 if (flag < last_flag)
1134 /* Non-ASCII flag. Abort. */
1135 if (flag > sizeof(flag_map) - 1)
1138 /* Repeated flag value. Abort. */
1147 /* Then set and clear our flags from tags. */
1148 for (flags = flags_to_set; *flags; flags++) {
1150 if (flag_map[flag] == 0) {
1156 for (flags = flags_to_clear; *flags; flags++) {
1158 if (flag_map[flag]) {
1164 filename_new = (char *) talloc_size (ctx,
1166 strlen (":2,") + flags_in_map + 1);
1167 if (unlikely (filename_new == NULL))
1170 strncpy (filename_new, filename, info - filename);
1171 filename_new[info - filename] = '\0';
1173 strcat (filename_new, ":2,");
1175 s = filename_new + strlen (filename_new);
1176 for (i = 0; i < sizeof (flag_map); i++)
1185 /* If message is in new/ move it under cur/. */
1186 dir = (char *) _filename_is_in_maildir (filename_new);
1187 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1188 memcpy (dir, "cur/", 4);
1190 return filename_new;
1194 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1196 notmuch_filenames_t *filenames;
1197 const char *filename;
1199 char *to_set, *to_clear;
1200 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1202 _get_maildir_flag_actions (message, &to_set, &to_clear);
1204 for (filenames = notmuch_message_get_filenames (message);
1205 notmuch_filenames_valid (filenames);
1206 notmuch_filenames_move_to_next (filenames))
1208 filename = notmuch_filenames_get (filenames);
1210 if (! _filename_is_in_maildir (filename))
1213 filename_new = _new_maildir_filename (message, filename,
1215 if (filename_new == NULL)
1218 if (strcmp (filename, filename_new)) {
1220 notmuch_status_t new_status;
1222 err = rename (filename, filename_new);
1226 new_status = _notmuch_message_remove_filename (message,
1228 /* Hold on to only the first error. */
1229 if (! status && new_status) {
1230 status = new_status;
1234 new_status = _notmuch_message_add_filename (message,
1236 /* Hold on to only the first error. */
1237 if (! status && new_status) {
1238 status = new_status;
1242 _notmuch_message_sync (message);
1245 talloc_free (filename_new);
1248 talloc_free (to_set);
1249 talloc_free (to_clear);
1251 return NOTMUCH_STATUS_SUCCESS;
1255 notmuch_message_remove_all_tags (notmuch_message_t *message)
1257 notmuch_private_status_t private_status;
1258 notmuch_status_t status;
1259 notmuch_tags_t *tags;
1262 status = _notmuch_database_ensure_writable (message->notmuch);
1266 for (tags = notmuch_message_get_tags (message);
1267 notmuch_tags_valid (tags);
1268 notmuch_tags_move_to_next (tags))
1270 tag = notmuch_tags_get (tags);
1272 private_status = _notmuch_message_remove_term (message, "tag", tag);
1273 if (private_status) {
1274 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1279 if (! message->frozen)
1280 _notmuch_message_sync (message);
1282 return NOTMUCH_STATUS_SUCCESS;
1286 notmuch_message_freeze (notmuch_message_t *message)
1288 notmuch_status_t status;
1290 status = _notmuch_database_ensure_writable (message->notmuch);
1296 return NOTMUCH_STATUS_SUCCESS;
1300 notmuch_message_thaw (notmuch_message_t *message)
1302 notmuch_status_t status;
1304 status = _notmuch_database_ensure_writable (message->notmuch);
1308 if (message->frozen > 0) {
1310 if (message->frozen == 0)
1311 _notmuch_message_sync (message);
1312 return NOTMUCH_STATUS_SUCCESS;
1314 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1319 notmuch_message_destroy (notmuch_message_t *message)
1321 talloc_free (message);