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