move url.[hc] into the lib-lib.
[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 void address_wipe(address_t *addr)
52 {
53     p_delete(&addr->personal);
54     p_delete(&addr->mailbox);
55     address_delete(&addr->next);
56 }
57
58 void parameter_wipe(PARAMETER *param)
59 {
60     p_delete(&param->attribute);
61     p_delete(&param->value);
62     parameter_delete(&param->next);
63 }
64
65 void envelope_wipe(ENVELOPE *p)
66 {
67     address_delete(&p->return_path);
68     address_delete(&p->from);
69     address_delete(&p->to);
70     address_delete(&p->cc);
71     address_delete(&p->bcc);
72     address_delete(&p->sender);
73     address_delete(&p->reply_to);
74     address_delete(&p->mail_followup_to);
75
76     p_delete(&p->list_post);
77     p_delete(&p->subject);
78     /* real_subj is just an offset to subject and shouldn't be freed */
79     p_delete(&p->message_id);
80     p_delete(&p->supersedes);
81     p_delete(&p->date);
82     p_delete(&p->x_label);
83     p_delete(&p->organization);
84 #ifdef USE_NNTP
85     p_delete(&p->newsgroups);
86     p_delete(&p->xref);
87     p_delete(&p->followup_to);
88     p_delete(&p->x_comment_to);
89 #endif
90
91     mutt_buffer_free (&p->spam);
92     mutt_free_list(&p->references);
93     mutt_free_list(&p->in_reply_to);
94     mutt_free_list(&p->userhdrs);
95 }
96
97 void header_wipe(HEADER *h)
98 {
99     envelope_delete(&h->env);
100     mutt_free_body (&h->content);
101     p_delete(&h->maildir_flags);
102     p_delete(&h->tree);
103     p_delete(&h->path);
104 #ifdef MIXMASTER
105     mutt_free_list(&h->chain);
106 #endif
107     p_delete(&h->data);
108 }
109
110 int url_parse_mailto(ENVELOPE *e, char **body, const char *src)
111 {
112     char *t;
113     char *tmp;
114     char *headers;
115     char *tag, *value;
116     char scratch[HUGE_STRING];
117
118     int taglen;
119
120     LIST **last = &e->userhdrs;
121
122     if (!(t = strchr (src, ':')))
123         return -1;
124
125     if ((tmp = m_strdup(t + 1)) == NULL)
126         return -1;
127
128     if ((headers = strchr (tmp, '?')))
129         *headers++ = '\0';
130
131     url_decode(tmp);
132     e->to = rfc822_parse_adrlist (e->to, tmp);
133
134     tag = headers ? strtok (headers, "&") : NULL;
135
136     for (; tag; tag = strtok (NULL, "&")) {
137         if ((value = strchr (tag, '=')))
138             *value++ = '\0';
139         if (!value || !*value)
140             continue;
141
142         url_decode (tag);
143         url_decode (value);
144
145         if (!ascii_strcasecmp (tag, "body")) {
146             if (body)
147                 m_strreplace(body, value);
148         }
149         else {
150 #define SAFEPFX (option (OPTSTRICTMAILTO) ? "" : "X-Mailto-")
151             taglen = m_strlen(tag) + m_strlen(SAFEPFX);
152             /* mutt_parse_rfc822_line makes some assumptions */
153             snprintf (scratch, sizeof (scratch), "%s%s: %s", SAFEPFX, tag, value);
154 #undef SAVEPFX
155             scratch[taglen] = '\0';
156             value = vskipspaces(&scratch[taglen + 1]);
157             last = mutt_parse_rfc822_line (e, NULL, scratch, value, 0, 0, last);
158             /* if $strict_mailto is set, force editing headers to let
159              * users have a look at what we got */
160             if (!option (OPTSTRICTMAILTO)) {
161                 set_option (OPTXMAILTO);
162                 set_option (OPTEDITHDRS);
163             }
164         }
165     }
166
167     p_delete(&tmp);
168     return 0;
169 }