1 /* database.cc - The database interfaces of the notmuch mail library
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 "database-private.h"
27 #include <glib.h> /* g_free, GPtrArray, GHashTable */
31 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
38 /* Here's the current schema for our database:
40 * We currently have two different types of documents: mail and timestamps.
44 * A mail document is associated with a particular email message file
45 * on disk. It is indexed with the following prefixed terms which the
46 * database uses to construct threads, etc.:
48 * Single terms of given prefix:
52 * id: Unique ID of mail, (from Message-ID header or generated
53 * as "notmuch-sha1-<sha1_sum_of_entire_file>.
55 * thread: The ID of the thread to which the mail belongs
57 * replyto: The ID from the In-Reply-To header of the mail (if any).
59 * Multiple terms of given prefix:
61 * reference: All message IDs from In-Reply-To and Re ferences
62 * headers in the message.
64 * tag: Any tags associated with this message by the user.
66 * A mail document also has two values:
68 * TIMESTAMP: The time_t value corresponding to the message's
71 * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
73 * In addition, terms from the content of the message are added with
74 * "from", "to", "attachment", and "subject" prefixes for use by the
75 * user in searching. But the database doesn't really care itself
80 * A timestamp document is used by a client of the notmuch library to
81 * maintain data necessary to allow for efficient polling of mail
82 * directories. The notmuch library does no interpretation of
83 * timestamps, but merely allows the user to store and retrieve
84 * timestamps as name/value pairs.
86 * The timestamp document is indexed with a single prefixed term:
88 * timestamp: The user's key value (likely a directory name)
90 * and has a single value:
92 * TIMESTAMP: The time_t value from the user.
95 /* With these prefix values we follow the conventions published here:
97 * http://xapian.org/docs/omega/termprefixes.html
99 * as much as makes sense. Note that I took some liberty in matching
100 * the reserved prefix values to notmuch concepts, (for example, 'G'
101 * is documented as "newsGroup (or similar entity - e.g. a web forum
102 * name)", for which I think the thread is the closest analogue in
103 * notmuch. This in spite of the fact that we will eventually be
104 * storing mailing-list messages where 'G' for "mailing list name"
105 * might be even a closer analogue. I'm treating the single-character
106 * prefixes preferentially for core notmuch concepts (which will be
107 * nearly universal to all mail messages).
110 prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
112 { "reference", "XREFERENCE" },
113 { "replyto", "XREPLYTO" },
114 { "timestamp", "XTIMESTAMP" },
117 prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
123 prefix_t PROBABILISTIC_PREFIX[]= {
126 { "attachment", "XATTACHMENT" },
127 { "subject", "XSUBJECT"}
131 _internal_error (const char *format, ...)
135 va_start (va_args, format);
137 fprintf (stderr, "Internal error: ");
138 vfprintf (stderr, format, va_args);
146 _find_prefix (const char *name)
150 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_INTERNAL); i++) {
151 if (strcmp (name, BOOLEAN_PREFIX_INTERNAL[i].name) == 0)
152 return BOOLEAN_PREFIX_INTERNAL[i].prefix;
155 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
156 if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
157 return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
160 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
161 if (strcmp (name, PROBABILISTIC_PREFIX[i].name) == 0)
162 return PROBABILISTIC_PREFIX[i].prefix;
165 INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
171 notmuch_status_to_string (notmuch_status_t status)
174 case NOTMUCH_STATUS_SUCCESS:
175 return "No error occurred";
176 case NOTMUCH_STATUS_OUT_OF_MEMORY:
177 return "Out of memory";
178 case NOTMUCH_STATUS_READONLY_DATABASE:
179 return "The database is read-only";
180 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
181 return "A Xapian exception occurred";
182 case NOTMUCH_STATUS_FILE_ERROR:
183 return "Something went wrong trying to read or write a file";
184 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
185 return "File is not an email";
186 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
187 return "Message ID is identical to a message in database";
188 case NOTMUCH_STATUS_NULL_POINTER:
189 return "Erroneous NULL pointer";
190 case NOTMUCH_STATUS_TAG_TOO_LONG:
191 return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
192 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
193 return "Unbalanced number of calls to notmuch_message_freeze/thaw";
195 case NOTMUCH_STATUS_LAST_STATUS:
196 return "Unknown error status value";
201 find_doc_ids (notmuch_database_t *notmuch,
202 const char *prefix_name,
204 Xapian::PostingIterator *begin,
205 Xapian::PostingIterator *end)
207 Xapian::PostingIterator i;
210 term = talloc_asprintf (notmuch, "%s%s",
211 _find_prefix (prefix_name), value);
213 *begin = notmuch->xapian_db->postlist_begin (term);
215 *end = notmuch->xapian_db->postlist_end (term);
220 static notmuch_private_status_t
221 find_unique_doc_id (notmuch_database_t *notmuch,
222 const char *prefix_name,
224 unsigned int *doc_id)
226 Xapian::PostingIterator i, end;
228 find_doc_ids (notmuch, prefix_name, value, &i, &end);
232 return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
235 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
239 static Xapian::Document
240 find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id)
242 return notmuch->xapian_db->get_document (doc_id);
245 static notmuch_private_status_t
246 find_unique_document (notmuch_database_t *notmuch,
247 const char *prefix_name,
249 Xapian::Document *document,
250 unsigned int *doc_id)
252 notmuch_private_status_t status;
254 status = find_unique_doc_id (notmuch, prefix_name, value, doc_id);
257 *document = Xapian::Document ();
261 *document = find_document_for_doc_id (notmuch, *doc_id);
262 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
266 notmuch_database_find_message (notmuch_database_t *notmuch,
267 const char *message_id)
269 notmuch_private_status_t status;
272 status = find_unique_doc_id (notmuch, "id", message_id, &doc_id);
274 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
277 return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
280 /* Advance 'str' past any whitespace or RFC 822 comments. A comment is
281 * a (potentially nested) parenthesized sequence with '\' used to
282 * escape any character (including parentheses).
284 * If the sequence to be skipped continues to the end of the string,
285 * then 'str' will be left pointing at the final terminating '\0'
289 skip_space_and_comments (const char **str)
294 while (*s && (isspace (*s) || *s == '(')) {
295 while (*s && isspace (*s))
300 while (*s && nesting) {
303 } else if (*s == ')') {
305 } else if (*s == '\\') {
317 /* Parse an RFC 822 message-id, discarding whitespace, any RFC 822
318 * comments, and the '<' and '>' delimeters.
320 * If not NULL, then *next will be made to point to the first character
321 * not parsed, (possibly pointing to the final '\0' terminator.
323 * Returns a newly talloc'ed string belonging to 'ctx'.
325 * Returns NULL if there is any error parsing the message-id. */
327 _parse_message_id (void *ctx, const char *message_id, const char **next)
332 if (message_id == NULL || *message_id == '\0')
337 skip_space_and_comments (&s);
339 /* Skip any unstructured text as well. */
340 while (*s && *s != '<')
351 skip_space_and_comments (&s);
354 while (*end && *end != '>')
363 if (end > s && *end == '>')
368 result = talloc_strndup (ctx, s, end - s + 1);
370 /* Finally, collapse any whitespace that is within the message-id
376 for (r = result, len = strlen (r); *r; r++, len--)
377 if (*r == ' ' || *r == '\t')
378 memmove (r, r+1, len);
384 /* Parse a References header value, putting a (talloc'ed under 'ctx')
385 * copy of each referenced message-id into 'hash'.
387 * We explicitly avoid including any reference identical to
388 * 'message_id' in the result (to avoid mass confusion when a single
389 * message references itself cyclically---and yes, mail messages are
390 * not infrequent in the wild that do this---don't ask me why).
393 parse_references (void *ctx,
394 const char *message_id,
400 if (refs == NULL || *refs == '\0')
404 ref = _parse_message_id (ctx, refs, &refs);
406 if (ref && strcmp (ref, message_id))
407 g_hash_table_insert (hash, ref, NULL);
412 notmuch_database_create (const char *path)
414 notmuch_database_t *notmuch = NULL;
415 char *notmuch_path = NULL;
420 fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
424 err = stat (path, &st);
426 fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
427 path, strerror (errno));
431 if (! S_ISDIR (st.st_mode)) {
432 fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
437 notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
439 err = mkdir (notmuch_path, 0755);
442 fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
443 notmuch_path, strerror (errno));
447 notmuch = notmuch_database_open (path,
448 NOTMUCH_DATABASE_MODE_READ_WRITE);
452 talloc_free (notmuch_path);
458 notmuch_database_open (const char *path,
459 notmuch_database_mode_t mode)
461 notmuch_database_t *notmuch = NULL;
462 char *notmuch_path = NULL, *xapian_path = NULL;
467 if (asprintf (¬much_path, "%s/%s", path, ".notmuch") == -1) {
469 fprintf (stderr, "Out of memory\n");
473 err = stat (notmuch_path, &st);
475 fprintf (stderr, "Error opening database at %s: %s\n",
476 notmuch_path, strerror (errno));
480 if (asprintf (&xapian_path, "%s/%s", notmuch_path, "xapian") == -1) {
482 fprintf (stderr, "Out of memory\n");
486 notmuch = talloc (NULL, notmuch_database_t);
487 notmuch->exception_reported = FALSE;
488 notmuch->path = talloc_strdup (notmuch, path);
490 if (notmuch->path[strlen (notmuch->path) - 1] == '/')
491 notmuch->path[strlen (notmuch->path) - 1] = '\0';
493 notmuch->mode = mode;
495 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
496 notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
497 Xapian::DB_CREATE_OR_OPEN);
499 notmuch->xapian_db = new Xapian::Database (xapian_path);
501 notmuch->query_parser = new Xapian::QueryParser;
502 notmuch->term_gen = new Xapian::TermGenerator;
503 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
504 notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
506 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
507 notmuch->query_parser->set_database (*notmuch->xapian_db);
508 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
509 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
510 notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor);
512 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
513 prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];
514 notmuch->query_parser->add_boolean_prefix (prefix->name,
518 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
519 prefix_t *prefix = &PROBABILISTIC_PREFIX[i];
520 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
522 } catch (const Xapian::Error &error) {
523 fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
524 error.get_msg().c_str());
538 notmuch_database_close (notmuch_database_t *notmuch)
541 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
542 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
543 } catch (const Xapian::Error &error) {
544 if (! notmuch->exception_reported) {
545 fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",
546 error.get_msg().c_str());
550 delete notmuch->term_gen;
551 delete notmuch->query_parser;
552 delete notmuch->xapian_db;
553 delete notmuch->value_range_processor;
554 talloc_free (notmuch);
558 notmuch_database_get_path (notmuch_database_t *notmuch)
560 return notmuch->path;
563 static notmuch_private_status_t
564 find_timestamp_document (notmuch_database_t *notmuch, const char *db_key,
565 Xapian::Document *doc, unsigned int *doc_id)
567 return find_unique_document (notmuch, "timestamp", db_key, doc, doc_id);
570 /* We allow the user to use arbitrarily long keys for timestamps,
571 * (they're for filesystem paths after all, which have no limit we
572 * know about). But we have a term-length limit. So if we exceed that,
573 * we'll use the SHA-1 of the user's key as the actual key for
574 * constructing a database term.
576 * Caution: This function returns a newly allocated string which the
577 * caller should free() when finished.
580 timestamp_db_key (const char *key)
582 int term_len = strlen (_find_prefix ("timestamp")) + strlen (key);
584 if (term_len > NOTMUCH_TERM_MAX)
585 return notmuch_sha1_of_string (key);
590 /* Given a legal 'path' for the database, return the relative path.
592 * The return value will be a pointer to the originl path contents,
593 * and will be either the original string (if 'path' was relative) or
594 * a portion of the string (if path was absolute and begins with the
598 _notmuch_database_relative_path (notmuch_database_t *notmuch,
601 const char *db_path, *relative;
602 unsigned int db_path_len;
604 db_path = notmuch_database_get_path (notmuch);
605 db_path_len = strlen (db_path);
609 if (*relative == '/') {
610 while (*relative == '/' && *(relative+1) == '/')
613 if (strncmp (relative, db_path, db_path_len) == 0)
615 relative += db_path_len;
616 while (*relative == '/')
625 notmuch_database_set_timestamp (notmuch_database_t *notmuch,
626 const char *key, time_t timestamp)
628 Xapian::Document doc;
629 Xapian::WritableDatabase *db;
631 notmuch_private_status_t status;
632 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
635 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
636 fprintf (stderr, "Attempted to update a read-only database.\n");
637 return NOTMUCH_STATUS_READONLY_DATABASE;
640 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
641 db_key = timestamp_db_key (key);
644 status = find_timestamp_document (notmuch, db_key, &doc, &doc_id);
646 doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
647 Xapian::sortable_serialise (timestamp));
649 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
650 char *term = talloc_asprintf (NULL, "%s%s",
651 _find_prefix ("timestamp"), db_key);
655 db->add_document (doc);
657 db->replace_document (doc_id, doc);
660 } catch (const Xapian::Error &error) {
661 fprintf (stderr, "A Xapian exception occurred setting timestamp: %s.\n",
662 error.get_msg().c_str());
663 notmuch->exception_reported = TRUE;
664 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
674 notmuch_database_get_timestamp (notmuch_database_t *notmuch, const char *key)
676 Xapian::Document doc;
678 notmuch_private_status_t status;
682 db_key = timestamp_db_key (key);
685 status = find_timestamp_document (notmuch, db_key, &doc, &doc_id);
687 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
690 ret = Xapian::sortable_unserialise (doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
691 } catch (Xapian::Error &error) {
703 /* Find the thread ID to which the message with 'message_id' belongs.
705 * Returns NULL if no message with message ID 'message_id' is in the
708 * Otherwise, returns a newly talloced string belonging to 'ctx'.
711 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
713 const char *message_id)
715 notmuch_message_t *message;
716 const char *ret = NULL;
718 message = notmuch_database_find_message (notmuch, message_id);
722 ret = talloc_steal (ctx, notmuch_message_get_thread_id (message));
726 notmuch_message_destroy (message);
731 static notmuch_status_t
732 _merge_threads (notmuch_database_t *notmuch,
733 const char *winner_thread_id,
734 const char *loser_thread_id)
736 Xapian::PostingIterator loser, loser_end;
737 notmuch_message_t *message = NULL;
738 notmuch_private_status_t private_status;
739 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
741 find_doc_ids (notmuch, "thread", loser_thread_id, &loser, &loser_end);
743 for ( ; loser != loser_end; loser++) {
744 message = _notmuch_message_create (notmuch, notmuch,
745 *loser, &private_status);
746 if (message == NULL) {
747 ret = COERCE_STATUS (private_status,
748 "Cannot find document for doc_id from query");
752 _notmuch_message_remove_term (message, "thread", loser_thread_id);
753 _notmuch_message_add_term (message, "thread", winner_thread_id);
754 _notmuch_message_sync (message);
756 notmuch_message_destroy (message);
762 notmuch_message_destroy (message);
768 _my_talloc_free_for_g_hash (void *ptr)
773 static notmuch_status_t
774 _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
775 notmuch_message_t *message,
776 notmuch_message_file_t *message_file,
777 const char **thread_id)
779 GHashTable *parents = NULL;
780 const char *refs, *in_reply_to, *in_reply_to_message_id;
781 GList *l, *keys = NULL;
782 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
784 parents = g_hash_table_new_full (g_str_hash, g_str_equal,
785 _my_talloc_free_for_g_hash, NULL);
787 refs = notmuch_message_file_get_header (message_file, "references");
788 parse_references (message, notmuch_message_get_message_id (message),
791 in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
792 parse_references (message, notmuch_message_get_message_id (message),
793 parents, in_reply_to);
795 /* Carefully avoid adding any self-referential in-reply-to term. */
796 in_reply_to_message_id = _parse_message_id (message, in_reply_to, NULL);
797 if (in_reply_to_message_id &&
798 strcmp (in_reply_to_message_id,
799 notmuch_message_get_message_id (message)))
801 _notmuch_message_add_term (message, "replyto",
802 _parse_message_id (message, in_reply_to, NULL));
805 keys = g_hash_table_get_keys (parents);
806 for (l = keys; l; l = l->next) {
807 char *parent_message_id;
808 const char *parent_thread_id;
810 parent_message_id = (char *) l->data;
811 parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
815 if (parent_thread_id == NULL) {
816 _notmuch_message_add_term (message, "reference",
819 if (*thread_id == NULL) {
820 *thread_id = talloc_strdup (message, parent_thread_id);
821 _notmuch_message_add_term (message, "thread", *thread_id);
822 } else if (strcmp (*thread_id, parent_thread_id)) {
823 ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
834 g_hash_table_unref (parents);
839 static notmuch_status_t
840 _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
841 notmuch_message_t *message,
842 const char **thread_id)
844 const char *message_id = notmuch_message_get_message_id (message);
845 Xapian::PostingIterator child, children_end;
846 notmuch_message_t *child_message = NULL;
847 const char *child_thread_id;
848 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
849 notmuch_private_status_t private_status;
851 find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
853 for ( ; child != children_end; child++) {
855 child_message = _notmuch_message_create (message, notmuch,
856 *child, &private_status);
857 if (child_message == NULL) {
858 ret = COERCE_STATUS (private_status,
859 "Cannot find document for doc_id from query");
863 child_thread_id = notmuch_message_get_thread_id (child_message);
864 if (*thread_id == NULL) {
865 *thread_id = talloc_strdup (message, child_thread_id);
866 _notmuch_message_add_term (message, "thread", *thread_id);
867 } else if (strcmp (*thread_id, child_thread_id)) {
868 _notmuch_message_remove_term (child_message, "reference",
870 _notmuch_message_sync (child_message);
871 ret = _merge_threads (notmuch, *thread_id, child_thread_id);
876 notmuch_message_destroy (child_message);
877 child_message = NULL;
882 notmuch_message_destroy (child_message);
887 /* Given a (mostly empty) 'message' and its corresponding
888 * 'message_file' link it to existing threads in the database.
890 * We first look at 'message_file' and its link-relevant headers
891 * (References and In-Reply-To) for message IDs. We also look in the
892 * database for existing message that reference 'message'.
894 * The end result is to call _notmuch_message_ensure_thread_id which
895 * generates a new thread ID if the message doesn't connect to any
898 static notmuch_status_t
899 _notmuch_database_link_message (notmuch_database_t *notmuch,
900 notmuch_message_t *message,
901 notmuch_message_file_t *message_file)
903 notmuch_status_t status;
904 const char *thread_id = NULL;
906 status = _notmuch_database_link_message_to_parents (notmuch, message,
912 status = _notmuch_database_link_message_to_children (notmuch, message,
917 if (thread_id == NULL)
918 _notmuch_message_ensure_thread_id (message);
920 return NOTMUCH_STATUS_SUCCESS;
924 notmuch_database_add_message (notmuch_database_t *notmuch,
925 const char *filename,
926 notmuch_message_t **message_ret)
928 notmuch_message_file_t *message_file;
929 notmuch_message_t *message = NULL;
930 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
931 notmuch_private_status_t private_status;
933 const char *date, *header;
934 const char *from, *to, *subject;
935 char *message_id = NULL;
940 message_file = notmuch_message_file_open (filename);
941 if (message_file == NULL) {
942 ret = NOTMUCH_STATUS_FILE_ERROR;
946 notmuch_message_file_restrict_headers (message_file,
957 /* Before we do any real work, (especially before doing a
958 * potential SHA-1 computation on the entire file's contents),
959 * let's make sure that what we're looking at looks like an
960 * actual email message.
962 from = notmuch_message_file_get_header (message_file, "from");
963 subject = notmuch_message_file_get_header (message_file, "subject");
964 to = notmuch_message_file_get_header (message_file, "to");
966 if ((from == NULL || *from == '\0') &&
967 (subject == NULL || *subject == '\0') &&
968 (to == NULL || *to == '\0'))
970 ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
974 /* Now that we're sure it's mail, the first order of business
975 * is to find a message ID (or else create one ourselves). */
977 header = notmuch_message_file_get_header (message_file, "message-id");
978 if (header && *header != '\0') {
979 message_id = _parse_message_id (message_file, header, NULL);
981 /* So the header value isn't RFC-compliant, but it's
982 * better than no message-id at all. */
983 if (message_id == NULL)
984 message_id = talloc_strdup (message_file, header);
986 /* Reject a Message ID that's too long. */
987 if (message_id && strlen (message_id) + 1 > NOTMUCH_TERM_MAX) {
988 talloc_free (message_id);
993 if (message_id == NULL ) {
994 /* No message-id at all, let's generate one by taking a
995 * hash over the file's contents. */
996 char *sha1 = notmuch_sha1_of_file (filename);
998 /* If that failed too, something is really wrong. Give up. */
1000 ret = NOTMUCH_STATUS_FILE_ERROR;
1004 message_id = talloc_asprintf (message_file,
1005 "notmuch-sha1-%s", sha1);
1009 /* Now that we have a message ID, we get a message object,
1010 * (which may or may not reference an existing document in the
1013 message = _notmuch_message_create_for_message_id (notmuch,
1017 talloc_free (message_id);
1019 if (message == NULL) {
1020 ret = COERCE_STATUS (private_status,
1021 "Unexpected status value from _notmuch_message_create_for_message_id");
1025 /* Is this a newly created message object? */
1026 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1027 _notmuch_message_set_filename (message, filename);
1028 _notmuch_message_add_term (message, "type", "mail");
1030 ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1034 ret = _notmuch_database_link_message (notmuch, message, message_file);
1038 date = notmuch_message_file_get_header (message_file, "date");
1039 _notmuch_message_set_date (message, date);
1041 _notmuch_message_index_file (message, filename);
1043 _notmuch_message_sync (message);
1044 } catch (const Xapian::Error &error) {
1045 fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
1046 error.get_description().c_str());
1047 notmuch->exception_reported = TRUE;
1048 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1054 if (ret == NOTMUCH_STATUS_SUCCESS && message_ret)
1055 *message_ret = message;
1057 notmuch_message_destroy (message);
1061 notmuch_message_file_close (message_file);
1067 _notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
1068 Xapian::TermIterator &end)
1070 const char *prefix = _find_prefix ("tag");
1071 notmuch_tags_t *tags;
1074 /* Currently this iteration is written with the assumption that
1075 * "tag" has a single-character prefix. */
1076 assert (strlen (prefix) == 1);
1078 tags = _notmuch_tags_create (ctx);
1079 if (unlikely (tags == NULL))
1087 if (tag.empty () || tag[0] != *prefix)
1090 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
1095 _notmuch_tags_prepare_iterator (tags);
1101 notmuch_database_get_all_tags (notmuch_database_t *db)
1103 Xapian::TermIterator i, end;
1104 i = db->xapian_db->allterms_begin();
1105 end = db->xapian_db->allterms_end();
1106 return _notmuch_convert_tags(db, i, end);