Rocco Rutte:
[apps/madmutt.git] / editmsg.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.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 /* simple, editor-based message editing */
11
12 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include "mutt.h"
17 #include "copy.h"
18 #include "mailbox.h"
19 #include "mx.h"
20
21 #include "lib/intl.h"
22 #include "lib/str.h"
23
24 #include <sys/stat.h>
25 #include <errno.h>
26
27 #include <time.h>
28
29 /*
30  * return value:
31  * 
32  * 1    message not modified
33  * 0    message edited successfully
34  * -1   error
35  */
36
37 static int edit_one_message (CONTEXT * ctx, HEADER * cur)
38 {
39   char tmp[_POSIX_PATH_MAX];
40   char buff[STRING];
41   int omagic;
42   int oerrno;
43   int rc;
44
45   unsigned short o_read;
46   unsigned short o_old;
47
48   int of, cf;
49
50   CONTEXT tmpctx;
51   MESSAGE *msg;
52
53   FILE *fp = NULL;
54
55   struct stat sb;
56   time_t mtime = 0;
57
58   mutt_mktemp (tmp);
59
60   omagic = DefaultMagic;
61   DefaultMagic = M_MBOX;
62
63   rc = (mx_open_mailbox (tmp, M_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0;
64
65   DefaultMagic = omagic;
66
67   if (rc == -1) {
68     mutt_error (_("could not create temporary folder: %s"), strerror (errno));
69     return -1;
70   }
71
72   rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
73                             ((ctx->magic == M_MBOX
74                               || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
75   oerrno = errno;
76
77   mx_close_mailbox (&tmpctx, NULL);
78
79   if (rc == -1) {
80     mutt_error (_("could not write temporary mail folder: %s"),
81                 strerror (oerrno));
82     goto bail;
83   }
84
85   if (stat (tmp, &sb) == 0)
86     mtime = sb.st_mtime;
87
88   /*
89    * 2002-09-05 me@sigpipe.org
90    * The file the user is going to edit is not a real mbox, so we need to
91    * truncate the last newline in the temp file, which is logically part of
92    * the message separator, and not the body of the message.  If we fail to
93    * remove it, the message will grow by one line each time the user edits
94    * the message.
95    */
96   if (sb.st_size != 0 && truncate (tmp, sb.st_size - 1) == -1) {
97     mutt_error (_("could not truncate temporary mail folder: %s"),
98                 strerror (errno));
99     goto bail;
100   }
101
102   mutt_edit_file (NONULL (Editor), tmp);
103
104   if ((rc = stat (tmp, &sb)) == -1) {
105     mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
106     goto bail;
107   }
108
109   if (sb.st_size == 0) {
110     mutt_message (_("Message file is empty!"));
111     rc = 1;
112     goto bail;
113   }
114
115   if (sb.st_mtime == mtime) {
116     mutt_message (_("Message not modified!"));
117     rc = 1;
118     goto bail;
119   }
120
121   if ((fp = fopen (tmp, "r")) == NULL) {
122     rc = -1;
123     mutt_error (_("Can't open message file: %s"), strerror (errno));
124     goto bail;
125   }
126
127   if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL) {
128     rc = -1;
129     mutt_error (_("Can't append to folder: %s"), strerror (errno));
130     goto bail;
131   }
132
133   of = 0;
134   cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
135
136   if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL)) {
137     if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
138       cf = CH_FROM | CH_FORCE_FROM;
139   }
140   else
141     of = M_ADD_FROM;
142
143   /* 
144    * XXX - we have to play games with the message flags to avoid
145    * problematic behaviour with maildir folders.
146    *
147    */
148
149   o_read = cur->read;
150   o_old = cur->old;
151   cur->read = cur->old = 0;
152   msg = mx_open_new_message (&tmpctx, cur, of);
153   cur->read = o_read;
154   cur->old = o_old;
155
156   if (msg == NULL) {
157     mutt_error (_("Can't append to folder: %s"), strerror (errno));
158     mx_close_mailbox (&tmpctx, NULL);
159     goto bail;
160   }
161
162   if ((rc =
163        mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf,
164                       NULL)) == 0) {
165     fputc ('\n', msg->fp);
166     rc = mutt_copy_stream (fp, msg->fp);
167   }
168
169   rc = mx_commit_message (msg, &tmpctx);
170   mx_close_message (&msg);
171
172   mx_close_mailbox (&tmpctx, NULL);
173
174 bail:
175   if (fp)
176     fclose (fp);
177
178   if (rc >= 0)
179     unlink (tmp);
180
181   if (rc == 0) {
182     mutt_set_flag (Context, cur, M_DELETE, 1);
183     mutt_set_flag (Context, cur, M_READ, 1);
184
185     if (option (OPTDELETEUNTAG))
186       mutt_set_flag (Context, cur, M_TAG, 0);
187   }
188   else if (rc == -1)
189     mutt_message (_("Error. Preserving temporary file: %s"), tmp);
190
191
192   return rc;
193 }
194
195 int mutt_edit_message (CONTEXT * ctx, HEADER * hdr)
196 {
197   int i, j;
198
199   if (hdr)
200     return edit_one_message (ctx, hdr);
201
202
203   for (i = 0; i < ctx->vcount; i++) {
204     j = ctx->v2r[i];
205     if (ctx->hdrs[j]->tagged) {
206       if (edit_one_message (ctx, ctx->hdrs[j]) == -1)
207         return -1;
208     }
209   }
210
211   return 0;
212 }