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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
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>
25 #include <lib-lib/lib-lib.h>
26 #include <lib-lua/lib-lua.h>
28 @import "../lib-lua/base.cpkg"
30 #define BOUNDARYLEN 16
31 const char MimeSpecials[] = "@.,;:<>[]\\\"()?/= \t";
33 const char *BodyTypes[] = {
45 const char *BodyEncodings[] = {
55 rx_t *SpamList = NULL, *NoSpamList = NULL;
56 string_list_t *AutoViewList, *AlternativeOrderList, *MimeLookupList;
57 string_list_t *Ignore, *UnIgnore, *HeaderOrderList;
59 static char *mailcap_init(void)
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);
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
77 string_t spam_separator = m_strdup(",");
81 ** This variable specifies which files to consult when attempting to
82 ** display MIME bodies not directly supported by Madmutt.
84 string_t mailcap_path = mailcap_init();
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.
92 ** \fBDON'T CHANGE THIS SETTING UNLESS YOU ARE REALLY SURE WHAT YOU ARE
95 bool mailcap_sanitize = 1;
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);
104 void nospam(rx_t rx) {
105 if (!m_strcmp(rx->pattern, "*")) {
106 rx_list_wipe(&SpamList);
107 rx_list_wipe(&NoSpamList);
110 rx_list_remove(&SpamList, rx);
111 rx_list_add2(&NoSpamList, &rx);
116 void auto_view(string_t s) {
117 string_list_add(&AutoViewList, s);
120 void unauto_view(string_t s) {
121 if (m_strcmp(s, "*")) {
122 string_list_remove(&AutoViewList, s);
124 string_list_wipe(&AutoViewList);
129 void alternative_order(string_t s) {
130 string_list_add(&AlternativeOrderList, s);
133 void unalternative_order(string_t s) {
134 if (m_strcmp(s, "*")) {
135 string_list_remove(&AlternativeOrderList, s);
137 string_list_wipe(&AlternativeOrderList);
142 void lookup(string_t s) {
143 string_list_add(&MimeLookupList, s);
146 void unlookup(string_t s) {
147 if (m_strcmp(s, "*")) {
148 string_list_remove(&MimeLookupList, s);
150 string_list_wipe(&MimeLookupList);
155 void hdr_order(string_t s) {
156 string_list_add(&HeaderOrderList, s);
159 void unhdr_order(string_t s) {
160 if (m_strcmp(s, "*")) {
161 string_list_remove(&HeaderOrderList, s);
163 string_list_wipe(&HeaderOrderList);
168 void ignore(string_t s) {
169 if (m_strcmp(s, "*")) {
170 string_list_remove(&UnIgnore, s);
172 string_list_wipe(&UnIgnore);
174 string_list_add(&Ignore, s);
177 void unignore(string_t s) {
178 if (m_strcmp(s, "*")) {
179 string_list_add(&UnIgnore, s);
180 string_list_remove(&Ignore, s);
182 string_list_wipe(&Ignore);
188 /****************************************************************************/
189 /* rfc822 header parameters */
190 /****************************************************************************/
192 char *parameter_getval(parameter_t *parm, const char *s)
195 if (!ascii_strcasecmp(parm->attribute, s))
202 void parameter_setval(parameter_t **p, const char *attribute, const char *value)
205 if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
207 m_strreplace(&(*p)->value, value);
209 parameter_t *q = parameter_list_pop(p);
210 parameter_delete(&q);
218 (*p) = parameter_new();
219 (*p)->attribute = m_strdup(attribute);
220 (*p)->value = m_strdup(value);
224 void parameter_delval(parameter_t **p, const char *attribute)
227 if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
228 parameter_t *q = parameter_list_pop(p);
229 parameter_delete(&q);
237 int parameter_equal(const parameter_t *p1, const parameter_t *p2)
240 if (m_strcmp(p1->attribute, p2->attribute)
241 || m_strcmp(p1->value, p2->value))
254 void parameter_set_boundary(parameter_t **parm)
256 char rs[BOUNDARYLEN + 1];
259 for (i = 0; i < BOUNDARYLEN; i++) {
260 rs[i] = __m_b64chars[lrand48() % sizeof(__m_b64chars)];
262 rs[BOUNDARYLEN] = '\0';
264 parameter_setval(parm, "boundary", rs);
268 /****************************************************************************/
270 /****************************************************************************/
272 void envelope_wipe(ENVELOPE *p)
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);
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);
289 p_delete(&p->x_label);
290 p_delete(&p->organization);
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);
298 void body_wipe(BODY *b)
301 parameter_list_wipe(&b->parameter);
303 if (b->unlink && b->filename) {
304 unlink (b->filename);
307 p_delete(&b->filename);
308 p_delete(&b->content);
310 p_delete(&b->subtype);
311 p_delete(&b->description);
312 p_delete(&b->form_name);
315 /* Don't free twice (b->hdr->content = b->parts) */
316 b->hdr->content = NULL;
317 header_delete(&b->hdr);
321 body_list_wipe(&b->parts);
324 void header_wipe(HEADER *h)
326 envelope_delete(&h->env);
327 body_list_wipe(&h->content);
328 p_delete(&h->maildir_flags);
335 /****************************************************************************/
337 /****************************************************************************/
339 int mutt_is_message_type(BODY *b)
343 if (b->type != TYPEMESSAGE)
346 tok = mime_which_token(b->subtype, -1);
347 return tok == MIME_RFC822 || tok == MIME_NEWS;
350 int mutt_is_text_part(BODY * b)
352 char *s = b->subtype;
354 if (mutt_is_application_pgp(b))
362 return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
364 case TYPEAPPLICATION:
365 return mime_which_token(s, -1) == MIME_PGP_KEYS;