forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enriched.c
608 lines (553 loc) · 15 KB
/
enriched.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
/**
* @file
* Rich text handler
*
* @authors
* Copyright (C) 2018-2023 Richard Russon <rich@flatcap.org>
*
* @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_enriched Rich text handler
*
* Rich text handler
*/
/**
* A (not so) minimal implementation of RFC1563.
*/
#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include "mutt/lib.h"
#include "email/lib.h"
#include "enriched.h"
#define INDENT_SIZE 4
/**
* enum RichAttribs - Rich text attributes
*/
enum RichAttribs
{
RICH_PARAM = 0, ///< Parameter label
RICH_BOLD, ///< Bold text
RICH_UNDERLINE, ///< Underlined text
RICH_ITALIC, ///< Italic text
RICH_NOFILL, ///< Text will not be reformatted
RICH_INDENT, ///< Indented text
RICH_INDENT_RIGHT, ///< Right-indented text
RICH_EXCERPT, ///< Excerpt text
RICH_CENTER, ///< Centred text
RICH_FLUSHLEFT, ///< Left-justified text
RICH_FLUSHRIGHT, ///< Right-justified text
RICH_COLOR, ///< Coloured text
RICH_MAX,
};
/**
* struct Etags - Enriched text tags
*/
struct Etags
{
const wchar_t *tag_name; ///< Tag name
int index; ///< Index number
};
/// EnrichedTags - Lookup table of tags allowed in enriched text
static const struct Etags EnrichedTags[] = {
// clang-format off
{ 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 },
// clang-format on
};
/**
* struct EnrichedState - State of enriched-text parser
*/
struct EnrichedState
{
wchar_t *buffer;
wchar_t *line;
wchar_t *param;
size_t buf_len;
size_t line_len;
size_t line_used;
size_t line_max;
size_t indent_len;
size_t word_len;
size_t buf_used;
size_t param_used;
size_t param_len;
int tag_level[RICH_MAX];
int wrap_margin;
struct State *state;
};
/**
* enriched_wrap - Wrap enriched text
* @param enriched State of enriched text
*/
static void enriched_wrap(struct EnrichedState *enriched)
{
if (!enriched)
return;
int x;
if (enriched->line_len)
{
if (enriched->tag_level[RICH_CENTER] || enriched->tag_level[RICH_FLUSHRIGHT])
{
/* Strip trailing white space */
size_t y = enriched->line_used - 1;
while (y && iswspace(enriched->line[y]))
{
enriched->line[y] = (wchar_t) '\0';
y--;
enriched->line_used--;
enriched->line_len--;
}
if (enriched->tag_level[RICH_CENTER])
{
/* Strip leading whitespace */
y = 0;
while (enriched->line[y] && iswspace(enriched->line[y]))
y++;
if (y)
{
for (size_t z = y; z <= enriched->line_used; z++)
{
enriched->line[z - y] = enriched->line[z];
}
enriched->line_len -= y;
enriched->line_used -= y;
}
}
}
const int extra = enriched->wrap_margin - enriched->line_len - enriched->indent_len -
(enriched->tag_level[RICH_INDENT_RIGHT] * INDENT_SIZE);
if (extra > 0)
{
if (enriched->tag_level[RICH_CENTER])
{
x = extra / 2;
while (x)
{
state_putc(enriched->state, ' ');
x--;
}
}
else if (enriched->tag_level[RICH_FLUSHRIGHT])
{
x = extra - 1;
while (x)
{
state_putc(enriched->state, ' ');
x--;
}
}
}
state_putws(enriched->state, (const wchar_t *) enriched->line);
}
state_putc(enriched->state, '\n');
enriched->line[0] = (wchar_t) '\0';
enriched->line_len = 0;
enriched->line_used = 0;
enriched->indent_len = 0;
if (enriched->state->prefix)
{
state_puts(enriched->state, enriched->state->prefix);
enriched->indent_len += mutt_str_len(enriched->state->prefix);
}
if (enriched->tag_level[RICH_EXCERPT])
{
x = enriched->tag_level[RICH_EXCERPT];
while (x)
{
if (enriched->state->prefix)
{
state_puts(enriched->state, enriched->state->prefix);
enriched->indent_len += mutt_str_len(enriched->state->prefix);
}
else
{
state_puts(enriched->state, "> ");
enriched->indent_len += mutt_str_len("> ");
}
x--;
}
}
else
{
enriched->indent_len = 0;
}
if (enriched->tag_level[RICH_INDENT])
{
x = enriched->tag_level[RICH_INDENT] * INDENT_SIZE;
enriched->indent_len += x;
while (x)
{
state_putc(enriched->state, ' ');
x--;
}
}
}
/**
* enriched_flush - Write enriched text to the State
* @param enriched State of Enriched text
* @param wrap true if the text should be wrapped
*/
static void enriched_flush(struct EnrichedState *enriched, bool wrap)
{
if (!enriched || !enriched->buffer)
return;
if (!enriched->tag_level[RICH_NOFILL] &&
((enriched->line_len + enriched->word_len) >
(enriched->wrap_margin - (enriched->tag_level[RICH_INDENT_RIGHT] * INDENT_SIZE) -
enriched->indent_len)))
{
enriched_wrap(enriched);
}
if (enriched->buf_used)
{
enriched->buffer[enriched->buf_used] = (wchar_t) '\0';
enriched->line_used += enriched->buf_used;
if (enriched->line_used > enriched->line_max)
{
enriched->line_max = enriched->line_used;
mutt_mem_realloc(&enriched->line, (enriched->line_max + 1) * sizeof(wchar_t));
}
wcscat(enriched->line, enriched->buffer);
enriched->line_len += enriched->word_len;
enriched->word_len = 0;
enriched->buf_used = 0;
}
if (wrap)
enriched_wrap(enriched);
fflush(enriched->state->fp_out);
}
/**
* enriched_putwc - Write one wide character to the state
* @param c Character to write
* @param enriched State of Enriched text
*/
static void enriched_putwc(wchar_t c, struct EnrichedState *enriched)
{
if (!enriched)
return;
if (enriched->tag_level[RICH_PARAM])
{
if (enriched->tag_level[RICH_COLOR])
{
if ((enriched->param_used + 1) >= enriched->param_len)
mutt_mem_realloc(&enriched->param, (enriched->param_len += 256) * sizeof(wchar_t));
enriched->param[enriched->param_used++] = c;
}
return; /* nothing to do */
}
/* see if more space is needed (plus extra for possible rich characters) */
if ((enriched->buf_len < (enriched->buf_used + 3)) || !enriched->buffer)
{
enriched->buf_len += 1024;
mutt_mem_realloc(&enriched->buffer, (enriched->buf_len + 1) * sizeof(wchar_t));
}
if ((!enriched->tag_level[RICH_NOFILL] && iswspace(c)) || (c == (wchar_t) '\0'))
{
if (c == (wchar_t) '\t')
enriched->word_len += 8 - (enriched->line_len + enriched->word_len) % 8;
else
enriched->word_len++;
enriched->buffer[enriched->buf_used++] = c;
enriched_flush(enriched, false);
}
else
{
if (enriched->state->flags & STATE_DISPLAY)
{
if (enriched->tag_level[RICH_BOLD])
{
enriched->buffer[enriched->buf_used++] = c;
enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
enriched->buffer[enriched->buf_used++] = c;
}
else if (enriched->tag_level[RICH_UNDERLINE])
{
enriched->buffer[enriched->buf_used++] = '_';
enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
enriched->buffer[enriched->buf_used++] = c;
}
else if (enriched->tag_level[RICH_ITALIC])
{
enriched->buffer[enriched->buf_used++] = c;
enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
enriched->buffer[enriched->buf_used++] = '_';
}
else
{
enriched->buffer[enriched->buf_used++] = c;
}
}
else
{
enriched->buffer[enriched->buf_used++] = c;
}
enriched->word_len++;
}
}
/**
* enriched_puts - Write an enriched text string to the State
* @param s String to write
* @param enriched State of Enriched text
*/
static void enriched_puts(const char *s, struct EnrichedState *enriched)
{
if (!enriched)
return;
const char *c = NULL;
if ((enriched->buf_len < (enriched->buf_used + mutt_str_len(s))) || !enriched->buffer)
{
enriched->buf_len += 1024;
mutt_mem_realloc(&enriched->buffer, (enriched->buf_len + 1) * sizeof(wchar_t));
}
c = s;
while (*c)
{
enriched->buffer[enriched->buf_used++] = (wchar_t) *c;
c++;
}
}
/**
* enriched_set_flags - Set flags on the enriched text state
* @param tag Tag to set
* @param enriched State of Enriched text
*/
static void enriched_set_flags(const wchar_t *tag, struct EnrichedState *enriched)
{
if (!enriched)
return;
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(enriched, true);
if (*tag == (wchar_t) '/')
{
if (enriched->tag_level[j]) /* make sure not to go negative */
enriched->tag_level[j]--;
if ((enriched->state->flags & STATE_DISPLAY) && (j == RICH_PARAM) &&
enriched->tag_level[RICH_COLOR])
{
enriched->param[enriched->param_used] = (wchar_t) '\0';
if (wcscasecmp(L"black", enriched->param) == 0)
{
enriched_puts("\033[30m", enriched); // Escape
}
else if (wcscasecmp(L"red", enriched->param) == 0)
{
enriched_puts("\033[31m", enriched); // Escape
}
else if (wcscasecmp(L"green", enriched->param) == 0)
{
enriched_puts("\033[32m", enriched); // Escape
}
else if (wcscasecmp(L"yellow", enriched->param) == 0)
{
enriched_puts("\033[33m", enriched); // Escape
}
else if (wcscasecmp(L"blue", enriched->param) == 0)
{
enriched_puts("\033[34m", enriched); // Escape
}
else if (wcscasecmp(L"magenta", enriched->param) == 0)
{
enriched_puts("\033[35m", enriched); // Escape
}
else if (wcscasecmp(L"cyan", enriched->param) == 0)
{
enriched_puts("\033[36m", enriched); // Escape
}
else if (wcscasecmp(L"white", enriched->param) == 0)
{
enriched_puts("\033[37m", enriched); // Escape
}
}
if ((enriched->state->flags & STATE_DISPLAY) && (j == RICH_COLOR))
{
enriched_puts("\033[0m", enriched); // Escape
}
/* flush parameter buffer when closing the tag */
if (j == RICH_PARAM)
{
enriched->param_used = 0;
enriched->param[0] = (wchar_t) '\0';
}
}
else
{
enriched->tag_level[j]++;
}
if (j == RICH_EXCERPT)
enriched_flush(enriched, true);
}
}
/**
* text_enriched_handler - Handler for enriched text - Implements ::handler_t - @ingroup handler_api
* @retval 0 Always
*/
int text_enriched_handler(struct Body *b_email, struct State *state)
{
enum
{
TEXT,
LANGLE,
TAG,
BOGUS_TAG,
NEWLINE,
ST_EOF,
DONE
} text_state = TEXT;
long bytes = b_email->length;
struct EnrichedState enriched = { 0 };
wint_t wc = 0;
int tag_len = 0;
wchar_t tag[1024 + 1];
enriched.state = state;
enriched.wrap_margin = ((state->wraplen > 4) &&
((state->flags & STATE_DISPLAY) || (state->wraplen < 76))) ?
state->wraplen - 4 :
72;
enriched.line_max = enriched.wrap_margin * 4;
enriched.line = mutt_mem_calloc((enriched.line_max + 1), sizeof(wchar_t));
enriched.param = mutt_mem_calloc(256, sizeof(wchar_t));
enriched.param_len = 256;
enriched.param_used = 0;
if (state->prefix)
{
state_puts(state, state->prefix);
enriched.indent_len += mutt_str_len(state->prefix);
}
while (text_state != DONE)
{
if (text_state != ST_EOF)
{
if (!bytes || ((wc = fgetwc(state->fp_in)) == WEOF))
text_state = ST_EOF;
else
bytes--;
}
switch (text_state)
{
case TEXT:
switch (wc)
{
case '<':
text_state = LANGLE;
break;
case '\n':
if (enriched.tag_level[RICH_NOFILL])
{
enriched_flush(&enriched, true);
}
else
{
enriched_putwc((wchar_t) ' ', &enriched);
text_state = NEWLINE;
}
break;
default:
enriched_putwc(wc, &enriched);
}
break;
case LANGLE:
if (wc == (wchar_t) '<')
{
enriched_putwc(wc, &enriched);
text_state = TEXT;
break;
}
else
{
tag_len = 0;
text_state = TAG;
}
/* Yes, (it wasn't a <<, so this char is first in TAG) */
FALLTHROUGH;
case TAG:
if (wc == (wchar_t) '>')
{
tag[tag_len] = (wchar_t) '\0';
enriched_set_flags(tag, &enriched);
text_state = TEXT;
}
else if (tag_len < 1024) /* ignore overly long tags */
{
tag[tag_len++] = wc;
}
else
{
text_state = BOGUS_TAG;
}
break;
case BOGUS_TAG:
if (wc == (wchar_t) '>')
text_state = TEXT;
break;
case NEWLINE:
if (wc == (wchar_t) '\n')
{
enriched_flush(&enriched, true);
}
else
{
ungetwc(wc, state->fp_in);
bytes++;
text_state = TEXT;
}
break;
case ST_EOF:
enriched_putwc((wchar_t) '\0', &enriched);
enriched_flush(&enriched, true);
text_state = DONE;
break;
default:
/* not reached */
break;
}
}
state_putc(state, '\n'); /* add a final newline */
FREE(&(enriched.buffer));
FREE(&(enriched.line));
FREE(&(enriched.param));
return 0;
}