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);
176 void unignore(string_t s) {
177 if (m_strcmp(s, "*")) {
178 string_list_add(&UnIgnore, s);
179 string_list_remove(&Ignore, s);
181 string_list_wipe(&Ignore);
186 /****************************************************************************/
187 /* rfc822 header parameters */
188 /****************************************************************************/
190 char *parameter_getval(parameter_t *parm, const char *s)
193 if (!ascii_strcasecmp(parm->attribute, s))
200 void parameter_setval(parameter_t **p, const char *attribute, const char *value)
203 if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
205 m_strreplace(&(*p)->value, value);
207 parameter_t *q = parameter_list_pop(p);
208 parameter_delete(&q);
216 (*p) = parameter_new();
217 (*p)->attribute = m_strdup(attribute);
218 (*p)->value = m_strdup(value);
222 void parameter_delval(parameter_t **p, const char *attribute)
225 if (!ascii_strcasecmp(attribute, (*p)->attribute)) {
226 parameter_t *q = parameter_list_pop(p);
227 parameter_delete(&q);
235 int parameter_equal(const parameter_t *p1, const parameter_t *p2)
238 if (m_strcmp(p1->attribute, p2->attribute)
239 || m_strcmp(p1->value, p2->value))
252 void parameter_set_boundary(parameter_t **parm)
254 char rs[BOUNDARYLEN + 1];
257 for (i = 0; i < BOUNDARYLEN; i++) {
258 rs[i] = __m_b64chars[lrand48() % sizeof(__m_b64chars)];
260 rs[BOUNDARYLEN] = '\0';
262 parameter_setval(parm, "boundary", rs);
266 /****************************************************************************/
268 /****************************************************************************/
270 void envelope_wipe(ENVELOPE *p)
272 address_list_wipe(&p->return_path);
273 address_list_wipe(&p->from);
274 address_list_wipe(&p->to);
275 address_list_wipe(&p->cc);
276 address_list_wipe(&p->bcc);
277 address_list_wipe(&p->sender);
278 address_list_wipe(&p->reply_to);
279 address_list_wipe(&p->mail_followup_to);
281 p_delete(&p->list_post);
282 p_delete(&p->subject);
283 /* real_subj is just an offset to subject and shouldn't be freed */
284 p_delete(&p->message_id);
285 p_delete(&p->supersedes);
287 p_delete(&p->x_label);
288 p_delete(&p->organization);
290 p_delete(&p->newsgroups);
292 p_delete(&p->followup_to);
293 p_delete(&p->x_comment_to);
296 mutt_buffer_free (&p->spam);
297 string_list_wipe(&p->references);
298 string_list_wipe(&p->in_reply_to);
299 string_list_wipe(&p->userhdrs);
302 void body_wipe(BODY *b)
305 parameter_list_wipe(&b->parameter);
307 if (b->unlink && b->filename) {
308 unlink (b->filename);
311 p_delete(&b->filename);
312 p_delete(&b->content);
314 p_delete(&b->subtype);
315 p_delete(&b->description);
316 p_delete(&b->form_name);
319 /* Don't free twice (b->hdr->content = b->parts) */
320 b->hdr->content = NULL;
321 header_delete(&b->hdr);
325 body_list_wipe(&b->parts);
328 void header_wipe(HEADER *h)
330 envelope_delete(&h->env);
331 body_list_wipe(&h->content);
332 p_delete(&h->maildir_flags);
335 string_list_wipe(&h->chain);
340 /****************************************************************************/
342 /****************************************************************************/
344 int mutt_is_message_type(BODY *b)
348 if (b->type != TYPEMESSAGE)
351 tok = mime_which_token(b->subtype, -1);
352 return tok == MIME_RFC822 || tok == MIME_NEWS;
355 int mutt_is_text_part(BODY * b)
357 char *s = b->subtype;
359 if (mutt_is_application_pgp(b))
367 return mime_which_token(s, -1) == MIME_DELIVERY_STATUS;
369 case TYPEAPPLICATION:
370 return mime_which_token(s, -1) == MIME_PGP_KEYS;