NeoMutt  2023-11-03-107-g582dc1
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
editmsg.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <errno.h>
31#include <stdbool.h>
32#include <stdio.h>
33#include <string.h>
34#include <sys/stat.h>
35#include <unistd.h>
36#include "mutt/lib.h"
37#include "config/lib.h"
38#include "email/lib.h"
39#include "core/lib.h"
40#include "gui/lib.h"
41#include "mutt.h"
42#include "copy.h"
43#include "mx.h"
44#include "protos.h"
45
55static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
56{
57 char buf[256] = { 0 };
58 int rc;
59 FILE *fp = NULL;
60 struct stat st = { 0 };
61 bool old_append = m->append;
62
63 struct Buffer *fname = buf_pool_get();
64 buf_mktemp(fname);
65
66 // Temporarily force $mbox_type to be MUTT_MBOX
67 const unsigned char c_mbox_type = cs_subset_enum(NeoMutt->sub, "mbox_type");
68 cs_subset_str_native_set(NeoMutt->sub, "mbox_type", MUTT_MBOX, NULL);
69
70 struct Mailbox *m_fname = mx_path_resolve(buf_string(fname));
71 if (!mx_mbox_open(m_fname, MUTT_NEWFOLDER))
72 {
73 mutt_error(_("could not create temporary folder: %s"), strerror(errno));
74 buf_pool_release(&fname);
75 mailbox_free(&m_fname);
76 return -1;
77 }
78
79 cs_subset_str_native_set(NeoMutt->sub, "mbox_type", c_mbox_type, NULL);
80
81 const CopyHeaderFlags chflags = CH_NOLEN |
82 (((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) ?
85 rc = mutt_append_message(m_fname, m, e, NULL, MUTT_CM_NO_FLAGS, chflags);
86 int oerrno = errno;
87
88 mx_mbox_close(m_fname);
89 mailbox_free(&m_fname);
90
91 if (rc == -1)
92 {
93 mutt_error(_("could not write temporary mail folder: %s"), strerror(oerrno));
94 goto bail;
95 }
96
97 rc = stat(buf_string(fname), &st);
98 if (rc == -1)
99 {
100 mutt_error(_("Can't stat %s: %s"), buf_string(fname), strerror(errno));
101 goto bail;
102 }
103
104 /* The file the user is going to edit is not a real mbox, so we need to
105 * truncate the last newline in the temp file, which is logically part of
106 * the message separator, and not the body of the message. If we fail to
107 * remove it, the message will grow by one line each time the user edits
108 * the message. */
109 if ((st.st_size != 0) && (truncate(buf_string(fname), st.st_size - 1) == -1))
110 {
111 rc = -1;
112 mutt_error(_("could not truncate temporary mail folder: %s"), strerror(errno));
113 goto bail;
114 }
115
116 if (action == EVM_VIEW)
117 {
118 /* remove write permissions */
119 rc = mutt_file_chmod_rm_stat(buf_string(fname), S_IWUSR | S_IWGRP | S_IWOTH, &st);
120 if (rc == -1)
121 {
122 mutt_debug(LL_DEBUG1, "Could not remove write permissions of %s: %s",
123 buf_string(fname), strerror(errno));
124 /* Do not bail out here as we are checking afterwards if we should adopt
125 * changes of the temporary file. */
126 }
127 }
128
129 /* re-stat after the truncate, to avoid false "modified" bugs */
130 rc = stat(buf_string(fname), &st);
131 if (rc == -1)
132 {
133 mutt_error(_("Can't stat %s: %s"), buf_string(fname), strerror(errno));
134 goto bail;
135 }
136
137 /* Do not reuse the stat st here as it is outdated. */
138 time_t mtime = mutt_file_decrease_mtime(buf_string(fname), NULL);
139 if (mtime == (time_t) -1)
140 {
141 rc = -1;
142 mutt_perror("%s", buf_string(fname));
143 goto bail;
144 }
145
146 const char *const c_editor = cs_subset_string(NeoMutt->sub, "editor");
147 mutt_edit_file(NONULL(c_editor), buf_string(fname));
148
149 rc = stat(buf_string(fname), &st);
150 if (rc == -1)
151 {
152 mutt_error(_("Can't stat %s: %s"), buf_string(fname), strerror(errno));
153 goto bail;
154 }
155
156 if (st.st_size == 0)
157 {
158 mutt_message(_("Message file is empty"));
159 rc = 1;
160 goto bail;
161 }
162
163 if ((action == EVM_EDIT) && (st.st_mtime == mtime))
164 {
165 mutt_message(_("Message not modified"));
166 rc = 1;
167 goto bail;
168 }
169
170 if ((action == EVM_VIEW) && (st.st_mtime != mtime))
171 {
172 mutt_message(_("Message of read-only mailbox modified! Ignoring changes."));
173 rc = 1;
174 goto bail;
175 }
176
177 if (action == EVM_VIEW)
178 {
179 /* stop processing here and skip right to the end */
180 rc = 1;
181 goto bail;
182 }
183
184 fp = fopen(buf_string(fname), "r");
185 if (!fp)
186 {
187 rc = -1;
188 mutt_error(_("Can't open message file: %s"), strerror(errno));
189 goto bail;
190 }
191
193 {
194 rc = -1;
195 /* L10N: %s is from strerror(errno) */
196 mutt_error(_("Can't append to folder: %s"), strerror(errno));
197 goto bail;
198 }
200 CopyHeaderFlags cf = (((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) ? CH_NO_FLAGS : CH_NOSTATUS);
201
202 if (fgets(buf, sizeof(buf), fp) && is_from(buf, NULL, 0, NULL))
203 {
204 if ((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF))
205 cf = CH_FROM | CH_FORCE_FROM;
206 }
207 else
208 {
209 of = MUTT_ADD_FROM;
210 }
211
212 /* XXX - we have to play games with the message flags to avoid
213 * problematic behavior with maildir folders. */
214
215 bool o_read = e->read;
216 bool o_old = e->old;
217 e->read = false;
218 e->old = false;
219 struct Message *msg = mx_msg_open_new(m, e, of);
220 e->read = o_read;
221 e->old = o_old;
222
223 if (!msg)
224 {
225 rc = -1;
226 mutt_error(_("Can't append to folder: %s"), strerror(errno));
227 mx_mbox_close(m);
228 goto bail;
229 }
230
231 rc = mutt_copy_hdr(fp, msg->fp, 0, st.st_size, CH_NOLEN | cf, NULL, 0);
232 if (rc == 0)
233 {
234 fputc('\n', msg->fp);
236 }
237
238 rc = mx_msg_commit(m, msg);
239 mx_msg_close(m, &msg);
240
241 mx_mbox_close(m);
243
244bail:
246
247 if (rc >= 0)
248 unlink(buf_string(fname));
249
250 if (rc == 0)
251 {
252 mutt_set_flag(m, e, MUTT_DELETE, true, true);
253 mutt_set_flag(m, e, MUTT_PURGE, true, true);
254 mutt_set_flag(m, e, MUTT_READ, true, true);
255
256 const bool c_delete_untag = cs_subset_bool(NeoMutt->sub, "delete_untag");
257 if (c_delete_untag)
258 mutt_set_flag(m, e, MUTT_TAG, false, true);
259 }
260 else if (rc == -1)
261 {
262 mutt_message(_("Error. Preserving temporary file: %s"), buf_string(fname));
263 }
264
265 m->append = old_append;
266
267 buf_pool_release(&fname);
268 return rc;
269}
270
280int mutt_ev_message(struct Mailbox *m, struct EmailArray *ea, enum EvMessage action)
281{
282 struct Email **ep = NULL;
283 ARRAY_FOREACH(ep, ea)
284 {
285 struct Email *e = *ep;
286 if (ev_message(action, m, e) == -1)
287 return -1;
288 }
289
290 return 0;
291}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:211
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:72
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
Convenience wrapper for the config headers.
int mutt_append_message(struct Mailbox *m_dst, struct Mailbox *m_src, struct Email *e, struct Message *msg, CopyMessageFlags cmflags, CopyHeaderFlags chflags)
Append a message.
Definition: copy.c:959
int mutt_copy_hdr(FILE *fp_in, FILE *fp_out, LOFF_T off_start, LOFF_T off_end, CopyHeaderFlags chflags, const char *prefix, int wraplen)
Copy header from one file to another.
Definition: copy.c:106
Duplicate the structure of an entire email.
#define CH_NOSTATUS
Suppress the status and x-status fields.
Definition: copy.h:58
#define CH_FROM
Retain the "From " message separator?
Definition: copy.h:56
uint32_t CopyHeaderFlags
Flags for mutt_copy_header(), e.g. CH_UPDATE.
Definition: copy.h:50
#define CH_FORCE_FROM
Give CH_FROM precedence over CH_WEED?
Definition: copy.h:66
#define MUTT_CM_NO_FLAGS
No flags are set.
Definition: copy.h:35
#define CH_NO_FLAGS
No flags are set.
Definition: copy.h:51
#define CH_NOLEN
Don't write Content-Length: and Lines:
Definition: copy.h:64
Convenience wrapper for the core headers.
void mutt_edit_file(const char *editor, const char *file)
Let the user edit a file.
Definition: curs_lib.c:113
static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e)
Edit an email or view it in an external editor.
Definition: editmsg.c:55
int mutt_ev_message(struct Mailbox *m, struct EmailArray *ea, enum EvMessage action)
Edit or view a message.
Definition: editmsg.c:280
Structs that make up an email.
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:262
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
Decrease a file's modification time by 1 second.
Definition: file.c:1031
int mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st)
Remove permissions from a file.
Definition: file.c:1214
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition: flags.c:53
bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
Is a string a 'From' header line?
Definition: from.c:48
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
Convenience wrapper for the gui headers.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:90
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:46
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:45
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
Many unsorted constants and some structs.
@ MUTT_READ
Messages that have been read.
Definition: mutt.h:72
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition: mutt.h:76
@ MUTT_TAG
Tagged messages.
Definition: mutt.h:79
@ MUTT_DELETE
Messages to be deleted.
Definition: mutt.h:74
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition: mx.c:1185
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:284
void mx_mbox_reset_check(void)
Reset last mail check time.
Definition: mx.c:1126
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1034
int mx_msg_commit(struct Mailbox *m, struct Message *msg)
Commit a message to a folder - Wrapper for MxOps::msg_commit()
Definition: mx.c:1164
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1641
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:593
API for mailboxes.
uint8_t MsgOpenFlags
Flags for mx_msg_open_new(), e.g. MUTT_ADD_FROM.
Definition: mx.h:38
#define MUTT_ADD_FROM
add a From_ line
Definition: mx.h:40
#define MUTT_MSG_NO_FLAGS
No flags are set.
Definition: mx.h:39
#define MUTT_NEWFOLDER
Create a new folder - same as MUTT_APPEND, but uses mutt_file_fopen() with mode "w" for mbox-style fo...
Definition: mxapi.h:45
#define MUTT_APPEND
Open mailbox for appending messages.
Definition: mxapi.h:42
#define MUTT_QUIET
Do not print any messages.
Definition: mxapi.h:44
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
Prototypes for many functions.
EvMessage
Edit or View a message.
Definition: protos.h:51
@ EVM_VIEW
View the message.
Definition: protos.h:52
@ EVM_EDIT
Edit the message.
Definition: protos.h:53
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:34
The envelope/body of an email.
Definition: email.h:37
bool read
Email is read.
Definition: email.h:48
bool old
Email is seen, but unread.
Definition: email.h:47
A mailbox.
Definition: mailbox.h:79
bool append
Mailbox is opened in append mode.
Definition: mailbox.h:108
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
A local copy of an email.
Definition: message.h:34
FILE * fp
pointer to the message data
Definition: message.h:35
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:304
#define buf_mktemp(buf)
Definition: tmp.h:33