058ec94f4d8b6b1ba3395ae3c6c1c2ddb9e078cc
[apps/madmutt.git] / headers.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <sys/stat.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 #include <lib-lib/lib-lib.h>
19 #include <lib-ui/curses.h>
20
21 #include "mutt.h"
22 #include "alias.h"
23 #include <lib-crypt/crypt.h>
24 #include "mutt_idna.h"
25
26 void mutt_edit_headers (const char *editor,
27                         const char *body,
28                         HEADER * msg, char *fcc, ssize_t fcclen)
29 {
30   char path[_POSIX_PATH_MAX];   /* tempfile used to edit headers + body */
31   char buffer[LONG_STRING];
32   char *p;
33   FILE *ifp, *ofp;
34   int i, keep;
35   ENVELOPE *n;
36   time_t mtime;
37   struct stat st;
38   string_list_t *cur, **last = NULL, *tmp;
39
40   mutt_mktemp (path);
41   if ((ofp = safe_fopen (path, "w")) == NULL) {
42     mutt_perror (path);
43     return;
44   }
45
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. */
49
50   /* now copy the body of the message. */
51   if ((ifp = fopen (body, "r")) == NULL) {
52     mutt_perror (body);
53     return;
54   }
55
56   mutt_copy_stream (ifp, ofp);
57
58   fclose (ifp);
59   fclose (ofp);
60
61   if (stat (path, &st) == -1) {
62     mutt_perror (path);
63     return;
64   }
65
66   mtime = mutt_decrease_mtime (path, &st);
67
68   mutt_edit_file (editor, path);
69   stat (path, &st);
70   if (mtime == st.st_mtime) {
71     /* the file has not changed! */
72     mutt_unlink (path);
73     return;
74   }
75
76   mutt_unlink (body);
77   string_list_wipe(&msg->env->userhdrs);
78
79   /* Read the temp file back in */
80   if ((ifp = fopen (path, "r")) == NULL) {
81     mutt_perror (path);
82     return;
83   }
84
85   if ((ofp = safe_fopen (body, "w")) == NULL) {
86     /* intentionally leak a possible temporary file here */
87     fclose (ifp);
88     mutt_perror (body);
89     return;
90   }
91
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);
95   fclose (ofp);
96   fclose (ifp);
97   mutt_unlink (path);
98
99   /* restore old info. */
100   n->references = msg->env->references;
101   msg->env->references = NULL;
102
103   envelope_delete(&msg->env);
104   msg->env = n;
105   n = NULL;
106
107   if (!msg->env->in_reply_to)
108 #ifdef USE_NNTP
109     if (!option (OPTNEWSSEND))
110 #endif
111       string_list_wipe(&msg->env->references);
112
113   mutt_expand_aliases_env (msg->env);
114
115   /* search through the user defined headers added to see if either a 
116    * fcc: or attach-file: field was specified.  
117    */
118
119   cur = msg->env->userhdrs;
120   last = &msg->env->userhdrs;
121   while (cur) {
122     keep = 1;
123
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.
127      */
128     if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) {
129       p = vskipspaces(cur->data + 4);
130       if (*p) {
131         m_strcpy(fcc, fcclen, p);
132         mutt_pretty_mailbox (fcc);
133       }
134       keep = 0;
135     }
136     else if (ascii_strncasecmp ("attach:", cur->data, 7) == 0) {
137       BODY *bbody;
138       BODY *parts;
139       char *q;
140
141       p = vskipspaces(cur->data + 7);
142       if (*p) {
143         if ((q = strpbrk (p, " \t"))) {
144           m_strncpy(path, sizeof(path), p, q - p);
145           q = vskipspaces(q);
146         }
147         else
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);
153           parts->next = bbody;
154         }
155         else {
156           mutt_pretty_mailbox (path);
157           mutt_error (_("%s: unable to attach file"), path);
158         }
159       }
160       keep = 0;
161     }
162
163
164     else if (ascii_strncasecmp("pgp:", cur->data, 4) == 0) {
165       msg->security = mutt_parse_crypt_hdr (cur->data + 4, 0);
166       if (msg->security)
167         msg->security |= APPLICATION_PGP;
168       keep = 0;
169     }
170
171     if (keep) {
172       last = &cur->next;
173       cur = cur->next;
174     }
175     else {
176       tmp = cur;
177       *last = cur->next;
178       cur = cur->next;
179       tmp->next = NULL;
180       string_list_wipe(&tmp);
181     }
182   }
183 }