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/curses.h>
15 #include <lib-crypt/crypt.h>
16 #include "mutt_idna.h"
18 void mutt_edit_headers (const char *editor,
20 HEADER * msg, char *fcc, ssize_t fcclen)
22 char path[_POSIX_PATH_MAX]; /* tempfile used to edit headers + body */
23 char buffer[LONG_STRING];
30 string_list_t *cur, **last = NULL, *tmp;
32 ofp = m_tempfile(path, sizeof(path), NONULL(Tempdir), NULL);
38 mutt_env_to_local (msg->env);
39 mutt_write_rfc822_header (ofp, msg->env, NULL, 1, 0);
40 fputc ('\n', ofp); /* tie off the header. */
42 /* now copy the body of the message. */
43 if ((ifp = fopen (body, "r")) == NULL) {
48 mutt_copy_stream (ifp, ofp);
53 if (stat (path, &st) == -1) {
58 mtime = mutt_decrease_mtime (path, &st);
60 mutt_edit_file (editor, path);
62 if (mtime == st.st_mtime) {
63 /* the file has not changed! */
69 string_list_wipe(&msg->env->userhdrs);
71 /* Read the temp file back in */
72 if ((ifp = fopen (path, "r")) == NULL) {
77 if ((ofp = safe_fopen (body, "w")) == NULL) {
78 /* intentionally leak a possible temporary file here */
84 n = mutt_read_rfc822_header (ifp, NULL, 1, 0);
85 while ((i = fread (buffer, 1, sizeof (buffer), ifp)) > 0)
86 fwrite (buffer, 1, i, ofp);
91 /* restore old info. */
92 n->references = msg->env->references;
93 msg->env->references = NULL;
95 envelope_delete(&msg->env);
99 if (!msg->env->in_reply_to)
101 if (!option (OPTNEWSSEND))
103 string_list_wipe(&msg->env->references);
105 mutt_expand_aliases_env (msg->env);
107 /* search through the user defined headers added to see if either a
108 * fcc: or attach-file: field was specified.
111 cur = msg->env->userhdrs;
112 last = &msg->env->userhdrs;
116 /* keep track of whether or not we see the in-reply-to field. if we did
117 * not, remove the references: field later so that we can generate a new
118 * message based upon this one.
120 if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) {
121 p = vskipspaces(cur->data + 4);
123 m_strcpy(fcc, fcclen, p);
124 mutt_pretty_mailbox (fcc);
128 else if (ascii_strncasecmp ("attach:", cur->data, 7) == 0) {
133 p = vskipspaces(cur->data + 7);
135 if ((q = strpbrk (p, " \t"))) {
136 m_strncpy(path, sizeof(path), p, q - p);
140 m_strcpy(path, sizeof(path), p);
141 mutt_expand_path (path, sizeof (path));
142 if ((bbody = mutt_make_file_attach (path))) {
143 bbody->description = m_strdup(q);
144 for (parts = msg->content; parts->next; parts = parts->next);
148 mutt_pretty_mailbox (path);
149 mutt_error (_("%s: unable to attach file"), path);
156 else if (ascii_strncasecmp("pgp:", cur->data, 4) == 0) {
157 msg->security = mutt_parse_crypt_hdr (cur->data + 4, 0);
159 msg->security |= APPLICATION_PGP;
172 string_list_wipe(&tmp);