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/macros.h>
19 #include <lib-lib/ascii.h>
20 #include <lib-lib/file.h>
21 #include <lib-lib/debug.h>
24 #include <lib-crypt/crypt.h>
25 #include "mutt_idna.h"
27 void mutt_edit_headers (const char *editor,
29 HEADER * msg, char *fcc, ssize_t fcclen)
31 char path[_POSIX_PATH_MAX]; /* tempfile used to edit headers + body */
32 char buffer[LONG_STRING];
39 string_list_t *cur, **last = NULL, *tmp;
42 if ((ofp = safe_fopen (path, "w")) == NULL) {
47 mutt_env_to_local (msg->env);
48 mutt_write_rfc822_header (ofp, msg->env, NULL, 1, 0);
49 fputc ('\n', ofp); /* tie off the header. */
51 /* now copy the body of the message. */
52 if ((ifp = fopen (body, "r")) == NULL) {
57 mutt_copy_stream (ifp, ofp);
62 if (stat (path, &st) == -1) {
67 mtime = mutt_decrease_mtime (path, &st);
69 mutt_edit_file (editor, path);
71 if (mtime == st.st_mtime) {
72 debug_print (1, ("temp file was not modified.\n"));
73 /* the file has not changed! */
79 string_list_wipe(&msg->env->userhdrs);
81 /* Read the temp file back in */
82 if ((ifp = fopen (path, "r")) == NULL) {
87 if ((ofp = safe_fopen (body, "w")) == NULL) {
88 /* intentionally leak a possible temporary file here */
94 n = mutt_read_rfc822_header (ifp, NULL, 1, 0);
95 while ((i = fread (buffer, 1, sizeof (buffer), ifp)) > 0)
96 fwrite (buffer, 1, i, ofp);
101 /* restore old info. */
102 n->references = msg->env->references;
103 msg->env->references = NULL;
105 envelope_delete(&msg->env);
109 if (!msg->env->in_reply_to)
111 if (!option (OPTNEWSSEND))
113 string_list_wipe(&msg->env->references);
115 mutt_expand_aliases_env (msg->env);
117 /* search through the user defined headers added to see if either a
118 * fcc: or attach-file: field was specified.
121 cur = msg->env->userhdrs;
122 last = &msg->env->userhdrs;
126 /* keep track of whether or not we see the in-reply-to field. if we did
127 * not, remove the references: field later so that we can generate a new
128 * message based upon this one.
130 if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) {
131 p = vskipspaces(cur->data + 4);
133 m_strcpy(fcc, fcclen, p);
134 mutt_pretty_mailbox (fcc);
138 else if (ascii_strncasecmp ("attach:", cur->data, 7) == 0) {
143 p = vskipspaces(cur->data + 7);
145 if ((q = strpbrk (p, " \t"))) {
146 m_strncpy(path, sizeof(path), p, q - p);
150 m_strcpy(path, sizeof(path), p);
151 mutt_expand_path (path, sizeof (path));
152 if ((bbody = mutt_make_file_attach (path))) {
153 bbody->description = m_strdup(q);
154 for (parts = msg->content; parts->next; parts = parts->next);
158 mutt_pretty_mailbox (path);
159 mutt_error (_("%s: unable to attach file"), path);
166 else if (ascii_strncasecmp("pgp:", cur->data, 4) == 0) {
167 msg->security = mutt_parse_crypt_hdr (cur->data + 4, 0);
169 msg->security |= APPLICATION_PGP;
182 string_list_wipe(&tmp);