]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/unicode.rb
fix: webservice message user type
[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       data.encode('UTF-16le', :invalid => :replace, :replace => '').encode('UTF-8')
26     end
27
28     def out(data)
29       data
30     end
31   end
32
33
34   def initialize(*a)
35     super
36     self.class.reconfigure_filter(@bot)
37   end
38
39   def cleanup
40     debug "cleaning up encodings"
41     @bot.socket.filter = nil
42     super
43   end
44
45   def UnicodePlugin.reconfigure_filter(bot)
46     debug "configuring encodings"
47     charset = bot.config['encoding.charset']
48     if bot.config['encoding.enable']
49       bot.socket.filter = UnicodeFilter.new charset
50     else
51       bot.socket.filter = nil
52     end
53   end
54 end
55
56 UnicodePlugin.new