4 # :title: Unicode plugin
6 # Author:: jsn (Dmitry Kim) <dmitry dot kim at gmail dot org>
7 # Copyright:: (C) 2007 Dmitry Kim
8 # License:: public domain
10 # This plugin adds unicode-awareness to rbot. When it's loaded, all the
11 # character strings inside of rbot are assumed to be in proper utf-8
12 # encoding. The plugin takes care of translation to/from utf-8 on server IO,
13 # if necessary (translation charsets are configurable).
17 class UnicodePlugin < CoreBotModule
18 Config.register Config::BooleanValue.new(
19 'encoding.enable', :default => true,
20 :desc => "Support for non-ascii charsets",
21 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
23 Config.register Config::ArrayValue.new(
24 'encoding.charsets', :default => ['utf-8', 'cp1252', 'iso-8859-15'],
25 :desc => "Ordered list of iconv(3) charsets the bot should try",
26 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
29 def initialize(oenc, *iencs)
31 o += '//ignore' if !o.include?('/')
33 # i += '//ignore' if !i.include?('/')
35 @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) }
36 debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}"
37 @default_in = Iconv.new('utf-8//ignore', i)
38 @default_out = Iconv.new(o, 'utf-8//ignore')
43 @iconvs.each_with_index { |ic, idx|
45 debug "trying #{@iencs[idx]}"
52 rv = @default_in.iconv(data) if !rv
53 debug ">> #{rv.inspect}"
58 rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
68 self.class.reconfigure_filter(@bot)
72 debug "cleaning up encodings"
73 @bot.socket.filter = nil
78 def UnicodePlugin.reconfigure_filter(bot)
79 debug "configuring encodings"
80 enable = bot.config['encoding.enable']
82 bot.socket.filter = nil
86 charsets = bot.config['encoding.charsets']
87 charsets = ['utf-8'] if charsets.empty?
88 bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)