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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "lib-config/iconfig.h"
28 static gboolean recode_get_charset(const char **charset)
30 *charset = settings_get_str("term_charset");
32 /* we use the same test as in src/fe-text/term.c:123 */
33 return (g_strcasecmp(*charset, "utf-8") == 0);
36 return g_get_charset(charset);
42 gboolean is_utf8(void)
46 return recode_get_charset(&charset);
50 static gboolean is_translit(const char *charset)
54 pos = stristr(charset, "//translit");
59 gboolean is_valid_charset(const char *charset)
62 const char *from="UTF-8";
63 const char *str="irssi";
64 char *recoded, *to = NULL;
67 if (!charset || *charset == '\0')
70 if (settings_get_bool("recode_transliterate") && !is_translit(charset))
71 charset = to = g_strconcat(charset, "//TRANSLIT", NULL);
73 recoded = g_convert(str, strlen(str), charset, from, NULL, NULL, NULL);
74 valid = (recoded != NULL);
79 if (!charset || *charset =='\0')
85 char *recode_in(const SERVER_REC *server, const char *str, const char *target)
88 const char *from = NULL;
89 const char *to = NULL;
90 char *translit_to = NULL;
92 gboolean term_is_utf8, str_is_utf8, translit, recode, autodetect;
99 recode = settings_get_bool("recode");
101 return g_strdup(str);
105 /* Only validate for UTF-8 if an 8-bit encoding. */
107 for (i = 0; i < len; ++i) {
109 str_is_utf8 = g_utf8_validate(str, len, NULL);
113 translit = settings_get_bool("recode_transliterate");
114 autodetect = settings_get_bool("recode_autodetect_utf8");
115 term_is_utf8 = recode_get_charset(&to);
117 if (autodetect && str_is_utf8)
119 return g_strdup(str);
124 if (server != NULL && server->tag != NULL && target != NULL) {
125 char *tagtarget = g_strdup_printf("%s/%s", server->tag, target);
126 from = iconfig_get_str("conversions", tagtarget, NULL);
130 if (target != NULL && from == NULL)
131 from = iconfig_get_str("conversions", target, NULL);
133 if (from == NULL && server != NULL)
134 from = iconfig_get_str("conversions", server->tag, NULL);
138 if (translit && !is_translit(to))
139 to = translit_to = g_strconcat(to, "//TRANSLIT", NULL);
142 recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
147 from = settings_get_str("recode_fallback");
149 } else if (str_is_utf8)
153 recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
156 recoded = g_strdup(str);
161 return g_strdup(str);
165 char *recode_out(const SERVER_REC *server, const char *str, const char *target)
168 char *recoded = NULL;
169 const char *from = NULL;
170 const char *to = NULL;
171 char *translit_to = NULL;
172 gboolean translit, term_is_utf8, recode;
178 recode = settings_get_bool("recode");
180 return g_strdup(str);
184 translit = settings_get_bool("recode_transliterate");
186 if (server != NULL && server->tag != NULL && target != NULL) {
187 char *tagtarget = g_strdup_printf("%s/%s", server->tag, target);
188 to = iconfig_get_str("conversions", tagtarget, NULL);
191 if (to == NULL || *to == '\0')
192 to = iconfig_get_str("conversions", target, NULL);
193 if ((to == NULL || *to == '\0') && server != NULL)
194 to = iconfig_get_str("conversions", server->tag, NULL);
195 if (to == NULL || *to == '\0')
196 /* default outgoing charset if set */
197 to = settings_get_str("recode_out_default_charset");
199 if (to && *to != '\0') {
200 if (translit && !is_translit(to))
201 to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);
203 term_is_utf8 = recode_get_charset(&from);
204 recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
208 recoded = g_strdup(str);
212 return g_strdup(str);
216 void recode_init(void)
218 settings_add_bool("misc", "recode", TRUE);
219 settings_add_str("misc", "recode_fallback", "CP1252");
220 settings_add_str("misc", "recode_out_default_charset", "");
221 settings_add_bool("misc", "recode_transliterate", TRUE);
222 settings_add_bool("misc", "recode_autodetect_utf8", TRUE);
225 void recode_deinit(void)