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