NeoMutt  2022-04-29-369-gff76d0
Teaching an old dog new tricks
DOXYGEN
mutt_header.c File Reference

Manipulate an email's header. More...

#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "mutt/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "mutt_header.h"
#include "enter/lib.h"
#include "index/lib.h"
#include "ncrypt/lib.h"
#include "postpone/lib.h"
#include "send/lib.h"
#include "muttlib.h"
#include "options.h"
+ Include dependency graph for mutt_header.c:

Go to the source code of this file.

Functions

static void label_ref_dec (struct Mailbox *m, char *label)
 Decrease the refcount of a label. More...
 
static void label_ref_inc (struct Mailbox *m, char *label)
 Increase the refcount of a label. More...
 
static bool label_message (struct Mailbox *m, struct Email *e, char *new_label)
 Add an X-Label: field. More...
 
int mutt_label_message (struct Mailbox *m, struct EmailList *el)
 Let the user label a message. More...
 
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. More...
 
void mutt_make_label_hash (struct Mailbox *m)
 Create a Hash Table to store the labels. More...
 
void mutt_label_hash_add (struct Mailbox *m, struct Email *e)
 Add a message's labels to the Hash Table. More...
 
void mutt_label_hash_remove (struct Mailbox *m, struct Email *e)
 Remove a message's labels from the Hash Table. More...
 

Detailed Description

Manipulate an email's header.

Authors
  • Michael R. Elkins

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 mutt_header.c.

Function Documentation

◆ label_ref_dec()

static void label_ref_dec ( struct Mailbox m,
char *  label 
)
static

Decrease the refcount of a label.

Parameters
mMailbox
labelLabel

Definition at line 56 of file mutt_header.c.

57{
58 struct HashElem *elem = mutt_hash_find_elem(m->label_hash, label);
59 if (!elem)
60 return;
61
62 uintptr_t count = (uintptr_t) elem->data;
63 if (count <= 1)
64 {
65 mutt_hash_delete(m->label_hash, label, NULL);
66 return;
67 }
68
69 count--;
70 elem->data = (void *) count;
71}
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 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
The item stored in a Hash Table.
Definition: hash.h:44
void * data
User-supplied data.
Definition: hash.h:47
struct HashTable * label_hash
Hash Table for x-labels.
Definition: mailbox.h:125
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ label_ref_inc()

static void label_ref_inc ( struct Mailbox m,
char *  label 
)
static

Increase the refcount of a label.

Parameters
mMailbox
labelLabel

Definition at line 78 of file mutt_header.c.

79{
80 uintptr_t count;
81
82 struct HashElem *elem = mutt_hash_find_elem(m->label_hash, label);
83 if (!elem)
84 {
85 count = 1;
86 mutt_hash_insert(m->label_hash, label, (void *) count);
87 return;
88 }
89
90 count = (uintptr_t) elem->data;
91 count++;
92 elem->data = (void *) count;
93}
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ label_message()

static bool label_message ( struct Mailbox m,
struct Email e,
char *  new_label 
)
static

Add an X-Label: field.

Parameters
[in]mMailbox
[in]eEmail
[out]new_labelSet to true if this is a new label
Return values
trueThe label was added

Definition at line 102 of file mutt_header.c.

103{
104 if (!e)
105 return false;
106 if (mutt_str_equal(e->env->x_label, new_label))
107 return false;
108
109 if (e->env->x_label)
110 label_ref_dec(m, e->env->x_label);
111 if (mutt_str_replace(&e->env->x_label, new_label))
112 label_ref_inc(m, e->env->x_label);
113
114 e->changed = true;
116 return true;
117}
#define MUTT_ENV_CHANGED_XLABEL
X-Label edited.
Definition: envelope.h:36
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
static void label_ref_inc(struct Mailbox *m, char *label)
Increase the refcount of a label.
Definition: mutt_header.c:78
static void label_ref_dec(struct Mailbox *m, char *label)
Decrease the refcount of a label.
Definition: mutt_header.c:56
struct Envelope * env
Envelope information.
Definition: email.h:66
bool changed
Email has been edited.
Definition: email.h:75
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition: envelope.h:92
char * x_label
X-Label.
Definition: envelope.h:76
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_label_message()

int mutt_label_message ( struct Mailbox m,
struct EmailList *  el 
)

Let the user label a message.

Parameters
mMailbox
elList of Emails to label
Return values
numNumber of messages changed

Definition at line 125 of file mutt_header.c.

126{
127 if (!m || !el)
128 return 0;
129
130 int changed = 0;
131 struct Buffer *buf = mutt_buffer_pool_get();
132
133 struct EmailNode *en = STAILQ_FIRST(el);
134 if (!STAILQ_NEXT(en, entries))
135 {
136 // If there's only one email, use its label as a template
137 if (en->email->env->x_label)
139 }
140
141 if (mutt_buffer_get_field("Label: ", buf, MUTT_COMP_LABEL /* | MUTT_COMP_CLEAR */,
142 false, NULL, NULL, NULL) != 0)
143 {
144 goto done;
145 }
146
147 char *new_label = buf->data;
148 SKIPWS(new_label);
149 if (*new_label == '\0')
150 new_label = NULL;
151
152 STAILQ_FOREACH(en, el, entries)
153 {
154 if (label_message(m, en->email, new_label))
155 {
156 changed++;
158 }
159 }
160
161done:
163 return changed;
164}
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:327
void mutt_set_header_color(struct Mailbox *m, struct Email *e)
Select a colour for a message.
Definition: dlg_index.c:1371
int mutt_buffer_get_field(const char *field, struct Buffer *buf, CompletionFlags complete, bool multiple, struct Mailbox *m, char ***files, int *numfiles)
Ask the user for a string.
Definition: window.c:179
#define MUTT_COMP_LABEL
Label completion.
Definition: mutt.h:58
static bool label_message(struct Mailbox *m, struct Email *e, char *new_label)
Add an X-Label: field.
Definition: mutt_header.c:102
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
#define SKIPWS(ch)
Definition: string2.h:46
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
List of Emails.
Definition: email.h:132
struct Email * email
Email in the list.
Definition: email.h:133
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_edit_headers()

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.

Parameters
editorEditor command
bodyFile containing message body
eEmail
fccBuffer for the fcc field

Definition at line 173 of file mutt_header.c.

