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