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