2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
11 #include <lib-ui/lib-ui.h>
16 #include "mutt_idna.h"
18 void mutt_edit_headers(const char *body,
19 HEADER * msg, char *fcc, ssize_t fcclen)
21 char path[_POSIX_PATH_MAX]; /* tempfile used to edit headers + body */
22 char buffer[LONG_STRING];
29 string_list_t *cur, **last = NULL, *tmp;
31 ofp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
37 mutt_env_to_local (msg->env);
38 mutt_write_rfc822_header (ofp, msg->env, NULL, 1);
39 fputc ('\n', ofp); /* tie off the header. */
41 /* now copy the body of the message. */
42 if ((ifp = fopen (body, "r")) == NULL) {
47 mutt_copy_stream (ifp, ofp);
52 if (stat (path, &st) == -1) {
57 mtime = m_decrease_mtime(path, &st);
61 if (mtime == st.st_mtime) {
62 /* the file has not changed! */
68 string_list_wipe(&msg->env->userhdrs);
70 /* Read the temp file back in */
71 if ((ifp = fopen (path, "r")) == NULL) {
76 if ((ofp = safe_fopen (body, "w")) == NULL) {
77 /* intentionally leak a possible temporary file here */
83 n = mutt_read_rfc822_header (ifp, NULL, 1, 0);
84 while ((i = fread (buffer, 1, sizeof (buffer), ifp)) > 0)
85 fwrite (buffer, 1, i, ofp);
90 /* restore old info. */
91 n->references = msg->env->references;
92 msg->env->references = NULL;
94 envelope_delete(&msg->env);
98 if (!msg->env->in_reply_to)
99 string_list_wipe(&msg->env->references);
101 mutt_expand_aliases_env (msg->env);
103 /* search through the user defined headers added to see if either a
104 * fcc: or attach-file: field was specified.
107 cur = msg->env->userhdrs;
108 last = &msg->env->userhdrs;
112 /* keep track of whether or not we see the in-reply-to field. if we did
113 * not, remove the references: field later so that we can generate a new
114 * message based upon this one.
116 if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) {
117 p = vskipspaces(cur->data + 4);
119 m_strcpy(fcc, fcclen, p);
120 mutt_pretty_mailbox (fcc);
124 else if (ascii_strncasecmp ("attach:", cur->data, 7) == 0) {
129 p = vskipspaces(cur->data + 7);
131 if ((q = strpbrk (p, " \t"))) {
132 m_strncpy(path, sizeof(path), p, q - p);
136 m_strcpy(path, sizeof(path), p);
137 mutt_expand_path (path, sizeof (path));
138 if ((bbody = mutt_make_file_attach (path))) {
139 bbody->description = m_strdup(q);
140 for (parts = msg->content; parts->next; parts = parts->next);
144 mutt_pretty_mailbox (path);
145 mutt_error (_("%s: unable to attach file"), path);
152 else if (ascii_strncasecmp("pgp:", cur->data, 4) == 0) {
153 msg->security = mutt_parse_crypt_hdr (cur->data + 4, 0);
155 msg->security |= APPLICATION_PGP;
168 string_list_wipe(&tmp);