next_word is m_strnextsp
[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/macros.h>
19 #include <lib-lib/ascii.h>
20 #include <lib-lib/file.h>
21
22 #include "mutt.h"
23 #include "alias.h"
24 #include <lib-crypt/crypt.h>
25 #include "mutt_idna.h"
26
27 void mutt_edit_headers (const char *editor,
28                         const char *body,
29                         HEADER * msg, char *fcc, ssize_t fcclen)
30 {
31   char path[_POSIX_PATH_MAX];   /* tempfile used to edit headers + body */
32   char buffer[LONG_STRING];
33   char *p;
34   FILE *ifp, *ofp;
35   int i, keep;
36   ENVELOPE *n;
37   time_t mtime;
38   struct stat st;
39   string_list_t *cur, **last = NULL, *tmp;
40
41   mutt_mktemp (path);
42   if ((ofp = safe_fopen (path, "w")) == NULL) {
43     mutt_perror (path);
44     return;
45   }
46
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. */
50
51   /* now copy the body of the message. */
52   if ((ifp = fopen (body, "r")) == NULL) {
53     mutt_perror (body);
54     return;
55   }
56
57   mutt_copy_stream (ifp, ofp);
58
59   fclose (ifp);
60   fclose (ofp);
61
62   if (stat (path, &st) == -1) {
63     mutt_perror (path);
64     return;
65   }
66
67   mtime = mutt_decrease_mtime (path, &st);
68
69   mutt_edit_file (editor, path);
70   stat (path, &st);
71   if (mtime == st.st_mtime) {
72     /* the file has not changed! */
73     mutt_unlink (path);
74     return;
75   }
76
77   mutt_unlink (body);
78   string_list_wipe(&msg->env->userhdrs);
79
80   /* Read the temp file back in */
81   if ((ifp = fopen (path, "r")) == NULL) {
82     mutt_perror (path);
83     return;
84   }
85
86   if ((ofp = safe_fopen (body, "w")) == NULL) {
87     /* intentionally leak a possible temporary file here */
88     fclose (ifp);
89     mutt_perror (body);
90     return;
91   }
92
93   n = mutt_read_rfc822_header (ifp, NULL, 1, 0);
94   while ((i = fread (buffer, 1, sizeof (buffer), ifp)) > 0)
95     fwrite (buffer, 1, i, ofp);
96   fclose (ofp);
97   fclose (ifp);
98   mutt_unlink (path);
99
100   /* restore old info. */
101   n->references = msg->env->references;
102   msg->env->references = NULL;
103
104   envelope_delete(&msg->env);
105   msg->env = n;
106   n = NULL;
107
108   if (!msg->env->in_reply_to)
109 #ifdef USE_NNTP
110     if (!option (OPTNEWSSEND))
111 #endif
112       string_list_wipe(&msg->env->references);
113
114   mutt_expand_aliases_env (msg->env);
115
116   /* search through the user defined headers added to see if either a 
117    * fcc: or attach-file: field was specified.  
118    */
119
120   cur = msg->env->userhdrs;
121   last = &msg->env->userhdrs;
122   while (cur) {
123     keep = 1;
124
125     /* keep track of whether or not we see the in-reply-to field.  if we did
126      * not, remove the references: field later so that we can generate a new
127      * message based upon this one.
128      */
129     if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) {
130       p = vskipspaces(cur->data + 4);
131       if (*p) {
132         m_strcpy(fcc, fcclen, p);
133         mutt_pretty_mailbox (fcc);
134       }
135       keep = 0;
136     }
137     else if (ascii_strncasecmp ("attach:", cur->data, 7) == 0) {
138       BODY *bbody;
139       BODY *parts;
140       char *q;
141
142       p = vskipspaces(cur->data + 7);
143       if (*p) {
144         if ((q = strpbrk (p, " \t"))) {
145           m_strncpy(path, sizeof(path), p, q - p);
146           q = vskipspaces(q);
147         }
148         else
149           m_strcpy(path, sizeof(path), p);
150         mutt_expand_path (path, sizeof (path));
151         if ((bbody = mutt_make_file_attach (path))) {
152           bbody->description = m_strdup(q);
153           for (parts = msg->content; parts->next; parts = parts->next);
154           parts->next = bbody;
155         }
156         else {
157           mutt_pretty_mailbox (path);
158           mutt_error (_("%s: unable to attach file"), path);
159         }
160       }
161       keep = 0;
162     }
163
164
165     else if (ascii_strncasecmp("pgp:", cur->data, 4) == 0) {
166       msg->security = mutt_parse_crypt_hdr (cur->data + 4, 0);
167       if (msg->security)
168         msg->security |= APPLICATION_PGP;
169       keep = 0;
170     }
171
172     if (keep) {
173       last = &cur->next;
174       cur = cur->next;
175     }
176     else {
177       tmp = cur;
178       *last = cur->next;
179       cur = cur->next;
180       tmp->next = NULL;
181       string_list_wipe(&tmp);
182     }
183   }
184 }