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.
|
|
|
|
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2005-01-12 07:10:48 -05:00
|
|
|
#include <locale.h>
|
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
#ifdef HAVE_GLIB2
|
|
|
|
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 */
|
|
|
|
return !g_strcasecmp(*charset, "utf-8");
|
2005-01-12 07:10:48 -05:00
|
|
|
|
|
|
|
/* we have to set LC_ALL to "" to get the charset of the user */
|
|
|
|
setlocale(LC_ALL, "");
|
2004-08-19 20:27:05 -04:00
|
|
|
return g_get_charset(charset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-09-15 08:11:43 -04:00
|
|
|
gboolean is_valid_charset(const char *charset)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_GLIB2
|
|
|
|
const char *from="UTF-8";
|
|
|
|
const char *str="irssi";
|
|
|
|
char *recoded;
|
|
|
|
gboolean valid;
|
|
|
|
|
|
|
|
if (!charset || *charset == '\0')
|
|
|
|
return FALSE;
|
|
|
|
recoded = g_convert(str, strlen(str), charset, from, NULL, NULL, NULL);
|
|
|
|
valid = (recoded != NULL);
|
|
|
|
g_free(recoded);
|
|
|
|
return valid;
|
|
|
|
#else
|
|
|
|
if (!charset || *charset =='\0')
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean is_translit(const char *charset)
|
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
pos = stristr(charset, "//translit");
|
|
|
|
return (pos != NULL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
#ifdef HAVE_GLIB2
|
|
|
|
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;
|
2004-12-20 08:59:15 -05:00
|
|
|
char *tagtarget = NULL;
|
2004-09-15 08:11:43 -04:00
|
|
|
gboolean term_is_utf8, str_is_utf8, translit;
|
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
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
len = strlen(str);
|
|
|
|
|
|
|
|
str_is_utf8 = g_utf8_validate(str, len, NULL);
|
|
|
|
translit = settings_get_bool("recode_transliterate");
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-12-20 08:59:15 -05:00
|
|
|
if (server != NULL)
|
|
|
|
tagtarget = server->tag == NULL ? NULL :
|
|
|
|
g_strdup_printf("%s/%s", server->tag, target);
|
|
|
|
if (tagtarget != NULL)
|
|
|
|
from = iconfig_get_str("conversions", tagtarget, NULL);
|
|
|
|
g_free(tagtarget);
|
|
|
|
if (target != NULL && from == NULL)
|
2004-08-19 20:27:05 -04:00
|
|
|
from = iconfig_get_str("conversions", target, NULL);
|
|
|
|
|
|
|
|
term_is_utf8 = recode_get_charset(&to);
|
2004-09-15 08:11:43 -04:00
|
|
|
|
|
|
|
if (translit && !is_translit(to))
|
|
|
|
to = translit_to = g_strconcat(to, "//TRANSLIT", NULL);
|
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (from)
|
|
|
|
recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (!recoded) {
|
|
|
|
if (term_is_utf8) {
|
|
|
|
if (!str_is_utf8)
|
|
|
|
from = settings_get_str("recode_fallback");
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
} else if (str_is_utf8)
|
|
|
|
from = "UTF-8";
|
2004-09-15 08:11:43 -04:00
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (from)
|
|
|
|
recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
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;
|
|
|
|
#else
|
|
|
|
return g_strdup(str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
#ifdef HAVE_GLIB2
|
|
|
|
char *recoded = NULL;
|
|
|
|
const char *from = NULL;
|
2004-09-20 08:45:49 -04:00
|
|
|
gboolean translit, term_is_utf8;
|
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
|
|
|
|
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
|
|
|
|
2004-08-19 20:27:05 -04:00
|
|
|
if (target) {
|
|
|
|
const char *to = NULL;
|
2004-09-15 08:11:43 -04:00
|
|
|
char *translit_to = NULL;
|
2004-12-20 08:59:15 -05:00
|
|
|
char *tagtarget = NULL;
|
|
|
|
|
|
|
|
if (server != NULL)
|
|
|
|
tagtarget = server->tag == NULL ? NULL :
|
|
|
|
g_strdup_printf("%s/%s", server->tag, target);
|
|
|
|
if (tagtarget != NULL)
|
|
|
|
to = iconfig_get_str("conversions", tagtarget, NULL);
|
|
|
|
g_free(tagtarget);
|
2004-12-20 10:06:29 -05:00
|
|
|
if (to == NULL || *to == '\0')
|
|
|
|
to = iconfig_get_str("conversions", target, NULL);
|
2004-09-15 08:11:43 -04:00
|
|
|
if (to == NULL || *to == '\0')
|
2004-09-20 08:45:49 -04:00
|
|
|
/* default outgoing charset if set */
|
|
|
|
to = settings_get_str("recode_out_default_charset");
|
|
|
|
|
2004-09-15 08:11:43 -04:00
|
|
|
if (to && *to != '\0') {
|
|
|
|
if (translit && !is_translit(to))
|
|
|
|
to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);
|
2004-08-19 20:27:05 -04:00
|
|
|
|
|
|
|
term_is_utf8 = recode_get_charset(&from);
|
|
|
|
recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
|
|
|
|
}
|
2004-09-15 08:11:43 -04:00
|
|
|
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;
|
|
|
|
#else
|
|
|
|
return g_strdup(str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void recode_init(void)
|
|
|
|
{
|
|
|
|
settings_add_str("misc", "recode_fallback", "ISO8859-1");
|
|
|
|
settings_add_str("misc", "recode_out_default_charset", "");
|
2004-09-15 08:11:43 -04:00
|
|
|
settings_add_bool("misc", "recode_transliterate", FALSE);
|
2004-08-19 20:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void recode_deinit(void)
|
|
|
|
{
|
|
|
|
settings_remove("recode_fallback");
|
|
|
|
settings_remove("recode_out_default_charset");
|
|
|
|
settings_remove("recode_transliterate");
|
|
|
|
}
|