diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-03-15 16:18:54 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-03-15 16:18:54 +0000 |
commit | 1b00588ccc35ec749bb01c8ded6fdd732c5d3bf6 (patch) | |
tree | eeb401b61d6f8c77e604a2408b2f7d04bf35eeb6 /data | |
parent | 15068f45e239c6e8fb9149215935c0d97927c304 (diff) |
Forgot to add the actual unicode I/O filter to the repository (see [753])
Diffstat (limited to 'data')
-rw-r--r-- | data/rbot/plugins/unicode.rb | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/data/rbot/plugins/unicode.rb b/data/rbot/plugins/unicode.rb new file mode 100644 index 00000000..9424a91e --- /dev/null +++ b/data/rbot/plugins/unicode.rb @@ -0,0 +1,95 @@ +#-- vim:sw=4:et +#++ +# +# :title: Unicode plugin +# +# Author:: jsn (Dmitry Kim) <dmitry dot kim at gmail dot org> +# Copyright:: (C) 2007 Dmitry Kim +# License:: public domain +# +# This plugin adds unicode-awareness to rbot. When it's loaded, all the +# character strings inside of rbot are assumed to be in proper utf-8 +# encoding. The plugin takes care of translation to/from utf-8 on server IO, +# if necessary (translation charsets are configurable). + +# TODO do we actually want this? +require 'jcode' + +require 'iconv' + +class UnicodePlugin < Plugin + BotConfig.register BotConfigBooleanValue.new( + 'encoding.enable', :default => true, + :desc => "Support for non-ascii charsets", + :on_change => Proc.new { |bot, v| reconfigure_filter(bot) }) + + BotConfig.register BotConfigArrayValue.new( + 'encoding.charsets', :default => ['utf-8', 'cp1252'], + :desc => "Ordered list of iconv(3) charsets the bot should try", + :on_change => Proc.new { |bot, v| reconfigure_filter(bot) }) + + class UnicodeFilter + def initialize(oenc, *iencs) + o = oenc.dup + o += '//ignore' if !o.include?('/') + i = iencs[0].dup + i += '//ignore' if !i.include?('/') + @iencs = iencs.dup + @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) } + debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}" + @default_in = Iconv.new('utf-8', i) + @default_out = Iconv.new(o, 'utf-8') + end + + def in(data) + rv = nil + @iconvs.each_with_index { |ic, idx| + begin + debug "trying #{@iencs[idx]}" + rv = ic.iconv(data) + break + rescue + end + } + + rv = @default_in.iconv(data) if !rv + debug ">> #{rv}" + return rv + end + + def out(data) + rv = @default_out.iconv(data) + debug "<< #{rv}" + rv + end + end + + + def initialize(*a) + super + @old_kcode = $KCODE + self.class.reconfigure_filter(@bot) + end + + def cleanup + debug "cleaning up encodings" + @bot.socket.filter = nil + $KCODE = @old_kcode + end + + def UnicodePlugin.reconfigure_filter(bot) + debug "configuring encodings" + enable = bot.config['encoding.enable'] + if not enable + bot.socket.filter = nil + $KCODE = @old_kcode + return + end + charsets = bot.config['encoding.charsets'] + charsets = ['utf-8'] if charsets.empty? + bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets) + $KCODE = 'u' + end +end + +UnicodePlugin.new |