]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/unicode.rb
* (core/unicode) validate encoding.charsets with Iconv
[user/henk/code/ruby/rbot.git] / lib / rbot / core / unicode.rb
1 #-- vim:sw=4:et
2 #++
3 #
4 # :title: Unicode plugin
5 #
6 # Author:: jsn (Dmitry Kim) <dmitry dot kim at gmail dot org>
7 #
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).
12
13 require 'iconv'
14
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) })
20
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) })
26
27     class UnicodeFilter
28         def initialize(oenc, *iencs)
29             o = oenc.dup
30             o += '//ignore' if !o.include?('/')
31             i = iencs[0].dup
32             # i += '//ignore' if !i.include?('/')
33             @iencs = iencs.dup
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')
38         end
39
40         def in(data)
41             rv = nil
42             @iconvs.each_with_index { |ic, idx|
43                 begin
44                     debug "trying #{@iencs[idx]}"
45                     rv = ic.iconv(data)
46                     break
47                 rescue
48                 end
49             }
50
51             rv = @default_in.iconv(data) if !rv
52             debug ">> #{rv.inspect}"
53             return rv
54         end
55
56         def out(data)
57             rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
58             debug "<< #{rv}"
59             rv
60         end
61     end
62
63
64     def initialize(*a)
65         super
66         @@old_kcode = $KCODE
67         self.class.reconfigure_filter(@bot)
68     end
69
70     def cleanup
71         debug "cleaning up encodings"
72         @bot.socket.filter = nil
73         $KCODE = @@old_kcode
74         super
75     end
76
77     def UnicodePlugin.reconfigure_filter(bot)
78         debug "configuring encodings"
79         enable = bot.config['encoding.enable']
80         if not enable
81             bot.socket.filter = nil
82             $KCODE = @@old_kcode
83             return
84         end
85         charsets = bot.config['encoding.charsets']
86         charsets = ['utf-8'] if charsets.empty?
87         bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)
88         $KCODE = 'u'
89     end
90 end
91
92 UnicodePlugin.new