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 */
37 * 1 message not modified
38 * 0 message edited successfully
42 static int edit_one_message (CONTEXT *ctx, HEADER *cur)
44 char tmp[_POSIX_PATH_MAX];
50 unsigned short o_read;
65 omagic = DefaultMagic;
66 DefaultMagic = M_MBOX;
68 rc = (mx_open_mailbox (tmp, M_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0;
70 DefaultMagic = omagic;
74 mutt_error (_("could not create temporary folder: %s"), strerror (errno));
78 rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN | CH_NOSTATUS); oerrno = errno;
80 mx_close_mailbox (&tmpctx, NULL);
84 mutt_error (_("could not write temporary mail folder: %s"), strerror (oerrno));
88 if (stat (tmp, &sb) == 0)
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
99 if (sb.st_size != 0 && truncate (tmp, sb.st_size - 1) == -1)
101 mutt_error (_("could not truncate temporary mail folder: %s"),
106 mutt_edit_file (NONULL(Editor), tmp);
108 if ((rc = stat (tmp, &sb)) == -1)
110 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
116 mutt_message (_("Message file is empty!"));
121 if (sb.st_mtime == mtime)
123 mutt_message (_("Message not modified!"));
128 if ((fp = fopen (tmp, "r")) == NULL)
131 mutt_error (_("Can't open message file: %s"), strerror (errno));
135 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL)
138 mutt_error (_("Can't append to folder: %s"), strerror (errno));
144 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.
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;
165 mutt_error (_("Can't append to folder: %s"), strerror (errno));
166 mx_close_mailbox (&tmpctx, NULL);
170 if ((rc = mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOSTATUS | CH_NOLEN | cf, NULL)) == 0)
172 fputc ('\n', msg->fp);
173 rc = mutt_copy_stream (fp, msg->fp);
176 rc = mx_commit_message (msg, &tmpctx);
177 mx_close_message (&msg);
179 mx_close_mailbox (&tmpctx, NULL);
189 mutt_set_flag (Context, cur, M_DELETE, 1);
190 mutt_set_flag (Context, cur, M_READ, 1);
192 if (option (OPTDELETEUNTAG))
193 mutt_set_flag (Context, cur, M_TAG, 0);
196 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
202 int mutt_edit_message (CONTEXT *ctx, HEADER *hdr)
207 return edit_one_message (ctx, hdr);
210 for (i = 0; i < ctx->vcount; i++)
213 if (ctx->hdrs[j]->tagged)
215 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)