+static const char *
+skip_re_in_subject (const char *subject)
+{
+ const char *s = subject;
+
+ while (*s) {
+ while (*s && isspace (*s))
+ s++;
+ if (strncasecmp (s, "re:", 3) == 0)
+ s += 3;
+ else
+ break;
+ }
+
+ return s;
+}
+
+static void
+find_messages_by_term (Xapian::Database db,
+ const char *prefix_name,
+ const char *value,
+ Xapian::PostingIterator *begin,
+ Xapian::PostingIterator *end)
+{
+ Xapian::PostingIterator i;
+ char *term;
+
+ term = g_strdup_printf ("%s%s", find_prefix (prefix_name), value);
+
+ *begin = db.postlist_begin (term);
+
+ if (end)
+ *end = db.postlist_end (term);
+
+ free (term);
+}
+
+Xapian::Document
+find_message_by_docid (Xapian::Database db, Xapian::docid docid)
+{
+ return db.get_document (docid);
+}
+
+Xapian::Document
+find_message_by_message_id (Xapian::Database db, const char *message_id)
+{
+ Xapian::PostingIterator i, end;
+
+ find_messages_by_term (db, "msgid", message_id, &i, &end);
+
+ if (i != end)
+ return find_message_by_docid (db, *i);
+ else
+ return Xapian::Document ();
+}
+
+static void
+insert_thread_id (GHashTable *thread_ids, Xapian::Document doc)
+{
+ string value_string;
+ const char *value;
+
+ value_string = doc.get_value (NOTMUCH_VALUE_THREAD);
+ value = value_string.c_str();
+ if (strlen (value))
+ g_hash_table_insert (thread_ids, strdup (value), NULL);
+}
+
+static char *
+find_thread_id (Xapian::Database db,
+ GPtrArray *parents,
+ const char *message_id)
+{
+ Xapian::PostingIterator child, children_end;
+ Xapian::Document doc;
+ GHashTable *thread_ids;
+ GList *keys, *l;
+ GString *result = NULL;
+ unsigned int i;
+ const char *parent_message_id;
+
+ thread_ids = g_hash_table_new (g_str_hash, g_str_equal);
+
+ find_messages_by_term (db, "ref", message_id, &child, &children_end);
+ for ( ; child != children_end; child++) {
+ doc = find_message_by_docid (db, *child);
+ insert_thread_id (thread_ids, doc);
+ }
+
+ for (i = 0; i < parents->len; i++) {
+ parent_message_id = (char *) g_ptr_array_index (parents, i);
+ doc = find_message_by_message_id (db, parent_message_id);
+ insert_thread_id (thread_ids, doc);
+ }
+
+ keys = g_hash_table_get_keys (thread_ids);
+ for (l = keys; l; l = l->next) {
+ char *id = (char *) l->data;
+ if (result == NULL) {
+ result = g_string_new (id);
+ } else {
+ g_string_append_printf (result, ",%s", id);
+ }
+ free (id);
+ }
+
+ if (result)
+ return g_string_free (result, FALSE);
+ else
+ return NULL;
+}
+
+/* Add a term for each message-id in the References header of the
+ * message. */
+static void
+parse_references (GPtrArray *array,
+ const char *refs)
+{
+ const char *end, *next;
+
+ if (refs == NULL)
+ return;
+
+ while (*refs) {
+ while (*refs && isspace (*refs))
+ refs++;
+ if (*refs == '<')
+ refs++;
+ end = refs;
+ while (*end && !isspace (*end))
+ end++;
+ next = end;
+ end--;
+ if (end > refs && *end == '>')
+ end--;
+ if (end > refs) {
+ g_ptr_array_add (array, g_strndup (refs, end - refs + 1));
+ }
+ refs = next;
+ }
+}
+
+/* Given a string representing the body of a message, generate terms
+ * for it, (skipping quoted portions and signatures). */
+static void
+gen_terms_body_str (Xapian::TermGenerator term_gen,
+ char *body)
+{
+ char *line, *line_end, *next_line;
+
+ if (body == NULL)
+ return;
+
+ next_line = body;
+
+ while (1) {
+ line = next_line;
+ if (*line == '\0')
+ break;
+
+ next_line = strchr (line, '\n');
+ if (next_line == NULL) {
+ next_line = line + strlen (line);
+ }
+ line_end = next_line - 1;
+
+ /* Trim whitespace. */
+ while (*next_line && isspace (*next_line))
+ next_line++;
+
+ /* Skip lines that are quotes. */
+ if (*line == '>')
+ continue;
+
+ /* Also skip lines introducing a quote on the next line. */
+ while (line_end > line && isspace (*line_end))
+ line_end--;
+
+ if (*line_end == ':' && *next_line == '>')
+ continue;
+
+ /* Finally, bail as soon as we see a signature. */
+ /* XXX: Should only do this if "near" the end of the message. */
+ if (strncmp (line, "-- ", 3) == 0 ||
+ strncmp (line, "----------", 10) == 0 ||
+ strncmp (line, "__________", 10) == 0)
+ break;
+
+ *(line_end + 1) = '\0';
+ gen_terms (term_gen, "body", line);
+ }
+}
+
+
+/* Callback to generate terms for each mime part of a message. */
+static void
+gen_terms_part (GMimeObject *parent,
+ GMimeObject *part,
+ gpointer user_data)
+{
+ Xapian::TermGenerator *term_gen = (Xapian::TermGenerator *) user_data;
+ GMimeStream *stream;
+ GMimeDataWrapper *wrapper;
+ GByteArray *byte_array;
+ char *body;
+
+ if (! GMIME_IS_PART (part)) {
+ fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n",
+ g_type_name (G_OBJECT_TYPE (part)));
+ return;
+ }
+
+ byte_array = g_byte_array_new ();
+
+ stream = g_mime_stream_mem_new_with_byte_array (byte_array);
+ g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
+ wrapper = g_mime_part_get_content_object (GMIME_PART (part));
+ g_mime_data_wrapper_write_to_stream (wrapper, stream);
+
+ g_object_unref (stream);
+
+ body = (char *) g_byte_array_free (byte_array, FALSE);
+
+ gen_terms_body_str (*term_gen, body);
+
+ free (body);
+}
+
+static void
+index_file (Xapian::WritableDatabase db,
+ Xapian::TermGenerator term_gen,
+ const char *filename)