2004-08-19 20:27:05 -04:00
|
|
|
/*
|
|
|
|
recode.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-08-19 20:27:05 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "settings.h"
|
2004-09-15 08:11:43 -04:00
|
|
|
#include "servers.h"
|
|
|
|
#include "signals.h"
|
2004-08-19 20:27:05 -04:00
|
|
|
#include "lib-config/iconfig.h"
|
2004-09-15 08:11:43 -04:00
|
|
|
#include "misc.h"
|
2004-08-19 20:27:05 -04:00
|
|
|
|
|
|
|
static gboolean recode_get_charset(const char **charset)
|
|
|
|
{
|
|
|
|
*charset = settings_get_str("term_charset");
|
|
|
|
if (**charset)
|
|
|
|
/* we use the same test as in src/fe-text/term.c:123 */
|
2008-03-09 08:01:16 -04:00
|
|
|
return (g_ascii_strcasecmp(*charset, "utf-8") == 0);
|
2005-01-12 11:40:44 -05:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
return g_get_charset(charset);
|
2006-09-20 19:04:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean is_utf8(void)
|
|
|
|
{
|
|
|
|
const char *charset;
|
|
|
|
|
|
|
|
return recode_get_charset(&charset);
|
|
|
|
}
|
2004-08-19 20:27:05 -04:00
|
|
|
|
2005-11-19 13:13:18 -05:00
|
|
|
static gboolean is_translit(const char *charset)
|
|
|
|
{
|
|
|
|
char *pos;
|
2006-09-20 19:47:20 -04:00
|
|
|
|
2005-11-19 13:13:18 -05:00
|
|
|
pos = stristr(charset, "//translit");
|
|
|
|
return (pos != NULL);
|
|
|
|
}
|
|
|
|
|
2004-09-15 08:11:43 -04:00
|
|
|
gboolean is_valid_charset(const char *charset)
|
|
|
|
{
|
2007-12-09 11:10:07 -05:00
|
|
|
GIConv cd;
|
|
|
|
char *to = NULL;
|
2004-09-15 08:11:43 -04:00
|
|
|
|
|
|
|
if (!charset || *charset == '\0')
|
|
|
|
return FALSE;
|
2005-11-19 13:13:18 -05:00
|
|
|
|
|
|
|
if (settings_get_bool("recode_transliterate") && !is_translit(charset))
|
|
|
|
charset = to = g_strconcat(charset, "//TRANSLIT", NULL);
|
|
|
|
|
2007-12-09 11:10:07 -05:00
|
|
|
cd = g_iconv_open(charset, "UTF-8");
|
2005-11-19 13:13:18 -05:00
|
|
|
g_free(to);
|
2007-12-09 11:10:07 -05:00
|
|
|
if (cd != (GIConv)-1) {
|
|
|
|
g_iconv_close(cd);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2004-09-15 08:11:43 -04:00
|
|
|
}
|
|
|
|
|
2007-05-18 10:41:44 -04:00
|
|
|
static char *find_conversion(const SERVER_REC *server, const char *target)
|
|
|
|
{
|
|
|
|
char *conv = NULL;
|
|
|
|
|
|
|
|
if (server != NULL && target != NULL) {
|
|
|
|
char *tagtarget = g_strdup_printf("%s/%s", server->tag, target);
|
|
|
|
conv = iconfig_get_str("conversions", tagtarget, NULL);
|
|
|
|
g_free(tagtarget);
|
|
|
|
}
|
|
|
|
if (conv == NULL && target != NULL)
|
|
|
|
conv = iconfig_get_str("conversions", target, NULL);
|
|
|
|
if (conv == NULL && server != NULL)
|
|
|
|
conv = iconfig_get_str("conversions", server->tag, NULL);
|
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
|
2004-12-20 08:59:15 -05:00
|
|
|
char *recode_in(const SERVER_REC *server, const char *str, const char *target)
|
2004-08-19 20:27:05 -04:00
|
|
|
{
|
|
|
|
const char *from = NULL;
|
|
|
|
const char *to = NULL;
|
2004-09-15 08:11:43 -04:00
|
|
|
char *translit_to = NULL;
|
2004-08-19 20:27:05 -04:00
|
|
|
char *recoded = NULL;
|
2005-11-07 14:06:42 -05:00
|
|
|
gboolean term_is_utf8, str_is_utf8, translit, recode, autodetect;
|
2004-08-19 20:27:05 -04:00
|
|
|
int len;
|
2006-07-25 07:02:10 -04:00
|
|
|
int i;
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (!str)
|
2004-09-20 08:45:49 -04:00
|
|
|
return NULL;
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2005-06-29 03:47:45 -04:00
|
|
|
recode = settings_get_bool("recode");
|
|
|
|
if (!recode)
|
|
|
|
return g_strdup(str);
|
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
len = strlen(str);
|
2005-06-29 03:47:45 -04:00
|
|
|
|
2006-07-25 07:02:10 -04:00
|
|
|
/* Only validate for UTF-8 if an 8-bit encoding. */
|
|
|
|
str_is_utf8 = 0;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (str[i] & 0x80) {
|
|
|
|
str_is_utf8 = g_utf8_validate(str, len, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-19 20:27:05 -04:00
|
|
|
translit = settings_get_bool("recode_transliterate");
|
2005-11-07 14:06:42 -05:00
|
|
|
autodetect = settings_get_bool("recode_autodetect_utf8");
|
|
|
|
term_is_utf8 = recode_get_charset(&to);
|
2005-07-24 15:16:14 -04:00
|
|
|
|
2005-11-07 14:06:42 -05:00
|
|
|
if (autodetect && str_is_utf8)
|
|
|
|
if (term_is_utf8)
|
|
|
|
return g_strdup(str);
|
|
|
|
else
|
|
|
|
from = "UTF-8";
|
2008-02-04 10:30:38 -05:00
|
|
|
|
2005-11-07 14:06:42 -05:00
|
|
|
else {
|
2007-05-18 10:41:44 -04:00
|
|
|
from = find_conversion(server, target);
|
2005-11-07 14:06:42 -05:00
|
|
|
}
|
2004-09-15 08:11:43 -04:00
|
|
|
|
|
|
|
if (translit && !is_translit(to))
|
|
|
|
to = translit_to = g_strconcat(to, "//TRANSLIT", NULL);
|
2008-02-04 10:30:38 -05:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (from)
|
2005-07-24 16:01:06 -04:00
|
|
|
recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
|
2004-08-19 20:27:05 -04:00
|
|
|
|
|
|
|
if (!recoded) {
|
2008-04-05 09:07:26 -04:00
|
|
|
if (str_is_utf8)
|
|
|
|
if (term_is_utf8)
|
|
|
|
return g_strdup(str);
|
|
|
|
else
|
|
|
|
from = "UTF-8";
|
|
|
|
else
|
|
|
|
from = settings_get_str("recode_fallback");
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (from)
|
2005-07-24 16:01:06 -04:00
|
|
|
recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
|
2004-08-19 20:27:05 -04:00
|
|
|
|
|
|
|
if (!recoded)
|
|
|
|
recoded = g_strdup(str);
|
|
|
|
}
|
2004-09-17 23:42:59 -04:00
|
|
|
g_free(translit_to);
|
2004-08-19 20:27:05 -04:00
|
|
|
return recoded;
|
|
|
|
}
|
|
|
|
|
2004-12-20 08:59:15 -05:00
|
|
|
char *recode_out(const SERVER_REC *server, const char *str, const char *target)
|
2004-08-19 20:27:05 -04:00
|
|
|
{
|
|
|
|
char *recoded = NULL;
|
|
|
|
const char *from = NULL;
|
2007-01-06 11:00:45 -05:00
|
|
|
const char *to = NULL;
|
|
|
|
char *translit_to = NULL;
|
2005-06-29 03:47:45 -04:00
|
|
|
gboolean translit, term_is_utf8, recode;
|
2004-08-19 20:27:05 -04:00
|
|
|
int len;
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (!str)
|
2004-09-20 08:45:49 -04:00
|
|
|
return NULL;
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2005-06-29 03:47:45 -04:00
|
|
|
recode = settings_get_bool("recode");
|
|
|
|
if (!recode)
|
|
|
|
return g_strdup(str);
|
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
len = strlen(str);
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
translit = settings_get_bool("recode_transliterate");
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2007-05-18 10:41:44 -04:00
|
|
|
to = find_conversion(server, target);
|
|
|
|
if (to == NULL)
|
2007-01-06 11:08:29 -05:00
|
|
|
/* default outgoing charset if set */
|
|
|
|
to = settings_get_str("recode_out_default_charset");
|
|
|
|
|
|
|
|
if (to && *to != '\0') {
|
|
|
|
if (translit && !is_translit(to))
|
|
|
|
to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);
|
|
|
|
|
|
|
|
term_is_utf8 = recode_get_charset(&from);
|
|
|
|
recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
g_free(translit_to);
|
2004-08-19 20:27:05 -04:00
|
|
|
if (!recoded)
|
|
|
|
recoded = g_strdup(str);
|
2004-09-20 08:45:49 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
return recoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void recode_init(void)
|
|
|
|
{
|
2005-06-29 03:47:45 -04:00
|
|
|
settings_add_bool("misc", "recode", TRUE);
|
2005-11-07 14:06:42 -05:00
|
|
|
settings_add_str("misc", "recode_fallback", "CP1252");
|
2004-08-19 20:27:05 -04:00
|
|
|
settings_add_str("misc", "recode_out_default_charset", "");
|
2005-11-19 11:15:55 -05:00
|
|
|
settings_add_bool("misc", "recode_transliterate", TRUE);
|
2005-11-07 14:06:42 -05:00
|
|
|
settings_add_bool("misc", "recode_autodetect_utf8", TRUE);
|
2004-08-19 20:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void recode_deinit(void)
|
2008-02-04 10:30:38 -05:00
|
|
|
{
|
2005-11-07 14:06:42 -05:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
}
|