2 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
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
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
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.
22 /* simple, editor-based message editing */
41 * 1 message not modified
42 * 0 message edited successfully
46 static int edit_one_message (CONTEXT * ctx, HEADER * cur)
48 char tmp[_POSIX_PATH_MAX];
54 unsigned short o_read;
69 omagic = DefaultMagic;
70 DefaultMagic = M_MBOX;
72 rc = (mx_open_mailbox (tmp, M_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0;
74 DefaultMagic = omagic;
77 mutt_error (_("could not create temporary folder: %s"), strerror (errno));
81 rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
82 ((ctx->magic == M_MBOX
83 || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
86 mx_close_mailbox (&tmpctx, NULL);
89 mutt_error (_("could not write temporary mail folder: %s"),
94 if (stat (tmp, &sb) == 0)
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
105 if (sb.st_size != 0 && truncate (tmp, sb.st_size - 1) == -1) {
106 mutt_error (_("could not truncate temporary mail folder: %s"),
111 mutt_edit_file (NONULL (Editor), tmp);
113 if ((rc = stat (tmp, &sb)) == -1) {
114 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
118 if (sb.st_size == 0) {
119 mutt_message (_("Message file is empty!"));
124 if (sb.st_mtime == mtime) {
125 mutt_message (_("Message not modified!"));
130 if ((fp = fopen (tmp, "r")) == NULL) {
132 mutt_error (_("Can't open message file: %s"), strerror (errno));
136 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL) {
138 mutt_error (_("Can't append to folder: %s"), strerror (errno));
143 cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
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;
153 * XXX - we have to play games with the message flags to avoid
154 * problematic behaviour with maildir folders.
160 cur->read = cur->old = 0;
161 msg = mx_open_new_message (&tmpctx, cur, of);
166 mutt_error (_("Can't append to folder: %s"), strerror (errno));
167 mx_close_mailbox (&tmpctx, NULL);
172 mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf,
174 fputc ('\n', msg->fp);
175 rc = mutt_copy_stream (fp, msg->fp);
178 rc = mx_commit_message (msg, &tmpctx);
179 mx_close_message (&msg);
181 mx_close_mailbox (&tmpctx, NULL);
191 mutt_set_flag (Context, cur, M_DELETE, 1);
192 mutt_set_flag (Context, cur, M_READ, 1);
194 if (option (OPTDELETEUNTAG))
195 mutt_set_flag (Context, cur, M_TAG, 0);
198 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
204 int mutt_edit_message (CONTEXT * ctx, HEADER * hdr)
209 return edit_one_message (ctx, hdr);
212 for (i = 0; i < ctx->vcount; i++) {
214 if (ctx->hdrs[j]->tagged) {
215 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)