forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailcap.c
621 lines (561 loc) · 17.8 KB
/
mailcap.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
/**
* @file
* RFC1524 Mailcap routines
*
* @authors
* Copyright (C) 1996-2000,2003,2012 Michael R. Elkins <me@mutt.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 mailcap RFC1524 Mailcap routines
*
* RFC1524 defines a format for the Multimedia Mail Configuration, which is the
* standard mailcap file format under Unix which specifies what external
* programs should be used to view/compose/edit multimedia files based on
* content type.
*
* This file contains various functions for implementing a fair subset of
* RFC1524.
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "mutt/lib.h"
#include "email/lib.h"
#include "mailcap.h"
#include "globals.h"
#include "mutt_attach.h"
#include "muttlib.h"
#include "protos.h"
/* These Config Variables are only used in rfc1524.c */
bool C_MailcapSanitize; ///< Config: Restrict the possible characters in mailcap expandos
/**
* mailcap_expand_command - Expand expandos in a command
* @param a Email Body
* @param filename File containing the email text
* @param type Type, e.g. "text/plain"
* @param command Buffer containing command
* @retval 0 Command works on a file
* @retval 1 Command works on a pipe
*
* The command semantics include the following:
* %s is the filename that contains the mail body data
* %t is the content type, like text/plain
* %{parameter} is replaced by the parameter value from the content-type field
* \% is %
* Unsupported rfc1524 parameters: these would probably require some doing
* by neomutt, and can probably just be done by piping the message to metamail
* %n is the integer number of sub-parts in the multipart
* %F is "content-type filename" repeated for each sub-part
*/
int mailcap_expand_command(struct Body *a, const char *filename,
const char *type, struct Buffer *command)
{
int needspipe = true;
struct Buffer *buf = mutt_buffer_pool_get();
struct Buffer *quoted = mutt_buffer_pool_get();
struct Buffer *param = NULL;
struct Buffer *type2 = NULL;
const char *cptr = mutt_b2s(command);
while (*cptr)
{
if (*cptr == '\\')
{
cptr++;
if (*cptr)
mutt_buffer_addch(buf, *cptr++);
}
else if (*cptr == '%')
{
cptr++;
if (*cptr == '{')
{
const char *pvalue2 = NULL;
if (param)
mutt_buffer_reset(param);
else
param = mutt_buffer_pool_get();
/* Copy parameter name into param buffer */
cptr++;
while (*cptr && (*cptr != '}'))
mutt_buffer_addch(param, *cptr++);
/* In send mode, use the current charset, since the message hasn't
* been converted yet. If noconv is set, then we assume the
* charset parameter has the correct value instead. */
if ((mutt_str_strcasecmp(mutt_b2s(param), "charset") == 0) && a->charset && !a->noconv)
pvalue2 = a->charset;
else
pvalue2 = mutt_param_get(&a->parameter, mutt_b2s(param));
/* Now copy the parameter value into param buffer */
if (C_MailcapSanitize)
mutt_buffer_sanitize_filename(param, NONULL(pvalue2), false);
else
mutt_buffer_strcpy(param, NONULL(pvalue2));
mutt_buffer_quote_filename(quoted, mutt_b2s(param), true);
mutt_buffer_addstr(buf, mutt_b2s(quoted));
}
else if ((*cptr == 's') && filename)
{
mutt_buffer_quote_filename(quoted, filename, true);
mutt_buffer_addstr(buf, mutt_b2s(quoted));
needspipe = false;
}
else if (*cptr == 't')
{
if (!type2)
{
type2 = mutt_buffer_pool_get();
if (C_MailcapSanitize)
mutt_buffer_sanitize_filename(type2, type, false);
else
mutt_buffer_strcpy(type2, type);
}
mutt_buffer_quote_filename(quoted, mutt_b2s(type2), true);
mutt_buffer_addstr(buf, mutt_b2s(quoted));
}
if (*cptr)
cptr++;
}
else
mutt_buffer_addch(buf, *cptr++);
}
mutt_buffer_copy(command, buf);
mutt_buffer_pool_release(&buf);
mutt_buffer_pool_release("ed);
mutt_buffer_pool_release(¶m);
mutt_buffer_pool_release(&type2);
return needspipe;
}
/**
* get_field - NUL terminate a RFC1524 field
* @param s String to alter
* @retval ptr Start of next field
* @retval NULL Error
*/
static char *get_field(char *s)
{
if (!s)
return NULL;
char *ch = NULL;
while ((ch = strpbrk(s, ";\\")))
{
if (*ch == '\\')
{
s = ch + 1;
if (*s)
s++;
}
else
{
*ch = '\0';
ch = mutt_str_skip_email_wsp(ch + 1);
break;
}
}
mutt_str_remove_trailing_ws(s);
return ch;
}
/**
* get_field_text - Get the matching text from a mailcap
* @param field String to parse
* @param entry Save the entry here
* @param type Type, e.g. "text/plain"
* @param filename Mailcap filename
* @param line Mailcap line
* @retval 1 Success
* @retval 0 Failure
*/
static int get_field_text(char *field, char **entry, const char *type,
const char *filename, int line)
{
field = mutt_str_skip_whitespace(field);
if (*field == '=')
{
if (entry)
{
field++;
field = mutt_str_skip_whitespace(field);
mutt_str_replace(entry, field);
}
return 1;
}
else
{
mutt_error(_("Improperly formatted entry for type %s in \"%s\" line %d"),
type, filename, line);
return 0;
}
}
/**
* rfc1524_mailcap_parse - Parse a mailcap entry
* @param a Email Body
* @param filename Filename
* @param type Type, e.g. "text/plain"
* @param entry Entry, e.g. "compose"
* @param opt Option, see #MailcapLookup
* @retval true Success
* @retval false Failure
*/
static bool rfc1524_mailcap_parse(struct Body *a, const char *filename, const char *type,
struct MailcapEntry *entry, enum MailcapLookup opt)
{
char *buf = NULL;
bool found = false;
int line = 0;
/* rfc1524 mailcap file is of the format:
* base/type; command; extradefs
* type can be * for matching all
* base with no /type is an implicit wild
* command contains a %s for the filename to pass, default to pipe on stdin
* extradefs are of the form:
* def1="definition"; def2="define \;";
* line wraps with a \ at the end of the line
* # for comments */
/* find length of basetype */
char *ch = strchr(type, '/');
if (!ch)
return false;
const int btlen = ch - type;
FILE *fp = fopen(filename, "r");
if (fp)
{
size_t buflen;
while (!found && (buf = mutt_file_read_line(buf, &buflen, fp, &line, MUTT_CONT)))
{
/* ignore comments */
if (*buf == '#')
continue;
mutt_debug(LL_DEBUG2, "mailcap entry: %s\n", buf);
/* check type */
ch = get_field(buf);
if ((mutt_str_strcasecmp(buf, type) != 0) &&
((mutt_str_strncasecmp(buf, type, btlen) != 0) ||
((buf[btlen] != '\0') && /* implicit wild */
(mutt_str_strcmp(buf + btlen, "/*") != 0)))) /* wildsubtype */
{
continue;
}
/* next field is the viewcommand */
char *field = ch;
ch = get_field(ch);
if (entry)
entry->command = mutt_str_strdup(field);
/* parse the optional fields */
found = true;
bool copiousoutput = false;
bool composecommand = false;
bool editcommand = false;
bool printcommand = false;
while (ch)
{
field = ch;
ch = get_field(ch);
mutt_debug(LL_DEBUG2, "field: %s\n", field);
size_t plen;
if (mutt_str_strcasecmp(field, "needsterminal") == 0)
{
if (entry)
entry->needsterminal = true;
}
else if (mutt_str_strcasecmp(field, "copiousoutput") == 0)
{
copiousoutput = true;
if (entry)
entry->copiousoutput = true;
}
else if ((plen = mutt_str_startswith(field, "composetyped", CASE_IGNORE)))
{
/* this compare most occur before compose to match correctly */
if (get_field_text(field + plen, entry ? &entry->composetypecommand : NULL,
type, filename, line))
{
composecommand = true;
}
}
else if ((plen = mutt_str_startswith(field, "compose", CASE_IGNORE)))
{
if (get_field_text(field + plen, entry ? &entry->composecommand : NULL,
type, filename, line))
{
composecommand = true;
}
}
else if ((plen = mutt_str_startswith(field, "print", CASE_IGNORE)))
{
if (get_field_text(field + plen, entry ? &entry->printcommand : NULL,
type, filename, line))
{
printcommand = true;
}
}
else if ((plen = mutt_str_startswith(field, "edit", CASE_IGNORE)))
{
if (get_field_text(field + plen, entry ? &entry->editcommand : NULL,
type, filename, line))
editcommand = true;
}
else if ((plen = mutt_str_startswith(field, "nametemplate", CASE_IGNORE)))
{
get_field_text(field + plen, entry ? &entry->nametemplate : NULL,
type, filename, line);
}
else if ((plen = mutt_str_startswith(field, "x-convert", CASE_IGNORE)))
{
get_field_text(field + plen, entry ? &entry->convert : NULL, type, filename, line);
}
else if ((plen = mutt_str_startswith(field, "test", CASE_IGNORE)))
{
/* This routine executes the given test command to determine
* if this is the right entry. */
char *test_command = NULL;
if (get_field_text(field + plen, &test_command, type, filename, line) && test_command)
{
struct Buffer *command = mutt_buffer_pool_get();
struct Buffer *afilename = mutt_buffer_pool_get();
mutt_buffer_strcpy(command, test_command);
if (C_MailcapSanitize)
mutt_buffer_sanitize_filename(afilename, NONULL(a->filename), true);
else
mutt_buffer_strcpy(afilename, NONULL(a->filename));
mailcap_expand_command(a, mutt_b2s(afilename), type, command);
if (mutt_system(mutt_b2s(command)))
{
/* a non-zero exit code means test failed */
found = false;
}
FREE(&test_command);
mutt_buffer_pool_release(&command);
mutt_buffer_pool_release(&afilename);
}
}
else if (mutt_str_startswith(field, "x-neomutt-keep", CASE_IGNORE))
{
if (entry)
entry->xneomuttkeep = true;
}
} /* while (ch) */
if (opt == MUTT_MC_AUTOVIEW)
{
if (!copiousoutput)
found = false;
}
else if (opt == MUTT_MC_COMPOSE)
{
if (!composecommand)
found = false;
}
else if (opt == MUTT_MC_EDIT)
{
if (!editcommand)
found = false;
}
else if (opt == MUTT_MC_PRINT)
{
if (!printcommand)
found = false;
}
if (!found)
{
/* reset */
if (entry)
{
FREE(&entry->command);
FREE(&entry->composecommand);
FREE(&entry->composetypecommand);
FREE(&entry->editcommand);
FREE(&entry->printcommand);
FREE(&entry->nametemplate);
FREE(&entry->convert);
entry->needsterminal = false;
entry->copiousoutput = false;
entry->xneomuttkeep = false;
}
}
} /* while (!found && (buf = mutt_file_read_line ())) */
mutt_file_fclose(&fp);
} /* if ((fp = fopen ())) */
FREE(&buf);
return found;
}
/**
* mailcap_entry_new - Allocate memory for a new rfc1524 entry
* @retval ptr An un-initialized struct MailcapEntry
*/
struct MailcapEntry *mailcap_entry_new(void)
{
return mutt_mem_calloc(1, sizeof(struct MailcapEntry));
}
/**
* mailcap_entry_free - Deallocate an struct MailcapEntry
* @param[out] ptr MailcapEntry to deallocate
*/
void mailcap_entry_free(struct MailcapEntry **ptr)
{
if (!ptr || !*ptr)
return;
struct MailcapEntry *me = *ptr;
FREE(&me->command);
FREE(&me->testcommand);
FREE(&me->composecommand);
FREE(&me->composetypecommand);
FREE(&me->editcommand);
FREE(&me->printcommand);
FREE(&me->nametemplate);
FREE(ptr);
}
/**
* mailcap_lookup - Find given type in the list of mailcap files
* @param a Message body
* @param type Text type in "type/subtype" format
* @param typelen Length of the type
* @param entry struct MailcapEntry to populate with results
* @param opt Type of mailcap entry to lookup, see #MailcapLookup
* @retval true If *entry is not NULL it populates it with the mailcap entry
* @retval false No matching entry is found
*
* Find the given type in the list of mailcap files.
*/
bool mailcap_lookup(struct Body *a, char *type, size_t typelen,
struct MailcapEntry *entry, enum MailcapLookup opt)
{
/* rfc1524 specifies that a path of mailcap files should be searched.
* joy. They say
* $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
* and overridden by the MAILCAPS environment variable, and, just to be nice,
* we'll make it specifiable in .neomuttrc */
if (!C_MailcapPath || (C_MailcapPath->count == 0))
{
mutt_error(_("No mailcap path specified"));
return false;
}
mutt_check_lookup_list(a, type, typelen);
struct Buffer *path = mutt_buffer_pool_get();
bool found = false;
struct ListNode *np = NULL;
STAILQ_FOREACH(np, &C_MailcapPath->head, entries)
{
mutt_buffer_strcpy(path, np->data);
mutt_buffer_expand_path(path);
mutt_debug(LL_DEBUG2, "Checking mailcap file: %s\n", mutt_b2s(path));
found = rfc1524_mailcap_parse(a, mutt_b2s(path), type, entry, opt);
if (found)
break;
}
mutt_buffer_pool_release(&path);
if (entry && !found)
mutt_error(_("mailcap entry for type %s not found"), type);
return found;
}
/**
* mailcap_expand_filename - Expand a new filename from a template or existing filename
* @param nametemplate Template
* @param oldfile Original filename
* @param newfile Buffer for new filename
*
* If there is no nametemplate, the stripped oldfile name is used as the
* template for newfile.
*
* If there is no oldfile, the stripped nametemplate name is used as the
* template for newfile.
*
* If both a nametemplate and oldfile are specified, the template is checked
* for a "%s". If none is found, the nametemplate is used as the template for
* newfile. The first path component of the nametemplate and oldfile are ignored.
*/
void mailcap_expand_filename(const char *nametemplate, const char *oldfile,
struct Buffer *newfile)
{
int i, j, k;
char *s = NULL;
bool lmatch = false, rmatch = false;
mutt_buffer_reset(newfile);
/* first, ignore leading path components */
if (nametemplate && (s = strrchr(nametemplate, '/')))
nametemplate = s + 1;
if (oldfile && (s = strrchr(oldfile, '/')))
oldfile = s + 1;
if (!nametemplate)
{
if (oldfile)
mutt_buffer_strcpy(newfile, oldfile);
}
else if (!oldfile)
{
mutt_file_expand_fmt(newfile, nametemplate, "neomutt");
}
else /* oldfile && nametemplate */
{
/* first, compare everything left from the "%s"
* (if there is one). */
lmatch = true;
bool ps = false;
for (i = 0; nametemplate[i]; i++)
{
if ((nametemplate[i] == '%') && (nametemplate[i + 1] == 's'))
{
ps = true;
break;
}
/* note that the following will _not_ read beyond oldfile's end. */
if (lmatch && (nametemplate[i] != oldfile[i]))
lmatch = false;
}
if (ps)
{
/* If we had a "%s", check the rest. */
/* now, for the right part: compare everything right from
* the "%s" to the final part of oldfile.
*
* The logic here is as follows:
*
* - We start reading from the end.
* - There must be a match _right_ from the "%s",
* thus the i + 2.
* - If there was a left hand match, this stuff
* must not be counted again. That's done by the
* condition (j >= (lmatch ? i : 0)). */
rmatch = true;
for (j = mutt_str_strlen(oldfile) - 1, k = mutt_str_strlen(nametemplate) - 1;
(j >= (lmatch ? i : 0)) && (k >= (i + 2)); j--, k--)
{
if (nametemplate[k] != oldfile[j])
{
rmatch = false;
break;
}
}
/* Now, check if we had a full match. */
if (k >= i + 2)
rmatch = false;
struct Buffer *left = mutt_buffer_pool_get();
struct Buffer *right = mutt_buffer_pool_get();
if (!lmatch)
mutt_buffer_strcpy_n(left, nametemplate, i);
if (!rmatch)
mutt_buffer_strcpy(right, nametemplate + i + 2);
mutt_buffer_printf(newfile, "%s%s%s", mutt_b2s(left), oldfile, mutt_b2s(right));
mutt_buffer_pool_release(&left);
mutt_buffer_pool_release(&right);
}
else
{
/* no "%s" in the name template. */
mutt_buffer_strcpy(newfile, nametemplate);
}
}
mutt_adv_mktemp(newfile);
}