2 * Incoming Mail Checker: Charsets
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * The code for parsing rfc2047 encoding of headers has been adapted
7 * from the Mutt 1.5.16 MUA. Here is the original copyright message:
9 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
10 * Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
42 static char *system_charset;
44 #define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0
52 static int Index_hex[128] = {
53 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
54 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
55 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
56 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
57 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
58 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
59 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
60 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
63 static int Index_64[128] = {
64 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
65 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
66 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
67 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
68 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
69 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
70 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
71 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
74 #define hexval(c) Index_hex[(unsigned int)(c)]
75 #define base64val(c) Index_64[(unsigned int)(c)]
77 #define OPTIGNORELWS 0
79 static int option(int opt UNUSED)
84 static size_t convert_string (char *f, size_t flen,
85 const char *from, const char *to,
86 char **t, size_t *tlen)
93 cd = iconv_open (to, from);
94 if (cd == (iconv_t)(-1))
97 ob = buf = xmalloc (obl);
98 n = iconv (cd, &f, &flen, &ob, &obl);
99 if (n == (size_t)(-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t)(-1))
111 buf = xrealloc (buf, ob - buf + 1);
118 static int rfc2047_decode_word (char *d, const char *s, size_t len)
120 const char *pp, *pp1;
123 int enc = 0, count = 0;
124 char *charset = NULL;
126 pd = d0 = xmalloc (strlen (s));
128 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1)
134 /* ignore language specification a la RFC 2231 */
136 if ((t1 = memchr (pp, '*', t - pp)))
138 charset = xmalloc (t - pp + 1);
139 memcpy (charset, pp, t - pp);
140 charset[t-pp] = '\0';
143 if (toupper ((unsigned char) *pp) == 'Q')
144 enc = ENCQUOTEDPRINTABLE;
145 else if (toupper ((unsigned char) *pp) == 'B')
155 if (enc == ENCQUOTEDPRINTABLE)
157 for (; pp < pp1; pp++)
161 else if (*pp == '=' &&
162 (!(pp[1] & ~127) && hexval(pp[1]) != -1) &&
163 (!(pp[2] & ~127) && hexval(pp[2]) != -1))
165 *pd++ = (hexval(pp[1]) << 4) | hexval(pp[2]);
173 else if (enc == ENCBASE64)
177 for (; pp < pp1; pp++)
181 if ((*pp & ~127) || (c = base64val(*pp)) == -1)
186 *pd++ = b | (c >> k);
202 if (charset && system_charset)
203 convert_string (d0, strlen(d0), charset, system_charset, &d0, &dlen);
204 strfcpy (d, d0, len);
211 * Find the start and end of the first encoded word in the string.
212 * We use the grammar in section 2 of RFC 2047, but the "encoding"
213 * must be B or Q. Also, we don't require the encoded word to be
214 * separated by linear-white-space (section 5(1)).
216 static const char *find_encoded_word (const char *s, const char **x)
221 while ((p = strstr (q, "=?")))
224 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q);
227 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
229 for (q = q + 3; 0x20 < *q && *q < 0x7f && *q != '?'; q++)
231 if (q[0] != '?' || q[1] != '=')
244 /* return length of linear-white-space */
245 static size_t lwslen (const char *s, size_t n)
253 for (; p < s + n; p++)
254 if (!strchr (" \t\r\n", *p))
256 len = (size_t)(p - s);
259 if (strchr ("\r\n", *(p-1))) /* LWS doesn't end with CRLF */
264 /* return length of linear-white-space : reverse */
265 static size_t lwsrlen (const char *s, size_t n)
267 const char *p = s + n - 1;
273 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
277 if (!strchr (" \t\r\n", *p))
279 len = (size_t)(s + n - 1 - p);
285 /* try to decode anything that looks like a valid RFC2047 encoded
286 * header field, ignoring RFC822 parsing rules
288 static void rfc2047_decode (char **pd)
292 int found_encoded = 0;
300 dlen = 4 * strlen (s); /* should be enough */
301 d = d0 = xmalloc (dlen + 1);
303 while (*s && dlen > 0)
305 if (!(p = find_encoded_word (s, &q)))
307 /* no encoded words */
308 if (option (OPTIGNORELWS))
311 if (found_encoded && (m = lwslen (s, n)) != 0)
314 *d = ' ', d++, dlen--;
318 strncpy (d, s, dlen);
325 n = (size_t) (p - s);
326 /* ignore spaces between encoded word
327 * and linear-white-space between encoded word and *text */
328 if (option (OPTIGNORELWS))
330 if (found_encoded && (m = lwslen (s, n)) != 0)
333 *d = ' ', d++, dlen--;
337 if ((m = n - lwsrlen (s, n)) != 0)
345 *d = ' ', d++, dlen--;
348 else if (!found_encoded || strspn (s, " \t\r\n") != n)
358 rfc2047_decode_word (d, p, dlen);
371 /* Initialize the whole machinery */
375 setlocale(LC_CTYPE, "");
376 system_charset = nl_langinfo(CODESET);
377 if (!system_charset[0])
378 system_charset = NULL;
379 debug("Charset is %s\n", system_charset);
383 add_snippet(char **ppos, char *term, char *add)
387 mbtowc(NULL, NULL, 0);
389 while (pos + MB_CUR_MAX < term)
392 int l = mbtowc(&c, add, MB_CUR_MAX);
419 add_subject_snippet(char **ppos, char *term, char *add)
421 char *buf = xstrdup(add);
422 rfc2047_decode(&buf);
423 add_snippet(ppos, term, buf);
428 add_addr_snippet(char **ppos, char *term, char *add, int add_mbox, int add_personal)
430 ADDRESS *addr = rfc822_parse_adrlist(NULL, add);
433 debug("%s: Cannot parse address (%s)\n", add, rfc822_error(RFC822Error));
434 add_subject_snippet(ppos, term, add);
437 // debug("%s: pers=%s mbox=%s\n", add, addr->personal, addr->mailbox);
438 rfc2047_decode(&addr->personal);
439 if (!addr->mailbox || !addr->mailbox[0])
441 if (!addr->personal || !addr->personal[0])
443 if (addr->mailbox && addr->mailbox[0])
445 char *c = strchr(addr->mailbox, '@');
452 if (add_mbox || add_personal)
455 add_snippet(ppos, term, addr->personal);
456 if (add_mbox && add_personal)
457 add_snippet(ppos, term, " <");
459 add_snippet(ppos, term, addr->mailbox);
460 if (add_mbox && add_personal)
461 add_snippet(ppos, term, ">");
464 add_snippet(ppos, term, "???");
465 rfc822_free_address(&addr);