forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.c
1957 lines (1730 loc) · 47.1 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
/**
* Copyright (C) 1996-2000,2002,2010,2013 Michael R. Elkins <[email protected]>
*
* 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/>.
*/
#include "config.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "mutt.h"
#include "charset.h"
#include "copy.h"
#include "filter.h"
#include "keymap.h"
#include "mime.h"
#include "mutt_crypt.h"
#include "mutt_curses.h"
#include "rfc1524.h"
#include "rfc3676.h"
#define BUFI_SIZE 1000
#define BUFO_SIZE 2000
typedef int (*handler_t)(BODY *, STATE *);
// clang-format off
const int Index_hex[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
};
const int Index_64[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 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,-1, -1,-1,-1,-1,
-1,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,-1, -1,-1,-1,-1
};
// clang-format on
static void print_part_line(STATE *s, BODY *b, int n)
{
char length[5];
mutt_pretty_size(length, sizeof(length), b->length);
state_mark_attach(s);
char *charset = mutt_get_parameter("charset", b->parameter);
if (n != 0)
state_printf(s, _("[-- Alternative Type #%d: "), n);
else
state_printf(s, _("[-- Type: "));
state_printf(s, _("%s/%s%s%s, Encoding: %s, Size: %s --]\n"), TYPE(b),
b->subtype, charset ? "; charset=" : "", charset ? charset : "",
ENCODING(b->encoding), length);
}
static void state_prefix_put(const char *d, size_t dlen, STATE *s)
{
if (s->prefix)
while (dlen--)
state_prefix_putc(*d++, s);
else
fwrite(d, dlen, 1, s->fpout);
}
static void convert_to_state(iconv_t cd, char *bufi, size_t *l, STATE *s)
{
char bufo[BUFO_SIZE];
ICONV_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, 0, 0, &ob, &obl);
if (ob != bufo)
state_prefix_put(bufo, ob - bufo, s);
}
return;
}
if (cd == (iconv_t)(-1))
{
state_prefix_put(bufi, *l, s);
*l = 0;
return;
}
ib = bufi, ibl = *l;
for (;;)
{
ob = bufo, obl = sizeof(bufo);
mutt_iconv(cd, &ib, &ibl, &ob, &obl, 0, "?");
if (ob == bufo)
break;
state_prefix_put(bufo, ob - bufo, s);
}
memmove(bufi, ib, ibl);
*l = ibl;
}
static void decode_xbit(STATE *s, long len, int istext, iconv_t cd)
{
int c, ch;
char bufi[BUFI_SIZE];
size_t l = 0;
if (istext)
{
state_set_prefix(s);
while ((c = fgetc(s->fpin)) != EOF && len--)
{
if (c == '\r' && len)
{
if ((ch = fgetc(s->fpin)) == '\n')
{
c = ch;
len--;
}
else
ungetc(ch, s->fpin);
}
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);
}
else
mutt_copy_bytes(s->fpin, s->fpout, len);
}
static int qp_decode_triple(char *s, char *d)
{
/* soft line break */
if (*s == '=' && !(*(s + 1)))
return 1;
/* quoted-printable triple */
if (*s == '=' && isxdigit((unsigned char) *(s + 1)) &&
isxdigit((unsigned char) *(s + 2)))
{
*d = (hexval(*(s + 1)) << 4) | hexval(*(s + 2));
return 0;
}
/* something else */
return -1;
}
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;
int soft = 0;
/* 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 = 1;
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 an attachment encoded with quoted-printable.
*
* 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 STRING > 78,
* so corrupted input will just be corrupted a bit more. That
* implies that STRING+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 STRING + 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 STRING*2 for the target buffer's size.
*
*/
static void decode_quoted(STATE *s, long len, int istext, iconv_t cd)
{
char line[STRING];
char decline[2 * STRING];
size_t l = 0;
size_t linelen; /* number of input bytes in `line' */
size_t l3;
int last; /* store the last character in the input line */
if (istext)
state_set_prefix(s);
while (len > 0)
{
last = 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->fpin) == NULL)
break;
linelen = strlen(line);
len -= linelen;
/*
* inspect the last character we read so we can tell if we got the
* entire line.
*/
last = linelen ? line[linelen - 1] : 0;
/* chop trailing whitespace if we got the full line */
if (last == '\n')
{
while (linelen > 0 && ISSPACE(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);
}
void mutt_decode_base64(STATE *s, long len, int istext, iconv_t cd)
{
char buf[5];
int c1, c2, c3, c4, ch, cr = 0, i;
char bufi[BUFI_SIZE];
size_t l = 0;
buf[4] = 0;
if (istext)
state_set_prefix(s);
while (len > 0)
{
for (i = 0; i < 4 && len > 0; len--)
{
if ((ch = fgetc(s->fpin)) == EOF)
break;
if (ch >= 0 && ch < 128 && (base64val(ch) != -1 || ch == '='))
buf[i++] = ch;
}
if (i != 4)
{
/* "i" may be zero if there is trailing whitespace, which is not an error */
if (i != 0)
mutt_debug(2, "%s:%d [mutt_decode_base64()]: "
"didn't get a multiple of 4 chars.\n",
__FILE__, __LINE__);
break;
}
c1 = base64val(buf[0]);
c2 = base64val(buf[1]);
ch = (c1 << 2) | (c2 >> 4);
if (cr && ch != '\n')
bufi[l++] = '\r';
cr = 0;
if (istext && ch == '\r')
cr = 1;
else
bufi[l++] = ch;
if (buf[2] == '=')
break;
c3 = base64val(buf[2]);
ch = ((c2 & 0xf) << 4) | (c3 >> 2);
if (cr && ch != '\n')
bufi[l++] = '\r';
cr = 0;
if (istext && ch == '\r')
cr = 1;
else
bufi[l++] = ch;
if (buf[3] == '=')
break;
c4 = base64val(buf[3]);
ch = ((c3 & 0x3) << 6) | c4;
if (cr && ch != '\n')
bufi[l++] = '\r';
cr = 0;
if (istext && ch == '\r')
cr = 1;
else
bufi[l++] = ch;
if (l + 8 >= sizeof(bufi))
convert_to_state(cd, bufi, &l, s);
}
if (cr)
bufi[l++] = '\r';
convert_to_state(cd, bufi, &l, s);
convert_to_state(cd, 0, 0, s);
state_reset_prefix(s);
}
static unsigned char decode_byte(char ch)
{
if (ch == 96)
return 0;
return ch - 32;
}
static void decode_uuencoded(STATE *s, long len, int istext, iconv_t cd)
{
char tmps[SHORT_STRING];
char linelen, c, l, out;
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->fpin)) == NULL)
return;
len -= mutt_strlen(tmps);
if ((mutt_strncmp(tmps, "begin", 5) == 0) && ISSPACE(tmps[5]))
break;
}
while (len > 0)
{
if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
return;
len -= mutt_strlen(tmps);
if (mutt_strncmp(tmps, "end", 3) == 0)
break;
pt = tmps;
linelen = decode_byte(*pt);
pt++;
for (c = 0; c < linelen;)
{
for (l = 2; l <= 6; l += 2)
{
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);
}
/* ----------------------------------------------------------------------------
* A (not so) minimal implementation of RFC1563.
*/
#define IndentSize (4)
enum
{
RICH_PARAM = 0,
RICH_BOLD,
RICH_UNDERLINE,
RICH_ITALIC,
RICH_NOFILL,
RICH_INDENT,
RICH_INDENT_RIGHT,
RICH_EXCERPT,
RICH_CENTER,
RICH_FLUSHLEFT,
RICH_FLUSHRIGHT,
RICH_COLOR,
RICH_LAST_TAG
};
static const struct
{
const wchar_t *tag_name;
int index;
} EnrichedTags[] = {
{ L"param", RICH_PARAM },
{ L"bold", RICH_BOLD },
{ L"italic", RICH_ITALIC },
{ L"underline", RICH_UNDERLINE },
{ L"nofill", RICH_NOFILL },
{ L"excerpt", RICH_EXCERPT },
{ L"indent", RICH_INDENT },
{ L"indentright", RICH_INDENT_RIGHT },
{ L"center", RICH_CENTER },
{ L"flushleft", RICH_FLUSHLEFT },
{ L"flushright", RICH_FLUSHRIGHT },
{ L"flushboth", RICH_FLUSHLEFT },
{ L"color", RICH_COLOR },
{ L"x-color", RICH_COLOR },
{ NULL, -1 },
};
struct enriched_state
{
wchar_t *buffer;
wchar_t *line;
wchar_t *param;
size_t buff_len;
size_t line_len;
size_t line_used;
size_t line_max;
size_t indent_len;
size_t word_len;
size_t buff_used;
size_t param_used;
size_t param_len;
int tag_level[RICH_LAST_TAG];
int WrapMargin;
STATE *s;
};
static void enriched_wrap(struct enriched_state *stte)
{
int x;
int extra;
if (stte->line_len)
{
if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
{
/* Strip trailing white space */
size_t y = stte->line_used - 1;
while (y && iswspace(stte->line[y]))
{
stte->line[y] = (wchar_t) '\0';
y--;
stte->line_used--;
stte->line_len--;
}
if (stte->tag_level[RICH_CENTER])
{
/* Strip leading whitespace */
y = 0;
while (stte->line[y] && iswspace(stte->line[y]))
y++;
if (y)
{
size_t z;
for (z = y; z <= stte->line_used; z++)
{
stte->line[z - y] = stte->line[z];
}
stte->line_len -= y;
stte->line_used -= y;
}
}
}
extra = stte->WrapMargin - stte->line_len - stte->indent_len -
(stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
if (extra > 0)
{
if (stte->tag_level[RICH_CENTER])
{
x = extra / 2;
while (x)
{
state_putc(' ', stte->s);
x--;
}
}
else if (stte->tag_level[RICH_FLUSHRIGHT])
{
x = extra - 1;
while (x)
{
state_putc(' ', stte->s);
x--;
}
}
}
state_putws((const wchar_t *) stte->line, stte->s);
}
state_putc('\n', stte->s);
stte->line[0] = (wchar_t) '\0';
stte->line_len = 0;
stte->line_used = 0;
stte->indent_len = 0;
if (stte->s->prefix)
{
state_puts(stte->s->prefix, stte->s);
stte->indent_len += mutt_strlen(stte->s->prefix);
}
if (stte->tag_level[RICH_EXCERPT])
{
x = stte->tag_level[RICH_EXCERPT];
while (x)
{
if (stte->s->prefix)
{
state_puts(stte->s->prefix, stte->s);
stte->indent_len += mutt_strlen(stte->s->prefix);
}
else
{
state_puts("> ", stte->s);
stte->indent_len += mutt_strlen("> ");
}
x--;
}
}
else
stte->indent_len = 0;
if (stte->tag_level[RICH_INDENT])
{
x = stte->tag_level[RICH_INDENT] * IndentSize;
stte->indent_len += x;
while (x)
{
state_putc(' ', stte->s);
x--;
}
}
}
static void enriched_flush(struct enriched_state *stte, int wrap)
{
if (!stte->tag_level[RICH_NOFILL] &&
(stte->line_len + stte->word_len >
(stte->WrapMargin - (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize) - stte->indent_len)))
enriched_wrap(stte);
if (stte->buff_used)
{
stte->buffer[stte->buff_used] = (wchar_t) '\0';
stte->line_used += stte->buff_used;
if (stte->line_used > stte->line_max)
{
stte->line_max = stte->line_used;
safe_realloc(&stte->line, (stte->line_max + 1) * sizeof(wchar_t));
}
wcscat(stte->line, stte->buffer);
stte->line_len += stte->word_len;
stte->word_len = 0;
stte->buff_used = 0;
}
if (wrap)
enriched_wrap(stte);
fflush(stte->s->fpout);
}
static void enriched_putwc(wchar_t c, struct enriched_state *stte)
{
if (stte->tag_level[RICH_PARAM])
{
if (stte->tag_level[RICH_COLOR])
{
if (stte->param_used + 1 >= stte->param_len)
safe_realloc(&stte->param, (stte->param_len += STRING) * sizeof(wchar_t));
stte->param[stte->param_used++] = c;
}
return; /* nothing to do */
}
/* see if more space is needed (plus extra for possible rich characters) */
if (stte->buff_len < stte->buff_used + 3)
{
stte->buff_len += LONG_STRING;
safe_realloc(&stte->buffer, (stte->buff_len + 1) * sizeof(wchar_t));
}
if ((!stte->tag_level[RICH_NOFILL] && iswspace(c)) || c == (wchar_t) '\0')
{
if (c == (wchar_t) '\t')
stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
else
stte->word_len++;
stte->buffer[stte->buff_used++] = c;
enriched_flush(stte, 0);
}
else
{
if (stte->s->flags & MUTT_DISPLAY)
{
if (stte->tag_level[RICH_BOLD])
{
stte->buffer[stte->buff_used++] = c;
stte->buffer[stte->buff_used++] = (wchar_t) '\010';
stte->buffer[stte->buff_used++] = c;
}
else if (stte->tag_level[RICH_UNDERLINE])
{
stte->buffer[stte->buff_used++] = '_';
stte->buffer[stte->buff_used++] = (wchar_t) '\010';
stte->buffer[stte->buff_used++] = c;
}
else if (stte->tag_level[RICH_ITALIC])
{
stte->buffer[stte->buff_used++] = c;
stte->buffer[stte->buff_used++] = (wchar_t) '\010';
stte->buffer[stte->buff_used++] = '_';
}
else
{
stte->buffer[stte->buff_used++] = c;
}
}
else
{
stte->buffer[stte->buff_used++] = c;
}
stte->word_len++;
}
}
static void enriched_puts(const char *s, struct enriched_state *stte)
{
const char *c = NULL;
if (stte->buff_len < stte->buff_used + mutt_strlen(s))
{
stte->buff_len += LONG_STRING;
safe_realloc(&stte->buffer, (stte->buff_len + 1) * sizeof(wchar_t));
}
c = s;
while (*c)
{
stte->buffer[stte->buff_used++] = (wchar_t) *c;
c++;
}
}
static void enriched_set_flags(const wchar_t *tag, struct enriched_state *stte)
{
const wchar_t *tagptr = tag;
int i, j;
if (*tagptr == (wchar_t) '/')
tagptr++;
for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
if (wcscasecmp(EnrichedTags[i].tag_name, tagptr) == 0)
{
j = EnrichedTags[i].index;
break;
}
if (j != -1)
{
if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
enriched_flush(stte, 1);
if (*tag == (wchar_t) '/')
{
if (stte->tag_level[j]) /* make sure not to go negative */
stte->tag_level[j]--;
if ((stte->s->flags & MUTT_DISPLAY) && j == RICH_PARAM && stte->tag_level[RICH_COLOR])
{
stte->param[stte->param_used] = (wchar_t) '\0';
if (wcscasecmp(L"black", stte->param) == 0)
{
enriched_puts("\033[30m", stte);
}
else if (wcscasecmp(L"red", stte->param) == 0)
{
enriched_puts("\033[31m", stte);
}
else if (wcscasecmp(L"green", stte->param) == 0)
{
enriched_puts("\033[32m", stte);
}
else if (wcscasecmp(L"yellow", stte->param) == 0)
{
enriched_puts("\033[33m", stte);
}
else if (wcscasecmp(L"blue", stte->param) == 0)
{
enriched_puts("\033[34m", stte);
}
else if (wcscasecmp(L"magenta", stte->param) == 0)
{
enriched_puts("\033[35m", stte);
}
else if (wcscasecmp(L"cyan", stte->param) == 0)
{
enriched_puts("\033[36m", stte);
}
else if (wcscasecmp(L"white", stte->param) == 0)
{
enriched_puts("\033[37m", stte);
}
}
if ((stte->s->flags & MUTT_DISPLAY) && j == RICH_COLOR)
{
enriched_puts("\033[0m", stte);
}
/* flush parameter buffer when closing the tag */
if (j == RICH_PARAM)
{
stte->param_used = 0;
stte->param[0] = (wchar_t) '\0';
}
}
else
stte->tag_level[j]++;
if (j == RICH_EXCERPT)
enriched_flush(stte, 1);
}
}
static int text_enriched_handler(BODY *a, STATE *s)
{
enum
{
TEXT,
LANGLE,
TAG,
BOGUS_TAG,
NEWLINE,
ST_EOF,
DONE
} state = TEXT;
long bytes = a->length;
struct enriched_state stte;
wchar_t wc = 0;
int tag_len = 0;
wchar_t tag[LONG_STRING + 1];
memset(&stte, 0, sizeof(stte));
stte.s = s;
stte.WrapMargin =
((s->flags & MUTT_DISPLAY) ?
(MuttIndexWindow->cols - 4) :
((MuttIndexWindow->cols - 4) < 72) ? (MuttIndexWindow->cols - 4) : 72);
stte.line_max = stte.WrapMargin * 4;
stte.line = safe_calloc(1, (stte.line_max + 1) * sizeof(wchar_t));
stte.param = safe_calloc(1, (STRING) * sizeof(wchar_t));
stte.param_len = STRING;
stte.param_used = 0;
if (s->prefix)
{
state_puts(s->prefix, s);
stte.indent_len += mutt_strlen(s->prefix);
}
while (state != DONE)
{
if (state != ST_EOF)
{
if (!bytes || (wc = fgetwc(s->fpin)) == WEOF)
state = ST_EOF;
else
bytes--;
}
switch (state)
{
case TEXT:
switch (wc)
{
case '<':
state = LANGLE;
break;
case '\n':
if (stte.tag_level[RICH_NOFILL])
{
enriched_flush(&stte, 1);
}
else
{
enriched_putwc((wchar_t) ' ', &stte);
state = NEWLINE;
}
break;
default:
enriched_putwc(wc, &stte);
}
break;
case LANGLE:
if (wc == (wchar_t) '<')
{
enriched_putwc(wc, &stte);
state = TEXT;
break;
}
else
{
tag_len = 0;
state = TAG;
}
/* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
case TAG:
if (wc == (wchar_t) '>')
{
tag[tag_len] = (wchar_t) '\0';
enriched_set_flags(tag, &stte);
state = TEXT;
}
else if (tag_len < LONG_STRING) /* ignore overly long tags */
tag[tag_len++] = wc;
else
state = BOGUS_TAG;
break;
case BOGUS_TAG:
if (wc == (wchar_t) '>')
state = TEXT;
break;
case NEWLINE:
if (wc == (wchar_t) '\n')
enriched_flush(&stte, 1);
else
{
ungetwc(wc, s->fpin);
bytes++;
state = TEXT;
}
break;
case ST_EOF:
enriched_putwc((wchar_t) '\0', &stte);
enriched_flush(&stte, 1);
state = DONE;
break;
case DONE: /* not reached, but gcc complains if this is absent */
break;
}
}
state_putc('\n', s); /* add a final newline */
FREE(&(stte.buffer));
FREE(&(stte.line));
FREE(&(stte.param));
return 0;
}
/* for compatibility with metamail */
static int is_mmnoask(const char *buf)
{
char tmp[LONG_STRING], *p = NULL, *q = NULL;
int lng;
if ((p = getenv("MM_NOASK")) != NULL && *p)
{
if (mutt_strcmp(p, "1") == 0)
return 1;
strfcpy(tmp, p, sizeof(tmp));
p = tmp;
while ((p = strtok(p, ",")) != NULL)
{
if ((q = strrchr(p, '/')) != NULL)
{
if (*(q + 1) == '*')
{
if (ascii_strncasecmp(buf, p, q - p) == 0)
return 1;
}
else
{
if (ascii_strcasecmp(buf, p) == 0)
return 1;
}
}
else
{
lng = mutt_strlen(p);
if (buf[lng] == '/' && (mutt_strncasecmp(buf, p, lng) == 0))
return 1;
}
p = NULL;
}
}
return 0;
}
/*
* Returns:
* 1 if the body part should be filtered by a mailcap entry prior to viewing inline.
*
* 0 otherwise
*/
static int is_autoview(BODY *b)
{