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;
42 Xapian::termcount termpos;
45 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
47 struct maildir_flag_tag {
53 /* ASCII ordered table of Maildir flags and associated tags */
54 static struct maildir_flag_tag flag2tag[] = {
55 { 'D', "draft", false},
56 { 'F', "flagged", false},
57 { 'P', "passed", false},
58 { 'R', "replied", false},
59 { 'S', "unread", true }
62 /* We end up having to call the destructor explicitly because we had
63 * to use "placement new" in order to initialize C++ objects within a
64 * block that we allocated with talloc. So C++ is making talloc
65 * slightly less simple to use, (we wouldn't need
66 * talloc_set_destructor at all otherwise).
69 _notmuch_message_destructor (notmuch_message_t *message)
71 message->doc.~Document ();
76 static notmuch_message_t *
77 _notmuch_message_create_for_document (const void *talloc_owner,
78 notmuch_database_t *notmuch,
81 notmuch_private_status_t *status)
83 notmuch_message_t *message;
86 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
88 message = talloc (talloc_owner, notmuch_message_t);
89 if (unlikely (message == NULL)) {
91 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
95 message->notmuch = notmuch;
96 message->doc_id = doc_id;
101 /* Each of these will be lazily created as needed. */
102 message->message_id = NULL;
103 message->thread_id = NULL;
104 message->in_reply_to = NULL;
105 message->filename_list = NULL;
106 message->message_file = NULL;
107 message->author = NULL;
109 message->replies = _notmuch_message_list_create (message);
110 if (unlikely (message->replies == NULL)) {
112 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
116 /* This is C++'s creepy "placement new", which is really just an
117 * ugly way to call a constructor for a pre-allocated object. So
118 * it's really not an error to not be checking for OUT_OF_MEMORY
119 * here, since this "new" isn't actually allocating memory. This
120 * is language-design comedy of the wrong kind. */
122 new (&message->doc) Xapian::Document;
124 talloc_set_destructor (message, _notmuch_message_destructor);
127 message->termpos = 0;
132 /* Create a new notmuch_message_t object for an existing document in
135 * Here, 'talloc owner' is an optional talloc context to which the new
136 * message will belong. This allows for the caller to not bother
137 * calling notmuch_message_destroy on the message, and know that all
138 * memory will be reclaimed when 'talloc_owner' is freed. The caller
139 * still can call notmuch_message_destroy when finished with the
140 * message if desired.
142 * The 'talloc_owner' argument can also be NULL, in which case the
143 * caller *is* responsible for calling notmuch_message_destroy.
145 * If no document exists in the database with document ID of 'doc_id'
146 * then this function returns NULL and optionally sets *status to
147 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
149 * This function can also fail to due lack of available memory,
150 * returning NULL and optionally setting *status to
151 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
153 * The caller can pass NULL for status if uninterested in
154 * distinguishing these two cases.
157 _notmuch_message_create (const void *talloc_owner,
158 notmuch_database_t *notmuch,
160 notmuch_private_status_t *status)
162 Xapian::Document doc;
165 doc = notmuch->xapian_db->get_document (doc_id);
166 } catch (const Xapian::DocNotFoundError &error) {
168 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
172 return _notmuch_message_create_for_document (talloc_owner, notmuch,
173 doc_id, doc, status);
176 /* Create a new notmuch_message_t object for a specific message ID,
177 * (which may or may not already exist in the database).
179 * The 'notmuch' database will be the talloc owner of the returned
182 * This function returns a valid notmuch_message_t whether or not
183 * there is already a document in the database with the given message
184 * ID. These two cases can be distinguished by the value of *status:
187 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
189 * There is already a document with message ID 'message_id' in the
190 * database. The returned message can be used to query/modify the
192 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
194 * No document with 'message_id' exists in the database. The
195 * returned message contains a newly created document (not yet
196 * added to the database) and a document ID that is known not to
197 * exist in the database. The caller can modify the message, and a
198 * call to _notmuch_message_sync will add * the document to the
201 * If an error occurs, this function will return NULL and *status
202 * will be set as appropriate. (The status pointer argument must
206 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
207 const char *message_id,
208 notmuch_private_status_t *status_ret)
210 notmuch_message_t *message;
211 Xapian::Document doc;
212 Xapian::WritableDatabase *db;
216 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
218 message = notmuch_database_find_message (notmuch, message_id);
220 return talloc_steal (notmuch, message);
222 term = talloc_asprintf (NULL, "%s%s",
223 _find_prefix ("id"), message_id);
225 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
229 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
230 INTERNAL_ERROR ("Failure to ensure database is writable.");
232 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
234 doc.add_term (term, 0);
237 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
239 doc_id = _notmuch_database_generate_doc_id (notmuch);
240 } catch (const Xapian::Error &error) {
241 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
242 error.get_msg().c_str());
243 notmuch->exception_reported = TRUE;
244 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
248 message = _notmuch_message_create_for_document (notmuch, notmuch,
249 doc_id, doc, status_ret);
251 /* We want to inform the caller that we had to create a new
253 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
254 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
260 _notmuch_message_get_doc_id (notmuch_message_t *message)
262 return message->doc_id;
266 notmuch_message_get_message_id (notmuch_message_t *message)
268 Xapian::TermIterator i;
270 if (message->message_id)
271 return message->message_id;
273 i = message->doc.termlist_begin ();
274 i.skip_to (_find_prefix ("id"));
276 if (i == message->doc.termlist_end ())
277 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
280 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
282 #if DEBUG_DATABASE_SANITY
285 if (i != message->doc.termlist_end () &&
286 strncmp ((*i).c_str (), _find_prefix ("id"),
287 strlen (_find_prefix ("id"))) == 0)
289 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
294 return message->message_id;
298 _notmuch_message_ensure_message_file (notmuch_message_t *message)
300 const char *filename;
302 if (message->message_file)
305 filename = notmuch_message_get_filename (message);
306 if (unlikely (filename == NULL))
309 message->message_file = _notmuch_message_file_open_ctx (message, filename);
313 notmuch_message_get_header (notmuch_message_t *message, const char *header)
315 _notmuch_message_ensure_message_file (message);
316 if (message->message_file == NULL)
319 return notmuch_message_file_get_header (message->message_file, header);
322 /* Return the message ID from the In-Reply-To header of 'message'.
324 * Returns an empty string ("") if 'message' has no In-Reply-To
327 * Returns NULL if any error occurs.
330 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
332 const char *prefix = _find_prefix ("replyto");
333 int prefix_len = strlen (prefix);
334 Xapian::TermIterator i;
335 std::string in_reply_to;
337 if (message->in_reply_to)
338 return message->in_reply_to;
340 i = message->doc.termlist_begin ();
343 if (i != message->doc.termlist_end ())
346 /* It's perfectly valid for a message to have no In-Reply-To
347 * header. For these cases, we return an empty string. */
348 if (i == message->doc.termlist_end () ||
349 strncmp (in_reply_to.c_str (), prefix, prefix_len))
351 message->in_reply_to = talloc_strdup (message, "");
352 return message->in_reply_to;
355 message->in_reply_to = talloc_strdup (message,
356 in_reply_to.c_str () + prefix_len);
358 #if DEBUG_DATABASE_SANITY
363 if (i != message->doc.termlist_end () &&
364 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
366 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
367 notmuch_message_get_message_id (message),
368 message->in_reply_to,
369 (*i).c_str () + prefix_len);
373 return message->in_reply_to;
377 notmuch_message_get_thread_id (notmuch_message_t *message)
379 const char *prefix = _find_prefix ("thread");
380 Xapian::TermIterator i;
383 /* This code is written with the assumption that "thread" has a
384 * single-character prefix. */
385 assert (strlen (prefix) == 1);
387 if (message->thread_id)
388 return message->thread_id;
390 i = message->doc.termlist_begin ();
393 if (i != message->doc.termlist_end ())
396 if (i == message->doc.termlist_end () || id[0] != *prefix)
397 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
400 message->thread_id = talloc_strdup (message, id.c_str () + 1);
402 #if DEBUG_DATABASE_SANITY
406 if (i != message->doc.termlist_end () && id[0] == *prefix)
408 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
409 notmuch_message_get_message_id (message),
415 return message->thread_id;
419 _notmuch_message_add_reply (notmuch_message_t *message,
420 notmuch_message_node_t *reply)
422 _notmuch_message_list_append (message->replies, reply);
426 notmuch_message_get_replies (notmuch_message_t *message)
428 return _notmuch_messages_create (message->replies);
431 /* Add an additional 'filename' for 'message'.
433 * This change will not be reflected in the database until the next
434 * call to _notmuch_message_sync. */
436 _notmuch_message_add_filename (notmuch_message_t *message,
437 const char *filename)
439 const char *relative, *directory;
440 notmuch_status_t status;
441 void *local = talloc_new (message);
444 if (filename == NULL)
445 INTERNAL_ERROR ("Message filename cannot be NULL.");
447 if (message->filename_list) {
448 _notmuch_filename_list_destroy (message->filename_list);
449 message->filename_list = NULL;
452 relative = _notmuch_database_relative_path (message->notmuch, filename);
454 status = _notmuch_database_split_path (local, relative, &directory, NULL);
458 status = _notmuch_database_filename_to_direntry (local,
460 filename, &direntry);
464 /* New file-direntry allows navigating to this message with
465 * notmuch_directory_get_child_files() . */
466 _notmuch_message_add_term (message, "file-direntry", direntry);
468 /* New terms allow user to search with folder: specification. */
469 _notmuch_message_gen_terms (message, "folder", directory);
473 return NOTMUCH_STATUS_SUCCESS;
476 /* Remove a particular 'filename' from 'message'.
478 * This change will not be reflected in the database until the next
479 * call to _notmuch_message_sync.
481 * Note: This function does not remove a document from the database,
482 * even if the specified filename is the only filename for this
483 * message. For that functionality, see
484 * _notmuch_database_remove_message. */
486 _notmuch_message_remove_filename (notmuch_message_t *message,
487 const char *filename)
489 const char *direntry_prefix = _find_prefix ("file-direntry");
490 int direntry_prefix_len = strlen (direntry_prefix);
491 const char *folder_prefix = _find_prefix ("folder");
492 int folder_prefix_len = strlen (folder_prefix);
493 void *local = talloc_new (message);
495 notmuch_private_status_t private_status;
496 notmuch_status_t status;
497 Xapian::TermIterator i, last;
499 if (message->filename_list) {
500 _notmuch_filename_list_destroy (message->filename_list);
501 message->filename_list = NULL;
504 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
505 filename, &direntry);
509 /* Unlink this file from its parent directory. */
510 private_status = _notmuch_message_remove_term (message,
511 "file-direntry", direntry);
512 status = COERCE_STATUS (private_status,
513 "Unexpected error from _notmuch_message_remove_term");
515 /* Re-synchronize "folder:" terms for this message. This requires
516 * first removing all "folder:" terms, then adding back terms for
517 * all remaining filenames of the message. */
519 i = message->doc.termlist_begin ();
520 i.skip_to (folder_prefix);
522 /* Terminate loop when no terms remain with desired prefix. */
523 if (i == message->doc.termlist_end () ||
524 strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))
530 message->doc.remove_term ((*i));
531 } catch (const Xapian::InvalidArgumentError) {
532 /* Ignore failure to remove non-existent term. */
536 i = message->doc.termlist_begin ();
537 i.skip_to (direntry_prefix);
539 for (; i != message->doc.termlist_end (); i++) {
540 unsigned int directory_id;
541 const char *direntry, *directory;
544 /* Terminate loop at first term without desired prefix. */
545 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
548 direntry = (*i).c_str ();
549 direntry += direntry_prefix_len;
551 directory_id = strtol (direntry, &colon, 10);
553 if (colon == NULL || *colon != ':')
554 INTERNAL_ERROR ("malformed direntry");
556 directory = _notmuch_database_get_directory_path (local,
559 if (strlen (directory))
560 _notmuch_message_gen_terms (message, "folder", directory);
569 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
571 return talloc_strdup (message, message->doc.get_data ().c_str ());
575 _notmuch_message_clear_data (notmuch_message_t *message)
577 message->doc.set_data ("");
581 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
583 const char *prefix = _find_prefix ("file-direntry");
584 int prefix_len = strlen (prefix);
585 Xapian::TermIterator i;
587 if (message->filename_list)
590 message->filename_list = _notmuch_filename_list_create (message);
592 i = message->doc.termlist_begin ();
595 if (i == message->doc.termlist_end () ||
596 strncmp ((*i).c_str (), prefix, prefix_len))
598 /* A message document created by an old version of notmuch
599 * (prior to rename support) will have the filename in the
600 * data of the document rather than as a file-direntry term.
602 * It would be nice to do the upgrade of the document directly
603 * here, but the database is likely open in read-only mode. */
606 data = message->doc.get_data ().c_str ();
609 INTERNAL_ERROR ("message with no filename");
611 _notmuch_filename_list_add_filename (message->filename_list, data);
616 for (; i != message->doc.termlist_end (); i++) {
617 void *local = talloc_new (message);
618 const char *db_path, *directory, *basename, *filename;
619 char *colon, *direntry = NULL;
620 unsigned int directory_id;
622 /* Terminate loop at first term without desired prefix. */
623 if (strncmp ((*i).c_str (), prefix, prefix_len))
626 direntry = talloc_strdup (local, (*i).c_str ());
628 direntry += prefix_len;
630 directory_id = strtol (direntry, &colon, 10);
632 if (colon == NULL || *colon != ':')
633 INTERNAL_ERROR ("malformed direntry");
635 basename = colon + 1;
639 db_path = notmuch_database_get_path (message->notmuch);
641 directory = _notmuch_database_get_directory_path (local,
645 if (strlen (directory))
646 filename = talloc_asprintf (message, "%s/%s/%s",
647 db_path, directory, basename);
649 filename = talloc_asprintf (message, "%s/%s",
652 _notmuch_filename_list_add_filename (message->filename_list,
660 notmuch_message_get_filename (notmuch_message_t *message)
662 _notmuch_message_ensure_filename_list (message);
664 if (message->filename_list == NULL)
667 if (message->filename_list->head == NULL ||
668 message->filename_list->head->filename == NULL)
670 INTERNAL_ERROR ("message with no filename");
673 return message->filename_list->head->filename;
676 notmuch_filenames_t *
677 notmuch_message_get_filenames (notmuch_message_t *message)
679 _notmuch_message_ensure_filename_list (message);
681 return _notmuch_filenames_create (message, message->filename_list);
685 notmuch_message_get_flag (notmuch_message_t *message,
686 notmuch_message_flag_t flag)
688 return message->flags & (1 << flag);
692 notmuch_message_set_flag (notmuch_message_t *message,
693 notmuch_message_flag_t flag, notmuch_bool_t enable)
696 message->flags |= (1 << flag);
698 message->flags &= ~(1 << flag);
702 notmuch_message_get_date (notmuch_message_t *message)
707 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
708 } catch (Xapian::Error &error) {
709 INTERNAL_ERROR ("Failed to read timestamp value from document.");
713 return Xapian::sortable_unserialise (value);
717 notmuch_message_get_tags (notmuch_message_t *message)
719 Xapian::TermIterator i, end;
720 i = message->doc.termlist_begin();
721 end = message->doc.termlist_end();
722 return _notmuch_convert_tags(message, i, end);
726 notmuch_message_get_author (notmuch_message_t *message)
728 return message->author;
732 notmuch_message_set_author (notmuch_message_t *message,
736 talloc_free(message->author);
737 message->author = talloc_strdup(message, author);
742 _notmuch_message_set_date (notmuch_message_t *message,
747 /* GMime really doesn't want to see a NULL date, so protect its
749 if (date == NULL || *date == '\0')
752 time_value = g_mime_utils_header_decode_date (date, NULL);
754 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
755 Xapian::sortable_serialise (time_value));
758 /* Synchronize changes made to message->doc out into the database. */
760 _notmuch_message_sync (notmuch_message_t *message)
762 Xapian::WritableDatabase *db;
764 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
767 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
768 db->replace_document (message->doc_id, message->doc);
771 /* Ensure that 'message' is not holding any file object open. Future
772 * calls to various functions will still automatically open the
773 * message file as needed.
776 _notmuch_message_close (notmuch_message_t *message)
778 if (message->message_file) {
779 notmuch_message_file_close (message->message_file);
780 message->message_file = NULL;
784 /* Add a name:value term to 'message', (the actual term will be
785 * encoded by prefixing the value with a short prefix). See
786 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
787 * names to prefix values.
789 * This change will not be reflected in the database until the next
790 * call to _notmuch_message_sync. */
791 notmuch_private_status_t
792 _notmuch_message_add_term (notmuch_message_t *message,
793 const char *prefix_name,
800 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
802 term = talloc_asprintf (message, "%s%s",
803 _find_prefix (prefix_name), value);
805 if (strlen (term) > NOTMUCH_TERM_MAX)
806 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
808 message->doc.add_term (term, 0);
812 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
815 /* Parse 'text' and add a term to 'message' for each parsed word. Each
816 * term will be added both prefixed (if prefix_name is not NULL) and
817 * also unprefixed). */
818 notmuch_private_status_t
819 _notmuch_message_gen_terms (notmuch_message_t *message,
820 const char *prefix_name,
823 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
826 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
828 term_gen->set_document (message->doc);
829 term_gen->set_termpos (message->termpos);
832 const char *prefix = _find_prefix (prefix_name);
834 term_gen->index_text (text, 1, prefix);
835 message->termpos = term_gen->get_termpos ();
838 term_gen->index_text (text);
840 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
843 /* Remove a name:value term from 'message', (the actual term will be
844 * encoded by prefixing the value with a short prefix). See
845 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
846 * names to prefix values.
848 * This change will not be reflected in the database until the next
849 * call to _notmuch_message_sync. */
850 notmuch_private_status_t
851 _notmuch_message_remove_term (notmuch_message_t *message,
852 const char *prefix_name,
858 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
860 term = talloc_asprintf (message, "%s%s",
861 _find_prefix (prefix_name), value);
863 if (strlen (term) > NOTMUCH_TERM_MAX)
864 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
867 message->doc.remove_term (term);
868 } catch (const Xapian::InvalidArgumentError) {
869 /* We'll let the philosopher's try to wrestle with the
870 * question of whether failing to remove that which was not
871 * there in the first place is failure. For us, we'll silently
872 * consider it all good. */
877 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
881 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
883 notmuch_private_status_t private_status;
884 notmuch_status_t status;
886 status = _notmuch_database_ensure_writable (message->notmuch);
891 return NOTMUCH_STATUS_NULL_POINTER;
893 if (strlen (tag) > NOTMUCH_TAG_MAX)
894 return NOTMUCH_STATUS_TAG_TOO_LONG;
896 private_status = _notmuch_message_add_term (message, "tag", tag);
897 if (private_status) {
898 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
902 if (! message->frozen)
903 _notmuch_message_sync (message);
905 return NOTMUCH_STATUS_SUCCESS;
909 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
911 notmuch_private_status_t private_status;
912 notmuch_status_t status;
914 status = _notmuch_database_ensure_writable (message->notmuch);
919 return NOTMUCH_STATUS_NULL_POINTER;
921 if (strlen (tag) > NOTMUCH_TAG_MAX)
922 return NOTMUCH_STATUS_TAG_TOO_LONG;
924 private_status = _notmuch_message_remove_term (message, "tag", tag);
925 if (private_status) {
926 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
930 if (! message->frozen)
931 _notmuch_message_sync (message);
933 return NOTMUCH_STATUS_SUCCESS;
937 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
940 notmuch_status_t status;
941 notmuch_filenames_t *filenames;
942 const char *filename;
943 char *combined_flags = talloc_strdup (message, "");
945 int seen_maildir_info = 0;
947 for (filenames = notmuch_message_get_filenames (message);
948 notmuch_filenames_valid (filenames);
949 notmuch_filenames_move_to_next (filenames))
951 filename = notmuch_filenames_get (filenames);
953 flags = strstr (filename, ":2,");
957 seen_maildir_info = 1;
960 combined_flags = talloc_strdup_append (combined_flags, flags);
963 /* If none of the filenames have any maildir info field (not even
964 * an empty info with no flags set) then there's no information to
965 * go on, so do nothing. */
966 if (! seen_maildir_info)
967 return NOTMUCH_STATUS_SUCCESS;
969 status = notmuch_message_freeze (message);
973 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
974 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
978 status = notmuch_message_add_tag (message, flag2tag[i].tag);
980 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
985 status = notmuch_message_thaw (message);
987 talloc_free (combined_flags);
992 /* Is the given filename within a maildir directory?
994 * Specifically, is the final directory component of 'filename' either
995 * "cur" or "new". If so, return a pointer to that final directory
996 * component within 'filename'. If not, return NULL.
998 * A non-NULL return value is guaranteed to be a valid string pointer
999 * pointing to the characters "new/" or "cur/", (but not
1003 _filename_is_in_maildir (const char *filename)
1005 const char *slash, *dir = NULL;
1007 /* Find the last '/' separating directory from filename. */
1008 slash = strrchr (filename, '/');
1012 /* Jump back 4 characters to where the previous '/' will be if the
1013 * directory is named "cur" or "new". */
1014 if (slash - filename < 4)
1024 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1025 STRNCMP_LITERAL (dir, "new/") == 0)
1033 /* From the set of tags on 'message' and the flag2tag table, compute a
1034 * set of maildir-flag actions to be taken, (flags that should be
1035 * either set or cleared).
1037 * The result is returned as two talloced strings: to_set, and to_clear
1040 _get_maildir_flag_actions (notmuch_message_t *message,
1042 char **to_clear_ret)
1044 char *to_set, *to_clear;
1045 notmuch_tags_t *tags;
1049 to_set = talloc_strdup (message, "");
1050 to_clear = talloc_strdup (message, "");
1052 /* First, find flags for all set tags. */
1053 for (tags = notmuch_message_get_tags (message);
1054 notmuch_tags_valid (tags);
1055 notmuch_tags_move_to_next (tags))
1057 tag = notmuch_tags_get (tags);
1059 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1060 if (strcmp (tag, flag2tag[i].tag) == 0) {
1061 if (flag2tag[i].inverse)
1062 to_clear = talloc_asprintf_append (to_clear,
1066 to_set = talloc_asprintf_append (to_set,
1073 /* Then, find the flags for all tags not present. */
1074 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1075 if (flag2tag[i].inverse) {
1076 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1077 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1079 if (strchr (to_set, flag2tag[i].flag) == NULL)
1080 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1084 *to_set_ret = to_set;
1085 *to_clear_ret = to_clear;
1088 /* Given 'filename' and a set of maildir flags to set and to clear,
1089 * compute the new maildir filename.
1091 * If the existing filename is in the directory "new", the new
1092 * filename will be in the directory "cur".
1094 * After a sequence of ":2," in the filename, any subsequent
1095 * single-character flags will be added or removed according to the
1096 * characters in flags_to_set and flags_to_clear. Any existing flags
1097 * not mentioned in either string will remain. The final list of flags
1098 * will be in ASCII order.
1100 * If the original flags seem invalid, (repeated characters or
1101 * non-ASCII ordering of flags), this function will return NULL
1102 * (meaning that renaming would not be safe and should not occur).
1105 _new_maildir_filename (void *ctx,
1106 const char *filename,
1107 const char *flags_to_set,
1108 const char *flags_to_clear)
1110 const char *info, *flags;
1111 unsigned int flag, last_flag;
1112 char *filename_new, *dir;
1114 int flags_in_map = 0;
1118 memset (flag_map, 0, sizeof (flag_map));
1120 info = strstr (filename, ":2,");
1123 info = filename + strlen(filename);
1127 /* Loop through existing flags in filename. */
1128 for (flags = info + 3, last_flag = 0;
1130 last_flag = flag, flags++)
1134 /* Original flags not in ASCII order. Abort. */
1135 if (flag < last_flag)
1138 /* Non-ASCII flag. Abort. */
1139 if (flag > sizeof(flag_map) - 1)
1142 /* Repeated flag value. Abort. */
1151 /* Then set and clear our flags from tags. */
1152 for (flags = flags_to_set; *flags; flags++) {
1154 if (flag_map[flag] == 0) {
1160 for (flags = flags_to_clear; *flags; flags++) {
1162 if (flag_map[flag]) {
1168 filename_new = (char *) talloc_size (ctx,
1170 strlen (":2,") + flags_in_map + 1);
1171 if (unlikely (filename_new == NULL))
1174 strncpy (filename_new, filename, info - filename);
1175 filename_new[info - filename] = '\0';
1177 strcat (filename_new, ":2,");
1179 s = filename_new + strlen (filename_new);
1180 for (i = 0; i < sizeof (flag_map); i++)
1189 /* If message is in new/ move it under cur/. */
1190 dir = (char *) _filename_is_in_maildir (filename_new);
1191 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1192 memcpy (dir, "cur/", 4);
1194 return filename_new;
1198 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1200 notmuch_filenames_t *filenames;
1201 const char *filename;
1203 char *to_set, *to_clear;
1204 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1206 _get_maildir_flag_actions (message, &to_set, &to_clear);
1208 for (filenames = notmuch_message_get_filenames (message);
1209 notmuch_filenames_valid (filenames);
1210 notmuch_filenames_move_to_next (filenames))
1212 filename = notmuch_filenames_get (filenames);
1214 if (! _filename_is_in_maildir (filename))
1217 filename_new = _new_maildir_filename (message, filename,
1219 if (filename_new == NULL)
1222 if (strcmp (filename, filename_new)) {
1224 notmuch_status_t new_status;
1226 err = rename (filename, filename_new);
1230 new_status = _notmuch_message_remove_filename (message,
1232 /* Hold on to only the first error. */
1233 if (! status && new_status) {
1234 status = new_status;
1238 new_status = _notmuch_message_add_filename (message,
1240 /* Hold on to only the first error. */
1241 if (! status && new_status) {
1242 status = new_status;
1246 _notmuch_message_sync (message);
1249 talloc_free (filename_new);
1252 talloc_free (to_set);
1253 talloc_free (to_clear);
1255 return NOTMUCH_STATUS_SUCCESS;
1259 notmuch_message_remove_all_tags (notmuch_message_t *message)
1261 notmuch_private_status_t private_status;
1262 notmuch_status_t status;
1263 notmuch_tags_t *tags;
1266 status = _notmuch_database_ensure_writable (message->notmuch);
1270 for (tags = notmuch_message_get_tags (message);
1271 notmuch_tags_valid (tags);
1272 notmuch_tags_move_to_next (tags))
1274 tag = notmuch_tags_get (tags);
1276 private_status = _notmuch_message_remove_term (message, "tag", tag);
1277 if (private_status) {
1278 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1283 if (! message->frozen)
1284 _notmuch_message_sync (message);
1286 return NOTMUCH_STATUS_SUCCESS;
1290 notmuch_message_freeze (notmuch_message_t *message)
1292 notmuch_status_t status;
1294 status = _notmuch_database_ensure_writable (message->notmuch);
1300 return NOTMUCH_STATUS_SUCCESS;
1304 notmuch_message_thaw (notmuch_message_t *message)
1306 notmuch_status_t status;
1308 status = _notmuch_database_ensure_writable (message->notmuch);
1312 if (message->frozen > 0) {
1314 if (message->frozen == 0)
1315 _notmuch_message_sync (message);
1316 return NOTMUCH_STATUS_SUCCESS;
1318 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1323 notmuch_message_destroy (notmuch_message_t *message)
1325 talloc_free (message);