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).
13 # TODO autoconfigure using server-provided allowed charset when these are
14 # available, see also comment in irc.rb
18 class UnicodePlugin < CoreBotModule
19 Config.register Config::BooleanValue.new(
20 'encoding.enable', :default => true,
21 :desc => "Support for non-ascii charsets",
22 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
24 Config.register Config::ArrayValue.new(
25 'encoding.charsets', :default => ['utf-8', 'cp1252', 'iso-8859-15'],
26 :desc => "Ordered list of iconv(3) charsets the bot should try",
27 :validate_item => Proc.new { |x| !!(Iconv.new('utf-8', x) rescue nil) },
28 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
31 def initialize(oenc, *iencs)
33 o += '//ignore' if !o.include?('/')
35 # i += '//ignore' if !i.include?('/')
37 @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) }
38 debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}"
39 @default_in = Iconv.new('utf-8//ignore', i)
40 @default_out = Iconv.new(o, 'utf-8//ignore')
45 @iconvs.each_with_index { |ic, idx|
47 debug "trying #{@iencs[idx]}"
54 rv = @default_in.iconv(data) if !rv
55 debug ">> #{rv.inspect}"
60 rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
70 self.class.reconfigure_filter(@bot)
74 debug "cleaning up encodings"
75 @bot.socket.filter = nil
80 def UnicodePlugin.reconfigure_filter(bot)
81 debug "configuring encodings"
82 enable = bot.config['encoding.enable']
84 bot.socket.filter = nil
88 charsets = bot.config['encoding.charsets']
89 charsets = ['utf-8'] if charsets.empty?
90 bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)