]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircsocket.rb
plugin(points): forgot one special case, see #34
[user/henk/code/ruby/rbot.git] / lib / rbot / ircsocket.rb
index 496486bb6667591fa5b7175bef13648b38cfa4a2..e5131c2b6b3edbeb3f542f358a698e90bbd027bd 100644 (file)
@@ -48,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
@@ -246,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)
@@ -275,11 +284,11 @@ 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
+      @ssl = opts[:ssl]
+      @ssl_verify = opts[:ssl_verify]
+      @ssl_ca_file = opts[:ssl_ca_file]
+      @ssl_ca_path = opts[:ssl_ca_path]
+      @penalty_pct = opts[:penalty_pct] || 100
     end
 
     def connected?
@@ -297,25 +306,41 @@ module Irc
       @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
+        if @ssl_verify
+          ssl_context.ca_file = @ssl_ca_file if @ssl_ca_file and not @ssl_ca_file.empty?
+          ssl_context.ca_path = @ssl_ca_path if @ssl_ca_path and not @ssl_ca_path.empty?
+          ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER 
+        else
+          ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
+        end
         sock = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
         sock.sync_close = true
         sock.connect
@@ -395,6 +420,7 @@ module Irc
         error "error while shutting down: #{e.pretty_inspect}"
       end
       @sock = nil
+      @server_uri = nil
       @sendq.clear
     end
 
@@ -437,7 +463,7 @@ module Irc
           @sock.syswrite actual
           @last_send = now
           @flood_send = now if @flood_send < now
-          @flood_send += message.irc_send_penalty if penalty
+          @flood_send += message.irc_send_penalty*@penalty_pct/100.0 if penalty
           @lines_sent += 1
         end
       rescue Exception => e