tmp
[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 /* XXX                                                                      */
190 /****************************************************************************/
191
192 void envelope_wipe(ENVELOPE *p)
193 {
194     address_list_wipe(&p->return_path);
195     address_list_wipe(&p->from);
196     address_list_wipe(&p->to);
197     address_list_wipe(&p->cc);
198     address_list_wipe(&p->bcc);
199     address_list_wipe(&p->sender);
200     address_list_wipe(&p->reply_to);
201     address_list_wipe(&p->mail_followup_to);
202
203     p_delete(&p->list_post);
204     p_delete(&p->subject);
205     /* real_subj is just an offset to subject and shouldn't be freed */
206     p_delete(&p->message_id);
207     p_delete(&p->supersedes);
208     p_delete(&p->date);
209     p_delete(&p->x_label);
210     p_delete(&p->organization);
211
212     mutt_buffer_free (&p->spam);
213     string_list_wipe(&p->references);
214     string_list_wipe(&p->in_reply_to);
215     string_list_wipe(&p->userhdrs);
216 }
217
218 void body_wipe(BODY *b)
219 {
220     b->parameter = NULL;
221     if (b->unlink && b->filename) {
222         unlink (b->filename);
223     }
224
225     p_delete(&b->filename);
226     p_delete(&b->content);
227     p_delete(&b->xtype);
228     p_delete(&b->subtype);
229     p_delete(&b->description);
230     p_delete(&b->form_name);
231
232     if (b->hdr) {
233         /* Don't free twice (b->hdr->content = b->parts) */
234         b->hdr->content = NULL;
235         header_delete(&b->hdr);
236     }
237
238     if (b->parts)
239         body_list_wipe(&b->parts);
240 }
241
242 void header_wipe(HEADER *h)
243 {
244     envelope_delete(&h->env);
245     body_list_wipe(&h->content);
246     p_delete(&h->maildir_flags);
247     p_delete(&h->tree);
248     p_delete(&h->path);
249     p_delete(&h->data);
250 }
251
252
253 /****************************************************************************/
254 /* misc functions                                                           */
255 /****************************************************************************/
256
257 int mutt_is_message_type(BODY *b)
258 {
259     int tok;
260
261     if (b->type != TYPEMESSAGE)
262         return 0;
263
264     tok = mime_which_token(b->subtype, -1);
265     return tok == MIME_RFC822 || tok == MIME_NEWS;
266 }
267
268 int mutt_is_text_part(BODY * b)
269 {
270     char *s = b->subtype;
271
272     if (mutt_is_application_pgp(b))
273         return 0;
274
275     switch (b->type) {
276       case TYPETEXT:
277         return 1;
278
279       case TYPEMESSAGE:
280         return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
281
282       case TYPEAPPLICATION:
283         return mime_which_token(s, -1) == MIME_PGP_KEYS;
284
285       default:
286         return 0;
287     }
288 }
289
290 /* vim:set ft=c: */