1340ad442c242eaf9d571857324d26e5dbd43f53
[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
58 static char *mailcap_init(void)
59 {
60     /* Default search path from RFC1524 */
61     const char *path = "~/.mailcap:" PKGDATADIR "/mailcap:"
62         SYSCONFDIR "/mailcap:/etc/mailcap:"
63         "/usr/etc/mailcap:/usr/local/etc/mailcap";
64     return m_strdup(getenv("MAILCAPS") ?: path);
65 }
66
67 @package Mime {
68     /*
69      ** .pp
70      ** ``$spam_separator'' controls what happens when multiple spam headers
71      ** are matched: if \fIunset\fP, each successive header will overwrite any
72      ** previous matches value for the spam label. If \fIset\fP, each successive
73      ** match will append to the previous, using ``$spam_separator'' as a
74      ** separator.
75      */
76     string_t spam_separator = m_strdup(",");
77
78     /*
79      ** .pp
80      ** This variable specifies which files to consult when attempting to
81      ** display MIME bodies not directly supported by Madmutt.
82      */
83     string_t mailcap_path   = mailcap_init();
84
85     /*
86      ** .pp
87      ** If \fIset\fP, Madmutt will restrict possible characters in mailcap \fT%\fP expandos
88      ** to a well-defined set of safe characters.  This is the safe setting,
89      ** but we are not sure it doesn't break some more advanced MIME stuff.
90      ** .pp
91      ** \fBDON'T CHANGE THIS SETTING UNLESS YOU ARE REALLY SURE WHAT YOU ARE
92      ** DOING!\fP
93      */
94     bool mailcap_sanitize   = 1;
95
96     void spam(rx_t rx, const string_t tpl) {
97         rx_set_template(rx, tpl);
98         rx_list_remove(&NoSpamList, rx);
99         rx_list_add2(&SpamList, &rx);
100         RETURN();
101     };
102
103     void nospam(rx_t rx) {
104         if (!m_strcmp(rx->pattern, "*")) {
105             rx_list_wipe(&SpamList);
106             rx_list_wipe(&NoSpamList);
107             rx_delete(&rx);
108         } else {
109             rx_list_remove(&SpamList, rx);
110             rx_list_add2(&NoSpamList, &rx);
111         }
112         RETURN();
113     };
114
115     void auto_view(string_t s) {
116         string_list_add(&AutoViewList, s);
117         RETURN();
118     };
119     void unauto_view(string_t s) {
120         if (m_strcmp(s, "*")) {
121             string_list_remove(&AutoViewList, s);
122         } else {
123             string_list_wipe(&AutoViewList);
124         }
125         RETURN();
126     };
127
128     void alternative_order(string_t s) {
129         string_list_add(&AlternativeOrderList, s);
130         RETURN();
131     };
132     void unalternative_order(string_t s) {
133         if (m_strcmp(s, "*")) {
134             string_list_remove(&AlternativeOrderList, s);
135         } else {
136             string_list_wipe(&AlternativeOrderList);
137         }
138         RETURN();
139     };
140
141     void lookup(string_t s) {
142         string_list_add(&MimeLookupList, s);
143         RETURN();
144     };
145     void unlookup(string_t s) {
146         if (m_strcmp(s, "*")) {
147             string_list_remove(&MimeLookupList, s);
148         } else {
149             string_list_wipe(&MimeLookupList);
150         }
151         RETURN();
152     };
153 };
154
155 /****************************************************************************/
156 /* rfc822 header parameters                                                 */
157 /****************************************************************************/
158
159 char *parameter_getval(parameter_t *parm, const char *s)
160 {
161     while (parm) {
162         if (!ascii_strcasecmp(parm->attribute, s))
163             return parm->value;
164         parm = parm->next;
165     }
166     return NULL;
167 }
168
169 void parameter_setval(parameter_t **p, const char *attribute, const char *value)
170 {
171     while (*p) {
172         if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
173             if (value) {
174                 m_strreplace(&(*p)->value, value);
175             } else {
176                 parameter_t *q = parameter_list_pop(p);
177                 parameter_delete(&q);
178             }
179             return;
180         }
181         p = &(*p)->next;
182     }
183
184     if (value) {
185         (*p) = parameter_new();
186         (*p)->attribute = m_strdup(attribute);
187         (*p)->value = m_strdup(value);
188     }
189 }
190
191 void parameter_delval(parameter_t **p, const char *attribute)
192 {
193     while (*p) {
194         if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
195             parameter_t *q = parameter_list_pop(p);
196             parameter_delete(&q);
197             return;
198         }
199
200         p = &(*p)->next;
201     }
202 }
203
204 int parameter_equal(const parameter_t *p1, const parameter_t *p2)
205 {
206     while (p1 && p2) {
207         if (m_strcmp(p1->attribute, p2->attribute)
208         ||  m_strcmp(p1->value, p2->value))
209             return 0;
210
211         p1 = p1->next;
212         p2 = p2->next;
213     }
214
215     if (p1 || p2)
216         return 0;
217
218     return 1;
219 }
220
221 void parameter_set_boundary(parameter_t **parm)
222 {
223     char rs[BOUNDARYLEN + 1];
224     int i;
225
226     for (i = 0; i < BOUNDARYLEN; i++) {
227         rs[i] = __m_b64chars[lrand48() % sizeof(__m_b64chars)];
228     }
229     rs[BOUNDARYLEN] = '\0';
230
231     parameter_setval(parm, "boundary", rs);
232 }
233
234
235 /****************************************************************************/
236 /* XXX                                                                      */
237 /****************************************************************************/
238
239 void envelope_wipe(ENVELOPE *p)
240 {
241     address_list_wipe(&p->return_path);
242     address_list_wipe(&p->from);
243     address_list_wipe(&p->to);
244     address_list_wipe(&p->cc);
245     address_list_wipe(&p->bcc);
246     address_list_wipe(&p->sender);
247     address_list_wipe(&p->reply_to);
248     address_list_wipe(&p->mail_followup_to);
249
250     p_delete(&p->list_post);
251     p_delete(&p->subject);
252     /* real_subj is just an offset to subject and shouldn't be freed */
253     p_delete(&p->message_id);
254     p_delete(&p->supersedes);
255     p_delete(&p->date);
256     p_delete(&p->x_label);
257     p_delete(&p->organization);
258 #ifdef USE_NNTP
259     p_delete(&p->newsgroups);
260     p_delete(&p->xref);
261     p_delete(&p->followup_to);
262     p_delete(&p->x_comment_to);
263 #endif
264
265     mutt_buffer_free (&p->spam);
266     string_list_wipe(&p->references);
267     string_list_wipe(&p->in_reply_to);
268     string_list_wipe(&p->userhdrs);
269 }
270
271 void body_wipe(BODY *b)
272 {
273     if (b->parameter)
274         parameter_list_wipe(&b->parameter);
275
276     if (b->unlink && b->filename) {
277         unlink (b->filename);
278     }
279
280     p_delete(&b->filename);
281     p_delete(&b->content);
282     p_delete(&b->xtype);
283     p_delete(&b->subtype);
284     p_delete(&b->description);
285     p_delete(&b->form_name);
286
287     if (b->hdr) {
288         /* Don't free twice (b->hdr->content = b->parts) */
289         b->hdr->content = NULL;
290         header_delete(&b->hdr);
291     }
292
293     if (b->parts)
294         body_list_wipe(&b->parts);
295 }
296
297 void header_wipe(HEADER *h)
298 {
299     envelope_delete(&h->env);
300     body_list_wipe(&h->content);
301     p_delete(&h->maildir_flags);
302     p_delete(&h->tree);
303     p_delete(&h->path);
304     string_list_wipe(&h->chain);
305     p_delete(&h->data);
306 }
307
308
309 /****************************************************************************/
310 /* misc functions                                                           */
311 /****************************************************************************/
312
313 int mutt_is_message_type(BODY *b)
314 {
315     int tok;
316
317     if (b->type != TYPEMESSAGE)
318         return 0;
319
320     tok = mime_which_token(b->subtype, -1);
321     return tok == MIME_RFC822 || tok == MIME_NEWS;
322 }
323
324 int mutt_is_text_part(BODY * b)
325 {
326     char *s = b->subtype;
327
328     if (mutt_is_application_pgp(b))
329         return 0;
330
331     switch (b->type) {
332       case TYPETEXT:
333         return 1;
334
335       case TYPEMESSAGE:
336         return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
337
338       case TYPEAPPLICATION:
339         return mime_which_token(s, -1) == MIME_PGP_KEYS;
340
341       default:
342         return 0;
343     }
344 }
345
346 /* vim:set ft=c: */