tmp
[apps/madmutt.git] / lib-mime / rfc2231.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 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
23  *
24  * This file is part of mutt-ng, see http://www.muttng.org/.
25  * It's licensed under the GNU General Public License,
26  * please see the file GPL in the top level source directory.
27  */
28
29 /*
30  * Yet another MIME encoding for header data.  This time, it's
31  * parameters, specified in RFC 2231, and modeled after the
32  * encoding used in URLs.
33  * 
34  * Additionally, continuations and encoding are mixed in an, errrm,
35  * interesting manner.
36  *
37  */
38
39 #include <lib-lib/lib-lib.h>
40
41 #include <lib-mime/mime.h>
42
43 #include "charset.h"
44 #include "mutt.h"
45
46 typedef struct rfc2231_param {
47     struct rfc2231_param *next;
48
49     char *attribute;
50     char *value;
51     int idx;
52     int encoded;
53 } rfc2231_param;
54
55 DO_INIT(rfc2231_param, rfc2231_param);
56 static inline void rfc2231_param_wipe(rfc2231_param *param)
57 {
58     p_delete(&param->attribute);
59     p_delete(&param->value);
60 }
61 DO_NEW(rfc2231_param, rfc2231_param);
62 DO_DELETE(rfc2231_param, rfc2231_param);
63
64 /* TODO: MC: replace with a str_unescape */
65 static void rfc2231_decode_one(char *dst, const char *src)
66 {
67     while (*src) {
68         int h1, h2;
69
70         if (*src == '%'
71         && (h1 = hexval(src[1])) >= 0 && (h2 = hexval(src[2])) >= 0)
72         {
73             *dst++ = (h1 << 4) | h2;
74             src += 3;
75         } else {
76             *dst++ = *src++;
77         }
78     }
79
80     *dst = '\0';
81 }
82
83 /* read the <charset>'foo' part into charset, and skip that */
84 static char *rfc2231_get_charset(char *value, char *charset, size_t chslen)
85 {
86     char *t, *u;
87
88     t = strchr(value, '\'');
89     if (!t) {
90         charset[0] = '\0';
91         return value;
92     }
93
94     *t = '\0';
95     m_strcpy(charset, chslen, value);
96
97     if ((u = strchr(t + 1, '\''))) {
98         return u + 1;
99     } else {
100         return t + 1;
101     }
102 }
103
104
105 /* insert parameter into an ordered list.
106  * 
107  * Primary sorting key: attribute
108  * Secondary sorting key: idx
109  *
110  * XXX: MC: looks very unclear to me
111  */
112 static void
113 rfc2231_list_insert(rfc2231_param **list, rfc2231_param *par)
114 {
115     int c;
116
117     while (*list) {
118         rfc2231_param *q = *list;
119
120         list = &(*list)->next;
121
122         c = m_strcmp(par->value, q->value);
123         if ((c > 0) || (c == 0 && par->idx >= q->idx))
124             break;
125     }
126
127     par->next = *list;
128     *list = par;
129 }
130
131 /* process continuation parameters */
132 /* XXX: MC: not read */
133 static void
134 rfc2231_join_continuations(parameters_t *head, rfc2231_param *par)
135 {
136     rfc2231_param *q;
137
138     char attribute[STRING];
139     char charset[STRING];
140     char *value = NULL;
141     char *valp;
142     int encoded;
143
144     size_t l, vl;
145
146     while (par) {
147         l = 0;
148
149         m_strcpy(attribute, sizeof(attribute), par->attribute);
150
151         if ((encoded = par->encoded))
152             valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
153         else
154             valp = par->value;
155
156         do {
157             if (encoded && par->encoded)
158                 rfc2231_decode_one (par->value, valp);
159
160             vl = m_strlen(par->value);
161
162             p_realloc(&value, l + vl + 1);
163             m_strcpy(value + l, vl + 1, par->value);
164             l += vl;
165
166             q = par->next;
167             rfc2231_param_delete (&par);
168             if ((par = q))
169                 valp = par->value;
170         } while (par && !m_strcmp(par->attribute, attribute));
171
172         if (!value)
173             continue;
174         if (encoded)
175             mutt_convert_string (&value, charset, mod_cset.charset, M_ICONV_HOOK_FROM);
176         parameter_setval(head, attribute, value);
177     }
178     p_delete(&value);
179 }
180
181 /****************************************************************************/
182 /* Public API                                                               */
183 /****************************************************************************/
184
185 /* XXX: MC: not read */
186 void rfc2231_decode_parameters(parameters_t *headp)
187 {
188 #if 0
189     parameter_t *head = NULL;
190     parameter_t **last;
191     parameter_t *p, *q;
192
193     rfc2231_param *conthead = NULL;
194     rfc2231_param *conttmp;
195
196     char *s, *t;
197     char charset[STRING];
198
199     int encoded;
200     int idx;
201     short dirty = 0;   /* 1 when we may have created empty parameters. */
202
203     if (!headp)
204         return;
205
206     parameter_purge_empty(headp);
207
208     for (last = &head, p = *headp; p; p = q) {
209         q = p->next;
210
211         if (!(s = strchr (p->attribute, '*'))) {
212
213             /* 
214              * Using RFC 2047 encoding in MIME parameters is explicitly
215              * forbidden by that document.  Nevertheless, it's being
216              * generated by some software, including certain Lotus Notes to 
217              * Internet Gateways.  So we actually decode it.
218              */
219
220             if (p->value && strstr(p->value, "=?")) {
221                 rfc2047_decode(&p->value);
222             } else {
223                 if (mime_which_token(mod_cset.assumed_charset, -1) == MIME_US_ASCII)
224                     mutt_convert_nonmime_string(&p->value);
225             }
226
227             *last = p;
228             last = &p->next;
229             p->next = NULL;
230         }
231         else if (*(s + 1) == '\0') {
232             *s = '\0';
233
234             s = rfc2231_get_charset (p->value, charset, sizeof (charset));
235             rfc2231_decode_one (p->value, s);
236             mutt_convert_string (&p->value, charset, mod_cset.charset, M_ICONV_HOOK_FROM);
237
238             *last = p;
239             last = &p->next;
240             p->next = NULL;
241
242             dirty = 1;
243         }
244         else {
245             *s = '\0';
246             s++;                      /* let s point to the first character of idx. */
247             for (t = s; *t && isdigit ((unsigned char) *t); t++);
248             encoded = (*t == '*');
249             *t = '\0';
250
251             idx = atoi (s);
252
253             conttmp = rfc2231_param_new ();
254             conttmp->attribute = p->attribute;
255             conttmp->value = p->value;
256             conttmp->encoded = encoded;
257             conttmp->idx = idx;
258
259             p->attribute = NULL;
260             p->value = NULL;
261             p_delete(&p);
262
263             rfc2231_list_insert (&conthead, conttmp);
264         }
265     }
266
267     if (conthead) {
268         rfc2231_join_continuations (last, conthead);
269         dirty = 1;
270     }
271
272     *headp = head;
273
274     if (dirty)
275         parameter_purge_empty(headp);
276 #endif
277 }
278
279 #define RFC2231_SPECIALS  "@.,;:<>[]\\\"()?/= \t*'%"
280
281 int rfc2231_encode_string(char **s)
282 {
283     char *charset = NULL;
284     char *e, *p, *t, *d = NULL;
285     int escapes = 0;
286     ssize_t dlen = 0;
287
288     /*
289      * A shortcut to detect pure 7bit data.
290      *
291      * This should prevent the worst when character set handling is flawed.
292      */
293
294     for (p = *s; ; p++) {
295         if (*p & 0x80)
296             break;
297         if (!*p)
298             return 0;
299     }
300
301     if (mod_cset.charset && mod_cset.send_charset) {
302         charset = mutt_choose_charset(mod_cset.charset, mod_cset.send_charset,
303                                       *s, m_strlen(*s), &d, &dlen);
304     }
305
306     if (!charset) {
307         charset = m_strdup(mod_cset.charset ?: "unknown-8bit");
308         d = *s;
309         dlen = m_strlen(d);
310     }
311
312     for (p = d; *p; p++) {
313         if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
314             ++escapes;
315         }
316     }
317
318     e = p_new(char, dlen + 2 * escapes + m_strlen(charset) + 3);
319
320     t = e + sprintf(e, "%s''", charset);
321     for (p = d; *p; p++) {
322         if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
323             *t++ = '%';
324             *t++ = __m_b36chars_upper[*p >> 4];
325             *t++ = __m_b36chars_upper[*p & 0xf];
326         } else {
327             *t++ = *p;
328         }
329     }
330     *t = '\0';
331
332     if (d != *s)
333         p_delete(&d);
334     p_delete(s);
335     p_delete(&charset);
336
337     *s = e;
338     return 1;
339 }