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).
15 # TODO do we actually want this?
20 class UnicodePlugin < Plugin
21 BotConfig.register BotConfigBooleanValue.new(
22 'encoding.enable', :default => true,
23 :desc => "Support for non-ascii charsets",
24 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
26 BotConfig.register BotConfigArrayValue.new(
27 'encoding.charsets', :default => ['utf-8', 'cp1252'],
28 :desc => "Ordered list of iconv(3) charsets the bot should try",
29 :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
32 def initialize(oenc, *iencs)
34 o += '//ignore' if !o.include?('/')
36 i += '//ignore' if !i.include?('/')
38 @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) }
39 debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}"
40 @default_in = Iconv.new('utf-8', i)
41 @default_out = Iconv.new(o, 'utf-8')
46 @iconvs.each_with_index { |ic, idx|
48 debug "trying #{@iencs[idx]}"
55 rv = @default_in.iconv(data) if !rv
61 rv = @default_out.iconv(data)
71 self.class.reconfigure_filter(@bot)
75 debug "cleaning up encodings"
76 @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)