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.
18 #include <lib-lib/lib-lib.h>
19 #include <lib-ui/curses.h>
23 #include <lib-crypt/crypt.h>
24 #include "mutt_idna.h"
26 void mutt_edit_headers (const char *editor,
28 HEADER * msg, char *fcc, ssize_t fcclen)
30 char path[_POSIX_PATH_MAX]; /* tempfile used to edit headers + body */
31 char buffer[LONG_STRING];
38 string_list_t *cur, **last = NULL, *tmp;
41 if ((ofp = safe_fopen (path, "w")) == NULL) {
46 mutt_env_to_local (msg->env);
47 mutt_write_rfc822_header (ofp, msg->env, NULL, 1, 0);
48 fputc ('\n', ofp); /* tie off the header. */
50 /* now copy the body of the message. */
51 if ((ifp = fopen (body, "r")) == NULL) {
56 mutt_copy_stream (ifp, ofp);
61 if (stat (path, &st) == -1) {
66 mtime = mutt_decrease_mtime (path, &st);
68 mutt_edit_file (editor, path);
70 if (mtime == st.st_mtime) {
71 /* the file has not changed! */
77 string_list_wipe(&msg->env->userhdrs);
79 /* Read the temp file back in */
80 if ((ifp = fopen (path, "r")) == NULL) {
85 if ((ofp = safe_fopen (body, "w")) == NULL) {
86 /* intentionally leak a possible temporary file here */
92 n = mutt_read_rfc822_header (ifp, NULL, 1, 0);
93 while ((i = fread (buffer, 1, sizeof (buffer), ifp)) > 0)
94 fwrite (buffer, 1, i, ofp);
99 /* restore old info. */
100 n->references = msg->env->references;
101 msg->env->references = NULL;
103 envelope_delete(&msg->env);
107 if (!msg->env->in_reply_to)
109 if (!option (OPTNEWSSEND))
111 string_list_wipe(&msg->env->references);
113 mutt_expand_aliases_env (msg->env);
115 /* search through the user defined headers added to see if either a
116 * fcc: or attach-file: field was specified.
119 cur = msg->env->userhdrs;
120 last = &msg->env->userhdrs;
124 /* keep track of whether or not we see the in-reply-to field. if we did
125 * not, remove the references: field later so that we can generate a new
126 * message based upon this one.
128 if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) {
129 p = vskipspaces(cur->data + 4);
131 m_strcpy(fcc, fcclen, p);
132 mutt_pretty_mailbox (fcc);
136 else if (ascii_strncasecmp ("attach:", cur->data, 7) == 0) {
141 p = vskipspaces(cur->data + 7);
143 if ((q = strpbrk (p, " \t"))) {
144 m_strncpy(path, sizeof(path), p, q - p);
148 m_strcpy(path, sizeof(path), p);
149 mutt_expand_path (path, sizeof (path));
150 if ((bbody = mutt_make_file_attach (path))) {
151 bbody->description = m_strdup(q);
152 for (parts = msg->content; parts->next; parts = parts->next);
156 mutt_pretty_mailbox (path);
157 mutt_error (_("%s: unable to attach file"), path);
164 else if (ascii_strncasecmp("pgp:", cur->data, 4) == 0) {
165 msg->security = mutt_parse_crypt_hdr (cur->data + 4, 0);
167 msg->security |= APPLICATION_PGP;
180 string_list_wipe(&tmp);