EXIT AUTOCRAP \o/
[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 static void purge_empty_parameters(parameter_t **headp)
132 {
133     while (*headp) {
134         parameter_t *p = *headp;
135
136         if (!p->attribute || !p->value) {
137             p = parameter_list_pop(headp);
138             parameter_delete(&p);
139         } else {
140             headp = &(*headp)->next;
141         }
142     }
143 }
144
145 /* process continuation parameters */
146 /* XXX: MC: not read */
147 static void
148 rfc2231_join_continuations(parameter_t **head, rfc2231_param *par)
149 {
150     rfc2231_param *q;
151
152     char attribute[STRING];
153     char charset[STRING];
154     char *value = NULL;
155     char *valp;
156     int encoded;
157
158     size_t l, vl;
159
160     while (par) {
161         value = NULL;
162         l = 0;
163
164         m_strcpy(attribute, sizeof(attribute), par->attribute);
165
166         if ((encoded = par->encoded))
167             valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
168         else
169             valp = par->value;
170
171         do {
172             if (encoded && par->encoded)
173                 rfc2231_decode_one (par->value, valp);
174
175             vl = m_strlen(par->value);
176
177             p_realloc(&value, l + vl + 1);
178             m_strcpy(value + l, vl + 1, par->value);
179             l += vl;
180
181             q = par->next;
182             rfc2231_param_delete (&par);
183             if ((par = q))
184                 valp = par->value;
185         } while (par && !m_strcmp(par->attribute, attribute));
186
187         if (value) {
188             if (encoded)
189                 mutt_convert_string (&value, charset, MCharset.charset, M_ICONV_HOOK_FROM);
190             *head = parameter_new();
191             (*head)->attribute = m_strdup(attribute);
192             (*head)->value = value;
193             head = &(*head)->next;
194         }
195     }
196 }
197
198 /****************************************************************************/
199 /* Public API                                                               */
200 /****************************************************************************/
201
202 /* XXX: MC: not read */
203 void rfc2231_decode_parameters (parameter_t ** headp)
204 {
205     parameter_t *head = NULL;
206     parameter_t **last;
207     parameter_t *p, *q;
208
209     rfc2231_param *conthead = NULL;
210     rfc2231_param *conttmp;
211
212     char *s, *t;
213     char charset[STRING];
214
215     int encoded;
216     int idx;
217     short dirty = 0;   /* 1 when we may have created empty parameters. */
218
219     if (!headp)
220         return;
221
222     purge_empty_parameters (headp);
223
224     for (last = &head, p = *headp; p; p = q) {
225         q = p->next;
226
227         if (!(s = strchr (p->attribute, '*'))) {
228
229             /* 
230              * Using RFC 2047 encoding in MIME parameters is explicitly
231              * forbidden by that document.  Nevertheless, it's being
232              * generated by some software, including certain Lotus Notes to 
233              * Internet Gateways.  So we actually decode it.
234              */
235
236             if (p->value && strstr(p->value, "=?")) {
237                 rfc2047_decode(&p->value);
238             } else {
239                 if (mime_which_token(MCharset.assumed_charset, -1) == MIME_US_ASCII)
240                     mutt_convert_nonmime_string(&p->value);
241             }
242
243             *last = p;
244             last = &p->next;
245             p->next = NULL;
246         }
247         else if (*(s + 1) == '\0') {
248             *s = '\0';
249
250             s = rfc2231_get_charset (p->value, charset, sizeof (charset));
251             rfc2231_decode_one (p->value, s);
252             mutt_convert_string (&p->value, charset, MCharset.charset, M_ICONV_HOOK_FROM);
253
254             *last = p;
255             last = &p->next;
256             p->next = NULL;
257
258             dirty = 1;
259         }
260         else {
261             *s = '\0';
262             s++;                      /* let s point to the first character of idx. */
263             for (t = s; *t && isdigit ((unsigned char) *t); t++);
264             encoded = (*t == '*');
265             *t = '\0';
266
267             idx = atoi (s);
268
269             conttmp = rfc2231_param_new ();
270             conttmp->attribute = p->attribute;
271             conttmp->value = p->value;
272             conttmp->encoded = encoded;
273             conttmp->idx = idx;
274
275             p->attribute = NULL;
276             p->value = NULL;
277             p_delete(&p);
278
279             rfc2231_list_insert (&conthead, conttmp);
280         }
281     }
282
283     if (conthead) {
284         rfc2231_join_continuations (last, conthead);
285         dirty = 1;
286     }
287
288     *headp = head;
289
290     if (dirty)
291         purge_empty_parameters (headp);
292 }
293
294 #define RFC2231_SPECIALS  "@.,;:<>[]\\\"()?/= \t*'%"
295
296 int rfc2231_encode_string(char **s)
297 {
298     char *charset = NULL;
299     char *e, *p, *t, *d = NULL;
300     int escapes = 0;
301     ssize_t dlen = 0;
302
303     /*
304      * A shortcut to detect pure 7bit data.
305      *
306      * This should prevent the worst when character set handling is flawed.
307      */
308
309     for (p = *s; ; p++) {
310         if (*p & 0x80)
311             break;
312         if (!*p)
313             return 0;
314     }
315
316     if (MCharset.charset && MCharset.send_charset) {
317         charset = mutt_choose_charset(MCharset.charset, MCharset.send_charset,
318                                       *s, m_strlen(*s), &d, &dlen);
319     }
320
321     if (!charset) {
322         charset = m_strdup(MCharset.charset ?: "unknown-8bit");
323         d = *s;
324         dlen = m_strlen(d);
325     }
326
327     for (p = d; *p; p++) {
328         if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
329             ++escapes;
330         }
331     }
332
333     e = p_new(char, dlen + 2 * escapes + m_strlen(charset) + 3);
334
335     t = e + sprintf(e, "%s''", charset);
336     for (p = d; *p; p++) {
337         if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
338             *t++ = '%';
339             *t++ = __m_b36chars_upper[*p >> 4];
340             *t++ = __m_b36chars_upper[*p & 0xf];
341         } else {
342             *t++ = *p;
343         }
344     }
345     *t = '\0';
346
347     if (d != *s)
348         p_delete(&d);
349     p_delete(s);
350     p_delete(&charset);
351
352     *s = e;
353     return 1;
354 }