4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "lib-config/iconfig.h"
28 static char *translit_charset;
29 static gboolean term_is_utf8;
31 gboolean is_utf8(void)
36 static gboolean is_translit(const char *charset)
40 pos = stristr(charset, "//translit");
44 gboolean is_valid_charset(const char *charset)
49 if (!charset || *charset == '\0')
52 if (settings_get_bool("recode_transliterate") && !is_translit(charset))
53 charset = to = g_strconcat(charset, "//TRANSLIT", NULL);
55 cd = g_iconv_open(charset, "UTF-8");
57 if (cd != (GIConv)-1) {
64 static char *find_conversion(const SERVER_REC *server, const char *target)
68 if (server != NULL && target != NULL) {
69 char *tagtarget = g_strdup_printf("%s/%s", server->tag, target);
70 conv = iconfig_get_str("conversions", tagtarget, NULL);
73 if (conv == NULL && target != NULL)
74 conv = iconfig_get_str("conversions", target, NULL);
75 if (conv == NULL && server != NULL)
76 conv = iconfig_get_str("conversions", server->tag, NULL);
80 static int str_is_ascii(const char *str)
84 for (i = 0; str[i] != '\0'; i++)
90 char *recode_in(const SERVER_REC *server, const char *str, const char *target)
92 const char *from = NULL;
93 const char *to = translit_charset;
95 gboolean str_is_utf8, recode, autodetect;
101 recode = settings_get_bool("recode");
103 return g_strdup(str);
107 /* Only validate for UTF-8 if an 8-bit encoding. */
109 if (!str_is_ascii(str))
110 str_is_utf8 = g_utf8_validate(str, len, NULL);
111 else if (!strchr(str, '\e'))
113 autodetect = settings_get_bool("recode_autodetect_utf8");
115 if (autodetect && str_is_utf8)
117 return g_strdup(str);
121 from = find_conversion(server, target);
124 recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
129 return g_strdup(str);
134 from = settings_get_str("recode_fallback");
139 recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
142 recoded = g_strdup(str);
147 char *recode_out(const SERVER_REC *server, const char *str, const char *target)
149 char *recoded = NULL;
150 const char *from = translit_charset;
151 const char *to = NULL;
152 char *translit_to = NULL;
153 gboolean translit, recode;
159 recode = settings_get_bool("recode");
161 return g_strdup(str);
165 translit = settings_get_bool("recode_transliterate");
167 to = find_conversion(server, target);
169 /* default outgoing charset if set */
170 to = settings_get_str("recode_out_default_charset");
172 if (to && *to != '\0') {
173 if (translit && !is_translit(to))
174 to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);
176 recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
180 recoded = g_strdup(str);
185 void recode_update_charset(void)
187 const char *charset = settings_get_str("term_charset");
188 term_is_utf8 = !g_ascii_strcasecmp(charset, "UTF-8");
189 g_free(translit_charset);
190 if (settings_get_bool("recode_transliterate") && !is_translit(charset))
191 translit_charset = g_strconcat(charset, "//TRANSLIT", NULL);
193 translit_charset = g_strdup(charset);
196 void recode_init(void)
198 settings_add_bool("misc", "recode", TRUE);
199 settings_add_str("misc", "recode_fallback", "CP1252");
200 settings_add_str("misc", "recode_out_default_charset", "");
201 settings_add_bool("misc", "recode_transliterate", TRUE);
202 settings_add_bool("misc", "recode_autodetect_utf8", TRUE);
205 void recode_deinit(void)
207 g_free(translit_charset);