fix inlines issues with gperf by stripping them out
[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 mod_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         RETURN();
176     };
177     void unignore(string_t s) {
178         if (m_strcmp(s, "*")) {
179             string_list_add(&UnIgnore, s);
180             string_list_remove(&Ignore, s);
181         } else {
182             string_list_wipe(&Ignore);
183         }
184         RETURN();
185     };
186 };
187
188 /****************************************************************************/
189 /* rfc822 header parameters                                                 */
190 /****************************************************************************/
191
192 char *parameter_getval(parameter_t *parm, const char *s)
193 {
194     while (parm) {
195         if (!ascii_strcasecmp(parm->attribute, s))
196             return parm->value;
197         parm = parm->next;
198     }
199     return NULL;
200 }
201
202 void parameter_setval(parameter_t **p, const char *attribute, const char *value)
203 {
204     while (*p) {
205         if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
206             if (value) {
207                 m_strreplace(&(*p)->value, value);
208             } else {
209                 parameter_t *q = parameter_list_pop(p);
210                 parameter_delete(&q);
211             }
212             return;
213         }
214         p = &(*p)->next;
215     }
216
217     if (value) {
218         (*p) = parameter_new();
219         (*p)->attribute = m_strdup(attribute);
220         (*p)->value = m_strdup(value);
221     }
222 }
223
224 void parameter_delval(parameter_t **p, const char *attribute)
225 {
226     while (*p) {
227         if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
228             parameter_t *q = parameter_list_pop(p);
229             parameter_delete(&q);
230             return;
231         }
232
233         p = &(*p)->next;
234     }
235 }
236
237 int parameter_equal(const parameter_t *p1, const parameter_t *p2)
238 {
239     while (p1 && p2) {
240         if (m_strcmp(p1->attribute, p2->attribute)
241         ||  m_strcmp(p1->value, p2->value))
242             return 0;
243
244         p1 = p1->next;
245         p2 = p2->next;
246     }
247
248     if (p1 || p2)
249         return 0;
250
251     return 1;
252 }
253
254 void parameter_set_boundary(parameter_t **parm)
255 {
256     char rs[BOUNDARYLEN + 1];
257     int i;
258
259     for (i = 0; i < BOUNDARYLEN; i++) {
260         rs[i] = __m_b64chars[lrand48() % sizeof(__m_b64chars)];
261     }
262     rs[BOUNDARYLEN] = '\0';
263
264     parameter_setval(parm, "boundary", rs);
265 }
266
267
268 /****************************************************************************/
269 /* XXX                                                                      */
270 /****************************************************************************/
271
272 void envelope_wipe(ENVELOPE *p)
273 {
274     address_list_wipe(&p->return_path);
275     address_list_wipe(&p->from);
276     address_list_wipe(&p->to);
277     address_list_wipe(&p->cc);
278     address_list_wipe(&p->bcc);
279     address_list_wipe(&p->sender);
280     address_list_wipe(&p->reply_to);
281     address_list_wipe(&p->mail_followup_to);
282
283     p_delete(&p->list_post);
284     p_delete(&p->subject);
285     /* real_subj is just an offset to subject and shouldn't be freed */
286     p_delete(&p->message_id);
287     p_delete(&p->supersedes);
288     p_delete(&p->date);
289     p_delete(&p->x_label);
290     p_delete(&p->organization);
291
292     mutt_buffer_free (&p->spam);
293     string_list_wipe(&p->references);
294     string_list_wipe(&p->in_reply_to);
295     string_list_wipe(&p->userhdrs);
296 }
297
298 void body_wipe(BODY *b)
299 {
300     if (b->parameter)
301         parameter_list_wipe(&b->parameter);
302
303     if (b->unlink && b->filename) {
304         unlink (b->filename);
305     }
306
307     p_delete(&b->filename);
308     p_delete(&b->content);
309     p_delete(&b->xtype);
310     p_delete(&b->subtype);
311     p_delete(&b->description);
312     p_delete(&b->form_name);
313
314     if (b->hdr) {
315         /* Don't free twice (b->hdr->content = b->parts) */
316         b->hdr->content = NULL;
317         header_delete(&b->hdr);
318     }
319
320     if (b->parts)
321         body_list_wipe(&b->parts);
322 }
323
324 void header_wipe(HEADER *h)
325 {
326     envelope_delete(&h->env);
327     body_list_wipe(&h->content);
328     p_delete(&h->maildir_flags);
329     p_delete(&h->tree);
330     p_delete(&h->path);
331     p_delete(&h->data);
332 }
333
334
335 /****************************************************************************/
336 /* misc functions                                                           */
337 /****************************************************************************/
338
339 int mutt_is_message_type(BODY *b)
340 {
341     int tok;
342
343     if (b->type != TYPEMESSAGE)
344         return 0;
345
346     tok = mime_which_token(b->subtype, -1);
347     return tok == MIME_RFC822 || tok == MIME_NEWS;
348 }
349
350 int mutt_is_text_part(BODY * b)
351 {
352     char *s = b->subtype;
353
354     if (mutt_is_application_pgp(b))
355         return 0;
356
357     switch (b->type) {
358       case TYPETEXT:
359         return 1;
360
361       case TYPEMESSAGE:
362         return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
363
364       case TYPEAPPLICATION:
365         return mime_which_token(s, -1) == MIME_PGP_KEYS;
366
367       default:
368         return 0;
369     }
370 }
371
372 /* vim:set ft=c: */