NeoMutt  2020-04-24
Teaching an old dog new tricks
DOXYGEN
mutt_header.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <sys/stat.h>
35 #include <time.h>
36 #include "mutt/lib.h"
37 #include "email/lib.h"
38 #include "core/lib.h"
39 #include "gui/lib.h"
40 #include "mutt.h"
41 #include "mutt_header.h"
42 #include "alias.h"
43 #include "index.h"
44 #include "muttlib.h"
45 #include "options.h"
46 #include "protos.h"
47 #include "sendlib.h"
48 #include "ncrypt/lib.h"
49 
55 static void label_ref_dec(struct Mailbox *m, char *label)
56 {
57  struct HashElem *elem = mutt_hash_find_elem(m->label_hash, label);
58  if (!elem)
59  return;
60 
61  uintptr_t count = (uintptr_t) elem->data;
62  if (count <= 1)
63  {
64  mutt_hash_delete(m->label_hash, label, NULL);
65  return;
66  }
67 
68  count--;
69  elem->data = (void *) count;
70 }
71 
77 static void label_ref_inc(struct Mailbox *m, char *label)
78 {
79  uintptr_t count;
80 
81  struct HashElem *elem = mutt_hash_find_elem(m->label_hash, label);
82  if (!elem)
83  {
84  count = 1;
85  mutt_hash_insert(m->label_hash, label, (void *) count);
86  return;
87  }
88 
89  count = (uintptr_t) elem->data;
90  count++;
91  elem->data = (void *) count;
92 }
93 
101 static bool label_message(struct Mailbox *m, struct Email *e, char *new_label)
102 {
103  if (!e)
104  return false;
105  if (mutt_str_strcmp(e->env->x_label, new_label) == 0)
106  return false;
107 
108  if (e->env->x_label)
109  label_ref_dec(m, e->env->x_label);
110  mutt_str_replace(&e->env->x_label, new_label);
111  if (e->env->x_label)
112  label_ref_inc(m, e->env->x_label);
113 
114  e->changed = true;
116  return true;
117 }
118 
125 int mutt_label_message(struct Mailbox *m, struct EmailList *el)
126 {
127  if (!m || !el)
128  return 0;
129 
130  char buf[1024] = { 0 };
131 
132  struct EmailNode *en = STAILQ_FIRST(el);
133  if (!STAILQ_NEXT(en, entries))
134  {
135  // If there's only one email, use its label as a template
136  if (en->email->env->x_label)
137  mutt_str_strfcpy(buf, en->email->env->x_label, sizeof(buf));
138  }
139 
140  if (mutt_get_field("Label: ", buf, sizeof(buf), MUTT_LABEL /* | MUTT_CLEAR */) != 0)
141  return 0;
142 
143  char *new_label = buf;
144  SKIPWS(new_label);
145  if (*new_label == '\0')
146  new_label = NULL;
147 
148  int changed = 0;
149  STAILQ_FOREACH(en, el, entries)
150  {
151  if (label_message(m, en->email, new_label))
152  {
153  changed++;
155  }
156  }
157 
158  return changed;
159 }
160 
168 void mutt_edit_headers(const char *editor, const char *body, struct Email *e,
169  struct Buffer *fcc)
170 {
171  char buf[1024];
172  const char *p = NULL;
173  int i;
174  struct Envelope *n = NULL;
175  time_t mtime;
176  struct stat st;
177 
178  struct Buffer *path = mutt_buffer_pool_get();
179  mutt_buffer_mktemp(path);
180  FILE *fp_out = mutt_file_fopen(mutt_b2s(path), "w");
181  if (!fp_out)
182  {
183  mutt_perror(mutt_b2s(path));
184  goto cleanup;
185  }
186 
188  mutt_rfc822_write_header(fp_out, e->env, NULL, MUTT_WRITE_HEADER_EDITHDRS, false, false);
189  fputc('\n', fp_out); /* tie off the header. */
190 
191  /* now copy the body of the message. */
192  FILE *fp_in = fopen(body, "r");
193  if (!fp_in)
194  {
195  mutt_perror(body);
196  mutt_file_fclose(&fp_out);
197  goto cleanup;
198  }
199 
200  mutt_file_copy_stream(fp_in, fp_out);
201 
202  mutt_file_fclose(&fp_in);
203  mutt_file_fclose(&fp_out);
204 
205  if (stat(mutt_b2s(path), &st) == -1)
206  {
207  mutt_perror(mutt_b2s(path));
208  goto cleanup;
209  }
210 
211  mtime = mutt_file_decrease_mtime(mutt_b2s(path), &st);
212 
213  mutt_edit_file(editor, mutt_b2s(path));
214  stat(mutt_b2s(path), &st);
215  if (mtime == st.st_mtime)
216  {
217  mutt_debug(LL_DEBUG1, "temp file was not modified\n");
218  /* the file has not changed! */
219  mutt_file_unlink(mutt_b2s(path));
220  goto cleanup;
221  }
222 
223  mutt_file_unlink(body);
225 
226  /* Read the temp file back in */
227  fp_in = fopen(mutt_b2s(path), "r");
228  if (!fp_in)
229  {
230  mutt_perror(mutt_b2s(path));
231  goto cleanup;
232  }
233 
234  fp_out = mutt_file_fopen(body, "w");
235  if (!fp_out)
236  {
237  /* intentionally leak a possible temporary file here */
238  mutt_file_fclose(&fp_in);
239  mutt_perror(body);
240  goto cleanup;
241  }
242 
243  n = mutt_rfc822_read_header(fp_in, NULL, true, false);
244  while ((i = fread(buf, 1, sizeof(buf), fp_in)) > 0)
245  fwrite(buf, 1, i, fp_out);
246  mutt_file_fclose(&fp_out);
247  mutt_file_fclose(&fp_in);
248  mutt_file_unlink(mutt_b2s(path));
249 
250  /* in case the user modifies/removes the In-Reply-To header with
251  * $edit_headers set, we remove References: as they're likely invalid;
252  * we can simply compare strings as we don't generate References for
253  * multiple Message-Ids in IRT anyways */
254 #ifdef USE_NNTP
255  if (!OptNewsSend)
256 #endif
257  {
258  if (!STAILQ_EMPTY(&e->env->in_reply_to) &&
259  (STAILQ_EMPTY(&n->in_reply_to) ||
261  STAILQ_FIRST(&e->env->in_reply_to)->data) != 0)))
262  {
264  }
265  }
266 
267  /* restore old info. */
270 
271  mutt_env_free(&e->env);
272  e->env = n;
273  n = NULL;
274 
276 
277  /* search through the user defined headers added to see if
278  * fcc: or attach: or pgp: was specified */
279 
280  struct ListNode *np = NULL, *tmp = NULL;
281  STAILQ_FOREACH_SAFE(np, &e->env->userhdrs, entries, tmp)
282  {
283  bool keep = true;
284  size_t plen;
285 
286  if (fcc && (plen = mutt_str_startswith(np->data, "fcc:", CASE_IGNORE)))
287  {
288  p = mutt_str_skip_email_wsp(np->data + plen);
289  if (*p)
290  {
291  mutt_buffer_strcpy(fcc, p);
293  }
294  keep = false;
295  }
296  else if ((plen = mutt_str_startswith(np->data, "attach:", CASE_IGNORE)))
297  {
298  struct Body *body2 = NULL;
299  struct Body *parts = NULL;
300 
301  p = mutt_str_skip_email_wsp(np->data + plen);
302  if (*p)
303  {
304  mutt_buffer_reset(path);
305  for (; (p[0] != '\0') && (p[0] != ' ') && (p[0] != '\t'); p++)
306  {
307  if (p[0] == '\\')
308  {
309  if (p[1] == '\0')
310  break;
311  p++;
312  }
313  mutt_buffer_addch(path, *p);
314  }
316 
318  body2 = mutt_make_file_attach(mutt_b2s(path));
319  if (body2)
320  {
321  body2->description = mutt_str_strdup(p);
322  for (parts = e->content; parts->next; parts = parts->next)
323  ;
324  parts->next = body2;
325  }
326  else
327  {
329  mutt_error(_("%s: unable to attach file"), mutt_b2s(path));
330  }
331  }
332  keep = false;
333  }
334  else if (((WithCrypto & APPLICATION_PGP) != 0) &&
335  (plen = mutt_str_startswith(np->data, "pgp:", CASE_IGNORE)))
336  {
337  e->security = mutt_parse_crypt_hdr(np->data + plen, false, APPLICATION_PGP);
338  if (e->security)
340  keep = false;
341  }
342 
343  if (!keep)
344  {
345  STAILQ_REMOVE(&e->env->userhdrs, np, ListNode, entries);
346  FREE(&np->data);
347  FREE(&np);
348  }
349  }
350 
351 cleanup:
353 }
354 
360 {
361  /* 131 is just a rough prime estimate of how many distinct
362  * labels someone might have in a m. */
364 }
365 
371 void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
372 {
373  if (!m || !m->label_hash)
374  return;
375  if (e->env->x_label)
376  label_ref_inc(m, e->env->x_label);
377 }
378 
384 void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
385 {
386  if (!m || !m->label_hash)
387  return;
388  if (e->env->x_label)
389  label_ref_dec(m, e->env->x_label);
390 }
Convenience wrapper for the gui headers.
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:81
void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data)
Remove an element from a Hash table.
Definition: hash.c:443
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:399
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
Representation of the email&#39;s header.
#define WithCrypto
Definition: lib.h:163
The envelope/body of an email.
Definition: email.h:37
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define mutt_perror(...)
Definition: logging.h:85
if(!test_colorize_)
Definition: acutest.h:499
Structs that make up an email.
GUI manage the main index (list of emails)
#define MUTT_LABEL
Do label completion.
Definition: mutt.h:65
void mutt_expand_aliases_env(struct Envelope *env)
Expand aliases in all the fields of an Envelope.
Definition: alias.c:317
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Body * content
List of MIME parts.
Definition: email.h:90
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:195
String manipulation buffer.
Definition: buffer.h:33
struct ListHead userhdrs
user defined headers
Definition: envelope.h:83
#define _(a)
Definition: message.h:28
bool changed
Email has been edited.
Definition: email.h:48
struct Body * next
next attachment in the list
Definition: body.h:53
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
Add a message&#39;s labels to the Hash Table.
Definition: mutt_header.c:371
void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
Remove a message&#39;s labels from the Hash Table.
Definition: mutt_header.c:384
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using &#39;~&#39; or &#39;=&#39;.
Definition: muttlib.c:688
Representation of a single alias to an email address.
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:123
#define mutt_get_field(field, buf, buflen, complete)
Definition: curs_lib.h:90
The body of an email.
Definition: body.h:34
int mutt_rfc822_write_header(FILE *fp, struct Envelope *env, struct Body *attach, enum MuttWriteHeaderMode mode, bool privacy, bool hide_protected_subject)
Write out one RFC822 header line.
Definition: sendlib.c:2325
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition: envelope.h:88
Some miscellaneous functions.
struct ListHead in_reply_to
in-reply-to header content
Definition: envelope.h:82
Many unsorted constants and some structs.
struct Body * mutt_make_file_attach(const char *path)
Create a file attachment.
Definition: sendlib.c:1650
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
struct Envelope * env
Envelope information.
Definition: email.h:89
Convenience wrapper for the core headers.
#define SKIPWS(ch)
Definition: string2.h:47
struct HashElem * mutt_hash_find_elem(const struct Hash *table, const char *strkey)
Find the HashElem in a Hash table element using a key.
Definition: hash.c:393
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:359
#define mutt_b2s(buf)
Definition: buffer.h:41
Prototypes for many functions.
#define MUTT_ENV_CHANGED_XLABEL
X-Label edited.
Definition: envelope.h:34
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:137
A mailbox.
Definition: mailbox.h:81
"light" mode (used for edit_hdrs)
Definition: sendlib.h:63
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:96
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:54
size_t mutt_str_strfcpy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:776
struct Hash * mutt_hash_new(size_t nelem, HashFlags flags)
Create a new Hash table (with string keys)
Definition: hash.c:275
API for encryption/signing of emails.
Ignore case when comparing strings.
Definition: string2.h:68
char * mutt_str_skip_email_wsp(const char *s)
Skip over whitespace as defined by RFC5322.
Definition: string.c:802
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
char * description
content-description
Definition: body.h:40
static void label_ref_dec(struct Mailbox *m, char *label)
Decrease the refcount of a label.
Definition: mutt_header.c:55
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
#define STAILQ_NEXT(elm, field)
Definition: queue.h:397
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib...
Definition: email.h:39
void mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:455
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
size_t mutt_str_startswith(const char *str, const char *prefix, enum CaseSensitivity cs)
Check whether a string starts with a prefix.
Definition: string.c:168
void * data
Definition: hash.h:46
void mutt_set_header_color(struct Mailbox *m, struct Email *e)
Select a colour for a message.
Definition: index.c:3955
void mutt_make_label_hash(struct Mailbox *m)
Create a Hash Table to store the labels.
Definition: mutt_header.c:359
void mutt_env_to_local(struct Envelope *env)
Convert an Envelope&#39;s Address fields to local format.
Definition: envelope.c:271
SecurityFlags mutt_parse_crypt_hdr(const char *p, bool set_empty_signas, SecurityFlags crypt_app)
Parse a crypto header string.
Definition: postpone.c:500
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:168
char * data
String.
Definition: list.h:35
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:327
int mutt_label_message(struct Mailbox *m, struct EmailList *el)
Let the user label a message.
Definition: mutt_header.c:125
Log at debug level 1.
Definition: logging.h:40
int n
Definition: acutest.h:492
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:271
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
struct Email * email
Email in the list.
Definition: email.h:116
#define mutt_error(...)
Definition: logging.h:84
time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
Decrease a file&#39;s modification time by 1 second.
Definition: file.c:964
#define FREE(x)
Definition: memory.h:40
#define STAILQ_EMPTY(head)
Definition: queue.h:345
#define STAILQ_SWAP(head1, head2, type)
Definition: queue.h:425
The item stored in a Hash Table.
Definition: hash.h:42
List of Emails.
Definition: email.h:114
Handling of global boolean variables.
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
Miscellaneous functions for sending an email.
WHERE bool OptNewsSend
(pseudo) used to change behavior when posting
Definition: options.h:47
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:76
Convenience wrapper for the library headers.
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:588
struct HashElem * mutt_hash_insert(struct Hash *table, const char *strkey, void *data)
Add a new element to the Hash table (with string keys)
Definition: hash.c:351
struct ListHead references
message references (in reverse order)
Definition: envelope.h:81
A List node for strings.
Definition: list.h:33
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
parses an RFC822 header
Definition: parse.c:1137
char * x_label
X-Label.
Definition: envelope.h:72
#define STAILQ_FIRST(head)
Definition: queue.h:347
void mutt_edit_file(const char *editor, const char *file)
Let the user edit a file.
Definition: curs_lib.c:353
struct Hash * label_hash
Hash table for x-labels.
Definition: mailbox.h:129
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:641
static bool label_message(struct Mailbox *m, struct Email *e, char *new_label)
add an X-Label: field
Definition: mutt_header.c:101
The header of an Email.
Definition: envelope.h:54
static void label_ref_inc(struct Mailbox *m, char *label)
Increase the refcount of a label.
Definition: mutt_header.c:77