]> 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 7c0253a5e8c966de1b300d02f8b882a6c0525123..029d1ca54026353518eab0b402e8a72a3b2670a7 100644 (file)
@@ -48,7 +48,7 @@ class ::String
       dests = pars.split($;,2).first
       penalty += dests.split(',').size
     when :WHO
-      args = parts.split
+      args = pars.split
       if args.length > 0
         penalty += args.inject(0){ |sum,x| sum += ((x.length > 4) ? 3 : 5) }
       else
@@ -303,20 +303,30 @@ 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'
@@ -401,6 +411,7 @@ module Irc
         error "error while shutting down: #{e.pretty_inspect}"
       end
       @sock = nil
+      @server_uri = nil
       @sendq.clear
     end