3 * Prepare an email to be edited
6 * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org>
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 2 of the License, or (at your option) any later
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
24 * @page editmsg Prepare an email to be edited
26 * Prepare an email to be edited
38 #include "mutt/mutt.h"
39 #include "config/lib.h"
40 #include "email/lib.h"
52 * edit_or_view_one_message - Edit an email or view it in an external editor
53 * @param edit true if the message should be editable. If false, changes
54 * to the message (in the editor) will be ignored.
57 * @retval 1 Message not modified
58 * @retval 0 Message edited successfully
61 static int edit_or_view_one_message(bool edit, struct Context *ctx, struct Email *cur)
65 enum MailboxType omagic;
74 struct Message *msg = NULL;
81 mutt_mktemp(tmp, sizeof(tmp));
86 struct Context *tmpctx = mx_mbox_open(NULL, tmp, MUTT_NEWFOLDER);
92 mutt_error(_("could not create temporary folder: %s"), strerror(errno));
98 ((ctx->mailbox->magic == MUTT_MBOX || ctx->mailbox->magic == MUTT_MMDF) ? 0 : CH_NOSTATUS);
99 rc = mutt_append_message(tmpctx, ctx, cur, 0, chflags);
102 mx_mbox_close(&tmpctx, NULL);
106 mutt_error(_("could not write temporary mail folder: %s"), strerror(oerrno));
113 mutt_error(_("Can't stat %s: %s"), tmp, strerror(errno));
117 /* The file the user is going to edit is not a real mbox, so we need to
118 * truncate the last newline in the temp file, which is logically part of
119 * the message separator, and not the body of the message. If we fail to
120 * remove it, the message will grow by one line each time the user edits
123 if (sb.st_size != 0 && truncate(tmp, sb.st_size - 1) == -1)
125 mutt_error(_("could not truncate temporary mail folder: %s"), strerror(errno));
131 /* remove write permissions */
132 rc = mutt_file_chmod_rm_stat(tmp, S_IWUSR | S_IWGRP | S_IWOTH, &sb);
135 mutt_debug(1, "Could not remove write permissions of %s: %s", tmp, strerror(errno));
136 /* Do not bail out here as we are checking afterwards if we should adopt
137 * changes of the temporary file. */
141 /* Do not reuse the stat sb here as it is outdated. */
142 mtime = mutt_file_decrease_mtime(tmp, NULL);
144 mutt_edit_file(NONULL(Editor), tmp);
149 mutt_error(_("Can't stat %s: %s"), tmp, strerror(errno));
155 mutt_message(_("Message file is empty"));
160 if (edit && sb.st_mtime == mtime)
162 mutt_message(_("Message not modified"));
167 if (!edit && sb.st_mtime != mtime)
169 mutt_message(_("Message of read-only mailbox modified! Ignoring changes."));
176 /* stop processing here and skip right to the end */
181 fp = fopen(tmp, "r");
185 mutt_error(_("Can't open message file: %s"), strerror(errno));
189 tmpctx = mx_mbox_open(ctx->mailbox, NULL, MUTT_APPEND);
193 /* L10N: %s is from strerror(errno) */
194 mutt_error(_("Can't append to folder: %s"), strerror(errno));
199 cf = (((tmpctx->mailbox->magic == MUTT_MBOX) || (tmpctx->mailbox->magic == MUTT_MMDF)) ?
203 if (fgets(buf, sizeof(buf), fp) && is_from(buf, NULL, 0, NULL))
205 if ((tmpctx->mailbox->magic == MUTT_MBOX) || (tmpctx->mailbox->magic == MUTT_MMDF))
206 cf = CH_FROM | CH_FORCE_FROM;
211 /* XXX - we have to play games with the message flags to avoid
212 * problematic behavior with maildir folders. */
218 msg = mx_msg_open_new(tmpctx->mailbox, cur, of);
224 mutt_error(_("Can't append to folder: %s"), strerror(errno));
225 mx_mbox_close(&tmpctx, NULL);
229 rc = mutt_copy_hdr(fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL);
232 fputc('\n', msg->fp);
233 mutt_file_copy_stream(fp, msg->fp);
236 rc = mx_msg_commit(tmpctx->mailbox, msg);
237 mx_msg_close(tmpctx->mailbox, &msg);
239 mx_mbox_close(&tmpctx, NULL);
243 mutt_file_fclose(&fp);
250 mutt_set_flag(Context->mailbox, cur, MUTT_DELETE, 1);
251 mutt_set_flag(Context->mailbox, cur, MUTT_PURGE, 1);
252 mutt_set_flag(Context->mailbox, cur, MUTT_READ, 1);
255 mutt_set_flag(Context->mailbox, cur, MUTT_TAG, 0);
258 mutt_message(_("Error. Preserving temporary file: %s"), tmp);
264 * edit_or_view_message - Edit an email or view it in an external editor
265 * @param edit true: Edit the email; false: view the email
266 * @param ctx Mailbox Context
268 * @retval 1 Message not modified
269 * @retval 0 Message edited successfully
272 int edit_or_view_message(bool edit, struct Context *ctx, struct Email *e)
275 return edit_or_view_one_message(edit, ctx, e);
277 for (int i = 0; i < ctx->mailbox->msg_count; i++)
279 if (!message_is_tagged(ctx, i))
282 if (edit_or_view_one_message(edit, ctx, ctx->mailbox->hdrs[i]) == -1)
290 * mutt_edit_message - Edit a message
291 * @param ctx Mailbox Context
293 * @retval 1 Message not modified
294 * @retval 0 Message edited successfully
297 int mutt_edit_message(struct Context *ctx, struct Email *e)
299 return edit_or_view_message(true, ctx, e); /* true means edit */
303 * mutt_view_message - Edit a message
304 * @param ctx Mailbox Context
306 * @retval 1 Message not modified
307 * @retval 0 Message edited successfully
310 int mutt_view_message(struct Context *ctx, struct Email *e)
312 return edit_or_view_message(false, ctx, e); /* false means only view */