Rocco Rutte:
[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   {
78     mutt_error (_("could not create temporary folder: %s"), strerror (errno));
79     return -1;
80   }
81
82   rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
83         ((ctx->magic == M_MBOX || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
84   oerrno = errno;
85
86   mx_close_mailbox (&tmpctx, NULL);
87
88   if (rc == -1)
89   {
90     mutt_error (_("could not write temporary mail folder: %s"), 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   {
107     mutt_error (_("could not truncate temporary mail folder: %s"),
108                 strerror (errno));
109     goto bail;
110   }
111
112   mutt_edit_file (NONULL(Editor), tmp);
113
114   if ((rc = stat (tmp, &sb)) == -1)
115   {
116     mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
117     goto bail;
118   }
119   
120   if (sb.st_size == 0)
121   {
122     mutt_message (_("Message file is empty!"));
123     rc = 1;
124     goto bail;
125   }
126
127   if (sb.st_mtime == mtime)
128   {
129     mutt_message (_("Message not modified!"));
130     rc = 1;
131     goto bail;
132   }
133
134   if ((fp = fopen (tmp, "r")) == NULL)
135   {
136     rc = -1;
137     mutt_error (_("Can't open message file: %s"), strerror (errno));
138     goto bail;
139   }
140
141   if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL)
142   {
143     rc = -1;
144     mutt_error (_("Can't append to folder: %s"), strerror (errno));
145     goto bail;
146   }
147
148   of = 0;
149   cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
150   
151   if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL))
152   {
153     if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
154       cf = CH_FROM | CH_FORCE_FROM;
155   }
156   else
157     of = M_ADD_FROM;
158
159   /* 
160    * XXX - we have to play games with the message flags to avoid
161    * problematic behaviour with maildir folders.
162    *
163    */
164
165   o_read = cur->read; o_old = cur->old;
166   cur->read = cur->old = 0;
167   msg = mx_open_new_message (&tmpctx, cur, of);
168   cur->read = o_read; cur->old = o_old;
169
170   if (msg == NULL)
171   {
172     mutt_error (_("Can't append to folder: %s"), strerror (errno));
173     mx_close_mailbox (&tmpctx, NULL);
174     goto bail;
175   }
176
177   if ((rc = mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL)) == 0)
178   {
179     fputc ('\n', msg->fp);
180     rc = mutt_copy_stream (fp, msg->fp);
181   }
182
183   rc = mx_commit_message (msg, &tmpctx);
184   mx_close_message (&msg);
185   
186   mx_close_mailbox (&tmpctx, NULL);
187   
188   bail:
189   if (fp) fclose (fp);
190
191   if (rc >= 0)
192     unlink (tmp);
193
194   if (rc == 0)
195   {
196     mutt_set_flag (Context, cur, M_DELETE, 1);
197     mutt_set_flag (Context, cur, M_READ, 1);
198
199     if (option (OPTDELETEUNTAG))
200       mutt_set_flag (Context, cur, M_TAG, 0);
201   }
202   else if (rc == -1)
203     mutt_message (_("Error. Preserving temporary file: %s"), tmp);
204
205     
206   return rc;
207 }
208
209 int mutt_edit_message (CONTEXT *ctx, HEADER *hdr)
210 {
211   int i, j;
212
213   if (hdr)
214     return edit_one_message (ctx, hdr);
215
216   
217   for (i = 0; i < ctx->vcount; i++)
218   {
219     j = ctx->v2r[i];
220     if (ctx->hdrs[j]->tagged)
221     {
222       if (edit_one_message (ctx, ctx->hdrs[j]) == -1)
223         return -1;
224     }
225   }
226
227   return 0;
228 }