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