+/* Given a (mostly empty) 'message' and its corresponding
+ * 'message_file' link it to existing threads in the database.
+ *
+ * We first looke at 'message_file' and its link-relevant headers
+ * (References and In-Reply-To) for message IDs. We also look in the
+ * database for existing message that reference 'message'.p
+ *
+ * The end result is to call _notmuch_message_add_thread_id with one
+ * or more thread IDs to which this message belongs, (including
+ * generating a new thread ID if necessary if the message doesn't
+ * connect to any existing threads).
+ */
+static notmuch_status_t
+_notmuch_database_link_message (notmuch_database_t *notmuch,
+ notmuch_message_t *message,
+ notmuch_message_file_t *message_file)
+{
+ GPtrArray *parents, *thread_ids;
+ const char *refs, *in_reply_to;
+ const char *message_id = notmuch_message_get_message_id (message);
+ unsigned int i;
+
+ parents = g_ptr_array_new ();
+
+ refs = notmuch_message_file_get_header (message_file, "references");
+ parse_references (parents, refs);
+
+ in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
+ parse_references (parents, in_reply_to);
+
+ for (i = 0; i < parents->len; i++)
+ _notmuch_message_add_term (message, "ref",
+ (char *) g_ptr_array_index (parents, i));
+
+ thread_ids = find_thread_ids (notmuch, parents, message_id);
+
+ for (i = 0; i < parents->len; i++)
+ g_free (g_ptr_array_index (parents, i));
+ g_ptr_array_free (parents, TRUE);
+
+ if (thread_ids->len) {
+ GString *thread_id;
+ char *id;
+
+ for (i = 0; i < thread_ids->len; i++) {
+ id = (char *) thread_ids->pdata[i];
+ _notmuch_message_add_thread_id (message, id);
+ if (i == 0)
+ thread_id = g_string_new (id);
+ else
+ g_string_append_printf (thread_id, ",%s", id);
+
+ free (id);
+ }
+ g_string_free (thread_id, TRUE);
+ } else {
+ _notmuch_message_ensure_thread_id (message);
+ }
+
+ g_ptr_array_free (thread_ids, TRUE);
+
+ return NOTMUCH_STATUS_SUCCESS;
+}
+