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