]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Socket IO filtering: rbot can now assume UTF-8 internally.
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 14 Mar 2007 01:19:01 +0000 (01:19 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 14 Mar 2007 01:19:01 +0000 (01:19 +0000)
ChangeLog
lib/rbot/ircbot.rb
lib/rbot/ircsocket.rb

index 1e74ab021d88596844cd499416229669ef4d8736..9479513679ce220386ffee8e70901b0f0544b534 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2007-03-14  Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
+
+       * Socket filtering: socket data, both input and output, can now be
+       filtered. This is used for example to transcode all input and output
+       so that messages are internally managed as UTF-8. By default, the bot
+       will try cp1252 (Windows Western European) encoding for non-UTF-8
+       strings. Thanks to jsn (Dmitry Kim <dmitry.kim@gmail.com>).
+
 2007-03-10  Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
 
        * IRC settings: ability to change the IRC name for the bot. Thanks to
index aee426b3cf294c0219d319267be608dfdf025521..b58ebe2efdf2dec192d71f6bfea1f687b7950f15 100644 (file)
@@ -440,15 +440,15 @@ class Bot
     @plugins = Plugins::pluginmanager
     @plugins.bot_associate(self)
     setup_plugins_path()
+
+    @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'], :ssl => @config['server.ssl'])
+    @client = Client.new
+
     @plugins.scan
 
     Utils.set_safe_save_dir("#{botclass}/safe_save")
     @httputil = Utils::HttpUtil.new(self)
 
-
-    @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'], :ssl => @config['server.ssl'])
-    @client = Client.new
-
     # Channels where we are quiet
     # Array of channels names where the bot should be quiet
     # '*' means all channels
index 973ae9b85f4e3c5d085b826fd5b7fb7b30c0bdf8..5163e0ef8fb3127dce665d8d851560ee97a43707 100644 (file)
@@ -258,6 +258,26 @@ module Irc
     # max lines to burst
     attr_reader :sendq_burst
 
+    # an optional filter object. we call @filter.in(data) for
+    # all incoming data and @filter.out(data) for all outgoing data
+    attr_reader :filter
+
+    # default trivial filter class
+    class IdentityFilter
+        def in(x)
+            x
+        end
+
+        def out(x)
+            x
+        end
+    end
+
+    # set filter to identity, not to nil
+    def filter=(f)
+        @filter = f || IdentityFilter.new
+    end
+
     # server:: server to connect to
     # port::   IRCd port
     # host::   optional local host to bind to (ruby 1.7+ required)
@@ -271,6 +291,7 @@ module Irc
       @port = port.to_i
       @host = host
       @sock = nil
+      @filter = IdentityFilter.new
       @spooler = false
       @lines_sent = 0
       @lines_received = 0
@@ -380,7 +401,7 @@ module Irc
         return nil
       end
       begin
-        reply = @sock.gets
+        reply = @filter.in(@sock.gets)
         @lines_received += 1
         reply.strip! if reply
         debug "RECV: #{reply.inspect}"
@@ -483,7 +504,7 @@ module Irc
         if @sock.nil?
           error "SEND attempted on closed socket"
         else
-          @sock.puts message
+          @sock.puts(@filter.out(message))
           @last_send = Time.new
           @flood_send += message.irc_send_penalty if penalty
           @lines_sent += 1