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;
58 static char *mailcap_init(void)
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);
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
76 string_t spam_separator = m_strdup(",");
80 ** This variable specifies which files to consult when attempting to
81 ** display MIME bodies not directly supported by Madmutt.
83 string_t mailcap_path = mailcap_init();
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.
91 ** \fBDON'T CHANGE THIS SETTING UNLESS YOU ARE REALLY SURE WHAT YOU ARE
94 bool mailcap_sanitize = 1;
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);
103 void nospam(rx_t rx) {
104 if (!m_strcmp(rx->pattern, "*")) {
105 rx_list_wipe(&SpamList);
106 rx_list_wipe(&NoSpamList);
109 rx_list_remove(&SpamList, rx);
110 rx_list_add2(&NoSpamList, &rx);
115 void auto_view(string_t s) {
116 string_list_add(&AutoViewList, s);
119 void unauto_view(string_t s) {
120 if (m_strcmp(s, "*")) {
121 string_list_remove(&AutoViewList, s);
123 string_list_wipe(&AutoViewList);
128 void alternative_order(string_t s) {
129 string_list_add(&AlternativeOrderList, s);
132 void unalternative_order(string_t s) {
133 if (m_strcmp(s, "*")) {
134 string_list_remove(&AlternativeOrderList, s);
136 string_list_wipe(&AlternativeOrderList);
141 void lookup(string_t s) {
142 string_list_add(&MimeLookupList, s);
145 void unlookup(string_t s) {
146 if (m_strcmp(s, "*")) {
147 string_list_remove(&MimeLookupList, s);
149 string_list_wipe(&MimeLookupList);
155 /****************************************************************************/
156 /* rfc822 header parameters */
157 /****************************************************************************/
159 char *parameter_getval(parameter_t *parm, const char *s)
162 if (!ascii_strcasecmp(parm->attribute, s))
169 void parameter_setval(parameter_t **p, const char *attribute, const char *value)
172 if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
174 m_strreplace(&(*p)->value, value);
176 parameter_t *q = parameter_list_pop(p);
177 parameter_delete(&q);
185 (*p) = parameter_new();
186 (*p)->attribute = m_strdup(attribute);
187 (*p)->value = m_strdup(value);
191 void parameter_delval(parameter_t **p, const char *attribute)
194 if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
195 parameter_t *q = parameter_list_pop(p);
196 parameter_delete(&q);
204 int parameter_equal(const parameter_t *p1, const parameter_t *p2)
207 if (m_strcmp(p1->attribute, p2->attribute)
208 || m_strcmp(p1->value, p2->value))
221 void parameter_set_boundary(parameter_t **parm)
223 char rs[BOUNDARYLEN + 1];
226 for (i = 0; i < BOUNDARYLEN; i++) {
227 rs[i] = __m_b64chars[lrand48() % sizeof(__m_b64chars)];
229 rs[BOUNDARYLEN] = '\0';
231 parameter_setval(parm, "boundary", rs);
235 /****************************************************************************/
237 /****************************************************************************/
239 void envelope_wipe(ENVELOPE *p)
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);
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);
256 p_delete(&p->x_label);
257 p_delete(&p->organization);
259 p_delete(&p->newsgroups);
261 p_delete(&p->followup_to);
262 p_delete(&p->x_comment_to);
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);
271 void body_wipe(BODY *b)
274 parameter_list_wipe(&b->parameter);
276 if (b->unlink && b->filename) {
277 unlink (b->filename);
280 p_delete(&b->filename);
281 p_delete(&b->content);
283 p_delete(&b->subtype);
284 p_delete(&b->description);
285 p_delete(&b->form_name);
288 /* Don't free twice (b->hdr->content = b->parts) */
289 b->hdr->content = NULL;
290 header_delete(&b->hdr);
294 body_list_wipe(&b->parts);
297 void header_wipe(HEADER *h)
299 envelope_delete(&h->env);
300 body_list_wipe(&h->content);
301 p_delete(&h->maildir_flags);
304 string_list_wipe(&h->chain);
309 /****************************************************************************/
311 /****************************************************************************/
313 int mutt_is_message_type(BODY *b)
317 if (b->type != TYPEMESSAGE)
320 tok = mime_which_token(b->subtype, -1);
321 return tok == MIME_RFC822 || tok == MIME_NEWS;
324 int mutt_is_text_part(BODY * b)
326 char *s = b->subtype;
328 if (mutt_is_application_pgp(b))
336 return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
338 case TYPEAPPLICATION:
339 return mime_which_token(s, -1) == MIME_PGP_KEYS;