]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/unicode.rb
script plugin: use m.okay
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / 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 # TODO do we actually want this?
16 require 'jcode'
17
18 require 'iconv'
19
20 class UnicodePlugin < Plugin
21     Config.register Config::BooleanValue.new(
22     'encoding.enable', :default => true,
23     :desc => "Support for non-ascii charsets",
24     :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
25
26     Config.register Config::ArrayValue.new(
27     'encoding.charsets', :default => ['utf-8', 'cp1252', 'iso-8859-15'],
28     :desc => "Ordered list of iconv(3) charsets the bot should try",
29     :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
30
31     class UnicodeFilter
32         def initialize(oenc, *iencs)
33             o = oenc.dup
34             o += '//ignore' if !o.include?('/')
35             i = iencs[0].dup
36             # i += '//ignore' if !i.include?('/')
37             @iencs = iencs.dup
38             @iconvs = @iencs.map { |_| Iconv.new('utf-8', _) }
39             debug "*** o = #{o}, i = #{i}, iencs = #{iencs.inspect}"
40             @default_in = Iconv.new('utf-8//ignore', i)
41             @default_out = Iconv.new(o, 'utf-8//ignore')
42         end
43
44         def in(data)
45             rv = nil
46             @iconvs.each_with_index { |ic, idx|
47                 begin
48                     debug "trying #{@iencs[idx]}"
49                     rv = ic.iconv(data)
50                     break
51                 rescue
52                 end
53             }
54
55             rv = @default_in.iconv(data) if !rv
56             debug ">> #{rv.inspect}"
57             return rv
58         end
59
60         def out(data)
61             rv = @default_out.iconv(data) rescue data # XXX: yeah, i know :/
62             debug "<< #{rv}"
63             rv
64         end
65     end
66
67
68     def initialize(*a)
69         super
70         @old_kcode = $KCODE
71         self.class.reconfigure_filter(@bot)
72     end
73
74     def cleanup
75         debug "cleaning up encodings"
76         @bot.socket.filter = nil
77         $KCODE = @old_kcode
78         super
79     end
80
81     def UnicodePlugin.reconfigure_filter(bot)
82         debug "configuring encodings"
83         enable = bot.config['encoding.enable']
84         if not enable
85             bot.socket.filter = nil
86             $KCODE = @old_kcode
87             return
88         end
89         charsets = bot.config['encoding.charsets']
90         charsets = ['utf-8'] if charsets.empty?
91         bot.socket.filter = UnicodeFilter.new(charsets[0], *charsets)
92         $KCODE = 'u'
93     end
94 end
95
96 UnicodePlugin.new