c4b8d8aa06bfdb844045d70a905eb514ee84ff18
[apps/madmutt.git] / lib-mime / mime.cpkg
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
22  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
23  */
24
25 #include <lib-lib/lib-lib.h>
26 #include <lib-lua/lib-lua.h>
27 #include "mime.h"
28 @import "../lib-lua/base.cpkg"
29
30 #define BOUNDARYLEN 16
31 const char MimeSpecials[] = "@.,;:<>[]\\\"()?/= \t";
32
33 const char *BodyTypes[] = {
34     "x-unknown",
35     "audio",
36     "application",
37     "image",
38     "message",
39     "model",
40     "multipart",
41     "text",
42     "video",
43 };
44
45 const char *BodyEncodings[] = {
46     "x-unknown",
47     "7bit",
48     "8bit",
49     "quoted-printable",
50     "base64",
51     "binary",
52     "x-uuencoded",
53 };
54
55 rx_t *SpamList = NULL, *NoSpamList = NULL;
56 string_list_t *AutoViewList, *AlternativeOrderList, *MimeLookupList;
57 string_list_t *Ignore, *UnIgnore, *HeaderOrderList;
58
59 static char *mailcap_init(void)
60 {
61     /* Default search path from RFC1524 */
62     const char *path = "~/.mailcap:" PKGDATADIR "/mailcap:"
63         SYSCONFDIR "/mailcap:/etc/mailcap:"
64         "/usr/etc/mailcap:/usr/local/etc/mailcap";
65     return m_strdup(getenv("MAILCAPS") ?: path);
66 }
67
68 @package Mime {
69     /*
70      ** .pp
71      ** ``$spam_separator'' controls what happens when multiple spam headers
72      ** are matched: if \fIunset\fP, each successive header will overwrite any
73      ** previous matches value for the spam label. If \fIset\fP, each successive
74      ** match will append to the previous, using ``$spam_separator'' as a
75      ** separator.
76      */
77     string_t spam_separator = m_strdup(",");
78
79     /*
80      ** .pp
81      ** This variable specifies which files to consult when attempting to
82      ** display MIME bodies not directly supported by Madmutt.
83      */
84     string_t mailcap_path   = mailcap_init();
85
86     /*
87      ** .pp
88      ** If \fIset\fP, Madmutt will restrict possible characters in mailcap \fT%\fP expandos
89      ** to a well-defined set of safe characters.  This is the safe setting,
90      ** but we are not sure it doesn't break some more advanced MIME stuff.
91      ** .pp
92      ** \fBDON'T CHANGE THIS SETTING UNLESS YOU ARE REALLY SURE WHAT YOU ARE
93      ** DOING!\fP
94      */
95     bool mailcap_sanitize   = 1;
96
97     void spam(rx_t rx, const string_t tpl) {
98         rx_set_template(rx, tpl);
99         rx_list_remove(&NoSpamList, rx);
100         rx_list_add2(&SpamList, &rx);
101         RETURN();
102     };
103
104     void nospam(rx_t rx) {
105         if (!m_strcmp(rx->pattern, "*")) {
106             rx_list_wipe(&SpamList);
107             rx_list_wipe(&NoSpamList);
108             rx_delete(&rx);
109         } else {
110             rx_list_remove(&SpamList, rx);
111             rx_list_add2(&NoSpamList, &rx);
112         }
113         RETURN();
114     };
115
116     void auto_view(string_t s) {
117         string_list_add(&AutoViewList, s);
118         RETURN();
119     };
120     void unauto_view(string_t s) {
121         if (m_strcmp(s, "*")) {
122             string_list_remove(&AutoViewList, s);
123         } else {
124             string_list_wipe(&AutoViewList);
125         }
126         RETURN();
127     };
128
129     void alternative_order(string_t s) {
130         string_list_add(&AlternativeOrderList, s);
131         RETURN();
132     };
133     void unalternative_order(string_t s) {
134         if (m_strcmp(s, "*")) {
135             string_list_remove(&AlternativeOrderList, s);
136         } else {
137             string_list_wipe(&AlternativeOrderList);
138         }
139         RETURN();
140     };
141
142     void lookup(string_t s) {
143         string_list_add(&MimeLookupList, s);
144         RETURN();
145     };
146     void unlookup(string_t s) {
147         if (m_strcmp(s, "*")) {
148             string_list_remove(&MimeLookupList, s);
149         } else {
150             string_list_wipe(&MimeLookupList);
151         }
152         RETURN();
153     };
154
155     void hdr_order(string_t s) {
156         string_list_add(&HeaderOrderList, s);
157         RETURN();
158     };
159     void unhdr_order(string_t s) {
160         if (m_strcmp(s, "*")) {
161             string_list_remove(&HeaderOrderList, s);
162         } else {
163             string_list_wipe(&HeaderOrderList);
164         }
165         RETURN();
166     };
167
168     void ignore(string_t s) {
169         if (m_strcmp(s, "*")) {
170             string_list_remove(&UnIgnore, s);
171         } else {
172             string_list_wipe(&UnIgnore);
173         }
174         string_list_add(&Ignore, s);
175     };
176     void unignore(string_t s) {
177         if (m_strcmp(s, "*")) {
178             string_list_add(&UnIgnore, s);
179             string_list_remove(&Ignore, s);
180         } else {
181             string_list_wipe(&Ignore);
182         }
183     };
184 };
185
186 /****************************************************************************/
187 /* rfc822 header parameters                                                 */
188 /****************************************************************************/
189
190 char *parameter_getval(parameter_t *parm, const char *s)
191 {
192     while (parm) {
193         if (!ascii_strcasecmp(parm->attribute, s))
194             return parm->value;
195         parm = parm->next;
196     }
197     return NULL;
198 }
199
200 void parameter_setval(parameter_t **p, const char *attribute, const char *value)
201 {
202     while (*p) {
203         if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
204             if (value) {
205                 m_strreplace(&(*p)->value, value);
206             } else {
207                 parameter_t *q = parameter_list_pop(p);
208                 parameter_delete(&q);
209             }
210             return;
211         }
212         p = &(*p)->next;
213     }
214
215     if (value) {
216         (*p) = parameter_new();
217         (*p)->attribute = m_strdup(attribute);
218         (*p)->value = m_strdup(value);
219     }
220 }
221
222 void parameter_delval(parameter_t **p, const char *attribute)
223 {
224     while (*p) {
225         if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
226             parameter_t *q = parameter_list_pop(p);
227             parameter_delete(&q);
228             return;
229         }
230
231         p = &(*p)->next;
232     }
233 }
234
235 int parameter_equal(const parameter_t *p1, const parameter_t *p2)
236 {
237     while (p1 && p2) {
238         if (m_strcmp(p1->attribute, p2->attribute)
239         ||  m_strcmp(p1->value, p2->value))
240             return 0;
241
242         p1 = p1->next;
243         p2 = p2->next;
244     }
245
246     if (p1 || p2)
247         return 0;
248
249     return 1;
250 }
251
252 void parameter_set_boundary(parameter_t **parm)
253 {
254     char rs[BOUNDARYLEN + 1];
255     int i;
256
257     for (i = 0; i < BOUNDARYLEN; i++) {
258         rs[i] = __m_b64chars[lrand48() % sizeof(__m_b64chars)];
259     }
260     rs[BOUNDARYLEN] = '\0';
261
262     parameter_setval(parm, "boundary", rs);
263 }
264
265
266 /****************************************************************************/
267 /* XXX                                                                      */
268 /****************************************************************************/
269
270 void envelope_wipe(ENVELOPE *p)
271 {
272     address_list_wipe(&p->return_path);
273     address_list_wipe(&p->from);
274     address_list_wipe(&p->to);
275     address_list_wipe(&p->cc);
276     address_list_wipe(&p->bcc);
277     address_list_wipe(&p->sender);
278     address_list_wipe(&p->reply_to);
279     address_list_wipe(&p->mail_followup_to);
280
281     p_delete(&p->list_post);
282     p_delete(&p->subject);
283     /* real_subj is just an offset to subject and shouldn't be freed */
284     p_delete(&p->message_id);
285     p_delete(&p->supersedes);
286     p_delete(&p->date);
287     p_delete(&p->x_label);
288     p_delete(&p->organization);
289 #ifdef USE_NNTP
290     p_delete(&p->newsgroups);
291     p_delete(&p->xref);
292     p_delete(&p->followup_to);
293     p_delete(&p->x_comment_to);
294 #endif
295
296     mutt_buffer_free (&p->spam);
297     string_list_wipe(&p->references);
298     string_list_wipe(&p->in_reply_to);
299     string_list_wipe(&p->userhdrs);
300 }
301
302 void body_wipe(BODY *b)
303 {
304     if (b->parameter)
305         parameter_list_wipe(&b->parameter);
306
307     if (b->unlink && b->filename) {
308         unlink (b->filename);
309     }
310
311     p_delete(&b->filename);
312     p_delete(&b->content);
313     p_delete(&b->xtype);
314     p_delete(&b->subtype);
315     p_delete(&b->description);
316     p_delete(&b->form_name);
317
318     if (b->hdr) {
319         /* Don't free twice (b->hdr->content = b->parts) */
320         b->hdr->content = NULL;
321         header_delete(&b->hdr);
322     }
323
324     if (b->parts)
325         body_list_wipe(&b->parts);
326 }
327
328 void header_wipe(HEADER *h)
329 {
330     envelope_delete(&h->env);
331     body_list_wipe(&h->content);
332     p_delete(&h->maildir_flags);
333     p_delete(&h->tree);
334     p_delete(&h->path);
335     string_list_wipe(&h->chain);
336     p_delete(&h->data);
337 }
338
339
340 /****************************************************************************/
341 /* misc functions                                                           */
342 /****************************************************************************/
343
344 int mutt_is_message_type(BODY *b)
345 {
346     int tok;
347
348     if (b->type != TYPEMESSAGE)
349         return 0;
350
351     tok = mime_which_token(b->subtype, -1);
352     return tok == MIME_RFC822 || tok == MIME_NEWS;
353 }
354
355 int mutt_is_text_part(BODY * b)
356 {
357     char *s = b->subtype;
358
359     if (mutt_is_application_pgp(b))
360         return 0;
361
362     switch (b->type) {
363       case TYPETEXT:
364         return 1;
365
366       case TYPEMESSAGE:
367         return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
368
369       case TYPEAPPLICATION:
370         return mime_which_token(s, -1) == MIME_PGP_KEYS;
371
372       default:
373         return 0;
374     }
375 }
376
377 /* vim:set ft=c: */