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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
20 * Copyright notice from original mutt:
21 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
23 * This file is part of mutt-ng, see http://www.muttng.org/.
24 * It's licensed under the GNU General Public License,
25 * please see the file GPL in the top level source directory.
28 #include <lib-lib/lib-lib.h>
30 #ifdef HAVE_LANGINFO_H
31 # include <langinfo.h>
37 # define EILSEQ EINVAL
39 @import "lib-lua/base.cpkg"
41 int Charset_is_utf8 = 0;
42 wchar_t CharsetReplacement = '?';
44 static rx_t *charset_hooks = NULL;
45 static rx_t *iconv_hooks = NULL;
47 static char *charset_init(void)
49 const char *res = "iso-8859-1";
50 #ifdef HAVE_LANGINFO_H
54 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
55 charset_canonicalize(buff2, sizeof(buff2), buff);
57 /* finally, set $charset */
58 if (!m_strisempty(buff2)) {
62 bind_textdomain_codeset(PACKAGE, res);
66 static void charset_onchange(const char *cset)
68 Charset_is_utf8 = charset_is_utf8(cset);
69 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
75 ** This variable is a colon-separated list of character encoding
76 ** schemes for messages without character encoding indication.
77 ** Header field values and message body content without character encoding
78 ** indication would be assumed that they are written in one of this list.
79 ** By default, all the header fields and message body without any charset
80 ** indication are assumed to be in \fTus-ascii\fP.
82 ** For example, Japanese users might prefer this:
84 ** \fTset assumed_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
86 ** However, only the first content is valid for the message body.
87 ** This variable is valid only if $$strict_mime is unset.
89 string_t assumed_charset = m_strdup("us-ascii");
93 ** Character set your terminal uses to display and enter textual data.
96 .init = charset_init();
97 .onchange = charset_onchange($$);
102 ** This variable is a colon-separated list of character encoding
103 ** schemes for text file attatchments.
104 ** If \fIunset\fP, $$charset value will be used instead.
105 ** For example, the following configuration would work for Japanese
108 ** \fTset file_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
110 ** Note: ``\fTiso-2022-*\fP'' must be put at the head of the value as shown above
113 string_t file_charset = NULL;
117 ** A list of character sets for outgoing messages. Madmutt will use the
118 ** first character set into which the text can be converted exactly.
119 ** If your ``$$charset'' is not \fTiso-8859-1\fP and recipients may not
120 ** understand \fTUTF-8\fP, it is advisable to include in the list an
121 ** appropriate widely used standard character set (such as
122 ** \fTiso-8859-2\fP, \fTkoi8-r\fP or \fTiso-2022-jp\fP) either
123 ** instead of or after \fTiso-8859-1\fP.
125 string_t send_charset = m_strdup("us-ascii:iso-8859-1:utf-8");
127 void charset_hook(rx_t local, const string_t alias) {
128 rx_set_template(local, alias);
129 rx_list_add2(&charset_hooks, &local);
133 void iconv_hook(rx_t local, const string_t alias) {
134 rx_set_template(local, alias);
135 rx_list_add2(&iconv_hooks, &local);
140 /****************************************************************************/
141 /* charset functions */
142 /****************************************************************************/
144 #include "charset.gperf"
145 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
147 const struct cset_pair *cp;
148 char scratch[STRING];
153 m_strcpy(dest, dlen, "us-ascii");
157 // canonize name: only keep a-z0-9 and dots, put into lowercase
158 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
159 if (isalnum(*p) || *p== '.') {
160 scratch[i++] = tolower((unsigned char)*p);
165 cp = charset_canonicalize_aux(scratch, strlen(scratch));
167 m_strcpy(dest, dlen, cp->pref);
169 m_strcpy(dest, dlen, name);
174 /* XXX: MC: UGLY return of local static */
175 const char *charset_getfirst(const char *charset)
177 static char fcharset[STRING];
180 if (m_strisempty(charset))
183 p = m_strchrnul(charset, ':');
184 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
188 int charset_is_utf8(const char *s)
191 charset_canonicalize(buf, sizeof(buf), s);
192 return !m_strcmp(buf, "utf-8");
195 int charset_is_us_ascii(const char *s)
198 charset_canonicalize(buf, sizeof(buf), s);
199 return !m_strcmp(buf, "us-ascii");
203 /****************************************************************************/
204 /* iconv-line functions */
205 /****************************************************************************/
207 /* Like iconv_open, but canonicalises the charsets */
208 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
210 char to1[STRING], to2[STRING];
211 char from1[STRING], from2[STRING];
215 if ((flags & M_ICONV_HOOK_TO)
216 && rx_list_match2(charset_hooks, tocode, tmp, sizeof(tmp))) {
217 charset_canonicalize(to1, sizeof(to1), tmp);
219 charset_canonicalize(to1, sizeof(to1), tocode);
222 if ((flags & M_ICONV_HOOK_FROM)
223 && rx_list_match2(charset_hooks, fromcode, tmp, sizeof(tmp))) {
224 charset_canonicalize(from1, sizeof(from1), tmp);
226 charset_canonicalize(from1, sizeof(from1), fromcode);
229 m_strcat(to1, sizeof(to1), "//TRANSLIT");
230 if ((cd = iconv_open(to1, from1)) != MUTT_ICONV_ERROR)
233 if (rx_list_match2(iconv_hooks, to1, to2, sizeof(to2))
234 && rx_list_match2(iconv_hooks, from1, from2, sizeof(from2))) {
235 m_strcat(to2, sizeof(to2), "//TRANSLIT");
236 return iconv_open(to2, from2);
239 return MUTT_ICONV_ERROR;
243 /* Convert a string */
245 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
248 if (m_strisempty(*ps))
251 cd = mutt_iconv_open(to, from, flags);
252 if (cd != MUTT_ICONV_ERROR) {
257 ibl = m_strlen(*ps) + 1;
260 obl = MB_LEN_MAX * ibl;
261 ob = buf = p_new(char, obl + 1);
263 mutt_iconv(cd, &ib, &ibl, &ob, &obl);
276 static ssize_t convert_string(const char *f, ssize_t flen,
277 const char *from, const char *to,
278 char **t, ssize_t * tlen)
286 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
290 ob = buf = p_new(char, obl);
291 n = mutt_iconv(cd, &f, &flen, &ob, &obl);
293 if (n < 0 || mutt_iconv(cd, 0, 0, &ob, &obl) < 0) {
308 int mutt_convert_nonmime_string(char **ps)
310 const char *p = mod_cset.assumed_charset;
311 ssize_t ulen = m_strlen(*ps);
316 char fromcode[LONG_STRING], *s = NULL;
325 q = m_strchrnul(p, ':');
326 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
329 if (convert_string(u, ulen, fromcode, mod_cset.charset, &s, &slen) >= 0) {
339 /****************************************************************************/
340 /* fgetconv functions */
341 /****************************************************************************/
343 /* fgetconv_t stuff for converting a file while reading it
344 Used in sendlib.c for converting from mutt's charset */
358 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
360 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
363 fc->cd = MUTT_ICONV_ERROR;
365 fc->cd = mutt_iconv_open(to, from, flags);
367 if (fc->cd != MUTT_ICONV_ERROR) {
368 fc->p = fc->ob = fc->bufo;
375 void fgetconv_close(fgetconv_t **fcp)
377 struct fgetconv_t *fc = *fcp;
379 if (fc->cd != MUTT_ICONV_ERROR)
385 int fgetconv(fgetconv_t *fc)
390 if (fc->cd == MUTT_ICONV_ERROR)
391 return fgetc(fc->file);
396 return (unsigned char)*(fc->p)++;
398 /* Try to convert some more */
399 fc->p = fc->ob = fc->bufo;
401 ssize_t obl = ssizeof(fc->bufo);
403 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
405 return (unsigned char)*(fc->p)++;
408 /* If we trusted iconv a bit more, we would at this point
409 * ask why it had stopped converting ... */
411 /* Try to read some more */
412 if (fc->ibl == sizeof(fc->bufi)
413 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
419 memcpy(fc->bufi, fc->ib, fc->ibl);
422 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
425 /* Try harder this time to convert some */
427 ssize_t obl = ssizeof(fc->bufo);
429 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
430 if (fc->p < fc->ob) {
431 return (unsigned char)*(fc->p)++;
435 /* Either the file has finished or one of the buffers is too small */
440 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
444 while (pos < len - 1) {
445 int c = fgetconv(fc);
455 return pos ? buf : NULL;