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