]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/unicode.rb
ircbot: do not use debug in sendmsg() because it breaks script. script plugin: handle...
[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     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) })
25
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) })
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', i)
41             @default_out = Iconv.new(o, 'utf-8')
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}"
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     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