]> 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 23f29086d21bb4d3eed0df955e2d9089b2541dca..5163e0ef8fb3127dce665d8d851560ee97a43707 100644 (file)
@@ -1,3 +1,67 @@
+class ::String
+  # Calculate the penalty which will be assigned to this message
+  # by the IRCd
+  def irc_send_penalty
+    # According to eggrdop, the initial penalty is
+    penalty = 1 + self.size/100
+    # on everything but UnderNET where it's
+    # penalty = 2 + self.size/120
+
+    cmd, pars = self.split($;,2)
+    debug "cmd: #{cmd}, pars: #{pars.inspect}"
+    case cmd.to_sym
+    when :KICK
+      chan, nick, msg = pars.split
+      chan = chan.split(',')
+      nick = nick.split(',')
+      penalty += nick.size
+      penalty *= chan.size
+    when :MODE
+      chan, modes, argument = pars.split
+      extra = 0
+      if modes
+        extra = 1
+        if argument
+          extra += modes.split(/\+|-/).size
+        else
+          extra += 3 * modes.split(/\+|-/).size
+        end
+      end
+      if argument
+        extra += 2 * argument.split.size
+      end
+      penalty += extra * chan.split.size
+    when :TOPIC
+      penalty += 1
+      penalty += 2 unless pars.split.size < 2
+    when :PRIVMSG, :NOTICE
+      dests = pars.split($;,2).first
+      penalty += dests.split(',').size
+    when :WHO
+      # I'm too lazy to implement this one correctly
+      penalty += 5
+    when :AWAY, :JOIN, :VERSION, :TIME, :TRACE, :WHOIS, :DNS
+      penalty += 2
+    when :INVITE, :NICK
+      penalty += 3
+    when :ISON
+      penalty += 1
+    else # Unknown messages
+      penalty += 1
+    end
+    if penalty > 99
+      debug "Wow, more than 99 secs of penalty!"
+      penalty = 99
+    end
+    if penalty < 2
+      debug "Wow, less than 2 secs of penalty!"
+      penalty = 2
+    end
+    debug "penalty: #{penalty}"
+    return penalty
+  end
+end
+
 module Irc
 
   require 'socket'
@@ -21,8 +85,13 @@ module Irc
     end
 
     def length
-      @storage.length
+      len = 0
+      @storage.each {|c|
+        len += c[1].size
+      }
+      return len
     end
+    alias :size :length
 
     def empty?
       @storage.empty?
@@ -45,10 +114,10 @@ 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
-      mess
+      return mess
     end
 
     def shift
@@ -56,10 +125,10 @@ 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] == []
-      mess
+      return mess
     end
 
   end
@@ -112,22 +181,24 @@ module Irc
     def length
       len = 0
       @rings.each { |r|
-        len += r.length
+        len += r.size
       }
-      len  
+      len
     end
+    alias :size :length
 
     def next
       if empty?
         warning "trying to access empty ring"
         return nil
       end
+      mess = nil
       if !@rings[0].empty?
         mess = @rings[0].first
       else
         save_ring = @last_ring
-        (@rings.length - 1).times {
-          @last_ring = ((@last_ring + 1) % (@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
@@ -135,6 +206,7 @@ module Irc
         }
         @last_ring = save_ring
       end
+      error "nil message" if mess.nil?
       return mess
     end
 
@@ -143,15 +215,18 @@ module Irc
         warning "trying to access empty ring"
         return nil
       end
+      mess = nil
       if !@rings[0].empty?
         return @rings[0].shift
       end
-      (@rings.length - 1).times {
-        @last_ring = ((@last_ring + 1) % (@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
       }
+      error "nil message" if mess.nil?
+      return mess
     end
 
   end
@@ -159,6 +234,9 @@ module Irc
   # wrapped TCPSocket for communication with the server.
   # emulates a subset of TCPSocket functionality
   class IrcSocket
+
+    MAX_IRC_SEND_PENALTY = 10
+
     # total number of lines sent to the irc server
     attr_reader :lines_sent
 
@@ -174,21 +252,37 @@ module Irc
     # accumulator for the throttle
     attr_reader :throttle_bytes
 
-    # byterate components
-    attr_reader :bytes_per
-    attr_reader :seconds_per
-
     # delay between lines sent
     attr_reader :sendq_delay
 
     # 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)
     # create a new IrcSocket
-    def initialize(server, port, host, sendq_delay=2, sendq_burst=4, brt="400/2")
+    def initialize(server, port, host, sendq_delay=2, sendq_burst=4, opts={})
       @timer = Timer::Timer.new
       @timer.add(0.2) do
         spool
@@ -197,15 +291,23 @@ module Irc
       @port = port.to_i
       @host = host
       @sock = nil
+      @filter = IdentityFilter.new
       @spooler = false
       @lines_sent = 0
       @lines_received = 0
+      if opts.kind_of?(Hash) and opts.key?(:ssl)
+        @ssl = opts[:ssl]
+      else
+        @ssl = false
+      end
+
       if sendq_delay
         @sendq_delay = sendq_delay.to_f
       else
         @sendq_delay = 2
       end
       @last_send = Time.new - @sendq_delay
+      @flood_send = Time.new
       @last_throttle = Time.new
       @burst = 0
       if sendq_burst
@@ -213,23 +315,6 @@ module Irc
       else
         @sendq_burst = 4
       end
-      @bytes_per = 400
-      @seconds_per = 2
-      @throttle_bytes = 0
-      @throttle_div = 1
-      setbyterate(brt)
-    end
-
-    def setbyterate(brt)
-      if brt.match(/(\d+)\/(\d)/)
-        @bytes_per = $1.to_i
-        @seconds_per = $2.to_i
-        debug "Byterate now #{byterate}"
-        return true
-      else
-        debug "Couldn't set byterate #{brt}"
-        return false
-      end
     end
 
     def connected?
@@ -255,6 +340,15 @@ module Irc
       else
         @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(@rawsock, ssl_context)
+        @sock.sync_close = true
+        @sock.connect
+      end
       @qthread = false
       @qmutex = Mutex.new
       @sendq = MessageQueue.new
@@ -279,36 +373,6 @@ module Irc
       end
     end
 
-    def byterate
-      return "#{@bytes_per}/#{@seconds_per}"
-    end
-
-    def byterate=(newrate)
-      @qmutex.synchronize do
-        setbyterate(newrate)
-      end
-    end
-
-    def run_throttle(more=0)
-      now = Time.new
-      if @throttle_bytes > 0
-        # If we ever reach the limit, we halve the actual allowed byterate
-        # until we manage to reset the throttle.
-        if @throttle_bytes >= @bytes_per
-          @throttle_div = 0.5
-        end
-        delta = ((now - @last_throttle)*@throttle_div*@bytes_per/@seconds_per).floor
-        if delta > 0
-          @throttle_bytes -= delta
-          @throttle_bytes = 0 if @throttle_bytes < 0
-          @last_throttle = now
-        end
-      else
-        @throttle_div = 1
-      end
-      @throttle_bytes += more
-    end
-
     # used to send lines to the remote IRCd by skipping the queue
     # message: IRC message to send
     # it should only be used for stuff that *must not* be queued,
@@ -321,21 +385,29 @@ 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?
-        debug "socket get attempted while closed"
+        warning "socket get attempted while closed"
         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
-        debug "socket get failed: #{e.inspect}"
-        return nil
+        handle_socket_error(:RECV, e)
       end
     end
 
@@ -354,41 +426,34 @@ module Irc
     # pop a message off the queue, send it
     def spool
       @qmutex.synchronize do
-        debug "in spooler"
-        if @sendq.empty?
-          @timer.stop
-          return
-        end
-        now = Time.new
-        if (now >= (@last_send + @sendq_delay))
-          # reset burst counter after @sendq_delay has passed
-          debug "resetting @burst"
-          @burst = 0
-        elsif (@burst >= @sendq_burst)
-          # nope. can't send anything, come back to us next tick...
-          debug "can't send yet"
-          @timer.start
-          return
-        end
-        # debug "Queue: #{@sendq.inspect}"
-        debug "can send #{@sendq_burst - @burst} lines, there are #{@sendq.length} to send"
-        (@sendq_burst - @burst).times do
-          break if @sendq.empty?
-          mess = @sendq.next
-          # debug "Next message is #{mess.inspect}"
-          if @throttle_bytes == 0 or mess.length+@throttle_bytes < @bytes_per
-            debug "flood protection: sending message of length #{mess.length}"
-            debug "(byterate: #{byterate}, throttle bytes: #{@throttle_bytes})"
-            puts_critical(@sendq.shift)
-          else
-            debug "flood protection: throttling message of length #{mess.length}"
-            debug "(byterate: #{byterate}, throttle bytes: #{@throttle_bytes})"
-            run_throttle
-            break
+        begin
+          debug "in spooler"
+          if @sendq.empty?
+            @timer.stop
+            return
           end
-        end
-        if @sendq.empty?
-          @timer.stop
+          now = Time.new
+          if (now >= (@last_send + @sendq_delay))
+            debug "resetting @burst"
+            @burst = 0
+          elsif (@burst > @sendq_burst)
+            # nope. can't send anything, come back to us next tick...
+            debug "can't send yet"
+            @timer.start
+            return
+          end
+          @flood_send = now if @flood_send < now
+          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
+          if @sendq.empty?
+            @timer.stop
+          end
+        rescue => e
+          error "Spooling failed: #{e.inspect}"
+          error e.backtrace.join("\n")
         end
       end
     end
@@ -401,7 +466,7 @@ module Irc
           end
         end
       else
-        debug "Clearing socket while disconnected"
+        warning "Clearing socket while disconnected"
       end
     end
 
@@ -417,7 +482,14 @@ module Irc
 
     # shutdown the connection to the server
     def shutdown(how=2)
-      @sock.shutdown(how) unless @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
@@ -425,21 +497,21 @@ module Irc
     private
 
     # same as puts, but expects to be called with a mutex held on @qmutex
-    def puts_critical(message)
+    def puts_critical(message, penalty=false)
       # debug "in puts_critical"
       begin
         debug "SEND: #{message.inspect}"
         if @sock.nil?
           error "SEND attempted on closed socket"
         else
-          @sock.send(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
-          run_throttle(message.length + 1)
         end
       rescue => e
-        error "SEND failed: #{e.inspect}"
+        handle_socket_error(:SEND, e)
       end
     end