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