]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircsocket.rb
Solve Socket vs URI IPv6 handling in Ruby
[user/henk/code/ruby/rbot.git] / lib / rbot / ircsocket.rb
index e3586b70f49f07a0c1407e90f518070a097d7a61..029d1ca54026353518eab0b402e8a72a3b2670a7 100644 (file)
@@ -1,8 +1,18 @@
+#-- vim:sw=2:et
+#++
+#
+# :title: IRC Socket
+#
+# This module implements the IRC socket interface, including IRC message
+# penalty computation and the message queue system
+
+require 'monitor'
+
 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
+    # According to eggdrop, the initial penalty is
     penalty = 1 + self.size/100
     # on everything but UnderNET where it's
     # penalty = 2 + self.size/120
@@ -38,8 +48,14 @@ class ::String
       dests = pars.split($;,2).first
       penalty += dests.split(',').size
     when :WHO
-      # I'm too lazy to implement this one correctly
-      penalty += 5
+      args = pars.split
+      if args.length > 0
+        penalty += args.inject(0){ |sum,x| sum += ((x.length > 4) ? 3 : 5) }
+      else
+        penalty += 10
+      end
+    when :PART
+      penalty += 4
     when :AWAY, :JOIN, :VERSION, :TIME, :TRACE, :WHOIS, :DNS
       penalty += 2
     when :INVITE, :NICK
@@ -66,7 +82,6 @@ module Irc
 
   require 'socket'
   require 'thread'
-  require 'rbot/timer'
 
   class QueueRing
     # A QueueRing is implemented as an array with elements in the form
@@ -134,6 +149,7 @@ module Irc
   end
 
   class MessageQueue
+
     def initialize
       # a MessageQueue is an array of QueueRings
       # rings have decreasing priority, so messages in ring 0
@@ -151,89 +167,66 @@ module Irc
       }
       # the other rings are satisfied round-robin
       @last_ring = 0
+      self.extend(MonitorMixin)
+      @non_empty = self.new_cond
     end
 
     def clear
-      @rings.each { |r|
-        r.clear
-      }
-      @last_ring = 0
+      self.synchronize do
+        @rings.each { |r| r.clear }
+        @last_ring = 0
+      end
     end
 
     def push(mess, chan=nil, cring=0)
       ring = cring
-      if ring == 0
-        warning "message #{mess} at ring 0 has channel #{chan}: channel will be ignored" if !chan.nil?
-        @rings[0] << mess
-      else
-        error "message #{mess} at ring #{ring} must have a channel" if chan.nil?
-        @rings[ring].push mess, chan
+      self.synchronize do
+        if ring == 0
+          warning "message #{mess} at ring 0 has channel #{chan}: channel will be ignored" if !chan.nil?
+          @rings[0] << mess
+        else
+          error "message #{mess} at ring #{ring} must have a channel" if chan.nil?
+          @rings[ring].push mess, chan
+        end
+        @non_empty.signal
+      end
+    end
+
+    def shift(tmout = nil)
+      self.synchronize do
+        @non_empty.wait(tmout) if self.empty?
+        return unsafe_shift
       end
     end
 
+    protected
+
     def empty?
-      @rings.each { |r|
-        return false unless r.empty?
-      }
-      return true
+      !@rings.find { |r| !r.empty? }
     end
 
     def length
-      len = 0
-      @rings.each { |r|
-        len += r.size
-      }
-      len
+      @rings.inject(0) { |s, r| s + r.size }
     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.size - 1).times {
-          @last_ring = (@last_ring % (@rings.size - 1)) + 1
-          if !@rings[@last_ring].empty?
-            mess = @rings[@last_ring].next
-            break
-          end
-        }
-        @last_ring = save_ring
-      end
-      error "nil message" if mess.nil?
-      return mess
-    end
-
-    def shift
-      if empty?
-        warning "trying to access empty ring"
-        return nil
-      end
-      mess = nil
+    def unsafe_shift
       if !@rings[0].empty?
         return @rings[0].shift
       end
