summaryrefslogtreecommitdiff
path: root/src/plugins/charset/charset.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/charset/charset.c')
-rw-r--r--src/plugins/charset/charset.c900
1 files changed, 461 insertions, 439 deletions
diff --git a/src/plugins/charset/charset.c b/src/plugins/charset/charset.c
index 45125307d..10a71af87 100644
--- a/src/plugins/charset/charset.c
+++ b/src/plugins/charset/charset.c
@@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-/* weechat-charset.c: Charset plugin for WeeChat */
+/* charset.c: Charset plugin for WeeChat */
#include <stdio.h>
@@ -26,6 +26,7 @@
#include <iconv.h>
#include "../weechat-plugin.h"
+#include "charset.h"
WEECHAT_PLUGIN_NAME("charset");
@@ -37,176 +38,292 @@ WEECHAT_PLUGIN_LICENSE("GPL");
struct t_weechat_plugin *weechat_charset_plugin = NULL;
#define weechat_plugin weechat_charset_plugin
-char *weechat_charset_terminal = NULL;
-char *weechat_charset_internal = NULL;
+struct t_config_file *charset_config_file = NULL;
+struct t_charset *charset_list = NULL;
+struct t_charset *last_charset = NULL;
-/* set to 1 by /charset debug (hidden option) */
-int weechat_charset_debug = 0;
+char *charset_terminal = NULL;
+char *charset_internal = NULL;
+
+int charset_debug = 0;
/*
- * weechat_charset_strndup: define strndup function if not existing
- * (FreeBSD and maybe other)
+ * charset_debug_cb: callback for "debug" signal
*/
-char *
-weechat_charset_strndup (char *string, int length)
+int
+charset_debug_cb (void *data, char *signal, char *type_data, void *signal_data)
+{
+ /* make C compiler happy */
+ (void) data;
+ (void) signal;
+
+ if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0)
+ {
+ if (weechat_strcasecmp ((char *)signal_data, "charset") == 0)
+ charset_debug ^= 1;
+ }
+
+ return WEECHAT_RC_OK;
+}
+
+/*
+ * charset_search: search a charset
+ */
+
+struct t_charset *
+charset_search (char *name)
{
- char *result;
+ struct t_charset *ptr_charset;
- if ((int)strlen (string) < length)
- return strdup (string);
+ for (ptr_charset = charset_list; ptr_charset;
+ ptr_charset = ptr_charset->next_charset)
+ {
+ if (strcmp (name, ptr_charset->name) == 0)
+ return ptr_charset;
+ }
+ return NULL;
+}
+
+/*
+ * charset_new: create new charset and add it to charset list
+ */
+
+struct t_charset *
+charset_new (char *name, char *charset)
+{
+ struct t_charset *new_charset, *ptr_charset;
- result = (char *)malloc (length + 1);
- if (!result)
+ if (!name || !name[0] || !charset || !charset[0])
return NULL;
- memcpy (result, string, length);
- result[length] = '\0';
+ ptr_charset = charset_search (name);
+ if (ptr_charset)
+ {
+ if (ptr_charset->charset)
+ free (ptr_charset->charset);
+ ptr_charset->charset = strdup (charset);
+ return ptr_charset;
+ }
+
+ new_charset = (struct t_charset *)malloc (sizeof (struct t_charset));
+ {
+ new_charset->name = strdup (name);
+ new_charset->charset = strdup (charset);
+
+ new_charset->prev_charset = last_charset;
+ new_charset->next_charset = NULL;
+ if (charset_list)
+ last_charset->next_charset = new_charset;
+ else
+ charset_list = new_charset;
+ last_charset = new_charset;
+ }
- return result;
+ return new_charset;
}
/*
- * weechat_charset_default_decode: set "global.decode" option if needed
+ * charset_free: free a charset and remove it from list
*/
void
-weechat_charset_default_decode (t_weechat_plugin *plugin)
+charset_free (struct t_charset *charset)
{
- char *global_decode;
- int rc;
-
- global_decode = plugin->get_plugin_config (plugin, "global.decode");
-
- /* if global decode is not set, we may set it, depending on terminal
- charset */
- if (!global_decode || !global_decode[0])
+ struct t_charset *new_charset_list;
+
+ /* remove charset from list */
+ if (last_charset == charset)
+ last_charset = charset->prev_charset;
+ if (charset->prev_charset)
{
- /* set global decode charset to terminal if different from internal */
- /* otherwise use ISO-8859-1 */
- if (weechat_charset_terminal && weechat_charset_internal
- && (strcasecmp (weechat_charset_terminal,
- weechat_charset_internal) != 0))
- rc = plugin->set_plugin_config (plugin,
- "global.decode",
- weechat_charset_terminal);
- else
- rc = plugin->set_plugin_config (plugin,
- "global.decode",
- "ISO-8859-1");
- if (rc)
- plugin->print_server (plugin,
- "Charset: setting \"charset.global.decode\" "
- "to %s",
- weechat_charset_terminal);
- else
- plugin->print_server (plugin,
- "Charset: failed to set "
- "\"charset.global.decode\" option.");
+ (charset->prev_charset)->next_charset = charset->next_charset;
+ new_charset_list = charset_list;
}
- if (global_decode)
- free (global_decode);
+ else
+ new_charset_list = charset->next_charset;
+ if (charset->next_charset)
+ (charset->next_charset)->prev_charset = charset->prev_charset;
+
+ /* free data */
+ if (charset->name)
+ free (charset->name);
+ if (charset->charset)
+ free (charset->charset);
+ free (charset);
+
+ charset_list = new_charset_list;
}
/*
- * weechat_charset_check: check if a charset is valid
- * if a charset is NULL, internal charset is used
+ * charset_free_all: free all charsets
*/
-int
-weechat_charset_check (char *charset)
+void
+charset_free_all ()
{
- iconv_t cd;
+ while (charset_list)
+ charset_free (charset_list);
+}
- if (!charset || !charset[0])
- return 0;
-
- cd = iconv_open (charset, weechat_charset_internal);
- if (cd == (iconv_t)(-1))
- return 0;
+/*
+ * charset_config_reaload: reload charset configuration file
+ */
+
+int
+charset_config_reload (struct t_config_file *config_file)
+{
+ /* make C compiler happy */
+ (void) config_file;
- iconv_close (cd);
- return 1;
+ charset_free_all ();
+ return weechat_config_reload (charset_config_file);
}
/*
- * weechat_charset_get_config: read a charset in config file
- * we first in this order: channel, server, global
+ * charset_config_read_line: read a charset in configuration file
*/
-char *
-weechat_charset_get_config (t_weechat_plugin *plugin,
- char *type, char *server, char *channel)
+void
+charset_config_read_line (struct t_config_file *config_file, char *option_name,
+ char *value)
{
- static char option[1024];
- char *value;
-
- /* we try channel first */
- if (server && channel)
- {
- snprintf (option, sizeof (option) - 1, "%s.%s.%s", type,
- server, channel);
- value = plugin->get_plugin_config (plugin, option);
- if (value && value[0])
- return value;
- if (value)
- free (value);
- }
+ /* make C compiler happy */
+ (void) config_file;
- /* channel not found, we try server only */
- if (server)
+ if (option_name && value)
{
- snprintf (option, sizeof (option) - 1, "%s.%s", type, server);
- value = plugin->get_plugin_config (plugin, option);
- if (value && value[0])
- return value;
- if (value)
- free (value);
+ /* create new charset */
+ if (!charset_new (option_name, value))
+ {
+ weechat_printf (NULL,
+ _("%s%s: error creating charset \"%s\" => \"%s\""),
+ weechat_prefix ("error"), "charset",
+ option_name, value);
+ }
}
+}
+
+/*
+ * charseet_config_write_section: write charset section in configuration file
+ * Return: 0 = successful
+ * -1 = write error
+ */
+
+void
+charset_config_write_section (struct t_config_file *config_file,
+ char *section_name)
+{
+ struct t_charset *ptr_charset;
- /* nothing found, we try global charset */
- snprintf (option, sizeof (option) - 1, "global.%s", type);
- value = plugin->get_plugin_config (plugin, option);
- if (value && value[0])
- return value;
- if (value)
- free (value);
+ weechat_config_write_line (config_file, section_name, NULL);
- /* nothing found => no decode/encode for this message! */
- return NULL;
+ for (ptr_charset = charset_list; ptr_charset;
+ ptr_charset = ptr_charset->next_charset)
+ {
+ weechat_config_write_line (config_file,
+ ptr_charset->name,
+ "\"%s\"",
+ ptr_charset->charset);
+ }
}
/*
- * weechat_charset_set_config: set a charset in config file
+ * charset_config_write_default_aliases: write default charsets in configuration file
*/
void
-weechat_charset_set_config (t_weechat_plugin *plugin,
- char *type, char *server, char *channel,
- char *value)
+charset_config_write_default_charsets (struct t_config_file *config_file,
+ char *section_name)
{
- static char option[1024];
-
- if (server && channel)
- snprintf (option, sizeof (option) - 1, "%s.%s.%s", type,
- server, channel);
- else if (server)
- snprintf (option, sizeof (option) - 1, "%s.%s", type, server);
+ weechat_config_write_line (config_file, section_name, NULL);
+
+ if (charset_terminal && charset_internal
+ && (strcasecmp (charset_terminal,
+ charset_internal) != 0))
+ weechat_config_write_line (config_file, "decode", "%s", charset_terminal);
else
- return;
+ weechat_config_write_line (config_file, "decode", "%s", "iso-8859-1");
+}
+
+/*
+ * charset_config_init: init charset configuration file
+ * return: 1 if ok, 0 if error
+ */
+
+int
+charset_config_init ()
+{
+ struct t_config_section *ptr_section;
+
+ charset_config_file = weechat_config_new (CHARSET_CONFIG_FILENAME,
+ &charset_config_reload);
+ if (!charset_config_file)
+ return 0;
+
+ ptr_section = weechat_config_new_section (charset_config_file, "charset",
+ charset_config_read_line,
+ charset_config_write_section,
+ charset_config_write_default_charsets);
+ if (!ptr_section)
+ {
+ weechat_config_free (charset_config_file);
+ return 0;
+ }
+
+ return 1;
+}
+
+/*
+ * charset_config_read: read charset configuration file
+ */
+
+int
+charset_config_read ()
+{
+ return weechat_config_read (charset_config_file);
+}
+
+/*
+ * charset_config_write: write charset configuration file
+ */
- plugin->set_plugin_config (plugin, option, value);
+int
+charset_config_write ()
+{
+ return weechat_config_write (charset_config_file);
}
/*
- * weechat_charset_parse_irc_msg: return nick, command, channel and position
- * of arguments in IRC message
+ * charset_check: check if a charset is valid
+ * return 1 if charset is valid, 0 if not valid
+ */
+
+int
+charset_check (char *charset)
+{
+ iconv_t cd;
+
+ if (!charset || !charset[0])
+ return 0;
+
+ cd = iconv_open (charset, charset_internal);
+ if (cd == (iconv_t)(-1))
+ return 0;
+
+ iconv_close (cd);
+ return 1;
+}
+
+/*
+ * charset_parse_irc_msg: return nick, command, channel and position
+ * of arguments in IRC message
*/
void
-weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
- char **channel, char **pos_args)
+charset_parse_irc_msg (char *message, char **nick, char **command,
+ char **channel, char **pos_args)
{
char *pos, *pos2, *pos3, *pos4, *pos_tmp;
@@ -223,12 +340,12 @@ weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
pos_tmp[0] = '\0';
pos2 = strchr (pos, '!');
if (pos2)
- *nick = weechat_charset_strndup (pos, pos2 - pos);
+ *nick = weechat_strndup (pos, pos2 - pos);
else
{
pos2 = strchr (pos, ' ');
if (pos2)
- *nick = weechat_charset_strndup (pos, pos2 - pos);
+ *nick = weechat_strndup (pos, pos2 - pos);
}
if (pos_tmp)
pos_tmp[0] = ' ';
@@ -246,7 +363,7 @@ weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
pos2 = strchr (pos, ' ');
if (pos2)
{
- *command = weechat_charset_strndup (pos, pos2 - pos);
+ *command = weechat_strndup (pos, pos2 - pos);
pos2++;
while (pos2[0] == ' ')
pos2++;
@@ -258,7 +375,7 @@ weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
{
pos3 = strchr (pos2, ' ');
if (pos3)
- *channel = weechat_charset_strndup (pos2, pos3 - pos2);
+ *channel = weechat_strndup (pos2, pos3 - pos2);
else
*channel = strdup (pos2);
}
@@ -268,8 +385,7 @@ weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
if (!*nick)
{
if (pos3)
- *nick = weechat_charset_strndup (pos2,
- pos3 - pos2);
+ *nick = weechat_strndup (pos2, pos3 - pos2);
else
*nick = strdup (pos2);
}
@@ -283,8 +399,7 @@ weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
{
pos4 = strchr (pos3, ' ');
if (pos4)
- *channel = weechat_charset_strndup (pos3,
- pos4 - pos3);
+ *channel = weechat_strndup (pos3, pos4 - pos3);
else
*channel = strdup (pos3);
}
@@ -296,390 +411,297 @@ weechat_charset_parse_irc_msg (char *message, char **nick, char **command,
}
/*
- * weechat_charset_irc_in: transform charset for incoming messages
- * convert from any charset to WeeChat internal
+ * charset_set: set a charset
+ * return 1 if ok, 0 if error
*/
-char *
-weechat_charset_irc_in (t_weechat_plugin *plugin, int argc, char **argv,
- char *handler_args, void *handler_pointer)
+int
+charset_set (char *name, char *charset)
{
- char *nick, *command, *channel, *charset, *ptr_args;
- char *output;
-
- /* make C compiler happy */
- (void) argc;
- (void) handler_args;
- (void) handler_pointer;
-
- output = NULL;
-
- weechat_charset_parse_irc_msg (argv[1], &nick, &command, &channel,
- &ptr_args);
-
- charset = weechat_charset_get_config (plugin,
- "decode", argv[0],
- (channel) ? channel : nick);
-
- if (weechat_charset_debug)
- plugin->print(plugin, NULL, NULL,
- "Charset IN: srv='%s', nick='%s', chan='%s', "
- "msg='%s', ptr_args='%s' => charset: %s",
- argv[0], nick, channel, argv[1], ptr_args, charset);
+ struct t_charset *ptr_charset;
- if (charset)
+ if (charset && charset[0])
{
- output = plugin->iconv_to_internal (plugin, charset, argv[1]);
- if (charset)
- free (charset);
+ if (charset_new (name, charset))
+ {
+ weechat_printf (NULL,
+ _("%sCharset \"%s\" => \"%s\""),
+ weechat_prefix ("info"), name, charset);
+ }
+ else
+ {
+ weechat_printf (NULL,
+ _("%s%s: error creating charset \"%s\" "
+ "=> \"%s\""),
+ weechat_prefix ("error"), "charset", name, charset);
+ return 0;
+ }
+ }
+ else
+ {
+ ptr_charset = charset_search (name);
+ if (!ptr_charset)
+ {
+ weechat_printf (NULL,
+ _("%s%s: charset \"%s\" not found"),
+ weechat_prefix ("error"), "charset", name);
+ return 0;
+ }
+ charset_free (ptr_charset);
+ weechat_printf (NULL,
+ _("%sCharset \"%s\" removed"),
+ weechat_prefix ("info"), name);
}
- if (nick)
- free (nick);
- if (command)
- free (command);
- if (channel)
- free (channel);
-
- return output;
+ return 1;
}
/*
- * weechat_charset_irc_out: transform charset for outgoing messages
- * convert from WeeChat internal charset to other
+ * charset_get: read a charset in config file
+ * type is "decode" or "encode"
+ * we first try with all arguments, then remove one by one
+ * to find charset (from specific to general charset)
*/
char *
-weechat_charset_irc_out (t_weechat_plugin *plugin, int argc, char **argv,
- char *handler_args, void *handler_pointer)
+charset_get (char *type, char *name)
{
- char *nick, *command, *channel, *charset, *ptr_args;
- char *output;
-
- /* make C compiler happy */
- (void) argc;
- (void) handler_args;
- (void) handler_pointer;
-
- output = NULL;
-
- weechat_charset_parse_irc_msg (argv[1], &nick, &command, &channel,
- &ptr_args);
-
- charset = weechat_charset_get_config (plugin,
- "encode", argv[0],
- (channel) ? channel : nick);
-
- if (weechat_charset_debug)
- plugin->print(plugin, NULL, NULL,
- "Charset OUT: srv='%s', nick='%s', chan='%s', "
- "msg='%s', ptr_args='%s' => charset: %s",
- argv[0], nick, channel, argv[1], ptr_args, charset);
+ char *option_name, *ptr_end;
+ struct t_charset *ptr_charset;
+ int length;
- if (charset)
+ length = strlen (type) + 1 + strlen (name) + 1;
+ option_name = (char *)malloc (length);
+ if (option_name)
{
- output = plugin->iconv_from_internal (plugin, charset, argv[1]);
- if (charset)
- free (charset);
+ snprintf (option_name, length, "%s.%s",
+ type, name);
+ ptr_end = option_name + strlen (option_name);
+ while (ptr_end >= option_name)
+ {
+ ptr_charset = charset_search (option_name);
+ if (ptr_charset)
+ {
+ free (option_name);
+ return ptr_charset->charset;
+ }
+ ptr_end--;
+ while ((ptr_end >= option_name) && (ptr_end[0] != '.'))
+ {
+ ptr_end--;
+ }
+ if ((ptr_end >= option_name) && (ptr_end[0] == '.'))
+ ptr_end[0] = '\0';
+ }
+ ptr_charset = charset_search (option_name);
+
+ free (option_name);
+
+ if (ptr_charset)
+ return ptr_charset->charset;
}
- if (nick)
- free (nick);
- if (command)
- free (command);
- if (channel)
- free (channel);
-
- return output;
+ /* nothing found => no decode/encode for this message! */
+ return NULL;
}
/*
- * weechat_charset_display: display charsets (global/server/channel)
+ * charset_decode: decode a string with a charset to internal charset
*/
-void
-weechat_charset_display (t_weechat_plugin *plugin,
- int display_on_server, char *server, char *channel)
+char *
+charset_decode (void *data, char *modifier, char *modifier_data,
+ char *string)
{
- char *decode, *encode;
- static char option[1024];
-
- decode = NULL;
- encode = NULL;
+ char *charset;
- /* display global settings */
- if (!server && !channel)
- {
- decode = plugin->get_plugin_config (plugin, "global.decode");
- encode = plugin->get_plugin_config (plugin, "global.encode");
-
- if (display_on_server)
- plugin->print_server (plugin,
- "Charset: global charsets: decode = %s, "
- "encode = %s",
- (decode) ? decode : "(none)",
- (encode) ? encode : "(none)");
- else
- plugin->print (plugin, NULL, NULL,
- "Charset: global charsets: decode = %s, "
- "encode = %s",
- (decode) ? decode : "(none)",
- (encode) ? encode : "(none)");
- }
-
- /* display server settings */
- if (server && !channel)
- {
- snprintf (option, sizeof (option) - 1, "decode.%s", server);
- decode = plugin->get_plugin_config (plugin, option);
- snprintf (option, sizeof (option) - 1, "encode.%s", server);
- encode = plugin->get_plugin_config (plugin, option);
-
- if (display_on_server)
- plugin->print_server (plugin,
- "Charset: decode / encode charset for "
- "server %s: %s / %s",
- server,
- (decode) ? decode : "(none)",
- (encode) ? encode : "(none)");
- else
- plugin->print (plugin, NULL, NULL,
- "Charset: decode / encode charset for server %s: "
- "%s / %s",
- server,
- (decode) ? decode : "(none)",
- (encode) ? encode : "(none)");
- }
+ /* make C compiler happy */
+ (void) data;
+ (void) modifier;
- /* display chan/nick settings */
- if (server && channel)
- {
- snprintf (option, sizeof (option) - 1,
- "decode.%s.%s", server, channel);
- decode = plugin->get_plugin_config (plugin, option);
- snprintf (option, sizeof (option) - 1,
- "encode.%s.%s", server, channel);
- encode = plugin->get_plugin_config (plugin, option);
-
- if (display_on_server)
- plugin->print_server (plugin,
- "Charset: decode / encode charset for "
- "%s/%s: %s / %s",
- server, channel,
- (decode) ? decode : "(none)",
- (encode) ? encode : "(none)");
- else
- plugin->print (plugin, NULL, NULL,
- "Charset: decode / encode charset for %s/%s: "
- "%s / %s",
- server, channel,
- (decode) ? decode : "(none)",
- (encode) ? encode : "(none)");
- }
+ charset = charset_get ("decode", modifier_data);
+ if (charset && charset[0])
+ return weechat_iconv_to_internal (charset, string);
- if (decode)
- free (decode);
- if (encode)
- free (encode);
+ return NULL;
}
/*
- * weechat_charset_cmd: /charset command
+ * charset_encode: encode a string from internal charset to another one
*/
-int
-weechat_charset_cmd (t_weechat_plugin *plugin,
- int cmd_argc, char **cmd_argv,
- char *handler_args, void *handler_pointer)
+char *
+charset_encode (void *data, char *modifier, char *modifier_data,
+ char *string)
{
- int argc;
- char **argv, *server, *channel;
-
- if (cmd_argc < 3)
- return PLUGIN_RC_KO;
+ char *charset;
/* make C compiler happy */
- (void) handler_args;
- (void) handler_pointer;
+ (void) data;
+ (void) modifier;
- if (cmd_argv[2])
- argv = plugin->explode_string (plugin, cmd_argv[2], " ", 0, &argc);
- else
- {
- argv = NULL;
- argc = 0;
- }
+ charset = charset_get ("encode", modifier_data);
+ if (charset && charset[0])
+ return weechat_iconv_from_internal (charset, string);
- /* get command context */
- server = plugin->get_info (plugin, "server", NULL);
- channel = plugin->get_info (plugin, "channel", NULL);
+ return NULL;
+}
+
+/*
+ * charset_command_cb: callback for /charset command
+ */
+
+int
+charset_command_cb (void *data, struct t_gui_buffer *buffer, int argc,
+ char **argv, char **argv_eol)
+{
+ int charset_found, length, rc;
+ struct t_charset *ptr_charset;
+ char *option_name, *ptr_value;
- switch (argc)
+ /* make C compiler happy */
+ (void) data;
+ (void) buffer;
+
+ if ((argc > 2) && (strcmp (argv[2], "=") == 0))
{
- case 0:
- plugin->print_server (plugin, "");
- weechat_charset_display (plugin, 1, NULL, NULL);
- weechat_charset_display (plugin, 1, server, NULL);
- if (channel)
- weechat_charset_display (plugin, 1, server, channel);
- break;
- case 1:
- if (strcasecmp (argv[0], "decode") == 0)
- {
- weechat_charset_set_config (plugin, "decode",
- server, channel, NULL);
- weechat_charset_display (plugin, 0, server, channel);
- }
- else if (strcasecmp (argv[0], "encode") == 0)
- {
- weechat_charset_set_config (plugin, "encode",
- server, channel, NULL);
- weechat_charset_display (plugin, 0, server, channel);
- }
- else if (strcasecmp (argv[0], "debug") == 0)
- {
- weechat_charset_debug ^= 1;
- plugin->print (plugin, NULL, NULL,
- "Charset: debug [%s].",
- (weechat_charset_debug) ? "ON" : "off");
- }
- else if (strcasecmp (argv[0], "reset") == 0)
+ ptr_value = (argc > 3) ? argv_eol[3] : NULL;
+ if ((weechat_strncasecmp (argv[1], "decode.", 7) != 0)
+ && (weechat_strncasecmp (argv[1], "encode.", 7) != 0))
+ {
+ length = strlen (argv[1]) + strlen ("decode.") + 1;
+ option_name = (char *)malloc (length);
+ if (option_name)
{
- weechat_charset_set_config (plugin, "decode",
- server, channel, NULL);
- weechat_charset_set_config (plugin, "encode",
- server, channel, NULL);
- weechat_charset_display (plugin, 0, server, channel);
+ rc = 1;
+ snprintf (option_name, length, "decode.%s", argv[1]);
+ if (!charset_set (option_name, ptr_value))
+ rc = 0;
+ snprintf (option_name, length, "encode.%s", argv[1]);
+ if (!charset_set (option_name, ptr_value))
+ rc = 0;
+ if (!rc)
+ return WEECHAT_RC_ERROR;
}
+ }
+ else
+ {
+ if (!charset_set (argv[1], ptr_value))
+ return WEECHAT_RC_ERROR;
+ }
+ }
+ else
+ {
+ /* list all charsets */
+ if (charset_list)
+ {
+ weechat_printf (NULL, "");
+ if (argc == 1)
+ weechat_printf (NULL, _("List of charsets:"));
else
+ weechat_printf (NULL, _("List of charsets with \"%s\":"),
+ argv_eol[1]);
+ charset_found = 0;
+ for (ptr_charset = charset_list; ptr_charset;
+ ptr_charset = ptr_charset->next_charset)
{
- if (!weechat_charset_check (argv[0]))
- plugin->print_server (plugin,
- "Charset error: invalid charset "
- "\"%s\"",
- argv[0]);
- else
- {
- weechat_charset_set_config (plugin, "decode",
- server, channel, argv[0]);
- weechat_charset_set_config (plugin, "encode",
- server, channel, argv[0]);
- weechat_charset_display (plugin, 0, server, channel);
- }
- }
- break;
- case 2:
- if (!weechat_charset_check (argv[1]))
- plugin->print_server (plugin,
- "Charset error: invalid charset \"%s\"",
- argv[1]);
- else
- {
- if (strcasecmp (argv[0], "decode") == 0)
- {
- weechat_charset_set_config (plugin, "decode",
- server, channel, argv[1]);
- weechat_charset_display (plugin, 0, server, channel);
- }
- else if (strcasecmp (argv[0], "encode") == 0)
+ if ((argc < 2)
+ || (weechat_strcasestr (ptr_charset->name, argv_eol[1])))
{
- weechat_charset_set_config (plugin, "encode",
- server, channel, argv[1]);
- weechat_charset_display (plugin, 0, server, channel);
+ charset_found = 1;
+ weechat_printf (NULL,
+ " %s %s=>%s %s",
+ ptr_charset->name,
+ weechat_color ("color_chat_delimiters"),
+ weechat_color ("color_chat"),
+ ptr_charset->charset);
}
- else
- plugin->print_server (plugin,
- "Charset error: unknown option "
- "\"%s\"",
- argv[0]);
}
- break;
+ if (!charset_found)
+ weechat_printf (NULL, _("No charset found"));
+ }
+ else
+ weechat_printf (NULL, _("No charset defined"));
}
-
- if (argv)
- plugin->free_exploded_string (plugin, argv);
- if (server)
- free (server);
- if (channel)
- free (channel);
- return PLUGIN_RC_OK;
+ return WEECHAT_RC_OK;
}
-
+
/*
* weechat_plugin_init: init charset plugin
*/
int
-weechat_plugin_init (t_weechat_plugin *plugin)
+weechat_plugin_init (struct t_weechat_plugin *plugin)
{
- t_plugin_modifier *msg_irc_in, *msg_irc_out;
- t_plugin_handler *cmd_handler;
-
weechat_plugin = plugin;
-
+
/* get terminal & internal charsets */
- weechat_charset_terminal = plugin->get_info (plugin, "charset_terminal",
- NULL);
- weechat_charset_internal = plugin->get_info (plugin, "charset_internal",
- NULL);
-
+ charset_terminal = weechat_info_get ("charset_terminal");
+ charset_internal = weechat_info_get ("charset_internal");
+
/* display message */
- plugin->print_server (plugin,
- "Charset plugin starting, terminal charset: "
- "%s (WeeChat internal: %s)",
- weechat_charset_terminal, weechat_charset_internal);
+ weechat_printf (NULL,
+ _("%s%s: terminal: %s, internal: %s"),
+ weechat_prefix ("info"), "charset",
+ charset_terminal, charset_internal);
- /* set global default decode charset */
- weechat_charset_default_decode (plugin);
+ if (!charset_config_init ())
+ {
+ weechat_printf (NULL,
+ _("%s%s: error creating configuration file \"%s\""),
+ weechat_prefix("error"), "charset",
+ CHARSET_CONFIG_FILENAME);
+ return WEECHAT_RC_ERROR;
+ }
+ charset_config_read ();
/* add command handler */
- cmd_handler = plugin->cmd_handler_add (plugin, "charset",
- "Charset management by server or "
- "channel",
- "[[decode | encode] charset] | "
- "[reset]",
- " decode: set a decoding charset "
- "for server/channel\n"
- " encode: set an encofing charset "
- "for server/channel\n"
- "charset: the charset for decoding "
- "or encoding messages\n"
- " reset: reset charsets for "
- "server/channel\n\n"
- "To set global decode/encode "
- "charset (for all servers), use "
- "/setp charset.global.decode "
- "or /setp charset.global.encode\n"
- "To see all charsets for all "
- "servers, use /setp charset",
- "decode|encode|reset",
- &weechat_charset_cmd,
- NULL, NULL);
-
- /* add messge modifier */
- msg_irc_in = plugin->modifier_add (plugin, "irc_in", "*",
- &weechat_charset_irc_in,
- NULL, NULL);
- msg_irc_out = plugin->modifier_add (plugin, "irc_out", "*",
- &weechat_charset_irc_out,
- NULL, NULL);
-
- return PLUGIN_RC_OK;
+ weechat_hook_command ("charset",
+ _("manage charsets"),
+ _("[[type.]plugin.string [= charset]]"),
+ _(" type: \"decode\" or \"encode\" (if type is "
+ "omitted, then both \"decode\" and \"encode\" are "
+ "set)\n"
+ " plugin: plugin name\n"
+ " string: string specific to plugin (for example "
+ "a server name or server.channel for IRC plugin)\n"
+ "charset: charset to use (if empty, then charset "
+ "is removed)\n\n"
+ "Examples :\n"
+ "/charset decode iso-8859-15 => set global "
+ "decode charset to iso-8859-15\n"
+ "/charset encode iso-8859-15 => set global "
+ "encode charset to iso-8859-15\n"
+ "/charset decode.irc.freenode => set decode "
+ "charset to iso-8859-15 for IRC server "
+ "\"freenode\" (all channels)\n"
+ "/charset decode.irc.freenode.#weechat => set "
+ "decode charset to iso-8859-15 for IRC channel "
+ "\"#weechat\" on server \"freenode\""),
+ "%(charset_name) %(charset)",
+ &charset_command_cb, NULL);
+
+ /* add messge modifiers */
+ weechat_hook_modifier ("charset_decode", &charset_decode, NULL);
+ weechat_hook_modifier ("charset_encode", &charset_encode, NULL);
+
+ return WEECHAT_RC_OK;
}
/*
* weechat_plugin_end: end charset plugin
*/
-void
-weechat_plugin_end (t_weechat_plugin *plugin)
+int
+weechat_plugin_end (struct t_weechat_plugin *plugin)
{
/* make C compiler happy */
(void) plugin;
- if (weechat_charset_terminal)
- free (weechat_charset_terminal);
- if (weechat_charset_internal)
- free (weechat_charset_internal);
+ charset_config_write ();
+ charset_free_all ();
+ weechat_config_free (charset_config_file);
+
+ return WEECHAT_RC_OK;
}