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