NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
editmsg.c File Reference

Prepare an email to be edited. More...

#include "config.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "copy.h"
#include "mx.h"
#include "protos.h"
+ Include dependency graph for editmsg.c:

Go to the source code of this file.

Functions

static int ev_message (enum EvMessage action, struct Mailbox *m, struct Email *e)
 Edit an email or view it in an external editor.
 
int mutt_ev_message (struct Mailbox *m, struct EmailArray *ea, enum EvMessage action)
 Edit or view a message.
 

Detailed Description

Prepare an email to be edited.

Authors
  • Thomas Roessler
  • Reis Radomil
  • Richard Russon
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file editmsg.c.

Function Documentation

◆ ev_message()

static int ev_message ( enum EvMessage  action,
struct Mailbox m,
struct Email e 
)
static

Edit an email or view it in an external editor.

Parameters
actionAction to perform, e.g. EVM_EDIT
mMailbox
eEmail
Return values
1Message not modified
0Message edited successfully
-1Error

Definition at line 58 of file editmsg.c.

59{
60 char buf[256] = { 0 };
61 int rc;
62 FILE *fp = NULL;
63 struct stat st = { 0 };
64 bool old_append = m->append;
65
66 struct Buffer *fname = buf_pool_get();
67 buf_mktemp(fname);
68
69 // Temporarily force $mbox_type to be MUTT_MBOX
70 const unsigned char c_mbox_type = cs_subset_enum(NeoMutt->sub, "mbox_type");
71 cs_subset_str_native_set(NeoMutt->sub, "mbox_type", MUTT_MBOX, NULL);
72
73 struct Mailbox *m_fname = mx_path_resolve(buf_string(fname));
74 if (!mx_mbox_open(m_fname, MUTT_NEWFOLDER))
75 {
76 mutt_error(_("could not create temporary folder: %s"), strerror(errno));
77 buf_pool_release(&fname);
78 mailbox_free(&m_fname);
79 return -1;
80 }
81
82 cs_subset_str_native_set(NeoMutt->sub, "mbox_type", c_mbox_type, NULL);
83
84 const CopyHeaderFlags chflags = CH_NOLEN |
85 (((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) ?
88 rc = mutt_append_message(m_fname, m, e, NULL, MUTT_CM_NO_FLAGS, chflags);
89 int oerrno = errno;
90
91 mx_mbox_close(m_fname);
92 mailbox_free(&m_fname);
93
94 if (rc == -1)
95 {
96 mutt_error(_("could not write temporary mail folder: %s"), strerror(oerrno));
97 goto bail;
98 }
99
100 rc = stat(buf_string(fname), &st);
101 if (rc == -1)
102 {
103 mutt_error(_("Can't stat %s: %s"), buf_string(fname), strerror(errno));
104 goto bail;
105 }
106
107 /* The file the user is going to edit is not a real mbox, so we need to
108 * truncate the last newline in the temp file, which is logically part of
109 * the message separator, and not the body of the message. If we fail to
110 * remove it, the message will grow by one line each time the user edits
111 * the message. */
112 if ((st.st_size != 0) && (truncate(buf_string(fname), st.st_size - 1) == -1))
113 {
114 rc = -1;
115 mutt_error(_("could not truncate temporary mail folder: %s"), strerror(errno));
116 goto bail;
117 }
118
119 if (action == EVM_VIEW)
120 {
121 /* remove write permissions */
122 rc = mutt_file_chmod_rm_stat(buf_string(fname), S_IWUSR | S_IWGRP | S_IWOTH, &st);
123 if (rc == -1)
124 {
125 mutt_debug(LL_DEBUG1, "Could not remove write permissions of %s: %s",
126 buf_string(fname), strerror(errno));
127 /* Do not bail out here as we are checking afterwards if we should adopt
128 * changes of the temporary file. */
129 }
130 }
131
132 /* re-stat after the truncate, to avoid false "modified" bugs */
133 rc = stat(buf_string(fname), &st);
134 if (rc == -1)
135 {
136 mutt_error(_("Can't stat %s: %s"), buf_string(fname), strerror(errno));
137 goto bail;
138 }
139
140 /* Do not reuse the stat st here as it is outdated. */
141 time_t mtime = mutt_file_decrease_mtime(buf_string(fname), NULL);
142 if (mtime == (time_t) -1)
143 {
144 rc = -1;
145 mutt_perror("%s", buf_string(fname));
146 goto bail;
147 }
148
149 const char *const c_editor = cs_subset_string(NeoMutt->sub, "editor");
150 mutt_edit_file(NONULL(c_editor), buf_string(fname));
151
152 rc = stat(buf_string(fname), &st);
153 if (rc == -1)
154 {
155 mutt_error(_("Can't stat %s: %s"), buf_string(fname), strerror(errno));
156 goto bail;
157 }
158
159 if (st.st_size == 0)
160 {
161 mutt_message(_("Message file is empty"));
162 rc = 1;
163 goto bail;
164 }
165
166 if ((action == EVM_EDIT) && (st.st_mtime == mtime))
167 {
168 mutt_message(_("Message not modified"));
169 rc = 1;
170 goto bail;
171 }
172
173 if ((action == EVM_VIEW) && (st.st_mtime != mtime))
174 {
175 mutt_message(_("Message of read-only mailbox modified! Ignoring changes."));
176 rc = 1;
177 goto bail;
178 }
179
180 if (action == EVM_VIEW)
181 {
182 /* stop processing here and skip right to the end */
183 rc = 1;
184 goto bail;
185 }
186
187 fp = mutt_file_fopen(buf_string(fname), "r");
188 if (!fp)
189 {
190 rc = -1;
191 mutt_error(_("Can't open message file: %s"), strerror(errno));
192 goto bail;
193 }
194
196 {
197 rc = -1;
198 /* L10N: %s is from strerror(errno) */
199 mutt_error(_("Can't append to folder: %s"), strerror(errno));
200 goto bail;
201 }
203 CopyHeaderFlags cf = (((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) ? CH_NO_FLAGS : CH_NOSTATUS);
204
205 if (fgets(buf, sizeof(buf), fp) && is_from(buf, NULL, 0, NULL))
206 {
207 if ((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF))
208 cf = CH_FROM | CH_FORCE_FROM;
209 }
210 else
211 {
212 of = MUTT_ADD_FROM;
213 }
214
215 /* XXX - we have to play games with the message flags to avoid
216 * problematic behavior with maildir folders. */
217
218 bool o_read = e->read;
219 bool o_old = e->old;
220 e->read = false;
221 e->old = false;
222 struct Message *msg = mx_msg_open_new(m, e, of);
223 e->read = o_read;
224 e->old = o_old;
225
226 if (!msg)
227 {
228 rc = -1;
229 mutt_error(_("Can't append to folder: %s"), strerror(errno));
230 mx_mbox_close(m);
231 goto bail;
232 }
233
234 rc = mutt_copy_hdr(fp, msg->fp, 0, st.st_size, CH_NOLEN | cf, NULL, 0);
235 if (rc == 0)
236 {
237 fputc('\n', msg->fp);
239 }
240
241 rc = mx_msg_commit(m, msg);
242 mx_msg_close(m, &msg);
243
244 mx_mbox_close(m);
245 m->last_checked = 0; // force a check on the next mx_mbox_check() call
246
247bail:
249
250 if (rc >= 0)
251 unlink(buf_string(fname));
252
253 if (rc == 0)
254 {
255 mutt_set_flag(m, e, MUTT_DELETE, true, true);
256 mutt_set_flag(m, e, MUTT_PURGE, true, true);
257 mutt_set_flag(m, e, MUTT_READ, true, true);
258
259 const bool c_delete_untag = cs_subset_bool(NeoMutt->sub, "delete_untag");
260 if (c_delete_untag)
261 mutt_set_flag(m, e, MUTT_TAG, false, true);
262 }
263 else if (rc == -1)
264 {
265 mutt_message(_("Error. Preserving temporary file: %s"), buf_string(fname));
266 }
267
268 m->append = old_append;
269
270 buf_pool_release(&fname);
271 return rc;
272}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
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
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:982
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:108
#define CH_NOSTATUS
Suppress the status and x-status fields.
Definition: copy.h:60
#define CH_FROM
Retain the "From " message separator?
Definition: copy.h:58
uint32_t CopyHeaderFlags
Flags for mutt_copy_header(), e.g. CH_UPDATE.
Definition: copy.h:52
#define CH_FORCE_FROM
Give CH_FROM precedence over CH_WEED?
Definition: copy.h:68
#define MUTT_CM_NO_FLAGS
No flags are set.
Definition: copy.h:37
#define CH_NO_FLAGS
No flags are set.
Definition: copy.h:53
#define CH_NOLEN
Don't write Content-Length: and Lines:
Definition: copy.h:66
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
void mutt_edit_file(const char *editor, const char *file)
Let the user edit a file.
Definition: curs_lib.c:116
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:286
time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
Decrease a file's modification time by 1 second.
Definition: file.c:1025
int mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st)
Remove permissions from a file.
Definition: file.c:1170
#define mutt_file_fclose(FP)
Definition: file.h:147
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:146
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:57
bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
Is a string a 'From' header line?
Definition: from.c:49
#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
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define _(a)
Definition: message.h:28
@ MUTT_READ
Messages that have been read.
Definition: mutt.h:73
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition: mutt.h:77
@ MUTT_TAG
Tagged messages.
Definition: mutt.h:80
@ MUTT_DELETE
Messages to be deleted.
Definition: mutt.h:75
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition: mx.c:1178
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:286
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1038
int mx_msg_commit(struct Mailbox *m, struct Message *msg)
Commit a message to a folder - Wrapper for MxOps::msg_commit()
Definition: mx.c:1157
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1634
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:596
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
@ EVM_VIEW
View the message.
Definition: protos.h:53
@ EVM_EDIT
Edit the message.
Definition: protos.h:54
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:36
bool read
Email is read.
Definition: email.h:50
bool old
Email is seen, but unread.
Definition: email.h:49
A mailbox.
Definition: mailbox.h:79
bool append
Mailbox is opened in append mode.
Definition: mailbox.h:109
time_t last_checked
Last time we checked this mailbox for new mail.
Definition: mailbox.h:105
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:297
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_ev_message()

int mutt_ev_message ( struct Mailbox m,
struct EmailArray *  ea,
enum EvMessage  action 
)

Edit or view a message.

Parameters
mMailbox
eaArray of Emails
actionAction to perform, e.g. EVM_EDIT
Return values
1Message not modified
0Message edited successfully
-1Error

Definition at line 283 of file editmsg.c.

284{
285 struct Email **ep = NULL;
286 ARRAY_FOREACH(ep, ea)
287 {
288 struct Email *e = *ep;
289 if (ev_message(action, m, e) == -1)
290 return -1;
291 }
292
293 return 0;
294}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
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:58
The envelope/body of an email.
Definition: email.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function: