diff options
Diffstat (limited to 'src/plugins/charset')
-rw-r--r-- | src/plugins/charset/Makefile.am | 2 | ||||
-rw-r--r-- | src/plugins/charset/charset.c | 673 | ||||
-rw-r--r-- | src/plugins/charset/charset.h | 33 |
3 files changed, 262 insertions, 446 deletions
diff --git a/src/plugins/charset/Makefile.am b/src/plugins/charset/Makefile.am index 0c73d8057..0f5a770bb 100644 --- a/src/plugins/charset/Makefile.am +++ b/src/plugins/charset/Makefile.am @@ -20,6 +20,6 @@ libdir = ${weechat_libdir}/plugins lib_LTLIBRARIES = charset.la -charset_la_SOURCES = charset.c charset.h +charset_la_SOURCES = charset.c charset_la_LDFLAGS = -module charset_la_LIBADD = $(CHARSET_LFLAGS) diff --git a/src/plugins/charset/charset.c b/src/plugins/charset/charset.c index 81e71d6ec..4055d994b 100644 --- a/src/plugins/charset/charset.c +++ b/src/plugins/charset/charset.c @@ -28,7 +28,6 @@ #include <iconv.h> #include "../weechat-plugin.h" -#include "charset.h" WEECHAT_PLUGIN_NAME("charset"); @@ -36,14 +35,18 @@ WEECHAT_PLUGIN_DESCRIPTION("Charset plugin for WeeChat"); WEECHAT_PLUGIN_AUTHOR("FlashCode <flashcode@flashtux.org>"); WEECHAT_PLUGIN_VERSION(WEECHAT_VERSION); WEECHAT_PLUGIN_WEECHAT_VERSION(WEECHAT_VERSION); -WEECHAT_PLUGIN_LICENSE("GPL"); +WEECHAT_PLUGIN_LICENSE("GPL3"); + +#define CHARSET_CONFIG_NAME "charset" struct t_weechat_plugin *weechat_charset_plugin = NULL; #define weechat_plugin weechat_charset_plugin struct t_config_file *charset_config_file = NULL; -struct t_charset *charset_list = NULL; -struct t_charset *last_charset = NULL; +struct t_config_option *charset_default_decode = NULL; +struct t_config_option *charset_default_encode = NULL; +struct t_config_section *charset_config_section_decode = NULL; +struct t_config_section *charset_config_section_encode = NULL; char *charset_terminal = NULL; char *charset_internal = NULL; @@ -65,112 +68,19 @@ charset_debug_cb (void *data, char *signal, char *type_data, void *signal_data) if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0) { if (weechat_strcasecmp ((char *)signal_data, "charset") == 0) + { charset_debug ^= 1; + if (charset_debug) + weechat_printf (NULL, _("%s: debug enabled"), "charset"); + else + weechat_printf (NULL, _("%s: debug disabled"), "charset"); + } } return WEECHAT_RC_OK; } /* - * charset_search: search a charset - */ - -struct t_charset * -charset_search (char *name) -{ - struct t_charset *ptr_charset; - - 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; - - if (!name || !name[0] || !charset || !charset[0]) - return NULL; - - 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 = malloc (sizeof (*new_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 new_charset; -} - -/* - * charset_free: free a charset and remove it from list - */ - -void -charset_free (struct t_charset *charset) -{ - struct t_charset *new_charset_list; - - /* remove charset from list */ - if (last_charset == charset) - last_charset = charset->prev_charset; - if (charset->prev_charset) - { - (charset->prev_charset)->next_charset = charset->next_charset; - new_charset_list = charset_list; - } - 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; -} - -/* - * charset_free_all: free all charsets - */ - -void -charset_free_all () -{ - while (charset_list) - charset_free (charset_list); -} - -/* * charset_config_reaload: reload charset configuration file */ @@ -179,84 +89,69 @@ charset_config_reload (void *data, struct t_config_file *config_file) { /* make C compiler happy */ (void) data; - (void) config_file; - charset_free_all (); - return weechat_config_reload (charset_config_file); + /* free all decode/encode charsets */ + weechat_config_section_free_options (charset_config_section_decode); + weechat_config_section_free_options (charset_config_section_encode); + + return weechat_config_reload (config_file); } /* - * charset_config_read_line: read a charset in configuration file + * charset_config_set_option: set a charset */ -void -charset_config_read_line (void *data, struct t_config_file *config_file, - char *option_name, char *value) +int +charset_config_create_option (void *data, struct t_config_file *config_file, + struct t_config_section *section, + char *option_name, char *value) { + struct t_config_option *ptr_option; + int rc; + /* make C compiler happy */ (void) data; - (void) config_file; - if (option_name && value) + rc = 0; + + if (option_name) { - /* create new charset */ - if (!charset_new (option_name, value)) + ptr_option = weechat_config_search_option (config_file, section, + option_name); + if (ptr_option) { - weechat_printf (NULL, - _("%s%s: error creating charset \"%s\" => \"%s\""), - weechat_prefix ("error"), "charset", - option_name, value); + if (value && value[0]) + rc = weechat_config_option_set (ptr_option, value, 1); + else + { + weechat_config_option_free (section, ptr_option); + rc = 1; + } + } + else + { + if (value && value[0]) + { + ptr_option = weechat_config_new_option ( + config_file, section, + option_name, "string", NULL, + NULL, 0, 0, value, NULL, NULL, NULL, NULL, NULL, NULL); + rc = (ptr_option) ? 1 : 0; + } + else + rc = 1; } } -} - -/* - * charseet_config_write_section: write charset section in configuration file - * Return: 0 = successful - * -1 = write error - */ - -void -charset_config_write_section (void *data, struct t_config_file *config_file, - char *section_name) -{ - struct t_charset *ptr_charset; - /* make C compiler happy */ - (void) data; - - weechat_config_write_line (config_file, section_name, NULL); - - for (ptr_charset = charset_list; ptr_charset; - ptr_charset = ptr_charset->next_charset) + if (rc == 0) { - weechat_config_write_line (config_file, - ptr_charset->name, - "\"%s\"", - ptr_charset->charset); + weechat_printf (NULL, + _("%s%s: error creating charset \"%s\" => \"%s\""), + weechat_prefix ("error"), "charset", + option_name, value); } -} - -/* - * charset_config_write_default_aliases: write default charsets in configuration file - */ - -void -charset_config_write_default_charsets (void *data, - struct t_config_file *config_file, - char *section_name) -{ - /* make C compiler happy */ - (void) data; - 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 - weechat_config_write_line (config_file, "decode", "%s", "iso-8859-1"); + return rc; } /* @@ -269,24 +164,65 @@ charset_config_init () { struct t_config_section *ptr_section; - charset_config_file = weechat_config_new (CHARSET_CONFIG_FILENAME, + charset_config_file = weechat_config_new (CHARSET_CONFIG_NAME, &charset_config_reload, NULL); if (!charset_config_file) return 0; - ptr_section = weechat_config_new_section (charset_config_file, "charset", - &charset_config_read_line, - NULL, - &charset_config_write_section, - NULL, - &charset_config_write_default_charsets, - NULL); + ptr_section = weechat_config_new_section (charset_config_file, "default", + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL); + if (!ptr_section) + { + weechat_config_free (charset_config_file); + return 0; + } + + charset_default_decode = weechat_config_new_option ( + charset_config_file, ptr_section, + "decode", "string", + N_("global decoding charset"), + NULL, 0, 0, + (charset_terminal && charset_internal + && (strcasecmp (charset_terminal, + charset_internal) != 0)) ? + charset_terminal : "iso-8859-1", + NULL, NULL, NULL, NULL, NULL, NULL); + charset_default_encode = weechat_config_new_option ( + charset_config_file, ptr_section, + "encode", "string", + N_("global encoding charset"), + NULL, 0, 0, "", + NULL, NULL, NULL, NULL, NULL, NULL); + + ptr_section = weechat_config_new_section (charset_config_file, "decode", + NULL, NULL, + NULL, NULL, + NULL, NULL, + &charset_config_create_option, NULL); if (!ptr_section) { weechat_config_free (charset_config_file); return 0; } + charset_config_section_decode = ptr_section; + + ptr_section = weechat_config_new_section (charset_config_file, "encode", + NULL, NULL, + NULL, NULL, + NULL, NULL, + &charset_config_create_option, NULL); + if (!ptr_section) + { + weechat_config_free (charset_config_file); + return 0; + } + + charset_config_section_encode = ptr_section; + return 1; } @@ -332,173 +268,31 @@ charset_check (char *charset) } /* - * charset_parse_irc_msg: return nick, command, channel and position - * of arguments in IRC message - */ - -void -charset_parse_irc_msg (char *message, char **nick, char **command, - char **channel, char **pos_args) -{ - char *pos, *pos2, *pos3, *pos4, *pos_tmp; - - *nick = NULL; - *command = NULL; - *channel = NULL; - *pos_args = NULL; - - if (message[0] == ':') - { - pos = message + 1; - pos_tmp = strchr (pos, ' '); - if (pos_tmp) - pos_tmp[0] = '\0'; - pos2 = strchr (pos, '!'); - if (pos2) - *nick = weechat_strndup (pos, pos2 - pos); - else - { - pos2 = strchr (pos, ' '); - if (pos2) - *nick = weechat_strndup (pos, pos2 - pos); - } - if (pos_tmp) - pos_tmp[0] = ' '; - pos = strchr (message, ' '); - if (!pos) - pos = message; - } - else - pos = message; - - if (pos && pos[0]) - { - while (pos[0] == ' ') - pos++; - pos2 = strchr (pos, ' '); - if (pos2) - { - *command = weechat_strndup (pos, pos2 - pos); - pos2++; - while (pos2[0] == ' ') - pos2++; - *pos_args = pos2; - if (pos2[0] != ':') - { - if ((pos2[0] == '#') || (pos2[0] == '&') - || (pos2[0] == '+') || (pos2[0] == '!')) - { - pos3 = strchr (pos2, ' '); - if (pos3) - *channel = weechat_strndup (pos2, pos3 - pos2); - else - *channel = strdup (pos2); - } - else - { - pos3 = strchr (pos2, ' '); - if (!*nick) - { - if (pos3) - *nick = weechat_strndup (pos2, pos3 - pos2); - else - *nick = strdup (pos2); - } - if (pos3) - { - pos3++; - while (pos3[0] == ' ') - pos3++; - if ((pos3[0] == '#') || (pos3[0] == '&') - || (pos3[0] == '+') || (pos3[0] == '!')) - { - pos4 = strchr (pos3, ' '); - if (pos4) - *channel = weechat_strndup (pos3, pos4 - pos3); - else - *channel = strdup (pos3); - } - } - } - } - } - } -} - -/* - * charset_set: set a charset - * return 1 if ok, 0 if error - */ - -int -charset_set (char *name, char *charset) -{ - struct t_charset *ptr_charset; - - if (charset && charset[0]) - { - 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); - } - - return 1; -} - -/* * 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 * -charset_get (char *type, char *name) +charset_get (struct t_config_section *section, char *name, + struct t_config_option *default_charset) { char *option_name, *ptr_end; - struct t_charset *ptr_charset; - int length; - - length = strlen (type) + 1 + strlen (name) + 1; - option_name = malloc (length); + struct t_config_option *ptr_option; + + option_name = strdup (name); if (option_name) { - 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) + ptr_option = weechat_config_search_option (charset_config_file, + section, + option_name); + if (ptr_option) { free (option_name); - return ptr_charset->charset; + return weechat_config_string (ptr_option); } ptr_end--; while ((ptr_end >= option_name) && (ptr_end[0] != '.')) @@ -508,15 +302,22 @@ charset_get (char *type, char *name) if ((ptr_end >= option_name) && (ptr_end[0] == '.')) ptr_end[0] = '\0'; } - ptr_charset = charset_search (option_name); + ptr_option = weechat_config_search_option (charset_config_file, + section, + option_name); free (option_name); - if (ptr_charset) - return ptr_charset->charset; + if (ptr_option) + return weechat_config_string (ptr_option); } - /* nothing found => no decode/encode for this message! */ + /* nothing found => return default decode/encode charset (if set) */ + if (weechat_config_string (default_charset) + && weechat_config_string (default_charset)[0]) + return weechat_config_string (default_charset); + + /* no default charset set */ return NULL; } @@ -534,7 +335,15 @@ charset_decode (void *data, char *modifier, char *modifier_data, (void) data; (void) modifier; - charset = charset_get ("decode", modifier_data); + charset = charset_get (charset_config_section_decode, modifier_data, + charset_default_decode); + if (charset_debug) + { + weechat_printf (NULL, + "charset: debug: using 'decode' charset: %s " + "(modifier='%s', modifier_data='%s', string='%s')", + charset, modifier, modifier_data, string); + } if (charset && charset[0]) return weechat_iconv_to_internal (charset, string); @@ -555,7 +364,15 @@ charset_encode (void *data, char *modifier, char *modifier_data, (void) data; (void) modifier; - charset = charset_get ("encode", modifier_data); + charset = charset_get (charset_config_section_encode, modifier_data, + charset_default_encode); + if (charset_debug) + { + weechat_printf (NULL, + "charset: debug: using 'encode' charset: %s " + "(modifier='%s', modifier_data='%s', string='%s')", + charset, modifier, modifier_data, string); + } if (charset && charset[0]) return weechat_iconv_from_internal (charset, string); @@ -563,6 +380,29 @@ charset_encode (void *data, char *modifier, char *modifier_data, } /* + * charset_set: set a charset + */ + +void +charset_set (struct t_config_section *section, char *type, + char *name, char *value) +{ + if (charset_config_create_option (NULL, + charset_config_file, + section, + name, + value) > 0) + { + if (value && value[0]) + weechat_printf (NULL, _("Charset: %s, %s => %s"), + type, name, value); + else + weechat_printf (NULL, _("Charset: %s, %s: removed"), + type, name); + } +} + +/* * charset_command_cb: callback for /charset command */ @@ -570,75 +410,98 @@ 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; + struct t_config_section *ptr_section; + int length; + char *ptr_charset, *option_name, *plugin_name, *category, *name; /* make C compiler happy */ (void) data; - (void) buffer; - if ((argc > 2) && (strcmp (argv[2], "=") == 0)) + if (argc < 2) + { + weechat_printf (NULL, + _("%s%s: missing parameters"), + weechat_prefix ("error"), "charset"); + return WEECHAT_RC_ERROR; + } + + ptr_section = NULL; + + plugin_name = weechat_buffer_get_string (buffer, "plugin"); + category = weechat_buffer_get_string (buffer, "category"); + name = weechat_buffer_get_string (buffer, "name"); + + length = ((plugin_name) ? strlen (plugin_name) : 0) + 1 + + strlen (category) + 1 + strlen (name) + 1; + option_name = malloc (length); + if (!option_name) + return WEECHAT_RC_ERROR; + + snprintf (option_name, length, "%s%s%s.%s", + (plugin_name) ? plugin_name : "", + (plugin_name) ? "." : "", + category, + name); + + if ((argc > 1) && (weechat_strcasecmp (argv[1], "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)) + charset_set (charset_config_section_decode, "decode", option_name, + NULL); + charset_set (charset_config_section_encode, "encode", option_name, + NULL); + } + else + { + if (argc > 2) { - length = strlen (argv[1]) + strlen ("decode.") + 1; - option_name = malloc (length); - if (option_name) + if (weechat_strcasecmp (argv[1], "decode") == 0) { - 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; + ptr_section = charset_config_section_decode; + ptr_charset = argv_eol[2]; + } + else if (weechat_strcasecmp (argv[1], "encode") == 0) + { + ptr_section = charset_config_section_encode; + ptr_charset = argv_eol[2]; + } + if (!ptr_section) + { + weechat_printf (NULL, + _("%s%s: wrong charset type (decode or encode " + "expected)"), + weechat_prefix ("error"), "charset"); + if (option_name) + free (option_name); + return WEECHAT_RC_ERROR; } } else + ptr_charset = argv_eol[1]; + + if (!charset_check (ptr_charset)) { - if (!charset_set (argv[1], ptr_value)) - return WEECHAT_RC_ERROR; + weechat_printf (NULL, + _("%s%s: invalid charset: \"%s\""), + weechat_prefix ("error"), "charset", ptr_charset); + if (option_name) + free (option_name); + return WEECHAT_RC_ERROR; } - } - else - { - /* list all charsets */ - if (charset_list) + if (ptr_section) { - 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 ((argc < 2) - || (weechat_strcasestr (ptr_charset->name, argv_eol[1]))) - { - 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); - } - } - if (!charset_found) - weechat_printf (NULL, _("No charset found")); + charset_set (ptr_section, argv[1], option_name, ptr_charset); } else - weechat_printf (NULL, _("No charset defined")); + { + charset_set (charset_config_section_decode, "decode", option_name, + ptr_charset); + charset_set (charset_config_section_encode, "encode", option_name, + ptr_charset); + } } + free (option_name); + return WEECHAT_RC_OK; } @@ -657,50 +520,36 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) /* display message */ weechat_printf (NULL, - _("%s%s: terminal: %s, internal: %s"), - weechat_prefix ("info"), "charset", - charset_terminal, charset_internal); + _("%s: terminal: %s, internal: %s"), + "charset", charset_terminal, charset_internal); if (!charset_config_init ()) { weechat_printf (NULL, - _("%s%s: error creating configuration file \"%s\""), - weechat_prefix("error"), "charset", - CHARSET_CONFIG_FILENAME); + _("%s%s: error creating configuration file"), + weechat_prefix("error"), "charset"); return WEECHAT_RC_ERROR; } charset_config_read (); - /* add command handler */ + /* /charset command */ 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)", + _("change charset for current buffer"), + _("[[decode | encode] charset] | [reset]"), + _(" decode: change decoding charset\n" + " encode: change encoding charset\n" + "charset: new charset for current buffer\n" + " reset: reset charsets for current buffer"), + "decode|encode|reset", &charset_command_cb, NULL); - /* add messge modifiers */ + /* modifiers hooks */ weechat_hook_modifier ("charset_decode", &charset_decode, NULL); weechat_hook_modifier ("charset_encode", &charset_encode, NULL); + /* callback for debug */ + weechat_hook_signal ("debug", &charset_debug_cb, NULL); + return WEECHAT_RC_OK; } @@ -715,7 +564,7 @@ weechat_plugin_end (struct t_weechat_plugin *plugin) (void) plugin; charset_config_write (); - charset_free_all (); + weechat_config_free (charset_config_file); return WEECHAT_RC_OK; diff --git a/src/plugins/charset/charset.h b/src/plugins/charset/charset.h deleted file mode 100644 index 248972587..000000000 --- a/src/plugins/charset/charset.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org> - * See README for License detail, AUTHORS for developers list. - * - * 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 3 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, see <http://www.gnu.org/licenses/>. - */ - - -#ifndef __WEECHAT_CHARSET_H -#define __WEECHAT_CHARSET_H 1 - -#define CHARSET_CONFIG_FILENAME "charset.rc" - -struct t_charset -{ - char *name; /* charset name (identifier) */ - char *charset; /* charset value for name */ - struct t_charset *prev_charset; /* link to previous charset */ - struct t_charset *next_charset; /* link to next charset */ -}; - -#endif /* charset.h */ |