]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircsocket.rb
use pretty printing with Logger (debug, info, warning and error messages)
[user/henk/code/ruby/rbot.git] / lib / rbot / ircsocket.rb
index 973ae9b85f4e3c5d085b826fd5b7fb7b30c0bdf8..dcc501a36657fe9ba2241f1506d8707d989bee17 100644 (file)
@@ -258,19 +258,43 @@ module Irc
     # max lines to burst
     attr_reader :sendq_burst
 
-    # server:: server to connect to
-    # port::   IRCd port
+    # an optional filter object. we call @filter.in(data) for
+    # all incoming data and @filter.out(data) for all outgoing data
+    attr_reader :filter
+
+    # normalized uri of the current server
+    attr_reader :server_uri
+
+    # 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_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, port, host, sendq_delay=2, sendq_burst=4, opts={})
+    def initialize(server_list, host, sendq_delay=2, sendq_burst=4, opts={})
       @timer = Timer::Timer.new
       @timer.add(0.2) do
         spool
       end
-      @server = server.dup
-      @port = port.to_i
+      @server_list = server_list.dup
+      @server_uri = nil
+      @conn_count = 0
       @host = host
       @sock = nil
+      @filter = IdentityFilter.new
       @spooler = false
       @lines_sent = 0
       @lines_received = 0
@@ -306,18 +330,25 @@ module Irc
         warning "reconnecting while connected"
         return
       end
+      srv_uri = @server_list[@conn_count % @server_list.size].dup
+      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(@host)
         begin
-          @sock=TCPSocket.new(@server, @port, @host)
+          @sock=TCPSocket.new(@server_uri.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, @port)
+          @sock=TCPSocket.new(@server_uri.host, @server_uri.port)
         end
       else
-        @sock=TCPSocket.new(@server, @port)
+        @sock=TCPSocket.new(@server_uri.host, @server_uri.port)
       end
       if(@ssl)
         require 'openssl'
@@ -364,13 +395,12 @@ module Irc
       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)
@@ -380,12 +410,12 @@ module Irc
         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
+      rescue Exception => e
         handle_socket_error(:RECV, e)
       end
     end
@@ -430,9 +460,8 @@ module Irc
           if @sendq.empty?
             @timer.stop
           end
-        rescue => e
-          error "Spooling failed: #{e.inspect}"
-          error e.backtrace.join("\n")
+        rescue Exception => e
+          error "Spooling failed: #{e.pretty_inspect}"
         end
       end
     end
@@ -464,9 +493,8 @@ module Irc
       return unless connected?
       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
@@ -483,13 +511,13 @@ module Irc
         if @sock.nil?
           error "SEND attempted on closed socket"
         else
-          @sock.puts message
+          @sock.puts(@filter.out(message))
           @last_send = Time.new
           @flood_send += message.irc_send_penalty if penalty
           @lines_sent += 1
           @burst += 1
         end
-      rescue => e
+      rescue Exception => e
         handle_socket_error(:SEND, e)
       end
     end