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;
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 (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)
107 mutt_error (_("could not truncate temporary mail folder: %s"),
112 mutt_edit_file (NONULL(Editor), tmp);
114 if ((rc = stat (tmp, &sb)) == -1)
116 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
122 mutt_message (_("Message file is empty!"));
127 if (sb.st_mtime == mtime)
129 mutt_message (_("Message not modified!"));
134 if ((fp = fopen (tmp, "r")) == NULL)
137 mutt_error (_("Can't open message file: %s"), strerror (errno));
141 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL)
144 mutt_error (_("Can't append to folder: %s"), strerror (errno));
149 cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
151 if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL))
153 if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
154 cf = CH_FROM | CH_FORCE_FROM;
160 * XXX - we have to play games with the message flags to avoid
161 * problematic behaviour with maildir folders.
165 o_read = cur->read; o_old = cur->old;
166 cur->read = cur->old = 0;
167 msg = mx_open_new_message (&tmpctx, cur, of);
168 cur->read = o_read; cur->old = o_old;
172 mutt_error (_("Can't append to folder: %s"), strerror (errno));
173 mx_close_mailbox (&tmpctx, NULL);
177 if ((rc = mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL)) == 0)
179 fputc ('\n', msg->fp);
180 rc = mutt_copy_stream (fp, msg->fp);
183 rc = mx_commit_message (msg, &tmpctx);
184 mx_close_message (&msg);
186 mx_close_mailbox (&tmpctx, NULL);
196 mutt_set_flag (Context, cur, M_DELETE, 1);
197 mutt_set_flag (Context, cur, M_READ, 1);
199 if (option (OPTDELETEUNTAG))
200 mutt_set_flag (Context, cur, M_TAG, 0);
203 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
209 int mutt_edit_message (CONTEXT *ctx, HEADER *hdr)
214 return edit_one_message (ctx, hdr);
217 for (i = 0; i < ctx->vcount; i++)
220 if (ctx->hdrs[j]->tagged)
222 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)