move mutt_get_parameter -> parameter_getval into mime.c
[apps/madmutt.git] / lib-mime / mime.c
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 #include <lib-lib/ascii.h>
21 #include <lib-lib/url.h>
22
23 #include "mime-types.h"
24
25 #include "mutt.h"
26
27 const char MimeSpecials[] = "@.,;:<>[]\\\"()?/= \t";
28
29 const char *BodyTypes[] = {
30     "x-unknown",
31     "audio",
32     "application",
33     "image",
34     "message",
35     "model",
36     "multipart",
37     "text",
38     "video",
39 };
40
41 const char *BodyEncodings[] = {
42     "x-unknown",
43     "7bit",
44     "8bit",
45     "quoted-printable",
46     "base64",
47     "binary",
48     "x-uuencoded",
49 };
50
51 /****************************************************************************/
52 /* rfc822 header parameters                                                 */
53 /****************************************************************************/
54
55 char *parameter_getval(PARAMETER *parm, const char *s)
56 {
57     while (parm) {
58         if (!ascii_strcasecmp(parm->attribute, s))
59             return parm->value;
60         parm = parm->next;
61     }
62     return NULL;
63 }
64
65 /****************************************************************************/
66 /* XXX                                                                      */
67 /****************************************************************************/
68
69 void rfc1524_entry_wipe(rfc1524_entry *p)
70 {
71     p_delete(&p->command);
72     p_delete(&p->testcommand);
73     p_delete(&p->composecommand);
74     p_delete(&p->composetypecommand);
75     p_delete(&p->editcommand);
76     p_delete(&p->printcommand);
77     p_delete(&p->nametemplate);
78     p_delete(&p->convert);
79 }
80
81 void envelope_wipe(ENVELOPE *p)
82 {
83     address_list_wipe(&p->return_path);
84     address_list_wipe(&p->from);
85     address_list_wipe(&p->to);
86     address_list_wipe(&p->cc);
87     address_list_wipe(&p->bcc);
88     address_list_wipe(&p->sender);
89     address_list_wipe(&p->reply_to);
90     address_list_wipe(&p->mail_followup_to);
91
92     p_delete(&p->list_post);
93     p_delete(&p->subject);
94     /* real_subj is just an offset to subject and shouldn't be freed */
95     p_delete(&p->message_id);
96     p_delete(&p->supersedes);
97     p_delete(&p->date);
98     p_delete(&p->x_label);
99     p_delete(&p->organization);
100 #ifdef USE_NNTP
101     p_delete(&p->newsgroups);
102     p_delete(&p->xref);
103     p_delete(&p->followup_to);
104     p_delete(&p->x_comment_to);
105 #endif
106
107     mutt_buffer_free (&p->spam);
108     string_list_wipe(&p->references);
109     string_list_wipe(&p->in_reply_to);
110     string_list_wipe(&p->userhdrs);
111 }
112
113 void header_wipe(HEADER *h)
114 {
115     envelope_delete(&h->env);
116     mutt_free_body (&h->content);
117     p_delete(&h->maildir_flags);
118     p_delete(&h->tree);
119     p_delete(&h->path);
120 #ifdef MIXMASTER
121     string_list_wipe(&h->chain);
122 #endif
123     p_delete(&h->data);
124 }
125
126 int url_parse_mailto(ENVELOPE *e, char **body, const char *src)
127 {
128     char *t;
129     char *tmp;
130     char *headers;
131     char *tag, *value;
132     char scratch[HUGE_STRING];
133
134     int taglen;
135
136     string_list_t **last = &e->userhdrs;
137
138     if (!(t = strchr (src, ':')))
139         return -1;
140
141     if ((tmp = m_strdup(t + 1)) == NULL)
142         return -1;
143
144     if ((headers = strchr (tmp, '?')))
145         *headers++ = '\0';
146
147     url_decode(tmp);
148     e->to = rfc822_parse_adrlist (e->to, tmp);
149
150     tag = headers ? strtok (headers, "&") : NULL;
151
152     for (; tag; tag = strtok (NULL, "&")) {
153         if ((value = strchr (tag, '=')))
154             *value++ = '\0';
155         if (!value || !*value)
156             continue;
157
158         url_decode (tag);
159         url_decode (value);
160
161         if (!ascii_strcasecmp (tag, "body")) {
162             if (body)
163                 m_strreplace(body, value);
164         }
165         else {
166 #define SAFEPFX (option (OPTSTRICTMAILTO) ? "" : "X-Mailto-")
167             taglen = m_strlen(tag) + m_strlen(SAFEPFX);
168             /* mutt_parse_rfc822_line makes some assumptions */
169             snprintf (scratch, sizeof (scratch), "%s%s: %s", SAFEPFX, tag, value);
170 #undef SAVEPFX
171             scratch[taglen] = '\0';
172             value = vskipspaces(&scratch[taglen + 1]);
173             last = mutt_parse_rfc822_line (e, NULL, scratch, value, 0, 0, last);
174             /* if $strict_mailto is set, force editing headers to let
175              * users have a look at what we got */
176             if (!option (OPTSTRICTMAILTO)) {
177                 set_option (OPTXMAILTO);
178                 set_option (OPTEDITHDRS);
179             }
180         }
181     }
182
183     p_delete(&tmp);
184     return 0;
185 }