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 */
12 #include <lib-lib/lib-lib.h>
13 #include <lib-ui/curses.h>
14 #include <lib-mx/mx.h>
22 * 1 message not modified
23 * 0 message edited successfully
27 static int edit_one_message (CONTEXT * ctx, HEADER * cur)
29 char tmp[_POSIX_PATH_MAX];
35 unsigned short o_read;
51 omagic = DefaultMagic;
52 DefaultMagic = M_MBOX;
54 rc = (mx_open_mailbox (tmp, M_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0;
56 DefaultMagic = omagic;
59 mutt_error (_("could not create temporary folder: %s"), strerror (errno));
63 rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN |
64 ((ctx->magic == M_MBOX
65 || ctx->magic == M_MMDF) ? 0 : CH_NOSTATUS));
68 mx_close_mailbox (&tmpctx, NULL);
71 mutt_error (_("could not write temporary mail folder: %s"),
76 if (stat (tmp, &sb) == 0) {
82 * 2002-09-05 me@sigpipe.org
83 * The file the user is going to edit is not a real mbox, so we need to
84 * truncate the last newline in the temp file, which is logically part of
85 * the message separator, and not the body of the message. If we fail to
86 * remove it, the message will grow by one line each time the user edits
89 if (size != 0 && truncate (tmp, --size) == -1) {
90 mutt_error (_("could not truncate temporary mail folder: %s"),
97 if ((rc = stat (tmp, &sb)) == -1) {
98 mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno));
102 if (sb.st_size == 0) {
103 mutt_message (_("Message file is empty!"));
108 if (sb.st_mtime == mtime && sb.st_size == size) {
109 mutt_message (_("Message not modified!"));
114 if ((fp = fopen (tmp, "r")) == NULL) {
116 mutt_error (_("Can't open message file: %s"), strerror (errno));
120 if (mx_open_mailbox (ctx->path, M_APPEND, &tmpctx) == NULL) {
122 mutt_error (_("Can't append to folder: %s"), strerror (errno));
127 cf = ((tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF) ? 0 : CH_NOSTATUS);
129 if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL)) {
130 if (tmpctx.magic == M_MBOX || tmpctx.magic == M_MMDF)
131 cf = CH_FROM | CH_FORCE_FROM;
137 * XXX - we have to play games with the message flags to avoid
138 * problematic behaviour with maildir folders.
144 cur->read = cur->old = 0;
145 msg = mx_open_new_message (&tmpctx, cur, of);
150 mutt_error (_("Can't append to folder: %s"), strerror (errno));
151 mx_close_mailbox (&tmpctx, NULL);
156 mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf,
158 fputc ('\n', msg->fp);
159 rc = mutt_copy_stream (fp, msg->fp);
162 rc = mx_commit_message (msg, &tmpctx);
163 mx_close_message (&msg);
165 mx_close_mailbox (&tmpctx, NULL);
174 mutt_set_flag (Context, cur, M_DELETE, 1);
175 mutt_set_flag (Context, cur, M_READ, 1);
177 if (option (OPTDELETEUNTAG))
178 mutt_set_flag (Context, cur, M_TAG, 0);
181 mutt_message (_("Error. Preserving temporary file: %s"), tmp);
187 int mutt_edit_message (CONTEXT * ctx, HEADER * hdr)
192 return edit_one_message (ctx, hdr);
195 for (i = 0; i < ctx->vcount; i++) {
197 if (ctx->hdrs[j]->tagged) {
198 if (edit_one_message (ctx, ctx->hdrs[j]) == -1)