forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.c
1920 lines (1713 loc) · 51.2 KB
/
handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Decide how to display email content
*
* @authors
* Copyright (C) 1996-2000,2002,2010,2013 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page neo_handler Decide how to display email content
*
* Decide how to display email content
*/
#include "config.h"
#include <stddef.h>
#include <ctype.h>
#include <iconv.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "mutt.h"
#include "handler.h"
#include "attach/lib.h"
#include "menu/lib.h"
#include "ncrypt/lib.h"
#include "pager/lib.h"
#include "copy.h"
#include "enriched.h"
#include "keymap.h"
#include "mailcap.h"
#include "mutt_globals.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "opcodes.h"
#include "options.h"
#include "rfc3676.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#define BUFI_SIZE 1000
#define BUFO_SIZE 2000
#define TXT_HTML 1
#define TXT_PLAIN 2
#define TXT_ENRICHED 3
/**
* @defgroup handler_api Mime Handler API
*
* Prototype for a function to handle MIME parts
*
* @param b Body of the email
* @param s State of text being processed
* @retval 0 Success
* @retval -1 Error
*/
typedef int (*handler_t)(struct Body *b, struct State *s);
/**
* print_part_line - Print a separator for the Mime part
* @param s State of text being processed
* @param b Body of the email
* @param n Part number for multipart emails (0 otherwise)
*/
static void print_part_line(struct State *s, struct Body *b, int n)
{
char length[5];
mutt_str_pretty_size(length, sizeof(length), b->length);
state_mark_attach(s);
char *charset = mutt_param_get(&b->parameter, "charset");
if (n == 0)
{
state_printf(s, _("[-- Type: %s/%s%s%s, Encoding: %s, Size: %s --]\n"),
TYPE(b), b->subtype, charset ? "; charset=" : "",
charset ? charset : "", ENCODING(b->encoding), length);
}
else
{
state_printf(s, _("[-- Alternative Type #%d: %s/%s%s%s, Encoding: %s, Size: %s --]\n"),
n, TYPE(b), b->subtype, charset ? "; charset=" : "",
charset ? charset : "", ENCODING(b->encoding), length);
}
}
/**
* convert_to_state - Convert text and write it to a file
* @param cd Iconv conversion descriptor
* @param bufi Buffer with text to convert
* @param l Length of buffer
* @param s State to write to
*/
static void convert_to_state(iconv_t cd, char *bufi, size_t *l, struct State *s)
{
char bufo[BUFO_SIZE];
const char *ib = NULL;
char *ob = NULL;
size_t ibl, obl;
if (!bufi)
{
if (cd != (iconv_t) (-1))
{
ob = bufo;
obl = sizeof(bufo);
iconv(cd, NULL, NULL, &ob, &obl);
if (ob != bufo)
state_prefix_put(s, bufo, ob - bufo);
}
return;
}
if (cd == (iconv_t) (-1))
{
state_prefix_put(s, bufi, *l);
*l = 0;
return;
}
ib = bufi;
ibl = *l;
while (true)
{
ob = bufo;
obl = sizeof(bufo);
mutt_ch_iconv(cd, &ib, &ibl, &ob, &obl, 0, "?", NULL);
if (ob == bufo)
break;
state_prefix_put(s, bufo, ob - bufo);
}
memmove(bufi, ib, ibl);
*l = ibl;
}
/**
* decode_xbit - Decode xbit-encoded text
* @param s State to work with
* @param len Length of text to decode
* @param istext Mime part is plain text
* @param cd Iconv conversion descriptor
*/
static void decode_xbit(struct State *s, long len, bool istext, iconv_t cd)
{
if (!istext)
{
mutt_file_copy_bytes(s->fp_in, s->fp_out, len);
return;
}
state_set_prefix(s);
int c;
char bufi[BUFI_SIZE];
size_t l = 0;
while (((c = fgetc(s->fp_in)) != EOF) && len--)
{
if ((c == '\r') && len)
{
const int ch = fgetc(s->fp_in);
if (ch == '\n')
{
c = ch;
len--;
}
else
ungetc(ch, s->fp_in);
}
bufi[l++] = c;
if (l == sizeof(bufi))
convert_to_state(cd, bufi, &l, s);
}
convert_to_state(cd, bufi, &l, s);
convert_to_state(cd, 0, 0, s);
state_reset_prefix(s);
}
/**
* qp_decode_triple - Decode a quoted-printable triplet
* @param s State to work with
* @param d Decoded character
* @retval 0 Success
* @retval -1 Error
*/
static int qp_decode_triple(char *s, char *d)
{
/* soft line break */
if ((s[0] == '=') && (s[1] == '\0'))
return 1;
/* quoted-printable triple */
if ((s[0] == '=') && isxdigit((unsigned char) s[1]) && isxdigit((unsigned char) s[2]))
{
*d = (hexval(s[1]) << 4) | hexval(s[2]);
return 0;
}
/* something else */
return -1;
}
/**
* qp_decode_line - Decode a line of quoted-printable text
* @param dest Buffer for result
* @param src Text to decode
* @param l Bytes written to buffer
* @param last Last character of the line
*/
static void qp_decode_line(char *dest, char *src, size_t *l, int last)
{
char *d = NULL, *s = NULL;
char c = 0;
int kind = -1;
bool soft = false;
/* decode the line */
for (d = dest, s = src; *s;)
{
switch ((kind = qp_decode_triple(s, &c)))
{
case 0:
*d++ = c;
s += 3;
break; /* qp triple */
case -1:
*d++ = *s++;
break; /* single character */
case 1:
soft = true;
s++;
break; /* soft line break */
}
}
if (!soft && (last == '\n'))
{
/* neither \r nor \n as part of line-terminating CRLF
* may be qp-encoded, so remove \r and \n-terminate;
* see RFC2045, sect. 6.7, (1): General 8bit representation */
if ((kind == 0) && (c == '\r'))
*(d - 1) = '\n';
else
*d++ = '\n';
}
*d = '\0';
*l = d - dest;
}
/**
* decode_quoted - Decode an attachment encoded with quoted-printable
* @param s State to work with
* @param len Length of text to decode
* @param istext Mime part is plain text
* @param cd Iconv conversion descriptor
*
* Why doesn't this overflow any buffers? First, it's guaranteed that the
* length of a line grows when you _en_-code it to quoted-printable. That
* means that we always can store the result in a buffer of at most the _same_
* size.
*
* Now, we don't special-case if the line we read with fgets() isn't
* terminated. We don't care about this, since 256 > 78, so corrupted input
* will just be corrupted a bit more. That implies that 256+1 bytes are
* always sufficient to store the result of qp_decode_line.
*
* Finally, at soft line breaks, some part of a multibyte character may have
* been left over by convert_to_state(). This shouldn't be more than 6
* characters, so 256+7 should be sufficient memory to store the decoded
* data.
*
* Just to make sure that I didn't make some off-by-one error above, we just
* use 512 for the target buffer's size.
*/
static void decode_quoted(struct State *s, long len, bool istext, iconv_t cd)
{
char line[256];
char decline[512];
size_t l = 0;
size_t l3;
if (istext)
state_set_prefix(s);
while (len > 0)
{
/* It's ok to use a fixed size buffer for input, even if the line turns
* out to be longer than this. Just process the line in chunks. This
* really shouldn't happen according the MIME spec, since Q-P encoded
* lines are at most 76 characters, but we should be liberal about what
* we accept. */
if (!fgets(line, MIN((ssize_t) sizeof(line), len + 1), s->fp_in))
break;
size_t linelen = strlen(line);
len -= linelen;
/* inspect the last character we read so we can tell if we got the
* entire line. */
const int last = (linelen != 0) ? line[linelen - 1] : 0;
/* chop trailing whitespace if we got the full line */
if (last == '\n')
{
while ((linelen > 0) && IS_SPACE(line[linelen - 1]))
linelen--;
line[linelen] = '\0';
}
/* decode and do character set conversion */
qp_decode_line(decline + l, line, &l3, last);
l += l3;
convert_to_state(cd, decline, &l, s);
}
convert_to_state(cd, 0, 0, s);
state_reset_prefix(s);
}
/**
* decode_byte - Decode a uuencoded byte
* @param ch Character to decode
* @retval num Decoded value
*/
static unsigned char decode_byte(char ch)
{
if ((ch < 32) || (ch > 95))
return 0;
return ch - 32;
}
/**
* decode_uuencoded - Decode uuencoded text
* @param s State to work with
* @param len Length of text to decode
* @param istext Mime part is plain text
* @param cd Iconv conversion descriptor
*/
static void decode_uuencoded(struct State *s, long len, bool istext, iconv_t cd)
{
char tmps[128];
char *pt = NULL;
char bufi[BUFI_SIZE];
size_t k = 0;
if (istext)
state_set_prefix(s);
while (len > 0)
{
if (!fgets(tmps, sizeof(tmps), s->fp_in))
return;
len -= mutt_str_len(tmps);
if (mutt_str_startswith(tmps, "begin "))
break;
}
while (len > 0)
{
if (!fgets(tmps, sizeof(tmps), s->fp_in))
return;
len -= mutt_str_len(tmps);
if (mutt_str_startswith(tmps, "end"))
break;
pt = tmps;
const unsigned char linelen = decode_byte(*pt);
pt++;
for (unsigned char c = 0; c < linelen;)
{
for (char l = 2; l <= 6; l += 2)
{
char out = decode_byte(*pt) << l;
pt++;
out |= (decode_byte(*pt) >> (6 - l));
bufi[k++] = out;
c++;
if (c == linelen)
break;
}
convert_to_state(cd, bufi, &k, s);
pt++;
}
}
convert_to_state(cd, bufi, &k, s);
convert_to_state(cd, 0, 0, s);
state_reset_prefix(s);
}
/**
* is_mmnoask - Metamail compatibility: should the attachment be autoviewed?
* @param buf Mime type, e.g. 'text/plain'
* @retval true Metamail "no ask" is true
*
* Test if the `MM_NOASK` environment variable should allow autoviewing of the
* attachment.
*
* @note If `MM_NOASK=1` then the function will automatically return true.
*/
static bool is_mmnoask(const char *buf)
{
const char *val = mutt_str_getenv("MM_NOASK");
if (!val)
return false;
char *p = NULL;
char tmp[1024];
char *q = NULL;
if (mutt_str_equal(val, "1"))
return true;
mutt_str_copy(tmp, val, sizeof(tmp));
p = tmp;
while ((p = strtok(p, ",")))
{
q = strrchr(p, '/');
if (q)
{
if (q[1] == '*')
{
if (mutt_istrn_equal(buf, p, q - p))
return true;
}
else
{
if (mutt_istr_equal(buf, p))
return true;
}
}
else
{
const size_t plen = mutt_istr_startswith(buf, p);
if ((plen != 0) && (buf[plen] == '/'))
return true;
}
p = NULL;
}
return false;
}
/**
* is_autoview - Should email body be filtered by mailcap
* @param b Body of the email
* @retval 1 body part should be filtered by a mailcap entry prior to viewing inline
* @retval 0 otherwise
*/
static bool is_autoview(struct Body *b)
{
char type[256];
bool is_av = false;
snprintf(type, sizeof(type), "%s/%s", TYPE(b), b->subtype);
const bool c_implicit_autoview =
cs_subset_bool(NeoMutt->sub, "implicit_autoview");
if (c_implicit_autoview)
{
/* $implicit_autoview is essentially the same as "auto_view *" */
is_av = true;
}
else
{
/* determine if this type is on the user's auto_view list */
mutt_check_lookup_list(b, type, sizeof(type));
struct ListNode *np = NULL;
STAILQ_FOREACH(np, &AutoViewList, entries)
{
int i = mutt_str_len(np->data) - 1;
if (((i > 0) && (np->data[i - 1] == '/') && (np->data[i] == '*') &&
mutt_istrn_equal(type, np->data, i)) ||
mutt_istr_equal(type, np->data))
{
is_av = true;
break;
}
}
if (is_mmnoask(type))
is_av = true;
}
/* determine if there is a mailcap entry suitable for auto_view
*
* @warning type is altered by this call as a result of 'mime_lookup' support */
if (is_av)
return mailcap_lookup(b, type, sizeof(type), NULL, MUTT_MC_AUTOVIEW);
return false;
}
/**
* autoview_handler - Handler for autoviewable attachments - Implements ::handler_t - @ingroup handler_api
*/
static int autoview_handler(struct Body *a, struct State *s)
{
struct MailcapEntry *entry = mailcap_entry_new();
char buf[1024];
char type[256];
struct Buffer *cmd = mutt_buffer_pool_get();
struct Buffer *tempfile = mutt_buffer_pool_get();
char *fname = NULL;
FILE *fp_in = NULL;
FILE *fp_out = NULL;
FILE *fp_err = NULL;
pid_t pid;
int rc = 0;
snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
mailcap_lookup(a, type, sizeof(type), entry, MUTT_MC_AUTOVIEW);
fname = mutt_str_dup(a->filename);
mutt_file_sanitize_filename(fname, true);
mailcap_expand_filename(entry->nametemplate, fname, tempfile);
FREE(&fname);
if (entry->command)
{
mutt_buffer_strcpy(cmd, entry->command);
/* mailcap_expand_command returns 0 if the file is required */
bool piped = mailcap_expand_command(a, mutt_buffer_string(tempfile), type, cmd);
if (s->flags & MUTT_DISPLAY)
{
state_mark_attach(s);
state_printf(s, _("[-- Autoview using %s --]\n"), mutt_buffer_string(cmd));
mutt_message(_("Invoking autoview command: %s"), mutt_buffer_string(cmd));
}
fp_in = mutt_file_fopen(mutt_buffer_string(tempfile), "w+");
if (!fp_in)
{
mutt_perror("fopen");
mailcap_entry_free(&entry);
rc = -1;
goto cleanup;
}
mutt_file_copy_bytes(s->fp_in, fp_in, a->length);
if (piped)
{
unlink(mutt_buffer_string(tempfile));
fflush(fp_in);
rewind(fp_in);
pid = filter_create_fd(mutt_buffer_string(cmd), NULL, &fp_out, &fp_err,
fileno(fp_in), -1, -1);
}
else
{
mutt_file_fclose(&fp_in);
pid = filter_create(mutt_buffer_string(cmd), NULL, &fp_out, &fp_err);
}
if (pid < 0)
{
mutt_perror(_("Can't create filter"));
if (s->flags & MUTT_DISPLAY)
{
state_mark_attach(s);
state_printf(s, _("[-- Can't run %s. --]\n"), mutt_buffer_string(cmd));
}
rc = -1;
goto bail;
}
if (s->prefix)
{
/* Remove ansi and formatting from autoview output in replies only. The
* user may want to see the formatting in the pager, but it shouldn't be
* in their quoted reply text too. */
struct Buffer *stripped = mutt_buffer_pool_get();
while (fgets(buf, sizeof(buf), fp_out))
{
mutt_buffer_strip_formatting(stripped, buf, false);
state_puts(s, s->prefix);
state_puts(s, mutt_buffer_string(stripped));
}
mutt_buffer_pool_release(&stripped);
/* check for data on stderr */
if (fgets(buf, sizeof(buf), fp_err))
{
if (s->flags & MUTT_DISPLAY)
{
state_mark_attach(s);
state_printf(s, _("[-- Autoview stderr of %s --]\n"), mutt_buffer_string(cmd));
}
state_puts(s, s->prefix);
state_puts(s, buf);
while (fgets(buf, sizeof(buf), fp_err))
{
state_puts(s, s->prefix);
state_puts(s, buf);
}
}
}
else
{
mutt_file_copy_stream(fp_out, s->fp_out);
/* Check for stderr messages */
if (fgets(buf, sizeof(buf), fp_err))
{
if (s->flags & MUTT_DISPLAY)
{
state_mark_attach(s);
state_printf(s, _("[-- Autoview stderr of %s --]\n"), mutt_buffer_string(cmd));
}
state_puts(s, buf);
mutt_file_copy_stream(fp_err, s->fp_out);
}
}
bail:
mutt_file_fclose(&fp_out);
mutt_file_fclose(&fp_err);
filter_wait(pid);
if (piped)
mutt_file_fclose(&fp_in);
else
mutt_file_unlink(mutt_buffer_string(tempfile));
if (s->flags & MUTT_DISPLAY)
mutt_clear_error();
}
cleanup:
mailcap_entry_free(&entry);
mutt_buffer_pool_release(&cmd);
mutt_buffer_pool_release(&tempfile);
return rc;
}
/**
* text_plain_handler - Handler for plain text - Implements ::handler_t - @ingroup handler_api
* @retval 0 Always
*
* When generating format=flowed ($text_flowed is set) from format=fixed, strip
* all trailing spaces to improve interoperability; if $text_flowed is unset,
* simply verbatim copy input.
*/
static int text_plain_handler(struct Body *b, struct State *s)
{
char *buf = NULL;
size_t sz = 0;
while ((buf = mutt_file_read_line(buf, &sz, s->fp_in, NULL, MUTT_RL_NO_FLAGS)))
{
const bool c_text_flowed = cs_subset_bool(NeoMutt->sub, "text_flowed");
if (!mutt_str_equal(buf, "-- ") && c_text_flowed)
{
size_t len = mutt_str_len(buf);
while ((len > 0) && (buf[len - 1] == ' '))
buf[--len] = '\0';
}
if (s->prefix)
state_puts(s, s->prefix);
state_puts(s, buf);
state_putc(s, '\n');
}
FREE(&buf);
return 0;
}
/**
* message_handler - Handler for message/rfc822 body parts - Implements ::handler_t - @ingroup handler_api
*/
static int message_handler(struct Body *a, struct State *s)
{
struct Body *b = NULL;
LOFF_T off_start;
int rc = 0;
off_start = ftello(s->fp_in);
if (off_start < 0)
return -1;
if ((a->encoding == ENC_BASE64) || (a->encoding == ENC_QUOTED_PRINTABLE) ||
(a->encoding == ENC_UUENCODED))
{
b = mutt_body_new();
b->length = mutt_file_get_size_fp(s->fp_in);
b->parts = mutt_rfc822_parse_message(s->fp_in, b);
}
else
b = a;
if (b->parts)
{
CopyHeaderFlags chflags = CH_DECODE | CH_FROM;
const bool c_weed = cs_subset_bool(NeoMutt->sub, "weed");
if ((s->flags & MUTT_WEED) || ((s->flags & (MUTT_DISPLAY | MUTT_PRINTING)) && c_weed))
chflags |= CH_WEED | CH_REORDER;
if (s->prefix)
chflags |= CH_PREFIX;
if (s->flags & MUTT_DISPLAY)
chflags |= CH_DISPLAY;
mutt_copy_hdr(s->fp_in, s->fp_out, off_start, b->parts->offset, chflags, s->prefix, 0);
if (s->prefix)
state_puts(s, s->prefix);
state_putc(s, '\n');
rc = mutt_body_handler(b->parts, s);
}
if ((a->encoding == ENC_BASE64) || (a->encoding == ENC_QUOTED_PRINTABLE) ||
(a->encoding == ENC_UUENCODED))
{
mutt_body_free(&b);
}
return rc;
}
/**
* external_body_handler - Handler for external-body emails - Implements ::handler_t - @ingroup handler_api
*/
static int external_body_handler(struct Body *b, struct State *s)
{
const char *str = NULL;
char strbuf[1024];
const char *access_type = mutt_param_get(&b->parameter, "access-type");
if (!access_type)
{
if (s->flags & MUTT_DISPLAY)
{
state_mark_attach(s);
state_puts(s, _("[-- Error: message/external-body has no access-type "
"parameter --]\n"));
return 0;
}
else
return -1;
}
const char *expiration = mutt_param_get(&b->parameter, "expiration");
time_t expire;
if (expiration)
expire = mutt_date_parse_date(expiration, NULL);
else
expire = -1;
const bool c_weed = cs_subset_bool(NeoMutt->sub, "weed");
if (mutt_istr_equal(access_type, "x-mutt-deleted"))
{
if (s->flags & (MUTT_DISPLAY | MUTT_PRINTING))
{
char pretty_size[10];
char *length = mutt_param_get(&b->parameter, "length");
if (length)
{
long size = strtol(length, NULL, 10);
mutt_str_pretty_size(pretty_size, sizeof(pretty_size), size);
if (expire != -1)
{
str = ngettext(
/* L10N: If the translation of this string is a multi line string, then
each line should start with "[-- " and end with " --]".
The first "%s/%s" is a MIME type, e.g. "text/plain". The last %s
expands to a date as returned by `mutt_date_parse_date()`.
Note: The size argument printed is not the actual number as passed
to gettext but the prettified version, e.g. size = 2048 will be
printed as 2K. Your language might be sensitive to that: For
example although '1K' and '1024' represent the same number your
language might inflect the noun 'byte' differently.
Sadly, we can't do anything about that at the moment besides
passing the precise size in bytes. If you are interested the
function responsible for the prettification is
mutt_str_pretty_size() in mutt/string.c. */
"[-- This %s/%s attachment (size %s byte) has been deleted --]\n"
"[-- on %s --]\n",
"[-- This %s/%s attachment (size %s bytes) has been deleted --]\n"
"[-- on %s --]\n",
size);
}
else
{
str = ngettext(
/* L10N: If the translation of this string is a multi line string, then
each line should start with "[-- " and end with " --]".
The first "%s/%s" is a MIME type, e.g. "text/plain".
Note: The size argument printed is not the actual number as passed
to gettext but the prettified version, e.g. size = 2048 will be
printed as 2K. Your language might be sensitive to that: For
example although '1K' and '1024' represent the same number your
language might inflect the noun 'byte' differently.
Sadly, we can't do anything about that at the moment besides
passing the precise size in bytes. If you are interested the
function responsible for the prettification is
mutt_str_pretty_size() in mutt/string.c. */
"[-- This %s/%s attachment (size %s byte) has been deleted --]\n",
"[-- This %s/%s attachment (size %s bytes) has been deleted "
"--]\n",
size);
}
}
else
{
pretty_size[0] = '\0';
if (expire != -1)
{
/* L10N: If the translation of this string is a multi line string, then
each line should start with "[-- " and end with " --]".
The first "%s/%s" is a MIME type, e.g. "text/plain". The last %s
expands to a date as returned by `mutt_date_parse_date()`.
Caution: Argument three %3$ is also defined but should not be used
in this translation! */
str = _("[-- This %s/%s attachment has been deleted --]\n[-- on %4$s "
"--]\n");
}
else
{
/* L10N: If the translation of this string is a multi line string, then
each line should start with "[-- " and end with " --]".
The first "%s/%s" is a MIME type, e.g. "text/plain". */
str = _("[-- This %s/%s attachment has been deleted --]\n");
}
}
snprintf(strbuf, sizeof(strbuf), str, TYPE(b->parts), b->parts->subtype,
pretty_size, expiration);
state_attach_puts(s, strbuf);
if (b->parts->filename)
{
state_mark_attach(s);
state_printf(s, _("[-- name: %s --]\n"), b->parts->filename);
}
CopyHeaderFlags chflags = CH_DECODE;
if (c_weed)
chflags |= CH_WEED | CH_REORDER;
mutt_copy_hdr(s->fp_in, s->fp_out, ftello(s->fp_in), b->parts->offset,
chflags, NULL, 0);
}
}
else if (expiration && (expire < mutt_date_epoch()))
{
if (s->flags & MUTT_DISPLAY)
{
/* L10N: If the translation of this string is a multi line string, then
each line should start with "[-- " and end with " --]".
The "%s/%s" is a MIME type, e.g. "text/plain". */
snprintf(strbuf, sizeof(strbuf), _("[-- This %s/%s attachment is not included, --]\n[-- and the indicated external source has --]\n[-- expired. --]\n"),
TYPE(b->parts), b->parts->subtype);
state_attach_puts(s, strbuf);
CopyHeaderFlags chflags = CH_DECODE | CH_DISPLAY;
if (c_weed)
chflags |= CH_WEED | CH_REORDER;
mutt_copy_hdr(s->fp_in, s->fp_out, ftello(s->fp_in), b->parts->offset,
chflags, NULL, 0);
}
}
else
{
if (s->flags & MUTT_DISPLAY)
{
/* L10N: If the translation of this string is a multi line string, then
each line should start with "[-- " and end with " --]".
The "%s/%s" is a MIME type, e.g. "text/plain". The %s after
access-type is an access-type as defined by the MIME RFCs, e.g. "FTP",
"LOCAL-FILE", "MAIL-SERVER". */
snprintf(strbuf, sizeof(strbuf), _("[-- This %s/%s attachment is not included, --]\n[-- and the indicated access-type %s is unsupported --]\n"),
TYPE(b->parts), b->parts->subtype, access_type);
state_attach_puts(s, strbuf);
CopyHeaderFlags chflags = CH_DECODE | CH_DISPLAY;
if (c_weed)
chflags |= CH_WEED | CH_REORDER;
mutt_copy_hdr(s->fp_in, s->fp_out, ftello(s->fp_in), b->parts->offset,
chflags, NULL, 0);
}
}
return 0;
}
/**
* alternative_handler - Handler for multipart alternative emails - Implements ::handler_t - @ingroup handler_api
*/
static int alternative_handler(struct Body *a, struct State *s)
{
struct Body *const head = a;
struct Body *choice = NULL;
struct Body *b = NULL;
bool mustfree = false;
int rc = 0;
if ((a->encoding == ENC_BASE64) || (a->encoding == ENC_QUOTED_PRINTABLE) ||
(a->encoding == ENC_UUENCODED))
{
mustfree = true;
b = mutt_body_new();
b->length = mutt_file_get_size_fp(s->fp_in);
b->parts =
mutt_parse_multipart(s->fp_in, mutt_param_get(&a->parameter, "boundary"),
b->length, mutt_istr_equal("digest", a->subtype));
}
else
b = a;
a = b;
/* First, search list of preferred types */
struct ListNode *np = NULL;
STAILQ_FOREACH(np, &AlternativeOrderList, entries)
{
int btlen; /* length of basetype */
bool wild; /* do we have a wildcard to match all subtypes? */
char *c = strchr(np->data, '/');
if (c)
{
wild = ((c[1] == '*') && (c[2] == '\0'));
btlen = c - np->data;
}
else
{
wild = true;
btlen = mutt_str_len(np->data);
}
if (a->parts)
b = a->parts;
else
b = a;
while (b)
{
const char *bt = TYPE(b);
if (mutt_istrn_equal(bt, np->data, btlen) && (bt[btlen] == 0))
{
/* the basetype matches */
if (wild || mutt_istr_equal(np->data + btlen + 1, b->subtype))
{
choice = b;
}
}
b = b->next;
}
if (choice)
break;
}
/* Next, look for an autoviewable type */
if (!choice)
{
if (a->parts)
b = a->parts;
else
b = a;
while (b)
{