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 :validate_item => Proc.new { |x| !!(Iconv.new('utf-8', x) rescue nil) },
25 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
28 def initialize(oenc, *iencs)
30 o += '//ignore' if !o.include?('/')
32 # i += '//ignore' if !i.include?('/')
34 @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) }
35 debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}"
36 @default_in = Iconv.new('utf-8//ignore', i)
37 @default_out = Iconv.new(o, 'utf-8//ignore')
42 @iconvs.each_with_index { |ic, idx|
44 debug "trying #{@iencs[idx]}"
51 rv = @default_in.iconv(data) if !rv
52 debug ">> #{rv.inspect}"
57 rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
67 self.class.reconfigure_filter(@bot)
71 debug "cleaning up encodings"
72 @bot.socket.filter = nil
77 def UnicodePlugin.reconfigure_filter(bot)
78 debug "configuring encodings"
79 enable = bot.config['encoding.enable']
81 bot.socket.filter = nil
85 charsets = bot.config['encoding.charsets']
86 charsets = ['utf-8'] if charsets.empty?
87 bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)