-      (@rings.size - 1).times {
+      (@rings.size - 1).times do
         @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
+        return @rings[@last_ring].shift unless @rings[@last_ring].empty?
+      end
+      warning "trying to access an empty message queue"
+      return nil
     end
 
   end
 
   # wrapped TCPSocket for communication with the server.
   # emulates a subset of TCPSocket functionality
-  class IrcSocket
+  class Socket
 
     MAX_IRC_SEND_PENALTY = 10
 
@@ -252,12 +245,6 @@ module Irc
     # accumulator for the throttle
     attr_reader :throttle_bytes
 
-    # 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
@@ -265,6 +252,9 @@ module Irc
     # normalized uri of the current server
     attr_reader :server_uri
 
+    # penalty multiplier (percent)
+    attr_accessor :penalty_pct
+
     # default trivial filter class
     class IdentityFilter
         def in(x)
@@ -283,12 +273,8 @@ module Irc
 
     # server_list:: list of servers to connect to
     # host::   optional local host to bind to (ruby 1.7+ required)
-    # create a new IrcSocket
-    def initialize(server_list, host, sendq_delay=2, sendq_burst=4, opts={})
-      @timer = Timer::Timer.new
-      @timer.add(0.2) do
-        spool
-      end
+    # create a new Irc::Socket
+    def initialize(server_list, host, opts={})
       @server_list = server_list.dup
       @server_uri = nil
       @conn_count = 0
@@ -298,26 +284,8 @@ 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
-        @sendq_burst = sendq_burst.to_i
-      else
-        @sendq_burst = 4
-      end
+      @ssl = opts[:ssl]
+      @penalty_pct = opts[:penalty_pct] || 100
     end
 
     def connected?
@@ -331,56 +299,50 @@ module Irc
         return
       end
       srv_uri = @server_list[@conn_count % @server_list.size].dup
-      srv_uri = 'irc://' + srv_uri if !srv_uri =~ /:\/\//
+      srv_uri = 'irc://' + srv_uri if !(srv_uri =~ /:\/\//)
       @conn_count += 1
       @server_uri = URI.parse(srv_uri)
       @server_uri.port = 6667 if !@server_uri.port
+
       debug "connection attempt \##{@conn_count} (#{@server_uri.host}:#{@server_uri.port})"
 
+      # if the host is a bracketed (IPv6) address, strip the brackets
+      # since Ruby doesn't like them in the Socket host parameter
+      # FIXME it would be safer to have it check for a valid
+      # IPv6 bracketed address rather than just stripping the brackets
+      srv_host = @server_uri.host
+      if srv_host.match(/\A\[(.*)\]\z/)
+        srv_host = $1
+      end
+
       if(@host)
         begin
-          @sock=TCPSocket.new(@server_uri.host, @server_uri.port, @host)
+          sock=TCPSocket.new(srv_host, @server_uri.port, @host)
         rescue ArgumentError => e
           error "Your version of ruby does not support binding to a "
           error "specific local address, please upgrade if you wish "
           error "to use HOST = foo"
           error "(this option has been disabled in order to continue)"
-          @sock=TCPSocket.new(@server_uri.host, @server_uri.port)
+          sock=TCPSocket.new(srv_host, @server_uri.port)
         end
       else
-        @sock=TCPSocket.new(@server_uri.host, @server_uri.port)
+        sock=TCPSocket.new(srv_host, @server_uri.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
+        sock = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
+        sock.sync_close = true
+        sock.connect
       end
-      @qthread = false
-      @qmutex = Mutex.new
+      @sock = sock
+      @last_send = Time.new
+      @flood_send = Time.new
+      @burst = 0
+      @sock.extend(MonitorMixin)
       @sendq = MessageQueue.new
-    end
-
-    def sendq_delay=(newfreq)
-      debug "changing sendq frequency to #{newfreq}"
-      @qmutex.synchronize do
-        @sendq_delay = newfreq
-        if newfreq == 0
-          clearq
-          @timer.stop
-        else
-          @timer.start
-        end
-      end
-    end
-
-    def sendq_burst=(newburst)
-      @qmutex.synchronize do
-        @sendq_burst = newburst
-      end
+      @qthread = Thread.new { writer_loop }
     end
 
     # used to send lines to the remote IRCd by skipping the queue
@@ -388,20 +350,19 @@ module Irc
     # it should only be used for stuff that *must not* be queued,
     # i.e. the initial PASS, NICK and USER command
     # or the final QUIT message
-    def emergency_puts(message)
-      @qmutex.synchronize do
-        # debug "In puts - got mutex"
-        puts_critical(message)
+    def emergency_puts(message, penalty = false)
+      @sock.synchronize do
+        # debug "In puts - got @sock"
+        puts_critical(message, penalty)
       end
     end
 
-    def handle_socket_error(string, err)
-      error "#{string} failed: #{err.inspect}"
-      debug err.backtrace.join("\n")
+    def handle_socket_error(string, e)
+      error "#{string} failed: #{e.pretty_inspect}"
       # We assume that an error means that there are connection
       # problems and that we should reconnect, so we
       shutdown
-      raise SocketError.new(err.inspect)
+      raise SocketError.new(e.inspect)
     end
 
     # get the next line from the server (blocks)
@@ -416,68 +377,17 @@ module Irc
         reply.strip! if reply
         debug "RECV: #{reply.inspect}"
         return reply
-      rescue => e
+      rescue Exception => e
         handle_socket_error(:RECV, e)
       end
     end
 
     def queue(msg, chan=nil, ring=0)
-      if @sendq_delay > 0
-        @qmutex.synchronize do
-          @sendq.push msg, chan, ring
-          @timer.start
-        end
-      else
-        # just send it if queueing is disabled
-        self.emergency_puts(msg)
-      end
-    end
-
-    # pop a message off the queue, send it
-    def spool
-      @qmutex.synchronize do
-        begin
-          debug "in spooler"
-          if @sendq.empty?
-            @timer.stop
-            return
-          end
-          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
+      @sendq.push msg, chan, ring
     end
 
     def clearq
-      if @sock
-        @qmutex.synchronize do
-          unless @sendq.empty?
-            @sendq.clear
-          end
-        end
-      else
-        warning "Clearing socket while disconnected"
-      end
+      @sendq.clear
     end
 
     # flush the TCPSocket
@@ -493,20 +403,42 @@ module Irc
     # shutdown the connection to the server
     def shutdown(how=2)
       return unless connected?
+      @qthread.kill
+      @qthread = nil
       begin
         @sock.close
-      rescue => err
-        error "error while shutting down: #{err.inspect}"
-        debug err.backtrace.join("\n")
+      rescue Exception => e
+        error "error while shutting down: #{e.pretty_inspect}"
       end
-      @rawsock = nil if @ssl
       @sock = nil
-      @burst = 0
+      @server_uri = nil
+      @sendq.clear
     end
 
     private
 
-    # same as puts, but expects to be called with a mutex held on @qmutex
+    def writer_loop
+      loop do
+        begin
+          now = Time.now
+          flood_delay = @flood_send - MAX_IRC_SEND_PENALTY - now
+          delay = [flood_delay, 0].max
+          if delay > 0
+            debug "sleep(#{delay}) # (f: #{flood_delay})"
+            sleep(delay)
+          end
+          msg = @sendq.shift
+          debug "got #{msg.inspect} from queue, sending"
+          emergency_puts(msg, true)
+        rescue Exception => e
+          error "Spooling failed: #{e.pretty_inspect}"
+          debug e.backtrace.join("\n")
+          raise e
+        end
+      end
+    end
+
+    # same as puts, but expects to be called with a lock held on @sock
     def puts_critical(message, penalty=false)
       # debug "in puts_critical"
       begin
@@ -514,13 +446,18 @@ module Irc
         if @sock.nil?
           error "SEND attempted on closed socket"
         else
-          @sock.puts(@filter.out(message))
-          @last_send = Time.new
-          @flood_send += message.irc_send_penalty if penalty
+          # we use Socket#syswrite() instead of Socket#puts() because
+          # the latter is racy and can cause double message output in
+          # some circumstances
+          actual = @filter.out(message) + "\n"
+          now = Time.new
+          @sock.syswrite actual
+          @last_send = now
+          @flood_send = now if @flood_send < now
+          @flood_send += message.irc_send_penalty*@penalty_pct/100.0 if penalty
           @lines_sent += 1
-          @burst += 1
         end
-      rescue => e
+      rescue Exception => e
         handle_socket_error(:SEND, e)
       end
     end