-
- return ret;
-}
-
-static void
-_my_talloc_free_for_g_hash (void *ptr)
-{
- talloc_free (ptr);
-}
-
-static notmuch_status_t
-_notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
- notmuch_message_t *message,
- notmuch_message_file_t *message_file,
- const char **thread_id)
-{
- GHashTable *parents = NULL;
- const char *refs, *in_reply_to, *in_reply_to_message_id;
- GList *l, *keys = NULL;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
-
- parents = g_hash_table_new_full (g_str_hash, g_str_equal,
- _my_talloc_free_for_g_hash, NULL);
-
- refs = notmuch_message_file_get_header (message_file, "references");
- parse_references (message, notmuch_message_get_message_id (message),
- parents, refs);
-
- in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
- parse_references (message, notmuch_message_get_message_id (message),
- parents, in_reply_to);
-
- /* Carefully avoid adding any self-referential in-reply-to term. */
- in_reply_to_message_id = _parse_message_id (message, in_reply_to, NULL);
- if (in_reply_to_message_id &&
- strcmp (in_reply_to_message_id,
- notmuch_message_get_message_id (message)))
- {
- _notmuch_message_add_term (message, "replyto",
- _parse_message_id (message, in_reply_to, NULL));
- }
-
- keys = g_hash_table_get_keys (parents);
- for (l = keys; l; l = l->next) {
- char *parent_message_id;
- const char *parent_thread_id;
-
- parent_message_id = (char *) l->data;
-
- _notmuch_message_add_term (message, "reference",
- parent_message_id);
-
- parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
- message,
- parent_message_id);
-
- if (*thread_id == NULL) {
- *thread_id = talloc_strdup (message, parent_thread_id);
- _notmuch_message_add_term (message, "thread", *thread_id);
- } else if (strcmp (*thread_id, parent_thread_id)) {
- ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
- if (ret)
- goto DONE;
- }
- }
-
- DONE:
- if (keys)
- g_list_free (keys);
- if (parents)
- g_hash_table_unref (parents);
-
- return ret;
-}
-
-static notmuch_status_t
-_notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
- notmuch_message_t *message,
- const char **thread_id)
-{
- const char *message_id = notmuch_message_get_message_id (message);
- Xapian::PostingIterator child, children_end;
- notmuch_message_t *child_message = NULL;
- const char *child_thread_id;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
- notmuch_private_status_t private_status;
-
- find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
-
- for ( ; child != children_end; child++) {
-
- child_message = _notmuch_message_create (message, notmuch,
- *child, &private_status);
- if (child_message == NULL) {
- ret = COERCE_STATUS (private_status,
- "Cannot find document for doc_id from query");
- goto DONE;
- }
-
- child_thread_id = notmuch_message_get_thread_id (child_message);
- if (*thread_id == NULL) {
- *thread_id = talloc_strdup (message, child_thread_id);
- _notmuch_message_add_term (message, "thread", *thread_id);
- } else if (strcmp (*thread_id, child_thread_id)) {
- _notmuch_message_remove_term (child_message, "reference",
- message_id);
- _notmuch_message_sync (child_message);
- ret = _merge_threads (notmuch, *thread_id, child_thread_id);
- if (ret)
- goto DONE;
- }
-
- notmuch_message_destroy (child_message);
- child_message = NULL;
- }
-
- DONE:
- if (child_message)
- notmuch_message_destroy (child_message);
-
- return ret;
-}
-
-/* Given a (mostly empty) 'message' and its corresponding
- * 'message_file' link it to existing threads in the database.
- *
- * The first check is in the metadata of the database to see if we
- * have pre-allocated a thread_id in advance for this message, (which
- * would have happened if a message was previously added that
- * referenced this one).
- *
- * Second, we look at 'message_file' and its link-relevant headers
- * (References and In-Reply-To) for message IDs.
- *
- * Finally, we look in the database for existing message that
- * reference 'message'.
- *
- * In all cases, we assign to the current message the first thread_id
- * found (through either parent or child). We will also merge any
- * existing, distinct threads where this message belongs to both,
- * (which is not uncommon when messages are processed out of order).
- *
- * Finally, if no thread ID has been found through parent or child, we
- * call _notmuch_message_generate_thread_id to generate a new thread
- * ID. This should only happen for new, top-level messages, (no
- * References or In-Reply-To header in this message, and no previously
- * added message refers to this message).
- */
-static notmuch_status_t
-_notmuch_database_link_message (notmuch_database_t *notmuch,
- notmuch_message_t *message,
- notmuch_message_file_t *message_file)
-{
- notmuch_status_t status;
- const char *message_id, *thread_id = NULL;
- char *metadata_key;
- string stored_id;
-
- message_id = notmuch_message_get_message_id (message);
- metadata_key = _get_metadata_thread_id_key (message, message_id);
-
- /* Check if we have already seen related messages to this one.
- * If we have then use the thread_id that we stored at that time.
- */
- stored_id = notmuch->xapian_db->get_metadata (metadata_key);
- if (! stored_id.empty()) {
- Xapian::WritableDatabase *db;
-
- db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-
- /* Clear the metadata for this message ID. We don't need it
- * anymore. */
- db->set_metadata (metadata_key, "");
- thread_id = stored_id.c_str();
-
- _notmuch_message_add_term (message, "thread", thread_id);
- }
- talloc_free (metadata_key);
-
- status = _notmuch_database_link_message_to_parents (notmuch, message,
- message_file,
- &thread_id);
- if (status)
- return status;
-
- status = _notmuch_database_link_message_to_children (notmuch, message,
- &thread_id);
- if (status)
- return status;
-
- /* If not part of any existing thread, generate a new thread ID. */
- if (thread_id == NULL) {
- thread_id = _notmuch_database_generate_thread_id (notmuch);
-
- _notmuch_message_add_term (message, "thread", thread_id);
- }
-
- return NOTMUCH_STATUS_SUCCESS;
-}
-
-notmuch_status_t
-notmuch_database_add_message (notmuch_database_t *notmuch,
- const char *filename,
- notmuch_message_t **message_ret)
-{
- notmuch_message_file_t *message_file;
- notmuch_message_t *message = NULL;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
- notmuch_private_status_t private_status;
-
- const char *date, *header;
- const char *from, *to, *subject;
- char *message_id = NULL;
-
- if (message_ret)
- *message_ret = NULL;
-
- ret = _notmuch_database_ensure_writable (notmuch);
- if (ret)
- return ret;
-
- message_file = notmuch_message_file_open (filename);
- if (message_file == NULL)
- return NOTMUCH_STATUS_FILE_ERROR;
-
- notmuch_message_file_restrict_headers (message_file,
- "date",
- "from",
- "in-reply-to",
- "message-id",
- "references",
- "subject",
- "to",
- (char *) NULL);
-
- try {
- /* Before we do any real work, (especially before doing a
- * potential SHA-1 computation on the entire file's contents),
- * let's make sure that what we're looking at looks like an
- * actual email message.
- */
- from = notmuch_message_file_get_header (message_file, "from");
- subject = notmuch_message_file_get_header (message_file, "subject");
- to = notmuch_message_file_get_header (message_file, "to");
-
- if ((from == NULL || *from == '\0') &&
- (subject == NULL || *subject == '\0') &&
- (to == NULL || *to == '\0'))
- {
- ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
- goto DONE;
- }
-
- /* Now that we're sure it's mail, the first order of business
- * is to find a message ID (or else create one ourselves). */
-
- header = notmuch_message_file_get_header (message_file, "message-id");
- if (header && *header != '\0') {
- message_id = _parse_message_id (message_file, header, NULL);
-
- /* So the header value isn't RFC-compliant, but it's
- * better than no message-id at all. */
- if (message_id == NULL)
- message_id = talloc_strdup (message_file, header);
-
- /* If a message ID is too long, substitute its sha1 instead. */
- if (message_id && strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) {
- char *compressed = _message_id_compressed (message_file,
- message_id);
- talloc_free (message_id);
- message_id = compressed;
- }
- }
-
- if (message_id == NULL ) {
- /* No message-id at all, let's generate one by taking a
- * hash over the file's contents. */
- char *sha1 = notmuch_sha1_of_file (filename);
-
- /* If that failed too, something is really wrong. Give up. */
- if (sha1 == NULL) {
- ret = NOTMUCH_STATUS_FILE_ERROR;
- goto DONE;
- }
-
- message_id = talloc_asprintf (message_file,
- "notmuch-sha1-%s", sha1);
- free (sha1);
- }
-
- /* Now that we have a message ID, we get a message object,
- * (which may or may not reference an existing document in the
- * database). */
-
- message = _notmuch_message_create_for_message_id (notmuch,
- message_id,
- &private_status);
-
- talloc_free (message_id);
-
- if (message == NULL) {
- ret = COERCE_STATUS (private_status,
- "Unexpected status value from _notmuch_message_create_for_message_id");
- goto DONE;
- }
-
- _notmuch_message_add_filename (message, filename);
-
- /* Is this a newly created message object? */
- if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
- _notmuch_message_add_term (message, "type", "mail");
-
- ret = _notmuch_database_link_message (notmuch, message,
- message_file);
- if (ret)
- goto DONE;
-
- date = notmuch_message_file_get_header (message_file, "date");
- _notmuch_message_set_date (message, date);
-
- _notmuch_message_index_file (message, filename);
- } else {
- ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
- }
-
- _notmuch_message_sync (message);
- } catch (const Xapian::Error &error) {
- fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
- error.get_msg().c_str());
- notmuch->exception_reported = TRUE;
- ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
- goto DONE;
- }
-
- DONE:
- if (message) {
- if ((ret == NOTMUCH_STATUS_SUCCESS ||
- ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) && message_ret)
- *message_ret = message;
- else
- notmuch_message_destroy (message);