2 * Copyright (C) 1999-2002 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., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, 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;
78 mutt_error (_("could not create temporary folder: %s"), strerror (errno));
82 rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
83 ((ctx->magic == M_MBOX || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
86 mx_close_mailbox (&tmpctx, NULL);
90 mutt_error (_("could not write temporary mail folder: %s"), strerror (oerrno));
94 if ((rc = stat (tmp, &sb)) == -1)
96 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
101 * 2002-09-05 me@sigpipe.org
102 * The file the user is going to edit is not a real mbox, so we need to
103 * truncate the last newline in the temp file, which is logically part of
104 * the message separator, and not the body of the message. If we fail to
105 * remove it, the message will grow by one line each time the user edits
108 if (sb.st_size != 0 && truncate (tmp, sb.st_size - 1) == -1)
110 mutt_error (_("could not truncate temporary mail folder: %s"),
115 mtime = mutt_decrease_mtime (tmp, &sb);
117 mutt_edit_file (NONULL(Editor), tmp);
119 if ((rc = stat (tmp, &sb)) == -1)
121 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
127 mutt_message (_("Message file is empty!"));
132 if (sb.st_mtime == mtime)
134 mutt_message (_("Message not modified!"));
139 if ((fp = fopen (tmp, "r")) == NULL)
142 mutt_error (_("Can't open message file: %s"), strerror (errno));
146 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL)
149 mutt_error (_("Can't append to folder: %s"), strerror (errno));
154 cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
156 if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL))
158 if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
159 cf = CH_FROM | CH_FORCE_FROM;
165 * XXX - we have to play games with the message flags to avoid
166 * problematic behaviour with maildir folders.
170 o_read = cur->read; o_old = cur->old;
171 cur->read = cur->old = 0;
172 msg = mx_open_new_message (&tmpctx, cur, of);
173 cur->read = o_read; cur->old = o_old;
177 mutt_error (_("Can't append to folder: %s"), strerror (errno));
178 mx_close_mailbox (&tmpctx, NULL);
182 if ((rc = mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL)) == 0)
184 fputc ('\n', msg->fp);
185 rc = mutt_copy_stream (fp, msg->fp);
188 rc = mx_commit_message (msg, &tmpctx);
189 mx_close_message (&msg);
191 mx_close_mailbox (&tmpctx, NULL);
194 if (fp) safe_fclose (&fp);
201 mutt_set_flag (Context, cur, M_DELETE, 1);
202 mutt_set_flag (Context, cur, M_READ, 1);
204 if (option (OPTDELETEUNTAG))
205 mutt_set_flag (Context, cur, M_TAG, 0);
208 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
214 int mutt_edit_message (CONTEXT *ctx, HEADER *hdr)
219 return edit_one_message (ctx, hdr);
222 for (i = 0; i < ctx->vcount; i++)
225 if (ctx->hdrs[j]->tagged)
227 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)