]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircsocket.rb
Fix IRCification of HTML strings when only whitespace is present between IRC bold...
[user/henk/code/ruby/rbot.git] / lib / rbot / ircsocket.rb
index ccc751f7cbccef7b096e782cbb7430a092d7a25b..e70528ea6cf0412aba54f459ba3e290f66004081 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.length/100
+    # on everything but UnderNET where it's
+    # penalty = 2 + self.length/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.length
+      penalty *= chan.length
+    when :MODE
+      chan, modes, argument = pars.split
+      extra = 0
+      if modes
+        extra = 1
+        if argument
+          extra += modes.split(/\+|-/).length
+        else
+          extra += 3 * modes.split(/\+|-/).length
+        end
+      end
+      if argument
+        extra += 2 * argument.split.length
+      end
+      penalty += extra * chan.split.length
+    when :TOPIC
+      penalty += 1
+      penalty += 2 unless pars.split.length < 2
+    when :PRIVMSG, :NOTICE
+      dests = pars.split($;,2).first
+      penalty += dests.split(',').length
+    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'
@@ -168,6 +232,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
 
@@ -183,10 +250,6 @@ 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
 
@@ -197,7 +260,7 @@ module Irc
     # 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
@@ -209,12 +272,19 @@ module Irc
       @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
@@ -222,23 +292,6 @@ module Irc
       else
         @sendq_burst = 4
       end
-      @bytes_per = 400
-      @seconds_per = 2
-      @throttle_bytes = 0
-      @hit_limit = 0 # how many times did we reach the limit?
-      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?
@@ -264,6 +317,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
@@ -288,38 +350,6 @@ module Irc
       end
     end
 
-    def byterate
-      return "#{@bytes_per}/#{@seconds_per} (limit hit #{@hit_limit} times)"
-    end
-
-    def byterate=(newrate)
-      @qmutex.synchronize do
-        setbyterate(newrate)
-      end
-    end
-
-    def run_throttle(more=0)
-      now = Time.new
-      # Each time we reach the limit, we reduce the bitrate. We reset the bitrate only if the throttle
-      # manages to reset twice. This way we have better flood control, although the really perfect way
-      # would be to calculate our penalty the way it's done serverside.
-      if @throttle_bytes > 0
-        if @throttle_bytes >= @bytes_per
-          @hit_limit += 1
-          @hit_limit = 3 if @hit_limit > 3
-        end
-        delta = ((now - @last_throttle)*(0.5**@hit_limit.ceil)*@bytes_per/@seconds_per).floor
-        if delta > 0
-          @throttle_bytes -= delta
-          @throttle_bytes = 0 if @throttle_bytes < 0
-          @last_throttle = now
-        end
-      else
-        @hit_limit -= 0.5 if @hit_limit > 0
-      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,
@@ -332,6 +362,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?
@@ -345,9 +384,7 @@ module Irc
         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
 
@@ -374,30 +411,19 @@ module Irc
           end
           now = Time.new
           if (now >= (@last_send + @sendq_delay))
-            # after @sendq_delay has passed, we allow more @burst
-            # instead of resetting it to 0, we reduce it by 1
-            debug "decreasing @burst"
-            @burst -= 1 if @burst > 0
-          elsif (@burst >= @sendq_burst)
+            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.length} to send"
-          (@sendq_burst - @burst).times do
-            break if @sendq.empty?
-            mess = @sendq.next
-            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
-            end
+          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
@@ -433,7 +459,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
@@ -441,22 +474,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 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}"
-       raise
+        handle_socket_error(:SEND, e)
       end
     end