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