]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/unicode.rb
New IRC Framework: don't raise on unknown casemap
[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 # TODO autoconfigure using server-provided allowed charset when these are
14 # available, see also comment in irc.rb
15
16 require 'iconv'
17
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) })
23
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) })
29
30     class UnicodeFilter
31         def initialize(oenc, *iencs)
32             o = oenc.dup
33             o += '//ignore' if !o.include?('/')
34             i = iencs[0].dup
35             # i += '//ignore' if !i.include?('/')
36             @iencs = iencs.dup
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')
41         end
42
43         def in(data)
44             rv = nil
45             @iconvs.each_with_index { |ic, idx|
46                 begin
47                     debug "trying #{@iencs[idx]}"
48                     rv = ic.iconv(data)
49                     break
50                 rescue
51                 end
52             }
53
54             rv = @default_in.iconv(data) if !rv
55             debug ">> #{rv.inspect}"
56             return rv
57         end
58
59         def out(data)
60             rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
61             debug "<< #{rv}"
62             rv
63         end
64     end
65
66
67     def initialize(*a)
68         super
69         @@old_kcode = $KCODE
70         self.class.reconfigure_filter(@bot)
71     end
72
73     def cleanup
74         debug "cleaning up encodings"
75         @bot.socket.filter = nil
76         $KCODE = @@old_kcode
77         super
78     end
79
80     def UnicodePlugin.reconfigure_filter(bot)
81         debug "configuring encodings"
82         enable = bot.config['encoding.enable']
83         if not enable
84             bot.socket.filter = nil
85             $KCODE = @@old_kcode
86             return
87         end
88         charsets = bot.config['encoding.charsets']
89         charsets = ['utf-8'] if charsets.empty?
90         bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)
91         $KCODE = 'u'
92     end
93 end
94
95 UnicodePlugin.new