2 * Copyright notice from original mutt:
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* simple, editor-based message editing */
16 #include <lib-lib/macros.h>
17 #include <lib-lib/file.h>
18 #include <lib-lib/str.h>
33 * 1 message not modified
34 * 0 message edited successfully
38 static int edit_one_message (CONTEXT * ctx, HEADER * cur)
40 char tmp[_POSIX_PATH_MAX];
46 unsigned short o_read;
62 omagic = DefaultMagic;
63 DefaultMagic = M_MBOX;
65 rc = (mx_open_mailbox (tmp, M_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0;
67 DefaultMagic = omagic;
70 mutt_error (_("could not create temporary folder: %s"), strerror (errno));
74 rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
75 ((ctx->magic == M_MBOX
76 || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
79 mx_close_mailbox (&tmpctx, NULL);
82 mutt_error (_("could not write temporary mail folder: %s"),
87 if (stat (tmp, &sb) == 0) {
93 * 2002-09-05 me@sigpipe.org
94 * The file the user is going to edit is not a real mbox, so we need to
95 * truncate the last newline in the temp file, which is logically part of
96 * the message separator, and not the body of the message. If we fail to
97 * remove it, the message will grow by one line each time the user edits
100 if (size != 0 && truncate (tmp, --size) == -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) {
109 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
113 if (sb.st_size == 0) {
114 mutt_message (_("Message file is empty!"));
119 if (sb.st_mtime == mtime && sb.st_size == size) {
120 mutt_message (_("Message not modified!"));
125 if ((fp = fopen (tmp, "r")) == NULL) {
127 mutt_error (_("Can't open message file: %s"), strerror (errno));
131 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL) {
133 mutt_error (_("Can't append to folder: %s"), strerror (errno));
138 cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
140 if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL)) {
141 if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
142 cf = CH_FROM | CH_FORCE_FROM;
148 * XXX - we have to play games with the message flags to avoid
149 * problematic behaviour with maildir folders.
155 cur->read = cur->old = 0;
156 msg = mx_open_new_message (&tmpctx, cur, of);
161 mutt_error (_("Can't append to folder: %s"), strerror (errno));
162 mx_close_mailbox (&tmpctx, NULL);
167 mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf,
169 fputc ('\n', msg->fp);
170 rc = mutt_copy_stream (fp, msg->fp);
173 rc = mx_commit_message (msg, &tmpctx);
174 mx_close_message (&msg);
176 mx_close_mailbox (&tmpctx, NULL);
186 mutt_set_flag (Context, cur, M_DELETE, 1);
187 mutt_set_flag (Context, cur, M_READ, 1);
189 if (option (OPTDELETEUNTAG))
190 mutt_set_flag (Context, cur, M_TAG, 0);
193 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
199 int mutt_edit_message (CONTEXT * ctx, HEADER * hdr)
204 return edit_one_message (ctx, hdr);
207 for (i = 0; i < ctx->vcount; i++) {
209 if (ctx->hdrs[j]->tagged) {
210 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)