* 'message_id' in the result (to avoid mass confusion when a single
* message references itself cyclically---and yes, mail messages are
* not infrequent in the wild that do this---don't ask me why).
-*/
-static void
+ *
+ * Return the last reference parsed, if it is not equal to message_id.
+ */
+static char *
parse_references (void *ctx,
const char *message_id,
GHashTable *hash,
char *ref;
if (refs == NULL || *refs == '\0')
- return;
+ return NULL;
while (*refs) {
ref = _parse_message_id (ctx, refs, &refs);
if (ref && strcmp (ref, message_id))
g_hash_table_insert (hash, ref, NULL);
}
+
+ /* The return value of this function is used to add a parent
+ * reference to the database. We should avoid making a message
+ * its own parent, thus the following check.
+ */
+
+ if (ref && strcmp(ref, message_id)) {
+ return ref;
+ } else {
+ return NULL;
+ }
}
notmuch_status_t
{
GHashTable *parents = NULL;
const char *refs, *in_reply_to, *in_reply_to_message_id;
+ const char *last_ref_message_id, *this_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);
+ this_message_id = notmuch_message_get_message_id (message);
refs = notmuch_message_file_get_header (message_file, "references");
- parse_references (message, notmuch_message_get_message_id (message),
- parents, refs);
+ last_ref_message_id = parse_references (message,
+ this_message_id,
+ 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)))
- {
+ in_reply_to_message_id = parse_references (message,
+ this_message_id,
+ parents, in_reply_to);
+
+ /* For the parent of this message, use the last message ID of the
+ * References header, if available. If not, fall back to the
+ * first message ID in the In-Reply-To header. */
+ if (last_ref_message_id) {
+ _notmuch_message_add_term (message, "replyto",
+ last_ref_message_id);
+ } else if (in_reply_to_message_id) {
_notmuch_message_add_term (message, "replyto",
- _parse_message_id (message, in_reply_to, NULL));
+ in_reply_to_message_id);
}
keys = g_hash_table_get_keys (parents);
. ./test-lib.sh
test_begin_subtest "Use References when In-Reply-To is broken"
-test_subtest_known_broken
add_message '[id]="foo@one.com"' \
'[subject]=one'
add_message '[in-reply-to]="mumble"' \
test_expect_equal_json "$output" "$expected"
test_begin_subtest "Prefer References to In-Reply-To"
-test_subtest_known_broken
add_message '[id]="foo@two.com"' \
'[subject]=two'
add_message '[in-reply-to]="<bar@baz.com>"' \
test_expect_equal_json "$output" "$expected"
test_begin_subtest "Use last Reference"
-test_subtest_known_broken
add_message '[id]="foo@four.com"' \
'[subject]="four"'
add_message '[id]="bar@four.com"' \