NeoMutt  2021-10-29-225-gb9986f
Teaching an old dog new tricks
DOXYGEN
attachments.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include "mutt/lib.h"
35 #include "config/lib.h"
36 #include "email/lib.h"
37 #include "core/lib.h"
38 #include "gui/lib.h"
39 #include "mutt.h"
40 #include "attachments.h"
41 #include "ncrypt/lib.h"
42 #include "init.h"
43 #include "mutt_commands.h"
44 
49 {
50  const char *major;
51  enum ContentType major_int;
52  const char *minor;
53  regex_t minor_regex;
54 };
55 
60 static struct Notify *AttachmentsNotify = NULL;
61 
69 static void attachmatch_free(struct AttachMatch **ptr)
70 {
71  if (!ptr || !*ptr)
72  return;
73 
74  struct AttachMatch *am = *ptr;
75  regfree(&am->minor_regex);
76  FREE(&am->major);
77  FREE(ptr);
78 }
79 
84 static struct AttachMatch *attachmatch_new(void)
85 {
86  return mutt_mem_calloc(1, sizeof(struct AttachMatch));
87 }
88 
92 void attach_free(void)
93 {
95 
96  /* Lists of AttachMatch */
101 }
102 
106 void attach_init(void)
107 {
108  if (AttachmentsNotify)
109  return;
110 
113 }
114 
122 static bool count_body_parts_check(struct ListHead *checklist, struct Body *b, bool dflt)
123 {
124  /* If list is null, use default behavior. */
125  if (!checklist || STAILQ_EMPTY(checklist))
126  {
127  return false;
128  }
129 
130  struct AttachMatch *a = NULL;
131  struct ListNode *np = NULL;
132  STAILQ_FOREACH(np, checklist, entries)
133  {
134  a = (struct AttachMatch *) np->data;
135  mutt_debug(LL_DEBUG3, "%s %d/%s ?? %s/%s [%d]... ", dflt ? "[OK] " : "[EXCL] ",
136  b->type, b->subtype ? b->subtype : "*", a->major, a->minor, a->major_int);
137  if (((a->major_int == TYPE_ANY) || (a->major_int == b->type)) &&
138  (!b->subtype || (regexec(&a->minor_regex, b->subtype, 0, NULL, 0) == 0)))
139  {
140  mutt_debug(LL_DEBUG3, "yes\n");
141  return true;
142  }
143  else
144  {
145  mutt_debug(LL_DEBUG3, "no\n");
146  }
147  }
148 
149  return false;
150 }
151 
157 static int count_body_parts(struct Body *body)
158 {
159  if (!body)
160  return 0;
161 
162  int count = 0;
163 
164  for (struct Body *bp = body; bp; bp = bp->next)
165  {
166  /* Initial disposition is to count and not to recurse this part. */
167  bool shallcount = true; /* default */
168  bool shallrecurse = false;
169 
170  mutt_debug(LL_DEBUG5, "desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
171  bp->description ? bp->description : ("none"),
172  bp->filename ? bp->filename :
173  bp->d_filename ? bp->d_filename :
174  "(none)",
175  bp->type, bp->subtype ? bp->subtype : "*");
176 
177  if (bp->type == TYPE_MESSAGE)
178  {
179  shallrecurse = true;
180 
181  /* If it's an external body pointer, don't recurse it. */
182  if (mutt_istr_equal(bp->subtype, "external-body"))
183  shallrecurse = false;
184  }
185  else if (bp->type == TYPE_MULTIPART)
186  {
187  /* Always recurse multiparts, except multipart/alternative. */
188  shallrecurse = true;
189  if (mutt_istr_equal(bp->subtype, "alternative"))
190  {
191  const bool c_count_alternatives =
192  cs_subset_bool(NeoMutt->sub, "count_alternatives");
193  shallrecurse = c_count_alternatives;
194  }
195  }
196 
197  if ((bp->disposition == DISP_INLINE) && (bp->type != TYPE_MULTIPART) &&
198  (bp->type != TYPE_MESSAGE) && (bp == body))
199  {
200  shallcount = false; /* ignore fundamental inlines */
201  }
202 
203  /* If this body isn't scheduled for enumeration already, don't bother
204  * profiling it further. */
205  if (shallcount)
206  {
207  /* Turn off shallcount if message type is not in ok list,
208  * or if it is in except list. Check is done separately for
209  * inlines vs. attachments. */
210 
211  if (bp->disposition == DISP_ATTACH)
212  {
213  if (!count_body_parts_check(&AttachAllow, bp, true))
214  shallcount = false; /* attach not allowed */
215  if (count_body_parts_check(&AttachExclude, bp, false))
216  shallcount = false; /* attach excluded */
217  }
218  else
219  {
220  if (!count_body_parts_check(&InlineAllow, bp, true))
221  shallcount = false; /* inline not allowed */
222  if (count_body_parts_check(&InlineExclude, bp, false))
223  shallcount = false; /* excluded */
224  }
225  }
226 
227  if (shallcount)
228  count++;
229  bp->attach_qualifies = shallcount;
230 
231  mutt_debug(LL_DEBUG3, "%p shallcount = %d\n", (void *) bp, shallcount);
232 
233  if (shallrecurse)
234  {
235  mutt_debug(LL_DEBUG3, "%p pre count = %d\n", (void *) bp, count);
236  bp->attach_count = count_body_parts(bp->parts);
237  count += bp->attach_count;
238  mutt_debug(LL_DEBUG3, "%p post count = %d\n", (void *) bp, count);
239  }
240  }
241 
242  mutt_debug(LL_DEBUG3, "return %d\n", (count < 0) ? 0 : count);
243  return (count < 0) ? 0 : count;
244 }
245 
253 int mutt_count_body_parts(struct Mailbox *m, struct Email *e, FILE *fp)
254 {
255  if (!m || !e)
256  return 0;
257 
258  bool keep_parts = false;
259 
260  if (e->attach_valid)
261  return e->attach_total;
262 
263  if (e->body->parts)
264  keep_parts = true;
265  else
267 
270  {
272  }
273  else
274  e->attach_total = 0;
275 
276  e->attach_valid = true;
277 
278  if (!keep_parts)
279  mutt_body_free(&e->body->parts);
280 
281  return e->attach_total;
282 }
283 
288 {
289  if (!m)
290  return;
291 
292  for (int i = 0; i < m->msg_count; i++)
293  {
294  struct Email *e = m->emails[i];
295  if (!e)
296  break;
297  e->attach_valid = false;
298  e->attach_total = 0;
299  }
300 }
301 
310 static enum CommandResult parse_attach_list(struct Buffer *buf, struct Buffer *s,
311  struct ListHead *head, struct Buffer *err)
312 {
313  struct AttachMatch *a = NULL;
314  char *p = NULL;
315  char *tmpminor = NULL;
316  size_t len;
317  int ret;
318 
319  do
320  {
322 
323  if (!buf->data || (*buf->data == '\0'))
324  continue;
325 
326  a = attachmatch_new();
327 
328  /* some cheap hacks that I expect to remove */
329  if (mutt_istr_equal(buf->data, "any"))
330  a->major = mutt_str_dup("*/.*");
331  else if (mutt_istr_equal(buf->data, "none"))
332  a->major = mutt_str_dup("cheap_hack/this_should_never_match");
333  else
334  a->major = mutt_str_dup(buf->data);
335 
336  p = strchr(a->major, '/');
337  if (p)
338  {
339  *p = '\0';
340  p++;
341  a->minor = p;
342  }
343  else
344  {
345  a->minor = "unknown";
346  }
347 
348  len = strlen(a->minor);
349  tmpminor = mutt_mem_malloc(len + 3);
350  strcpy(&tmpminor[1], a->minor);
351  tmpminor[0] = '^';
352  tmpminor[len + 1] = '$';
353  tmpminor[len + 2] = '\0';
354 
356  ret = REG_COMP(&a->minor_regex, tmpminor, REG_ICASE);
357 
358  FREE(&tmpminor);
359 
360  if (ret != 0)
361  {
362  regerror(ret, &a->minor_regex, err->data, err->dsize);
363  FREE(&a->major);
364  FREE(&a);
365  return MUTT_CMD_ERROR;
366  }
367 
368  mutt_debug(LL_DEBUG3, "added %s/%s [%d]\n", a->major, a->minor, a->major_int);
369 
370  mutt_list_insert_tail(head, (char *) a);
371  } while (MoreArgs(s));
372 
373  if (!a)
374  return MUTT_CMD_ERROR;
375 
376  mutt_debug(LL_NOTIFY, "NT_ATTACH_ADD: %s/%s\n", a->major, a->minor);
378 
379  return MUTT_CMD_SUCCESS;
380 }
381 
390 static enum CommandResult parse_unattach_list(struct Buffer *buf, struct Buffer *s,
391  struct ListHead *head, struct Buffer *err)
392 {
393  struct AttachMatch *a = NULL;
394  char *tmp = NULL;
395  char *minor = NULL;
396 
397  do
398  {
400  FREE(&tmp);
401 
402  if (mutt_istr_equal(buf->data, "any"))
403  tmp = mutt_str_dup("*/.*");
404  else if (mutt_istr_equal(buf->data, "none"))
405  tmp = mutt_str_dup("cheap_hack/this_should_never_match");
406  else
407  tmp = mutt_str_dup(buf->data);
408 
409  minor = strchr(tmp, '/');
410  if (minor)
411  {
412  *minor = '\0';
413  minor++;
414  }
415  else
416  {
417  minor = "unknown";
418  }
419  const enum ContentType major = mutt_check_mime_type(tmp);
420 
421  struct ListNode *np = NULL, *tmp2 = NULL;
422  STAILQ_FOREACH_SAFE(np, head, entries, tmp2)
423  {
424  a = (struct AttachMatch *) np->data;
425  mutt_debug(LL_DEBUG3, "check %s/%s [%d] : %s/%s [%d]\n", a->major,
426  a->minor, a->major_int, tmp, minor, major);
427  if ((a->major_int == major) && mutt_istr_equal(minor, a->minor))
428  {
429  mutt_debug(LL_DEBUG3, "removed %s/%s [%d]\n", a->major, a->minor, a->major_int);
430  mutt_debug(LL_NOTIFY, "NT_ATTACH_DELETE: %s/%s\n", a->major, a->minor);
431 
432  regfree(&a->minor_regex);
433  FREE(&a->major);
434  STAILQ_REMOVE(head, np, ListNode, entries);
435  FREE(&np->data);
436  FREE(&np);
437  }
438  }
439 
440  } while (MoreArgs(s));
441 
442  FREE(&tmp);
443 
445 
446  return MUTT_CMD_SUCCESS;
447 }
448 
456 static int print_attach_list(struct ListHead *h, const char op, const char *name)
457 {
458  struct ListNode *np = NULL;
459  STAILQ_FOREACH(np, h, entries)
460  {
461  printf("attachments %c%s %s/%s\n", op, name,
462  ((struct AttachMatch *) np->data)->major,
463  ((struct AttachMatch *) np->data)->minor);
464  }
465 
466  return 0;
467 }
468 
472 enum CommandResult parse_attachments(struct Buffer *buf, struct Buffer *s,
473  intptr_t data, struct Buffer *err)
474 {
476  if (!buf->data || (*buf->data == '\0'))
477  {
478  mutt_buffer_strcpy(err, _("attachments: no disposition"));
479  return MUTT_CMD_WARNING;
480  }
481 
482  char *category = buf->data;
483  char op = *category++;
484 
485  if (op == '?')
486  {
487  mutt_endwin();
488  fflush(stdout);
489  printf("\n%s\n\n", _("Current attachments settings:"));
490  print_attach_list(&AttachAllow, '+', "A");
491  print_attach_list(&AttachExclude, '-', "A");
492  print_attach_list(&InlineAllow, '+', "I");
493  print_attach_list(&InlineExclude, '-', "I");
495  return MUTT_CMD_SUCCESS;
496  }
497 
498  if ((op != '+') && (op != '-'))
499  {
500  op = '+';
501  category--;
502  }
503 
504  struct ListHead *head = NULL;
505  if (mutt_istr_startswith("attachment", category))
506  {
507  if (op == '+')
508  head = &AttachAllow;
509  else
510  head = &AttachExclude;
511  }
512  else if (mutt_istr_startswith("inline", category))
513  {
514  if (op == '+')
515  head = &InlineAllow;
516  else
517  head = &InlineExclude;
518  }
519  else
520  {
521  mutt_buffer_strcpy(err, _("attachments: invalid disposition"));
522  return MUTT_CMD_ERROR;
523  }
524 
525  return parse_attach_list(buf, s, head, err);
526 }
527 
531 enum CommandResult parse_unattachments(struct Buffer *buf, struct Buffer *s,
532  intptr_t data, struct Buffer *err)
533 {
534  char op;
535  char *p = NULL;
536  struct ListHead *head = NULL;
537 
539  if (!buf->data || (*buf->data == '\0'))
540  {
541  mutt_buffer_strcpy(err, _("unattachments: no disposition"));
542  return MUTT_CMD_WARNING;
543  }
544 
545  p = buf->data;
546  op = *p++;
547 
548  if (op == '*')
549  {
554 
555  mutt_debug(LL_NOTIFY, "NT_ATTACH_DELETE_ALL\n");
557  return 0;
558  }
559 
560  if ((op != '+') && (op != '-'))
561  {
562  op = '+';
563  p--;
564  }
565  if (mutt_istr_startswith("attachment", p))
566  {
567  if (op == '+')
568  head = &AttachAllow;
569  else
570  head = &AttachExclude;
571  }
572  else if (mutt_istr_startswith("inline", p))
573  {
574  if (op == '+')
575  head = &InlineAllow;
576  else
577  head = &InlineExclude;
578  }
579  else
580  {
581  mutt_buffer_strcpy(err, _("unattachments: invalid disposition"));
582  return MUTT_CMD_ERROR;
583  }
584 
585  return parse_unattach_list(buf, s, head, err);
586 }
587 
593 void mutt_parse_mime_message(struct Email *e, FILE *fp)
594 {
595  const bool right_type =
596  (e->body->type == TYPE_MESSAGE) || (e->body->type == TYPE_MULTIPART);
597  const bool not_parsed = (e->body->parts == NULL);
598 
599  if (right_type && fp && not_parsed)
600  {
601  mutt_parse_part(fp, e->body);
602  if (WithCrypto)
603  {
604  e->security = crypt_query(e->body);
605  }
606  }
607 
608  e->attach_valid = false;
609 }
struct ListHead AttachAllow
List of attachment types to be counted.
Definition: attachments.c:56
void mutt_attachments_reset(struct Mailbox *m)
Reset the attachment count for all Emails.
Definition: attachments.c:287
static struct AttachMatch * attachmatch_new(void)
Create a new AttachMatch.
Definition: attachments.c:84
static int print_attach_list(struct ListHead *h, const char op, const char *name)
Print a list of attachments.
Definition: attachments.c:456
struct ListHead InlineExclude
List of inline types to ignore.
Definition: attachments.c:59
void mutt_parse_mime_message(struct Email *e, FILE *fp)
Parse a MIME email.
Definition: attachments.c:593
static int count_body_parts(struct Body *body)
Count the MIME Body parts.
Definition: attachments.c:157
void attach_free(void)
Free the attachments lists.
Definition: attachments.c:92
struct ListHead AttachExclude
List of attachment types to be ignored.
Definition: attachments.c:57
void attach_init(void)
Set up the attachments lists.
Definition: attachments.c:106
static struct Notify * AttachmentsNotify
Definition: attachments.c:60
static enum CommandResult parse_unattach_list(struct Buffer *buf, struct Buffer *s, struct ListHead *head, struct Buffer *err)
Parse the "unattachments" command.
Definition: attachments.c:390
int mutt_count_body_parts(struct Mailbox *m, struct Email *e, FILE *fp)
Count the MIME Body parts.
Definition: attachments.c:253
static bool count_body_parts_check(struct ListHead *checklist, struct Body *b, bool dflt)
Compares mime types to the ok and except lists.
Definition: attachments.c:122
static enum CommandResult parse_attach_list(struct Buffer *buf, struct Buffer *s, struct ListHead *head, struct Buffer *err)
Parse the "attachments" command.
Definition: attachments.c:310
struct ListHead InlineAllow
List of inline types to counted.
Definition: attachments.c:58
Miscellaneous email parsing routines.
@ NT_ATTACH_DELETE
Attachment regex has been deleted.
Definition: attachments.h:39
@ NT_ATTACH_DELETE_ALL
All Attachment regexes have been deleted.
Definition: attachments.h:40
@ NT_ATTACH_ADD
Attachment regex has been added.
Definition: attachments.h:38
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
#define MoreArgs(buf)
Definition: buffer.h:40
CommandResult
Error codes for command_t parse functions.
Definition: command.h:34
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:37
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:35
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:36
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
SecurityFlags crypt_query(struct Body *b)
Check out the type of encryption used.
Definition: crypt.c:692
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:427
void mutt_endwin(void)
Shutdown curses.
Definition: curs_lib.c:394
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
Structs that make up an email.
enum CommandResult parse_unattachments(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'unattachments' command - Implements Command::parse() -.
Definition: attachments.c:531
enum CommandResult parse_attachments(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'attachments' command - Implements Command::parse() -.
Definition: attachments.c:472
static void attachmatch_free(struct AttachMatch **ptr)
Free an AttachMatch - Implements list_free_t -.
Definition: attachments.c:69
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
Convenience wrapper for the gui headers.
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:399
Config/command parsing.
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_type(struct ListHead *h, list_free_t fn)
Free a List of type.
Definition: list.c:144
void(* list_free_t)(void **ptr)
Definition: list.h:48
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:40
ContentType
Content-Type.
Definition: mime.h:30
@ TYPE_MESSAGE
Type: 'message/*'.
Definition: mime.h:35
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition: mime.h:37
@ TYPE_ANY
Type: '*' or '.*'.
Definition: mime.h:40
@ DISP_ATTACH
Content is attached.
Definition: mime.h:63
@ DISP_INLINE
Content is inline.
Definition: mime.h:62
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
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
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:727
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:181
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:170
Many unsorted constants and some structs.
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:66
Definitions of NeoMutt commands.
API for encryption/signing of emails.
#define WithCrypto
Definition: lib.h:113
@ NT_ATTACH
Attachment command changed, NotifyAttach.
Definition: notify_type.h:37
void mutt_parse_part(FILE *fp, struct Body *b)
Parse a MIME part.
Definition: parse.c:1730
enum ContentType mutt_check_mime_type(const char *s)
Check a MIME type string.
Definition: parse.c:324
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_EMPTY(head)
Definition: queue.h:348
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:54
An attachment matching a regex for attachment counter.
Definition: attachments.c:49
const char * minor
Minor mime type, e.g. "html".
Definition: attachments.c:52
regex_t minor_regex
Minor mime type regex.
Definition: attachments.c:53
const char * major
Major mime type, e.g. "text".
Definition: attachments.c:50
enum ContentType major_int
Major mime type, e.g. TYPE_TEXT.
Definition: attachments.c:51
The body of an email.
Definition: body.h:35
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:71
struct Body * next
next attachment in the list
Definition: body.h:70
char * subtype
content-type subtype
Definition: body.h:59
unsigned int type
content-type primary type, ContentType
Definition: body.h:39
String manipulation buffer.
Definition: buffer.h:34
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
The envelope/body of an email.
Definition: email.h:37
bool attach_valid
true when the attachment count is valid
Definition: email.h:100
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
short attach_total
Number of qualifying attachments in message, if attach_valid.
Definition: email.h:115
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
A mailbox.
Definition: mailbox.h:82
int msg_count
Total number of messages.
Definition: mailbox.h:91
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Notification API.
Definition: notify.c:51