]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/unicode.rb
[core] unicode plugin that sets server encoding
[user/henk/code/ruby/rbot.git] / lib / rbot / core / unicode.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Unicode plugin
5 # To set the encoding of strings coming from the irc server.
6
7 class UnicodePlugin < CoreBotModule
8   Config.register Config::BooleanValue.new('encoding.enable',
9     :default => true,
10     :desc => "Support for non-ascii charsets",
11     :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
12
13   Config.register Config::StringValue.new('encoding.charset',
14     :default => 'utf-8',
15     :desc => 'Server encoding.',
16     :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
17
18   class UnicodeFilter
19     def initialize(charset)
20       @charset = charset
21     end
22
23     def in(data)
24       data.force_encoding @charset if data
25     end
26
27     def out(data)
28       data
29     end
30   end
31
32
33   def initialize(*a)
34     super
35     self.class.reconfigure_filter(@bot)
36   end
37
38   def cleanup
39     debug "cleaning up encodings"
40     @bot.socket.filter = nil
41     super
42   end
43
44   def UnicodePlugin.reconfigure_filter(bot)
45     debug "configuring encodings"
46     charset = bot.config['encoding.charset']
47     if bot.config['encoding.enable']
48       bot.socket.filter = UnicodeFilter.new charset
49     else
50       bot.socket.filter = nil
51     end
52   end
53 end
54
55 UnicodePlugin.new