175{
176 struct Buffer *path = mutt_buffer_pool_get();
177 mutt_buffer_mktemp(path);
178 FILE *fp_out = mutt_file_fopen(mutt_buffer_string(path), "w");
179 if (!fp_out)
180 {
182 goto cleanup;
183 }
184
187 false, false, NeoMutt->sub);
188 fputc('\n', fp_out); /* tie off the header. */
189
190 /* now copy the body of the message. */
191 FILE *fp_in = fopen(body, "r");
192 if (!fp_in)
193 {
194 mutt_perror(body);
195 mutt_file_fclose(&fp_out);
196 goto cleanup;
197 }
198
199 mutt_file_copy_stream(fp_in, fp_out);
200
201 mutt_file_fclose(&fp_in);
202 mutt_file_fclose(&fp_out);
203
204 struct stat st = { 0 };
205 if (stat(mutt_buffer_string(path), &st) == -1)
206 {
208 goto cleanup;
209 }
210
211 time_t mtime = mutt_file_decrease_mtime(mutt_buffer_string(path), &st);
212 if (mtime == (time_t) -1)
213 {
215 goto cleanup;
216 }
217
218 mutt_edit_file(editor, mutt_buffer_string(path));
219 if ((stat(mutt_buffer_string(path), &st) != 0) || (mtime == st.st_mtime))
220 {
221 mutt_debug(LL_DEBUG1, "temp file was not modified\n");
222 /* the file has not changed! */
224 goto cleanup;
225 }
226
227 mutt_file_unlink(body);
229
230 /* Read the temp file back in */
231 fp_in = fopen(mutt_buffer_string(path), "r");
232 if (!fp_in)
233 {
235 goto cleanup;
236 }
237
238 fp_out = mutt_file_fopen(body, "w");
239 if (!fp_out)
240 {
241 /* intentionally leak a possible temporary file here */
242 mutt_file_fclose(&fp_in);
243 mutt_perror(body);
244 goto cleanup;
245 }
246
247 struct Envelope *env_new = NULL;
248 char buf[1024] = { 0 };
249 env_new = mutt_rfc822_read_header(fp_in, NULL, true, false);
250 int bytes_read;
251 while ((bytes_read = fread(buf, 1, sizeof(buf), fp_in)) > 0)
252 fwrite(buf, 1, bytes_read, fp_out);
253 mutt_file_fclose(&fp_out);
254 mutt_file_fclose(&fp_in);
256
257 /* in case the user modifies/removes the In-Reply-To header with
258 * $edit_headers set, we remove References: as they're likely invalid;
259 * we can simply compare strings as we don't generate References for
260 * multiple Message-Ids in IRT anyways */
261#ifdef USE_NNTP
262 if (!OptNewsSend)
263#endif
264 {
265 if (!STAILQ_EMPTY(&e->env->in_reply_to) &&
266 (STAILQ_EMPTY(&env_new->in_reply_to) ||
267 !mutt_str_equal(STAILQ_FIRST(&env_new->in_reply_to)->data,
268 STAILQ_FIRST(&e->env->in_reply_to)->data)))
269 {
271 }
272 }
273
274 /* restore old info. */
275 mutt_list_free(&env_new->references);
276 STAILQ_SWAP(&env_new->references, &e->env->references, ListNode);
277
278 mutt_env_free(&e->env);
279 e->env = env_new;
280 env_new = NULL;
281
283
284 /* search through the user defined headers added to see if
285 * fcc: or attach: or pgp: or smime: was specified */
286
287 struct ListNode *np = NULL, *tmp = NULL;
288 STAILQ_FOREACH_SAFE(np, &e->env->userhdrs, entries, tmp)
289 {
290 bool keep = true;
291 size_t plen = 0;
292
293 // Check for header names: most specific first
294 if (fcc && ((plen = mutt_istr_startswith(np->data, "X-Mutt-Fcc:")) ||
295 (plen = mutt_istr_startswith(np->data, "Mutt-Fcc:")) ||
296 (plen = mutt_istr_startswith(np->data, "fcc:"))))
297 {
298 const char *p = mutt_str_skip_email_wsp(np->data + plen);
299 if (*p)
300 {
301 mutt_buffer_strcpy(fcc, p);
303 }
304 keep = false;
305 }
306 // Check for header names: most specific first
307 else if ((plen = mutt_istr_startswith(np->data, "X-Mutt-Attach:")) ||
308 (plen = mutt_istr_startswith(np->data, "Mutt-Attach:")) ||
309 (plen = mutt_istr_startswith(np->data, "attach:")))
310 {
311 struct Body *body2 = NULL;
312 struct Body *parts = NULL;
313
314 const char *p = mutt_str_skip_email_wsp(np->data + plen);
315 if (*p)
316 {
317 mutt_buffer_reset(path);
318 for (; (p[0] != '\0') && (p[0] != ' ') && (p[0] != '\t'); p++)
319 {
320 if (p[0] == '\\')
321 {
322 if (p[1] == '\0')
323 break;
324 p++;
325 }
326 mutt_buffer_addch(path, *p);
327 }
329
332 if (body2)
333 {
334 body2->description = mutt_str_dup(p);
335 for (parts = e->body; parts->next; parts = parts->next)
336 ; // do nothing
337
338 parts->next = body2;
339 }
340 else
341 {
343 mutt_error(_("%s: unable to attach file"), mutt_buffer_string(path));
344 }
345 }
346 keep = false;
347 }
348 // Check for header names: most specific first
349 else if (((WithCrypto & APPLICATION_PGP) != 0) &&
350 ((plen = mutt_istr_startswith(np->data, "X-Mutt-PGP:")) ||
351 (plen = mutt_istr_startswith(np->data, "Mutt-PGP:")) ||
352 (plen = mutt_istr_startswith(np->data, "pgp:"))))
353 {
355 if (sec != SEC_NO_FLAGS)
356 sec |= APPLICATION_PGP;
357 if (sec != e->security)
358 {
359 e->security = sec;
361 }
362 keep = false;
363 }
364 // Check for header names: most specific first
365 else if (((WithCrypto & APPLICATION_SMIME) != 0) &&
366 ((plen = mutt_istr_startswith(np->data, "X-Mutt-SMIME:")) ||
367 (plen = mutt_istr_startswith(np->data, "Mutt-SMIME:")) ||
368 (plen = mutt_istr_startswith(np->data, "smime:"))))
369 {
371 if (sec != SEC_NO_FLAGS)
372 sec |= APPLICATION_SMIME;
373 if (sec != e->security)
374 {
375 e->security = sec;
377 }
378 keep = false;
379 }
380#ifdef MIXMASTER
381 // Check for header names: most specific first
382 else if ((plen = mutt_istr_startswith(np->data, "X-Mutt-Mix:")) ||
383 (plen = mutt_istr_startswith(np->data, "Mutt-Mix:")))
384 {
386
387 char *t = strtok(np->data + plen, ", \t\n");
388 while (t)
389 {
391 t = strtok(NULL, ", \t\n");
392 }
393 keep = false;
394 }
395#endif
396
397 if (!keep)
398 {
399 STAILQ_REMOVE(&e->env->userhdrs, np, ListNode, entries);
400 FREE(&np->data);
401 FREE(&np);
402 }
403 }
404
405cleanup:
407}
void mutt_expand_aliases_env(struct Envelope *env)
Expand aliases in all the fields of an Envelope.
Definition: alias.c:312
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:248
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:85
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
void mutt_edit_file(const char *editor, const char *file)
Let the user edit a file.
Definition: curs_lib.c:312
@ NT_EMAIL_CHANGE
Email has changed.
Definition: email.h:151
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:97
void mutt_env_to_local(struct Envelope *env)
Convert an Envelope's Address fields to local format.
Definition: envelope.c:288
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:259
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:151
time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
Decrease a file's modification time by 1 second.
Definition: file.c:1035
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:193
#define mutt_error(...)
Definition: logging.h:87
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
#define mutt_perror(...)
Definition: logging.h:88
int mutt_rfc822_write_header(FILE *fp, struct Envelope *env, struct Body *attach, enum MuttWriteHeaderMode mode, bool privacy, bool hide_protected_subject, struct ConfigSubset *sub)
Write out one RFC822 header line.
Definition: header.c:572
@ MUTT_WRITE_HEADER_EDITHDRS
"light" mode (used for edit_hdrs)
Definition: header.h:43
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
#define FREE(x)
Definition: memory.h:43
#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:171
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
char * mutt_str_skip_email_wsp(const char *s)
Skip over whitespace as defined by RFC5322.
Definition: string.c:679
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:239
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:600
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
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
bool OptNewsSend
(pseudo) used to change behavior when posting
Definition: options.h:51
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition: parse.c:1157
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
SecurityFlags mutt_parse_crypt_hdr(const char *p, bool set_empty_signas, SecurityFlags crypt_app)
Parse a crypto header string.
Definition: postpone.c:210
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#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
struct Body * mutt_make_file_attach(const char *path, struct ConfigSubset *sub)
Create a file attachment.
Definition: sendlib.c:598
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
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:41
struct Body * body
List of MIME parts.
Definition: email.h:67
struct ListHead chain
Mixmaster chain.
Definition: email.h:90
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:71
The header of an Email.
Definition: envelope.h:57
struct ListHead userhdrs
user defined headers
Definition: envelope.h:87
struct ListHead references
message references (in reverse order)
Definition: envelope.h:85
struct ListHead in_reply_to
in-reply-to header content
Definition: envelope.h:86
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_make_label_hash()

void mutt_make_label_hash ( struct Mailbox m)

Create a Hash Table to store the labels.

Parameters
mMailbox

Definition at line 413 of file mutt_header.c.

414{
415 /* 131 is just a rough prime estimate of how many distinct
416 * labels someone might have in a m. */
418}
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:111
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_label_hash_add()

void mutt_label_hash_add ( struct Mailbox m,
struct Email e 
)

Add a message's labels to the Hash Table.

Parameters
mMailbox
eEmail

Definition at line 425 of file mutt_header.c.

426{
427 if (!m || !m->label_hash)
428 return;
429 if (e->env->x_label)
430 label_ref_inc(m, e->env->x_label);
431}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_label_hash_remove()

void mutt_label_hash_remove ( struct Mailbox m,
struct Email e 
)

Remove a message's labels from the Hash Table.

Parameters
mMailbox
eEmail

Definition at line 438 of file mutt_header.c.

439{
440 if (!m || !m->label_hash)
441 return;
442 if (e->env->x_label)
443 label_ref_dec(m, e->env->x_label);
444}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: