summaryrefslogtreecommitdiff
path: root/lib/rbot/core/unicode.rb
blob: 02638bbd3141235e6909012fb56191d9d545bdd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#-- vim:sw=2:et
#++
#
# :title: Unicode plugin
# To set the encoding of strings coming from the irc server.

class UnicodePlugin < CoreBotModule
  Config.register Config::BooleanValue.new('encoding.enable',
    :default => true,
    :desc => "Support for non-ascii charsets",
    :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })

  Config.register Config::StringValue.new('encoding.charset',
    :default => 'utf-8',
    :desc => 'Server encoding.',
    :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })

  class UnicodeFilter
    def initialize(charset)
      @charset = charset
    end

    def in(data)
      data.force_encoding @charset if data
      data.encode('UTF-16le', :invalid => :replace, :replace => '').encode('UTF-8')
    end

    def out(data)
      data
    end
  end


  def initialize(*a)
    super
    self.class.reconfigure_filter(@bot)
  end

  def cleanup
    debug "cleaning up encodings"
    @bot.socket.filter = nil
    super
  end

  def UnicodePlugin.reconfigure_filter(bot)
    debug "configuring encodings"
    charset = bot.config['encoding.charset']
    if bot.config['encoding.enable']
      bot.socket.filter = UnicodeFilter.new charset
    else
      bot.socket.filter = nil
    end
  end
end

UnicodePlugin.new