This config variable permits adding a References header to forwarded
messages.

Strictly speaking, this is not condoned by the RFCs, but it does allow
the sent message to be threaded underneath the original forwarded
message, which some users prefer.

To allow the user to change their mind, the References header is added
to the list of headers in the draft email when $edit_headers is set,
but only for the $forward_references case.  The header can be removed,
but editing is not allowed.  The full list of References might be
long, so currently the list is trimmed down to the first three.
---
 headers.c | 11 ++++++++---
 init.h    |  9 +++++++++
 mutt.h    |  1 +
 recvcmd.c | 23 +++++++++++++++++++++++
 send.c    |  8 ++++++++
 sendlib.c | 14 ++++++++++++++
 6 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/headers.c b/headers.c
index d5fca3af..542a1afe 100644
--- a/headers.c
+++ b/headers.c
@@ -142,9 +142,14 @@ int mutt_edit_headers(const char *editor,
        $edit_headers set, we remove References: as they're likely invalid;
        we can simply compare strings as we don't generate References for
        multiple Message-Ids in IRT anyways */
-    if (sctx->msg->env->in_reply_to &&
-        (!n->in_reply_to || mutt_strcmp(n->in_reply_to->data,
-                                        sctx->msg->env->in_reply_to->data) != 
0))
+    if (sctx->msg->env->in_reply_to)
+    {
+      if (!n->in_reply_to ||
+          mutt_strcmp(n->in_reply_to->data, sctx->msg->env->in_reply_to->data) 
!= 0)
+        mutt_free_list(&sctx->msg->env->references);
+    }
+    /* For $forward_references, allow the user to remove the references */
+    else if (sctx->msg->env->references && !n->references)
       mutt_free_list(&sctx->msg->env->references);
 
     /* restore old info. */
diff --git a/init.h b/init.h
index d9fc0378..c0ede52e 100644
--- a/init.h
+++ b/init.h
@@ -1399,6 +1399,15 @@ struct option_t MuttVars[] = {
   { "forw_quote",       DT_SYN,  R_NONE, {.p="forward_quote"}, {.p=0} },
   /*
   */
+  { "forward_references", DT_QUAD, R_NONE, {.l=OPT_FORWREFS}, {.l=MUTT_NO} },
+  /*
+  ** .pp
+  ** This quadoption controls whether or not a \fIReferences:\fP header
+  ** is included in the forwarded email.  Strictly speaking, this is
+  ** not condoned by the RFCs, but it does allow the sent message to
+  ** be threaded underneath the original forwarded message, which some users
+  ** prefer.
+  **/
   { "from",             DT_ADDR, R_NONE, {.p=&From}, {.p=0} },
   /*
   ** .pp
diff --git a/mutt.h b/mutt.h
index cc3b0baa..262c7e54 100644
--- a/mutt.h
+++ b/mutt.h
@@ -309,6 +309,7 @@ enum
   OPT_FORWATTS,
   OPT_FORWEDIT,
   OPT_FORWDECRYPT,
+  OPT_FORWREFS,
   OPT_FCCATTACH,
   OPT_INCLUDE,
   OPT_MFUPTO,
diff --git a/recvcmd.c b/recvcmd.c
index d61b4191..10d79dda 100644
--- a/recvcmd.c
+++ b/recvcmd.c
@@ -656,6 +656,29 @@ static void attach_forward_msgs(FILE * fp, HEADER * hdr,
   tmphdr->env = mutt_new_envelope();
   mutt_make_forward_subject(tmphdr->env, Context, curhdr);
 
+  /* L10N: Used for the $forward_references prompt */
+  if (query_quadoption(OPT_FORWREFS, _("Include References header in 
forward?")) == MUTT_YES)
+  {
+    if (cur)
+      mutt_add_to_reference_headers(tmphdr->env, curhdr->env, NULL, NULL);
+    else
+    {
+      LIST **p = NULL, **q = NULL;
+      int tagged = 0;
+
+      for (i = 0; i < actx->idxlen; i++)
+      {
+        if (actx->idx[i]->content->tagged)
+        {
+          tagged++;
+          mutt_add_to_reference_headers(tmphdr->env, 
actx->idx[i]->content->hdr->env, &p, &q);
+        }
+      }
+      if (tagged > 1 && tmphdr->env->in_reply_to && 
tmphdr->env->in_reply_to->next)
+        mutt_free_list(&tmphdr->env->references);
+    }
+    mutt_free_list(&tmphdr->env->in_reply_to);
+  }
 
   tmpbody = mutt_buffer_pool_get();
 
diff --git a/send.c b/send.c
index 815a8afe..6db871d3 100644
--- a/send.c
+++ b/send.c
@@ -889,7 +889,15 @@ envelope_defaults(ENVELOPE *env, CONTEXT *ctx, HEADER 
*cur, int flags)
     }
   }
   else if (flags & SENDFORWARD)
+  {
+    /* L10N: Used for the $forward_references prompt */
+    if (query_quadoption(OPT_FORWREFS, _("Include References header in 
forward?")) == MUTT_YES)
+    {
+      mutt_make_reference_headers(tag ? NULL : curenv, env, ctx);
+      mutt_free_list(&env->in_reply_to);
+    }
     mutt_make_forward_subject(env, ctx, cur);
+  }
 
   return (0);
 }
diff --git a/sendlib.c b/sendlib.c
index f529d15a..50976525 100644
--- a/sendlib.c
+++ b/sendlib.c
@@ -2442,6 +2442,20 @@ int mutt_write_rfc822_header(FILE *fp, ENVELOPE *env, 
BODY *attach, char *date,
     fputs("MIME-Version: 1.0\n", fp);
     mutt_write_mime_header(attach, fp);
   }
+  else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
+  {
+    /* Only write these out for the $forward_references case,
+     * so the user can decide to remove the references header if they
+     * change their mind.  Reduce the trim size so it doesn't take
+     * up too much space in the editor and is easy to remove.
+     */
+    if (env->references && !env->in_reply_to)
+    {
+      fputs("References:", fp);
+      mutt_write_references(env->references, fp, 3);
+      fputc('\n', fp);
+    }
+  }
 
   if (env->in_reply_to)
   {
-- 
2.55.0

Reply via email to