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