]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/rfc2812.rb
Fix casemap/server mismatch problems when moving the bots between servers with differ...
[user/henk/code/ruby/rbot.git] / lib / rbot / rfc2812.rb
index 7832f9980a403d540c7c7d591738f99d32e1a3a1..9ca6571a85b374e1f9723b0f3cd590344ab7ff71 100644 (file)
@@ -820,8 +820,8 @@ module Irc
 
     # create a new IrcClient instance
     def initialize
-      @server = Server.new      # The Server
-      @client = User.new        # The User representing the client on this Server
+      @server = Server.new         # The Server
+      @client = @server.user("")   # The User representing the client on this Server
 
       @handlers = Hash.new
 
@@ -886,16 +886,27 @@ module Irc
       prefix, command, params = $2, $3, $5
 
       if prefix != nil
-        data[:source] = prefix
-        if prefix =~ /^(\S+)!(\S+)$/
+        # Most servers will send a full nick!user@host prefix for
+        # messages from users. Therefore, when the prefix doesn't match this
+        # syntax it's usually the server hostname.
+        #
+        # This is not always true, though, since some servers do not send a
+        # full hostmask for user messages.
+        #
+        if prefix =~ /^(?:\S+)(?:!\S+)?@(?:\S+)$/
           data[:source] = @server.user(prefix)
         else
-          if @server.hostname && @server.hostname != data[:source]
-            warning "Unknown origin #{data[:source]} for message\n#{serverstring.inspect}"
+          if @server.hostname
+            if @server.hostname != prefix
+              debug "Origin #{prefix} for message\n\t#{serverstring.inspect}\nis neither a user hostmask nor the server hostname, assuming it's a nick"
+              data[:source] = @server.user(prefix)
+            else
+              data[:source] = @server
+            end
           else
             @server.instance_variable_set(:@hostname, data[:source])
+            data[:source] = @server
           end
-          data[:source] = @server
         end
       end
 
@@ -1002,13 +1013,14 @@ module Irc
 
           chan = @server.get_channel(data[:channel])
           unless chan
-            warning "Received topic #{data[:topic].inspect} for channel #{data[:channel].inspect} I was not on"
+            warning "Received names #{data[:topic].inspect} for channel #{data[:channel].inspect} I was not on"
             return
           end
 
           users = []
           argv[3].scan(/\S+/).each { |u|
-            if(u =~ /^(#{@server.supports[:prefix][:prefixes].join})?(.*)$/)
+            # FIXME beware of servers that allow multiple prefixes
+            if(u =~ /^([#{@server.supports[:prefix][:prefixes].join}])?(.*)$/)
               umode = $1
               user = $2
               users << [user, umode]
@@ -1017,11 +1029,13 @@ module Irc
 
           users.each { |ar|
             u = @server.user(ar[0])
-            chan.users << u unless chan.users.include?(u)
+            chan.add_user(u, :silent => true)
+            debug "Adding user #{u}"
             if ar[1]
               m = @server.supports[:prefix][:prefixes].index(ar[1].to_sym)
-              m = @server.supports[:prefix][:modes][m]
-              chan.mode[m.to_sym].set(u)
+              ms = @server.supports[:prefix][:modes][m]
+              debug "\twith mode #{ar[1]} (#{ms})"
+              chan.mode[ms].set(u)
             end
           }
           @tmpusers += users
@@ -1094,24 +1108,40 @@ module Irc
         # parse it yourself, or you can bind to 'MSG', 'PUBLIC',
         # etc and get it all nicely split up for you.
 
-        data[:target] = @server.user_or_channel(argv[0])
+        begin
+          data[:target] = @server.user_or_channel(argv[0])
+        rescue
+          # The previous may fail e.g. when the target is a server or something
+          # like that (e.g. $<mask>). In any of these cases, we just use the
+          # String as a target
+          # FIXME we probably want to explicitly check for the #<mask> $<mask>
+          data[:target] = argv[0]
+        end
         data[:message] = argv[1]
         handle(:privmsg, data)
 
         # Now we split it
-        if(data[:target].class <= Channel)
+        if data[:target].kind_of?(Channel)
           handle(:public, data)
         else
           handle(:msg, data)
         end
       when 'NOTICE'
-        data[:target] = @server.user_or_channel(argv[0])
+        begin
+          data[:target] = @server.user_or_channel(argv[0])
+        rescue
+          # The previous may fail e.g. when the target is a server or something
+          # like that (e.g. $<mask>). In any of these cases, we just use the
+          # String as a target
+          # FIXME we probably want to explicitly check for the #<mask> $<mask>
+          data[:target] = argv[0]
+        end
         data[:message] = argv[1]
         case data[:source]
         when User
           handle(:notice, data)
         else
-          # "server notice" (not from user, noone to reply to
+          # "server notice" (not from user, noone to reply to)
           handle(:snotice, data)
         end
       when 'KICK'
@@ -1138,7 +1168,8 @@ module Irc
       when 'QUIT'
         data[:message] = argv[0]
         data[:was_on] = @server.channels.inject(ChannelList.new) { |list, ch|
-          list << ch if ch.users.include?(data[:source])
+          list << ch if ch.has_user?(data[:source])
+          list
         }
 
         @server.delete_user(data[:source])
@@ -1146,12 +1177,12 @@ module Irc
         handle(:quit, data)
       when 'JOIN'
         data[:channel] = @server.channel(argv[0])
-        data[:channel].users << data[:source]
+        data[:channel].add_user(data[:source])
 
         handle(:join, data)
       when 'TOPIC'
         data[:channel] = @server.channel(argv[0])
-        data[:topic] = ChannelTopic.new(argv[1], data[:source], Time.new)
+        data[:topic] = Channel::Topic.new(argv[1], data[:source], Time.new)
         data[:channel].topic.replace(data[:topic])
 
         handle(:changetopic, data)
@@ -1162,7 +1193,8 @@ module Irc
         handle(:invite, data)
       when 'NICK'
         data[:is_on] = @server.channels.inject(ChannelList.new) { |list, ch|
-          list << ch if ch.users.include?(data[:source])
+          list << ch if ch.has_user?(data[:source])
+          list
         }
 
         data[:newnick] = argv[0]
@@ -1184,6 +1216,7 @@ module Irc
         case data[:channel]
         when User
           # TODO
+          warn "Unhandled user mode message '#{serverstring}'"
         else
           # data[:modes] is an array where each element
           # is either a flag which doesn't need parameters
@@ -1219,13 +1252,13 @@ module Irc
                   data[:modes] << [setting + m]
                   who_wants_params << data[:modes].length - 1
                 else
-                  warn "Unknown mode #{m} in #{serverstring}"
+                  warn "Unknown mode #{m} in #{serverstring.inspect}"
                 end
               }
             else
               idx = who_wants_params.shift
               if idx.nil?
-                warn "Oops, problems parsing #{serverstring}"
+                warn "Oops, problems parsing #{serverstring.inspect}"
                 break
               end
               data[:modes][idx] << arg
@@ -1249,6 +1282,7 @@ module Irc
 
         handle(:mode, data)
       else
+        warn "Unknown message #{serverstring.inspect}"
         handle(:unknown, data)
       end
     end