NeoMutt  2024-04-25-76-g20fe7b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mutt_header.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stdbool.h>
33#include <stddef.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <string.h>
37#include <sys/stat.h>
38#include <time.h>
39#include "mutt/lib.h"
40#include "email/lib.h"
41#include "core/lib.h"
42#include "alias/lib.h"
43#include "gui/lib.h"
44#include "mutt.h"
45#include "mutt_header.h"
46#include "complete/lib.h"
47#include "editor/lib.h"
48#include "history/lib.h"
49#include "index/lib.h"
50#include "ncrypt/lib.h"
51#include "postpone/lib.h"
52#include "send/lib.h"
53#include "globals.h"
54#include "muttlib.h"
55#include "mview.h"
56
62static void label_ref_dec(struct Mailbox *m, char *label)
63{
64 struct HashElem *he = mutt_hash_find_elem(m->label_hash, label);
65 if (!he)
66 return;
67
68 uintptr_t count = (uintptr_t) he->data;
69 if (count <= 1)
70 {
71 mutt_hash_delete(m->label_hash, label, NULL);
72 return;
73 }
74
75 count--;
76 he->data = (void *) count;
77}
78
84static void label_ref_inc(struct Mailbox *m, char *label)
85{
86 uintptr_t count;
87
88 struct HashElem *he = mutt_hash_find_elem(m->label_hash, label);
89 if (!he)
90 {
91 count = 1;
92 mutt_hash_insert(m->label_hash, label, (void *) count);
93 return;
94 }
95
96 count = (uintptr_t) he->data;
97 count++;
98 he->data = (void *) count;
99}
100
108static bool label_message(struct Mailbox *m, struct Email *e, char *new_label)
109{
110 if (!e)
111 return false;
112 if (mutt_str_equal(e->env->x_label, new_label))
113 return false;
114
115 if (e->env->x_label)
116 label_ref_dec(m, e->env->x_label);
117 if (mutt_str_replace(&e->env->x_label, new_label))
118 label_ref_inc(m, e->env->x_label);
119
120 e->changed = true;
122 return true;
123}
124
131int mutt_label_message(struct MailboxView *mv, struct EmailArray *ea)
132{
133 if (!mv || !mv->mailbox || !ea)
134 return 0;
135
136 struct Mailbox *m = mv->mailbox;
137
138 int changed = 0;
139 struct Buffer *buf = buf_pool_get();
140
141 struct Email **ep = ARRAY_GET(ea, 0);
142 if (ARRAY_SIZE(ea) == 1)
143 {
144 // If there's only one email, use its label as a template
145 struct Email *e = *ep;
146 if (e->env->x_label)
147 buf_strcpy(buf, e->env->x_label);
148 }
149
150 if (mw_get_field("Label: ", buf, MUTT_COMP_NO_FLAGS, HC_OTHER, &CompleteLabelOps, NULL) != 0)
151 {
152 goto done;
153 }
154
155 char *new_label = buf->data;
156 SKIPWS(new_label);
157 if (*new_label == '\0')
158 new_label = NULL;
159
160 ARRAY_FOREACH(ep, ea)
161 {
162 struct Email *e = *ep;
163 if (label_message(m, e, new_label))
164 {
165 changed++;
167 }
168 }
169
170done:
171 buf_pool_release(&buf);
172 return changed;
173}
174
182void mutt_edit_headers(const char *editor, const char *body, struct Email *e,
183 struct Buffer *fcc)
184{
185 struct Buffer *path = buf_pool_get();
186 buf_mktemp(path);
187 FILE *fp_out = mutt_file_fopen(buf_string(path), "w");
188 if (!fp_out)
189 {
190 mutt_perror("%s", buf_string(path));
191 goto cleanup;
192 }
193
196 false, false, NeoMutt->sub);
197 fputc('\n', fp_out); /* tie off the header. */
198
199 /* now copy the body of the message. */
200 FILE *fp_in = mutt_file_fopen(body, "r");
201 if (!fp_in)
202 {
203 mutt_perror("%s", body);
204 mutt_file_fclose(&fp_out);
205 goto cleanup;
206 }
207
208 mutt_file_copy_stream(fp_in, fp_out);
209
210 mutt_file_fclose(&fp_in);
211 mutt_file_fclose(&fp_out);
212
213 struct stat st = { 0 };
214 if (stat(buf_string(path), &st) == -1)
215 {
216 mutt_perror("%s", buf_string(path));
217 goto cleanup;
218 }
219
220 time_t mtime = mutt_file_decrease_mtime(buf_string(path), &st);
221 if (mtime == (time_t) -1)
222 {
223 mutt_perror("%s", buf_string(path));
224 goto cleanup;
225 }
226
227 mutt_edit_file(editor, buf_string(path));
228 if ((stat(buf_string(path), &st) != 0) || (mtime == st.st_mtime))
229 {
230 mutt_debug(LL_DEBUG1, "temp file was not modified\n");
231 /* the file has not changed! */
233 goto cleanup;
234 }
235
236 mutt_file_unlink(body);
238
239 /* Read the temp file back in */
240 fp_in = mutt_file_fopen(buf_string(path), "r");
241 if (!fp_in)
242 {
243 mutt_perror("%s", buf_string(path));
244 goto cleanup;
245 }
246
247 fp_out = mutt_file_fopen(body, "w");
248 if (!fp_out)
249 {
250 /* intentionally leak a possible temporary file here */
251 mutt_file_fclose(&fp_in);
252 mutt_perror("%s", body);
253 goto cleanup;
254 }
255
256 struct Envelope *env_new = NULL;
257 char buf[1024] = { 0 };
258 env_new = mutt_rfc822_read_header(fp_in, NULL, true, false);
259 int bytes_read;
260 while ((bytes_read = fread(buf, 1, sizeof(buf), fp_in)) > 0)
261 fwrite(buf, 1, bytes_read, fp_out);
262 mutt_file_fclose(&fp_out);
263 mutt_file_fclose(&fp_in);
265
266 /* in case the user modifies/removes the In-Reply-To header with
267 * $edit_headers set, we remove References: as they're likely invalid;
268 * we can simply compare strings as we don't generate References for
269 * multiple Message-Ids in IRT anyways */
270 if (!OptNewsSend)
271 {
272 if (!STAILQ_EMPTY(&e->env->in_reply_to) &&
273 (STAILQ_EMPTY(&env_new->in_reply_to) ||
274 !mutt_str_equal(STAILQ_FIRST(&env_new->in_reply_to)->data,
275 STAILQ_FIRST(&e->env->in_reply_to)->data)))
276 {
278 }
279 }
280
281 /* restore old info. */
282 mutt_list_free(&env_new->references);
283 STAILQ_SWAP(&env_new->references, &e->env->references, ListNode);
284
285 mutt_env_free(&e->env);
286 e->env = env_new;
287 env_new = NULL;
288
290
291 /* search through the user defined headers added to see if
292 * fcc: or attach: or pgp: or smime: was specified */
293
294 struct ListNode *np = NULL, *tmp = NULL;
295 STAILQ_FOREACH_SAFE(np, &e->env->userhdrs, entries, tmp)
296 {
297 bool keep = true;
298 size_t plen = 0;
299
300 // Check for header names: most specific first
301 if (fcc && ((plen = mutt_istr_startswith(np->data, "X-Mutt-Fcc:")) ||
302 (plen = mutt_istr_startswith(np->data, "Mutt-Fcc:")) ||
303 (plen = mutt_istr_startswith(np->data, "fcc:"))))
304 {
305 const char *p = mutt_str_skip_email_wsp(np->data + plen);
306 if (*p)
307 {
308 buf_strcpy(fcc, p);
310 }
311 keep = false;
312 }
313 // Check for header names: most specific first
314 else if ((plen = mutt_istr_startswith(np->data, "X-Mutt-Attach:")) ||
315 (plen = mutt_istr_startswith(np->data, "Mutt-Attach:")) ||
316 (plen = mutt_istr_startswith(np->data, "attach:")))
317 {
318 struct Body *body2 = NULL;
319 struct Body *parts = NULL;
320
321 const char *p = mutt_str_skip_email_wsp(np->data + plen);
322 if (*p)
323 {
324 buf_reset(path);
325 for (; (p[0] != '\0') && (p[0] != ' ') && (p[0] != '\t'); p++)
326 {
327 if (p[0] == '\\')
328 {
329 if (p[1] == '\0')
330 break;
331 p++;
332 }
333 buf_addch(path, *p);
334 }
336
337 buf_expand_path(path);
339 if (body2)
340 {
341 body2->description = mutt_str_dup(p);
342 for (parts = e->body; parts->next; parts = parts->next)
343 ; // do nothing
344
345 parts->next = body2;
346 }
347 else
348 {
349 buf_pretty_mailbox(path);
350 mutt_error(_("%s: unable to attach file"), buf_string(path));
351 }
352 }
353 keep = false;
354 }
355 // Check for header names: most specific first
356 else if (((WithCrypto & APPLICATION_PGP) != 0) &&
357 ((plen = mutt_istr_startswith(np->data, "X-Mutt-PGP:")) ||
358 (plen = mutt_istr_startswith(np->data, "Mutt-PGP:")) ||
359 (plen = mutt_istr_startswith(np->data, "pgp:"))))
360 {
361 SecurityFlags sec = mutt_parse_crypt_hdr(np->data + plen, false, APPLICATION_PGP);
362 if (sec != SEC_NO_FLAGS)
363 sec |= APPLICATION_PGP;
364 if (sec != e->security)
365 {
366 e->security = sec;
368 }
369 keep = false;
370 }
371 // Check for header names: most specific first
372 else if (((WithCrypto & APPLICATION_SMIME) != 0) &&
373 ((plen = mutt_istr_startswith(np->data, "X-Mutt-SMIME:")) ||
374 (plen = mutt_istr_startswith(np->data, "Mutt-SMIME:")) ||
375 (plen = mutt_istr_startswith(np->data, "smime:"))))
376 {
378 if (sec != SEC_NO_FLAGS)
379 sec |= APPLICATION_SMIME;
380 if (sec != e->security)
381 {
382 e->security = sec;
384 }
385 keep = false;
386 }
387#ifdef MIXMASTER
388 // Check for header names: most specific first
389 else if ((plen = mutt_istr_startswith(np->data, "X-Mutt-Mix:")) ||
390 (plen = mutt_istr_startswith(np->data, "Mutt-Mix:")))
391 {
393
394 char *t = strtok(np->data + plen, ", \t\n");
395 while (t)
396 {
398 t = strtok(NULL, ", \t\n");
399 }
400 keep = false;
401 }
402#endif
403
404 if (!keep)
405 {
406 STAILQ_REMOVE(&e->env->userhdrs, np, ListNode, entries);
407 FREE(&np->data);
408 FREE(&np);
409 }
410 }
411
412cleanup:
413 buf_pool_release(&path);
414}
415
421{
422 /* 131 is just a rough prime estimate of how many distinct
423 * labels someone might have in a mailbox. */
425}
426
432void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
433{
434 if (!m || !m->label_hash)
435 return;
436 if (e->env->x_label)
437 label_ref_inc(m, e->env->x_label);
438}
439
445void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
446{
447 if (!m || !m->label_hash)
448 return;
449 if (e->env->x_label)
450 label_ref_dec(m, e->env->x_label);
451}
Email Aliases.
void mutt_expand_aliases_env(struct Envelope *env)
Expand aliases in all the fields of an Envelope.
Definition: alias.c:309
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:109
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:241
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const struct CompleteOps CompleteLabelOps
Auto-Completion of Labels.
Definition: helpers.c:484
Auto-completion.
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:116
void mutt_set_header_color(struct Mailbox *m, struct Email *e)
Select a colour for a message.
Definition: dlg_index.c:1373
Edit a string.
Structs that make up an email.
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition: parse.c:1200
@ NT_EMAIL_CHANGE
Email has changed.
Definition: email.h:189
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:126
void mutt_env_to_local(struct Envelope *env)
Convert an Envelope's Address fields to local format.
Definition: envelope.c:317
#define MUTT_ENV_CHANGED_XLABEL
X-Label edited.
Definition: envelope.h:36
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:287
time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
Decrease a file's modification time by 1 second.
Definition: file.c:1028
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:221
#define mutt_file_fclose(FP)
Definition: file.h:149
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:148
bool OptNewsSend
(pseudo) used to change behavior when posting
Definition: globals.c:71
Global variables.
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition: window.c:274
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
Convenience wrapper for the gui headers.
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
struct HashElem * mutt_hash_find_elem(const struct HashTable *table, const char *strkey)
Find the HashElem in a Hash Table element using a key.
Definition: hash.c:377
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:111
int mutt_rfc822_write_header(FILE *fp, struct Envelope *env, struct Body *b, enum MuttWriteHeaderMode mode, bool privacy, bool hide_protected_subject, struct ConfigSubset *sub)
Write out one RFC822 header line.
Definition: header.c:577
@ MUTT_WRITE_HEADER_EDITHDRS
"light" mode (used for edit_hdrs)
Definition: header.h:43
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:56
GUI manage the main index (list of emails)
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:65
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:123
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
char * mutt_str_skip_email_wsp(const char *s)
Skip over whitespace as defined by RFC5322.
Definition: string.c:608
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:242
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
Many unsorted constants and some structs.
#define MUTT_COMP_NO_FLAGS
No flags are set.
Definition: mutt.h:56
int mutt_label_message(struct MailboxView *mv, struct EmailArray *ea)
Let the user label a message.
Definition: mutt_header.c:131
void mutt_make_label_hash(struct Mailbox *m)
Create a Hash Table to store the labels.
Definition: mutt_header.c:420
void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
Remove a message's labels from the Hash Table.
Definition: mutt_header.c:445
static void label_ref_inc(struct Mailbox *m, char *label)
Increase the refcount of a label.
Definition: mutt_header.c:84
static bool label_message(struct Mailbox *m, struct Email *e, char *new_label)
Add an X-Label: field.
Definition: mutt_header.c:108
static void label_ref_dec(struct Mailbox *m, char *label)
Decrease the refcount of a label.
Definition: mutt_header.c:62
void mutt_edit_headers(const char *editor, const char *body, struct Email *e, struct Buffer *fcc)
Let the user edit the message header and body.
Definition: mutt_header.c:182
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
Add a message's labels to the Hash Table.
Definition: mutt_header.c:432
Representation of the email's header.
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:554
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:328
Some miscellaneous functions.
View of a Mailbox.
API for encryption/signing of emails.
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition: lib.h:76
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:91
#define SEC_NO_FLAGS
No flags are set.
Definition: lib.h:77
#define WithCrypto
Definition: lib.h:116
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.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
Postponed Emails.
SecurityFlags mutt_parse_crypt_hdr(const char *p, bool set_empty_signas, SecurityFlags crypt_app)
Parse a crypto header string.
Definition: postpone.c:205
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_EMPTY(head)
Definition: queue.h:348
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
#define STAILQ_SWAP(head1, head2, type)
Definition: queue.h:428
Convenience wrapper for the send headers.
struct Body * mutt_make_file_attach(const char *path, struct ConfigSubset *sub)
Create a file attachment.
Definition: sendlib.c:607
#define SKIPWS(ch)
Definition: string2.h:45
The body of an email.
Definition: body.h:36
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:72
char * description
content-description
Definition: body.h:55
struct Body * next
next attachment in the list
Definition: body.h:71
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
The envelope/body of an email.
Definition: email.h:39
struct Envelope * env
Envelope information.
Definition: email.h:68
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:43
struct Body * body
List of MIME parts.
Definition: email.h:69
bool changed
Email has been edited.
Definition: email.h:77
struct ListHead chain
Mixmaster chain.
Definition: email.h:93
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:73
The header of an Email.
Definition: envelope.h:57
struct ListHead userhdrs
user defined headers
Definition: envelope.h:85
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition: envelope.h:90
struct ListHead references
message references (in reverse order)
Definition: envelope.h:83
struct ListHead in_reply_to
in-reply-to header content
Definition: envelope.h:84
char * x_label
X-Label.
Definition: envelope.h:76
The item stored in a Hash Table.
Definition: hash.h:43
void * data
User-supplied data.
Definition: hash.h:46
A List node for strings.
Definition: list.h:36
char * data
String.
Definition: list.h:37
View of a Mailbox.
Definition: mview.h:40
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:51
A mailbox.
Definition: mailbox.h:79
bool changed
Mailbox has been modified.
Definition: mailbox.h:110
struct HashTable * label_hash
Hash Table: "x-labels" -> Email.
Definition: mailbox.h:125
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
#define buf_mktemp(buf)
Definition: tmp.h:33