]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircsocket.rb
Socket IO filtering: rbot can now assume UTF-8 internally.
[user/henk/code/ruby/rbot.git] / lib / rbot / ircsocket.rb
index 6b49cf00e151b77137ace91411a9517b85e36c87..5163e0ef8fb3127dce665d8d851560ee97a43707 100644 (file)
@@ -3,9 +3,9 @@ class ::String
   # by the IRCd
   def irc_send_penalty
     # According to eggrdop, the initial penalty is
-    penalty = 1 + self.length/100
+    penalty = 1 + self.size/100
     # on everything but UnderNET where it's
-    # penalty = 2 + self.length/120
+    # penalty = 2 + self.size/120
 
     cmd, pars = self.split($;,2)
     debug "cmd: #{cmd}, pars: #{pars.inspect}"
@@ -14,29 +14,29 @@ class ::String
       chan, nick, msg = pars.split
       chan = chan.split(',')
       nick = nick.split(',')
-      penalty += nick.length
-      penalty *= chan.length
+      penalty += nick.size
+      penalty *= chan.size
     when :MODE
       chan, modes, argument = pars.split
       extra = 0
       if modes
         extra = 1
         if argument
-          extra += modes.split(/\+|-/).length
+          extra += modes.split(/\+|-/).size
         else
-          extra += 3 * modes.split(/\+|-/).length
+          extra += 3 * modes.split(/\+|-/).size
         end
       end
       if argument
-        extra += 2 * argument.split.length
+        extra += 2 * argument.split.size
       end
-      penalty += extra * chan.split.length
+      penalty += extra * chan.split.size
     when :TOPIC
       penalty += 1
-      penalty += 2 unless pars.split.length < 2
+      penalty += 2 unless pars.split.size < 2
     when :PRIVMSG, :NOTICE
       dests = pars.split($;,2).first
-      penalty += dests.split(',').length
+      penalty += dests.split(',').size
     when :WHO
       # I'm too lazy to implement this one correctly
       penalty += 5
@@ -85,12 +85,13 @@ module Irc
     end
 
     def length
-      length = 0
+      len = 0
       @storage.each {|c|
-        length += c[1].length 
+        len += c[1].size
       }
-      return length
+      return len
     end
+    alias :size :length
 
     def empty?
       @storage.empty?
@@ -113,7 +114,7 @@ module Irc
         return nil
       end
       save_idx = @last_idx
-      @last_idx = (@last_idx + 1) % @storage.length
+      @last_idx = (@last_idx + 1) % @storage.size
       mess = @storage[@last_idx][1].first
       @last_idx = save_idx
       return mess
@@ -124,7 +125,7 @@ module Irc
         warning "trying to access empty ring"
         return nil
       end
-      @last_idx = (@last_idx + 1) % @storage.length
+      @last_idx = (@last_idx + 1) % @storage.size
       mess = @storage[@last_idx][1].shift
       @storage.delete(@storage[@last_idx]) if @storage[@last_idx][1] == []
       return mess
@@ -180,10 +181,11 @@ module Irc
     def length
       len = 0
       @rings.each { |r|
-        len += r.length
+        len += r.size
       }
       len
     end
+    alias :size :length
 
     def next
       if empty?
@@ -195,8 +197,8 @@ module Irc
         mess = @rings[0].first
       else
         save_ring = @last_ring
-        (@rings.length - 1).times {
-          @last_ring = (@last_ring % (@rings.length - 1)) + 1
+        (@rings.size - 1).times {
+          @last_ring = (@last_ring % (@rings.size - 1)) + 1
           if !@rings[@last_ring].empty?
             mess = @rings[@last_ring].next
             break
@@ -217,8 +219,8 @@ module Irc
       if !@rings[0].empty?
         return @rings[0].shift
       end
-      (@rings.length - 1).times {
-        @last_ring = (@last_ring % (@rings.length - 1)) + 1
+      (@rings.size - 1).times {
+        @last_ring = (@last_ring % (@rings.size - 1)) + 1
         if !@rings[@last_ring].empty?
           return @rings[@last_ring].shift
         end
@@ -256,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)
@@ -269,6 +291,7 @@ module Irc
       @port = port.to_i
       @host = host
       @sock = nil
+      @filter = IdentityFilter.new
       @spooler = false
       @lines_sent = 0
       @lines_received = 0
@@ -318,13 +341,13 @@ module Irc
         @sock=TCPSocket.new(@server, @port)
       end
       if(@ssl)
-       require 'openssl'
-       ssl_context = OpenSSL::SSL::SSLContext.new()
-       ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
-       @rawsock = @sock
-       @sock = OpenSSL::SSL::SSLSocket.new(@sock, ssl_context)
-       @sock.sync_close = true
-       @sock.connect
+        require 'openssl'
+        ssl_context = OpenSSL::SSL::SSLContext.new()
+        ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
+        @rawsock = @sock
+        @sock = OpenSSL::SSL::SSLSocket.new(@rawsock, ssl_context)
+        @sock.sync_close = true
+        @sock.connect
       end
       @qthread = false
       @qmutex = Mutex.new
@@ -362,6 +385,15 @@ module Irc
       end
     end
 
+    def handle_socket_error(string, err)
+      error "#{string} failed: #{err.inspect}"
+      debug err.backtrace.join("\n")
+      # We assume that an error means that there are connection
+      # problems and that we should reconnect, so we
+      shutdown
+      raise SocketError.new(err.inspect)
+    end
+
     # get the next line from the server (blocks)
     def gets
       if @sock.nil?
@@ -369,15 +401,13 @@ 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}"
         return reply
       rescue => e
-        warning "socket get failed: #{e.inspect}"
-        debug e.backtrace.join("\n")
-        return nil
+        handle_socket_error(:RECV, e)
       end
     end
 
@@ -413,8 +443,8 @@ module Irc
             return
           end
           @flood_send = now if @flood_send < now
-          debug "can send #{@sendq_burst - @burst} lines, there are #{@sendq.length} to send"
-         while !@sendq.empty? and @burst < @sendq_burst and @flood_send - now < MAX_IRC_SEND_PENALTY
+          debug "can send #{@sendq_burst - @burst} lines, there are #{@sendq.size} to send"
+          while !@sendq.empty? and @burst < @sendq_burst and @flood_send - now < MAX_IRC_SEND_PENALTY
             debug "sending message (#{@flood_send - now} < #{MAX_IRC_SEND_PENALTY})"
             puts_critical(@sendq.shift, true)
           end
@@ -452,13 +482,15 @@ module Irc
 
     # shutdown the connection to the server
     def shutdown(how=2)
-      if(@ssl)
-       @rawsock.shutdown(how) unless @rawsock.nil?
-       @rawsock = nil
-      else
-       @sock.shutdown(how) unless @sock.nil?
-       @sock = nil
+      return unless connected?
+      begin
+        @sock.close
+      rescue => err
+        error "error while shutting down: #{err.inspect}"
+        debug err.backtrace.join("\n")
       end
+      @rawsock = nil if @ssl
+      @sock = nil
       @burst = 0
     end
 
@@ -472,15 +504,14 @@ module Irc
         if @sock.nil?
           error "SEND attempted on closed socket"
         else
-          @sock.puts(message + "\n",0)
+          @sock.puts(@filter.out(message))
           @last_send = Time.new
           @flood_send += message.irc_send_penalty if penalty
           @lines_sent += 1
           @burst += 1
         end
       rescue => e
-        error "SEND failed: #{e.inspect}"
-       raise
+        handle_socket_error(:SEND, e)
       end
     end