2012-05-16 [paul] 3.8.0cvs43
[claws.git] / src / unmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 #include "codeconv.h"
29 #include "base64.h"
30 #include "quoted-printable.h"
31
32 #define ENCODED_WORD_BEGIN      "=?"
33 #define ENCODED_WORD_END        "?="
34
35 /* Decodes headers based on RFC2045 and RFC2047. */
36
37 gchar *unmime_header(const gchar *encoded_str, gboolean addr_field)
38 {
39         const gchar *p = encoded_str;
40         const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
41                     *eword_end_p;
42         gchar charset[32];
43         gchar encoding;
44         gchar *conv_str;
45         GString *outbuf;
46         gchar *out_str;
47         gsize out_len;
48         int in_quote = FALSE;
49
50         outbuf = g_string_sized_new(strlen(encoded_str) * 2);
51
52         while (*p != '\0') {
53                 gchar *decoded_text = NULL;
54                 const gchar *quote_p;
55                 gint len;
56
57                 eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
58                 if (!eword_begin_p) {
59                         g_string_append(outbuf, p);
60                         break;
61                 }
62                 
63                 quote_p = p;
64                 while ((quote_p = strchr(quote_p, '"')) != NULL) {
65                         if (quote_p && quote_p < eword_begin_p) {
66                                 /* Found a quote before the encoded word. */
67                                 in_quote = !in_quote;
68                                 quote_p++;
69                         }
70                         if (quote_p >= eword_begin_p)
71                                 break;
72                 }
73
74                 encoding_begin_p = strchr(eword_begin_p + 2, '?');
75                 if (!encoding_begin_p) {
76                         g_string_append(outbuf, p);
77                         break;
78                 }
79                 text_begin_p = strchr(encoding_begin_p + 1, '?');
80                 if (!text_begin_p) {
81                         g_string_append(outbuf, p);
82                         break;
83                 }
84                 eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
85                 if (!eword_end_p) {
86                         g_string_append(outbuf, p);
87                         break;
88                 }
89
90                 if (p == encoded_str) {
91                         g_string_append_len(outbuf, p, eword_begin_p - p);
92                         p = eword_begin_p;
93                 } else {
94                         /* ignore spaces between encoded words */
95                         const gchar *sp;
96
97                         for (sp = p; sp < eword_begin_p; sp++) {
98                                 if (!g_ascii_isspace(*sp)) {
99                                         g_string_append_len
100                                                 (outbuf, p, eword_begin_p - p);
101                                         p = eword_begin_p;
102                                         break;
103                                 }
104                         }
105                 }
106
107                 len = MIN(sizeof(charset) - 1,
108                           encoding_begin_p - (eword_begin_p + 2));
109                 memcpy(charset, eword_begin_p + 2, len);
110                 charset[len] = '\0';
111                 encoding = g_ascii_toupper(*(encoding_begin_p + 1));
112
113                 if (encoding == 'B') {
114                         decoded_text = g_malloc
115                                 (eword_end_p - (text_begin_p + 1) + 1);
116                         len = base64_decode(decoded_text, text_begin_p + 1,
117                                             eword_end_p - (text_begin_p + 1));
118                         decoded_text[len] = '\0';
119                 } else if (encoding == 'Q') {
120                         decoded_text = g_malloc
121                                 (eword_end_p - (text_begin_p + 1) + 1);
122                         len = qp_decode_q_encoding
123                                 (decoded_text, text_begin_p + 1,
124                                  eword_end_p - (text_begin_p + 1));
125                 } else {
126                         g_string_append_len(outbuf, p, eword_end_p + 2 - p);
127                         p = eword_end_p + 2;
128                         continue;
129                 }
130
131                 /* An encoded word MUST not appear within a quoted string,
132                  * so quoting that word after decoding should be safe.
133                  * We check there are no quotes just to be sure. If there
134                  * are, well, the comma won't pose a problem, probably.
135                  */
136                 if (addr_field && strchr(decoded_text, ',') && !in_quote) {
137                         gchar *tmp = g_strdup_printf("\"%s\"", decoded_text);
138                         g_free(decoded_text);
139                         decoded_text = tmp;
140                 }
141
142                 /* convert to UTF-8 */
143                 conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
144                 if (!conv_str || !g_utf8_validate(conv_str, -1, NULL)) {
145                         g_free(conv_str);
146                         conv_str = g_malloc(len + 1);
147                         conv_utf8todisp(conv_str, len + 1, decoded_text);
148                 }
149                 g_string_append(outbuf, conv_str);
150                 g_free(conv_str);
151
152                 g_free(decoded_text);
153
154                 p = eword_end_p + 2;
155         }
156         
157         out_str = outbuf->str;
158         out_len = outbuf->len;
159         g_string_free(outbuf, FALSE);
160
161         return g_realloc(out_str, out_len + 1);
162 }