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