Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / fe-common / core / fe-recode.c
1 /*
2  fe-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 "modules.h"
23 #include "module-formats.h"
24 #include "commands.h"
25 #include "levels.h"
26 #include "lib-config/iconfig.h"
27 #include "settings.h"
28 #include "printtext.h"
29 #include "formats.h"
30 #include "recode.h"
31
32 #ifdef HAVE_NL_LANGINFO
33 #  include <langinfo.h>
34 #endif
35
36 #define SLIST_FOREACH(var, head)                \
37 for ((var) = (head);                            \
38                  (var);                                 \
39                  (var) = g_slist_next((var)))
40
41 #ifdef HAVE_GLIB2
42 char *recode_fallback = NULL;
43 char *recode_out_default = NULL;
44 char *term_charset = NULL;
45
46 static const char *fe_recode_get_target (WI_ITEM_REC *witem)
47 {
48         if (witem && (witem->type == module_get_uniq_id_str("WINDOW ITEM TYPE", "QUERY")
49             || witem->type == module_get_uniq_id_str("WINDOW ITEM TYPE", "CHANNEL")))
50                 return window_item_get_target(witem);
51
52         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_CHANNEL_OR_QUERY);
53         return NULL;
54 }
55
56 static int fe_recode_compare_func (CONFIG_NODE *node1, CONFIG_NODE *node2)
57 {
58         return strcmp(node1->key, node2->key);
59 }
60
61 /* SYNTAX: RECODE */
62 static void fe_recode_cmd (const char *data, SERVER_REC *server, WI_ITEM_REC *witem)
63 {
64         if (*data)
65                 command_runsub("recode", data, server, witem);
66         else {
67                 CONFIG_NODE *conversions;
68                 GSList *tmp;
69                 GSList *sorted = NULL;
70
71                 conversions = iconfig_node_traverse("conversions", FALSE);
72
73                 for (tmp = conversions ? config_node_first(conversions->value) : NULL;
74                      tmp != NULL;
75                      tmp = config_node_next(tmp)) {
76                         CONFIG_NODE *node = tmp->data;
77
78                         if (node->type == NODE_TYPE_KEY)
79                                 sorted = g_slist_insert_sorted(sorted, node, (GCompareFunc) fe_recode_compare_func);
80                 }
81
82                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_RECODE_HEADER);
83                 SLIST_FOREACH(tmp, sorted)
84                 {
85                         CONFIG_NODE *node = tmp->data;
86                         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_RECODE_LINE, node->key, node->value);
87                 }
88
89                 g_slist_free(sorted);
90         }
91 }
92
93 /* SYNTAX: RECODE ADD [[<tag>/]<target>] <charset> */
94 static void fe_recode_add_cmd (const char *data, SERVER_REC *server, WI_ITEM_REC *witem)
95 {
96         const char *first;
97         const char *second;
98         const char *target;
99         const char *charset;
100         void *free_arg;
101
102         if (! cmd_get_params(data, &free_arg, 2, &first, &second))
103                 return;
104
105         if (! *first)
106                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
107
108         if (*second) {
109                 target = first;
110                 charset = second;
111         } else {
112                 target = fe_recode_get_target(witem);
113                 charset = first;
114                 if (! target)
115                         goto end;
116         }
117         if (is_valid_charset(charset)) {
118                 iconfig_set_str("conversions", target, charset);
119                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONVERSION_ADDED, target, charset);
120         } else
121                 signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_INVALID_CHARSET), charset);
122  end:
123         cmd_params_free(free_arg);
124 }
125
126 /* SYNTAX: RECODE REMOVE [<target>] */
127 static void fe_recode_remove_cmd (const char *data, SERVER_REC *server, WI_ITEM_REC *witem)
128 {
129         const char *target;
130         void *free_arg;
131
132         if (! cmd_get_params(data, &free_arg, 1, &target))
133                 return;
134
135         if (! *target) {
136                 target = fe_recode_get_target(witem);
137                 if (! target)
138                         goto end;
139         }
140
141         if (iconfig_get_str("conversions", target, NULL) == NULL)
142                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONVERSION_NOT_FOUND, target);
143         else    {
144                 iconfig_set_str("conversions", target, NULL);
145                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONVERSION_REMOVED, target);
146         }
147
148  end:
149         cmd_params_free(free_arg);
150 }
151
152 static void read_settings(void)
153 {
154         /* preserve the valid values */
155         char *old_term_charset = g_strdup(term_charset);
156         char *old_recode_fallback = g_strdup(recode_fallback);
157         char *old_recode_out_default = g_strdup(recode_out_default);
158
159         if (settings_get_bool("recode_transliterate")) {
160                 /* check if transliterations are supported in this system */
161                 if (!is_valid_charset("ASCII")) {
162                         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
163                                     TXT_CONVERSION_NO_TRANSLITS);
164                         settings_set_bool("recode_transliterate", FALSE);
165                 }
166         }
167
168         if (recode_fallback)
169                 g_free(recode_fallback);
170         recode_fallback = g_strdup(settings_get_str("recode_fallback"));
171         if (!is_valid_charset(recode_fallback)) {
172                 signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_INVALID_CHARSET), recode_fallback);
173                 g_free(recode_fallback);
174                 recode_fallback = is_valid_charset(old_recode_fallback) ? g_strdup(old_recode_fallback) : "ISO8859-1";
175                 settings_set_str("recode_fallback", recode_fallback);
176         }
177
178         if (term_charset)
179                 g_free(term_charset);
180         term_charset = g_strdup(settings_get_str("term_charset"));
181         if (!is_valid_charset(term_charset)) {
182                 g_free(term_charset);
183 #if defined (HAVE_NL_LANGINFO) && defined(CODESET)
184                 term_charset = is_valid_charset(old_term_charset) ? g_strdup(old_term_charset) : 
185                                *nl_langinfo(CODESET) != '\0' ? g_strdup(nl_langinfo(CODESET)) : 
186                                "ISO8859-1";
187 #else
188                 term_charset = is_valid_charset(old_term_charset) ? g_strdup(old_term_charset) : "ISO8859-1";
189 #endif          
190                 settings_set_str("term_charset", term_charset);
191                 /* FIXME: move the check of term_charset into fe-text/term.c 
192                           it breaks the proper term_input_type 
193                           setup and reemitting of the signal is kludgy */
194                 if (g_strcasecmp(term_charset, old_term_charset) != 0)
195                         signal_emit("setup changed", 0);
196         }
197         
198         if (recode_out_default)
199                 g_free(recode_out_default);
200         recode_out_default = g_strdup(settings_get_str("recode_out_default_charset"));
201         if (recode_out_default != NULL && *recode_out_default != '\0' && 
202             !is_valid_charset(recode_out_default)) {
203                 signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_INVALID_CHARSET), recode_out_default);
204                 g_free(recode_out_default);
205                 recode_out_default = is_valid_charset(old_recode_out_default) ? g_strdup(old_recode_out_default) : NULL;
206                 settings_set_str("recode_out_default_charset", recode_out_default);
207         }
208
209         g_free(old_term_charset);
210         g_free(old_recode_fallback);
211         g_free(old_recode_out_default);
212 }
213 #endif
214
215 void fe_recode_init (void)
216 {
217 /* FIXME: print this is not supported instead */
218 #ifdef HAVE_GLIB2
219         command_bind("recode", NULL, (SIGNAL_FUNC) fe_recode_cmd);
220         command_bind("recode add", NULL, (SIGNAL_FUNC) fe_recode_add_cmd);
221         command_bind("recode remove", NULL, (SIGNAL_FUNC) fe_recode_remove_cmd);
222         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
223         read_settings();
224 #endif
225 }
226
227 void fe_recode_deinit (void)
228 {
229 /* FIXME: print this is not supported instead */
230 #ifdef HAVE_GLIB2
231         command_unbind("recode", (SIGNAL_FUNC) fe_recode_cmd);
232         command_unbind("recode add", (SIGNAL_FUNC) fe_recode_add_cmd);
233         command_unbind("recode remove", (SIGNAL_FUNC) fe_recode_remove_cmd);
234         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
235 #endif
236 }