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 */
31 * 1 message not modified
32 * 0 message edited successfully
36 static int edit_one_message (CONTEXT * ctx, HEADER * cur)
38 char tmp[_POSIX_PATH_MAX];
44 unsigned short o_read;
59 omagic = DefaultMagic;
60 DefaultMagic = M_MBOX;
62 rc = (mx_open_mailbox (tmp, M_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0;
64 DefaultMagic = omagic;
67 mutt_error (_("could not create temporary folder: %s"), strerror (errno));
71 rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
72 ((ctx->magic == M_MBOX
73 || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
76 mx_close_mailbox (&tmpctx, NULL);
79 mutt_error (_("could not write temporary mail folder: %s"),
84 if (stat (tmp, &sb) == 0)
88 * 2002-09-05 me@sigpipe.org
89 * The file the user is going to edit is not a real mbox, so we need to
90 * truncate the last newline in the temp file, which is logically part of
91 * the message separator, and not the body of the message. If we fail to
92 * remove it, the message will grow by one line each time the user edits
95 if (sb.st_size != 0 && truncate (tmp, sb.st_size - 1) == -1) {
96 mutt_error (_("could not truncate temporary mail folder: %s"),
101 mutt_edit_file (NONULL (Editor), tmp);
103 if ((rc = stat (tmp, &sb)) == -1) {
104 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
108 if (sb.st_size == 0) {
109 mutt_message (_("Message file is empty!"));
114 if (sb.st_mtime == mtime) {
115 mutt_message (_("Message not modified!"));
120 if ((fp = fopen (tmp, "r")) == NULL) {
122 mutt_error (_("Can't open message file: %s"), strerror (errno));
126 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL) {
128 mutt_error (_("Can't append to folder: %s"), strerror (errno));
133 cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
135 if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL)) {
136 if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
137 cf = CH_FROM | CH_FORCE_FROM;
143 * XXX - we have to play games with the message flags to avoid
144 * problematic behaviour with maildir folders.
150 cur->read = cur->old = 0;
151 msg = mx_open_new_message (&tmpctx, cur, of);
156 mutt_error (_("Can't append to folder: %s"), strerror (errno));
157 mx_close_mailbox (&tmpctx, NULL);
162 mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf,
164 fputc ('\n', msg->fp);
165 rc = mutt_copy_stream (fp, msg->fp);
168 rc = mx_commit_message (msg, &tmpctx);
169 mx_close_message (&msg);
171 mx_close_mailbox (&tmpctx, NULL);
181 mutt_set_flag (Context, cur, M_DELETE, 1);
182 mutt_set_flag (Context, cur, M_READ, 1);
184 if (option (OPTDELETEUNTAG))
185 mutt_set_flag (Context, cur, M_TAG, 0);
188 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
194 int mutt_edit_message (CONTEXT * ctx, HEADER * hdr)
199 return edit_one_message (ctx, hdr);
202 for (i = 0; i < ctx->vcount; i++) {
204 if (ctx->hdrs[j]->tagged) {
205 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)