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"
29
static gboolean recode_get_charset(const char **charset)
31
*charset = settings_get_str("term_charset");
33
/* we use the same test as in src/fe-text/term.c:123 */
34
return !g_strcasecmp(*charset, "utf-8");
36
return g_get_charset(charset);
40
static gboolean is_translit(const char *charset)
43
pos = stristr(charset, "//translit");
47
gboolean is_valid_charset(const char *charset)
50
const char *from="UTF-8";
51
const char *str="irssi";
52
char *recoded, *to = NULL;
55
if (!charset || *charset == '\0')
58
if (settings_get_bool("recode_transliterate") && !is_translit(charset))
59
charset = to = g_strconcat(charset, "//TRANSLIT", NULL);
61
recoded = g_convert(str, strlen(str), charset, from, NULL, NULL, NULL);
62
valid = (recoded != NULL);
67
if (!charset || *charset =='\0')
73
char *recode_in(const SERVER_REC *server, const char *str, const char *target)
76
const char *from = NULL;
77
const char *to = NULL;
78
char *translit_to = NULL;
80
char *tagtarget = NULL;
81
gboolean term_is_utf8, str_is_utf8, translit, recode, autodetect;
87
recode = settings_get_bool("recode");
93
str_is_utf8 = g_utf8_validate(str, len, NULL);
94
translit = settings_get_bool("recode_transliterate");
95
autodetect = settings_get_bool("recode_autodetect_utf8");
96
term_is_utf8 = recode_get_charset(&to);
98
if (autodetect && str_is_utf8)
100
return g_strdup(str);
105
if (server != NULL && target != NULL)
106
tagtarget = server->tag == NULL ? NULL :
107
g_strdup_printf("%s/%s", server->tag, target);
108
if (tagtarget != NULL)
109
from = iconfig_get_str("conversions", tagtarget, NULL);
112
if (target != NULL && from == NULL)
113
from = iconfig_get_str("conversions", target, NULL);
115
if (from == NULL && server != NULL)
116
from = iconfig_get_str("conversions", server->tag, NULL);
120
if (translit && !is_translit(to))
121
to = translit_to = g_strconcat(to, "//TRANSLIT", NULL);
124
recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
129
from = settings_get_str("recode_fallback");
131
} else if (str_is_utf8)
135
recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
138
recoded = g_strdup(str);
143
return g_strdup(str);
147
char *recode_out(const SERVER_REC *server, const char *str, const char *target)
150
char *recoded = NULL;
151
const char *from = NULL;
152
gboolean translit, term_is_utf8, recode;
158
recode = settings_get_bool("recode");
160
return g_strdup(str);
164
translit = settings_get_bool("recode_transliterate");
167
const char *to = NULL;
168
char *translit_to = NULL;
169
char *tagtarget = NULL;
171
if (server != NULL && target != NULL)
172
tagtarget = server->tag == NULL ? NULL :
173
g_strdup_printf("%s/%s", server->tag, target);
174
if (tagtarget != NULL)
175
to = iconfig_get_str("conversions", tagtarget, NULL);
177
if (to == NULL || *to == '\0')
178
to = iconfig_get_str("conversions", target, NULL);
179
if ((to == NULL || *to == '\0') && server != NULL)
180
to = iconfig_get_str("conversions", server->tag, NULL);
181
if (to == NULL || *to == '\0')
182
/* default outgoing charset if set */
183
to = settings_get_str("recode_out_default_charset");
185
if (to && *to != '\0') {
186
if (translit && !is_translit(to))
187
to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);
189
term_is_utf8 = recode_get_charset(&from);
190
recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
195
recoded = g_strdup(str);
199
return g_strdup(str);
203
void recode_init(void)
205
settings_add_bool("misc", "recode", TRUE);
206
settings_add_str("misc", "recode_fallback", "CP1252");
207
settings_add_str("misc", "recode_out_default_charset", "");
208
settings_add_bool("misc", "recode_transliterate", TRUE);
209
settings_add_bool("misc", "recode_autodetect_utf8", TRUE);
212
void recode_deinit(void)