~ubuntu-branches/ubuntu/feisty/irssi/feisty-backports

« back to all changes in this revision

Viewing changes to src/core/recode.c

  • Committer: Bazaar Package Importer
  • Author(s): David Pashley
  • Date: 2005-12-10 21:25:51 UTC
  • Revision ID: james.westby@ubuntu.com-20051210212551-5qwm108g7inyu2f2
Tags: upstream-0.8.10
ImportĀ upstreamĀ versionĀ 0.8.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 recode.c : irssi
 
3
 
 
4
    Copyright (C) 1999-2000 Timo Sirainen
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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
 
19
*/
 
20
 
 
21
#include "module.h"
 
22
#include "settings.h"
 
23
#include "servers.h"
 
24
#include "signals.h"
 
25
#include "lib-config/iconfig.h"
 
26
#include "misc.h"
 
27
 
 
28
#ifdef HAVE_GLIB2
 
29
static gboolean recode_get_charset(const char **charset)
 
30
{
 
31
        *charset = settings_get_str("term_charset");
 
32
        if (**charset)
 
33
                /* we use the same test as in src/fe-text/term.c:123 */
 
34
                        return !g_strcasecmp(*charset, "utf-8");
 
35
 
 
36
        return g_get_charset(charset);
 
37
}
 
38
#endif
 
39
 
 
40
static gboolean is_translit(const char *charset)
 
41
{
 
42
        char *pos;
 
43
        pos = stristr(charset, "//translit");
 
44
        return (pos != NULL);
 
45
}
 
46
 
 
47
gboolean is_valid_charset(const char *charset)
 
48
{
 
49
#ifdef HAVE_GLIB2
 
50
        const char *from="UTF-8";
 
51
        const char *str="irssi";
 
52
        char *recoded, *to = NULL;
 
53
        gboolean valid;
 
54
 
 
55
        if (!charset || *charset == '\0')
 
56
                return FALSE;
 
57
 
 
58
        if (settings_get_bool("recode_transliterate") && !is_translit(charset))
 
59
                charset = to = g_strconcat(charset, "//TRANSLIT", NULL);
 
60
 
 
61
        recoded = g_convert(str, strlen(str), charset, from, NULL, NULL, NULL);
 
62
        valid = (recoded != NULL);
 
63
        g_free(recoded);
 
64
        g_free(to);
 
65
        return valid;
 
66
#else
 
67
        if (!charset || *charset =='\0')
 
68
                return FALSE;
 
69
        return TRUE;
 
70
#endif
 
71
}
 
72
 
 
73
char *recode_in(const SERVER_REC *server, const char *str, const char *target)
 
74
{
 
75
#ifdef HAVE_GLIB2
 
76
        const char *from = NULL;
 
77
        const char *to = NULL;
 
78
        char *translit_to = NULL;
 
79
        char *recoded = NULL;
 
80
        char *tagtarget = NULL;
 
81
        gboolean term_is_utf8, str_is_utf8, translit, recode, autodetect;
 
82
        int len;
 
83
 
 
84
        if (!str)
 
85
                return NULL;
 
86
 
 
87
        recode = settings_get_bool("recode");
 
88
        if (!recode)
 
89
                return g_strdup(str);
 
90
 
 
91
        len = strlen(str);
 
92
 
 
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);
 
97
 
 
98
        if (autodetect && str_is_utf8)
 
99
                if (term_is_utf8)
 
100
                        return g_strdup(str);
 
101
                else
 
102
                        from = "UTF-8";
 
103
                        
 
104
        else {
 
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);
 
110
                g_free(tagtarget);
 
111
 
 
112
                if (target != NULL && from == NULL)
 
113
                        from = iconfig_get_str("conversions", target, NULL);
 
114
 
 
115
                if (from == NULL && server != NULL)
 
116
                        from = iconfig_get_str("conversions", server->tag, NULL);
 
117
 
 
118
        }
 
119
 
 
120
        if (translit && !is_translit(to))
 
121
                to = translit_to = g_strconcat(to, "//TRANSLIT", NULL);
 
122
                
 
123
        if (from)
 
124
                recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
 
125
 
 
126
        if (!recoded) {
 
127
                if (term_is_utf8) {
 
128
                        if (!str_is_utf8)
 
129
                                from = settings_get_str("recode_fallback");
 
130
 
 
131
                } else if (str_is_utf8)
 
132
                        from = "UTF-8";
 
133
 
 
134
                if (from)
 
135
                        recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);
 
136
 
 
137
                if (!recoded)
 
138
                        recoded = g_strdup(str);
 
139
        }
 
140
        g_free(translit_to);
 
141
        return recoded;
 
142
#else
 
143
        return g_strdup(str);
 
144
#endif
 
145
}
 
146
 
 
147
char *recode_out(const SERVER_REC *server, const char *str, const char *target)
 
148
{
 
149
#ifdef HAVE_GLIB2
 
150
        char *recoded = NULL;
 
151
        const char *from = NULL;
 
152
        gboolean translit, term_is_utf8, recode;
 
153
        int len;
 
154
 
 
155
        if (!str)
 
156
                return NULL;
 
157
 
 
158
        recode = settings_get_bool("recode");
 
159
        if (!recode)
 
160
                return g_strdup(str);
 
161
 
 
162
        len = strlen(str);
 
163
 
 
164
        translit = settings_get_bool("recode_transliterate");
 
165
 
 
166
        if (target) {
 
167
                const char *to = NULL;
 
168
                char *translit_to = NULL;
 
169
                char *tagtarget = NULL;
 
170
 
 
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);
 
176
                g_free(tagtarget);
 
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");
 
184
 
 
185
                if (to && *to != '\0') {
 
186
                        if (translit && !is_translit(to))
 
187
                                to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);
 
188
 
 
189
                        term_is_utf8 = recode_get_charset(&from);
 
190
                        recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
 
191
                }
 
192
                g_free(translit_to);
 
193
        }
 
194
        if (!recoded)
 
195
                recoded = g_strdup(str);
 
196
 
 
197
        return recoded;
 
198
#else
 
199
        return g_strdup(str);
 
200
#endif
 
201
}
 
202
 
 
203
void recode_init(void)
 
204
{
 
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);
 
210
}
 
211
 
 
212
void recode_deinit(void)
 
213
{       
 
214
 
 
215
}