4 # :title: Unicode plugin
6 # Author:: jsn (Dmitry Kim) <dmitry dot kim at gmail dot org>
8 # This plugin adds unicode-awareness to rbot. When it's loaded, all the
9 # character strings inside of rbot are assumed to be in proper utf-8
10 # encoding. The plugin takes care of translation to/from utf-8 on server IO,
11 # if necessary (translation charsets are configurable).
15 class UnicodePlugin < CoreBotModule
16 Config.register Config::BooleanValue.new(
17 'encoding.enable', :default => true,
18 :desc => "Support for non-ascii charsets",
19 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
21 Config.register Config::ArrayValue.new(
22 'encoding.charsets', :default => ['utf-8', 'cp1252', 'iso-8859-15'],
23 :desc => "Ordered list of iconv(3) charsets the bot should try",
24 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
27 def initialize(oenc, *iencs)
29 o += '//ignore' if !o.include?('/')
31 # i += '//ignore' if !i.include?('/')
33 @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) }
34 debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}"
35 @default_in = Iconv.new('utf-8//ignore', i)
36 @default_out = Iconv.new(o, 'utf-8//ignore')
41 @iconvs.each_with_index { |ic, idx|
43 debug "trying #{@iencs[idx]}"
50 rv = @default_in.iconv(data) if !rv
51 debug ">> #{rv.inspect}"
56 rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
66 self.class.reconfigure_filter(@bot)
70 debug "cleaning up encodings"
71 @bot.socket.filter = nil
76 def UnicodePlugin.reconfigure_filter(bot)
77 debug "configuring encodings"
78 enable = bot.config['encoding.enable']
80 bot.socket.filter = nil
84 charsets = bot.config['encoding.charsets']
85 charsets = ['utf-8'] if charsets.empty?
86 bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)