]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/message.rb
New Auth Framework: no need to have TransientBotUser as a separate class
[user/henk/code/ruby/rbot.git] / lib / rbot / message.rb
index 42042b88a2884b2d367ba5a3292cfed5cc87baf0..6331825de66d512df0cb21da9c65d35be9adc2fa 100644 (file)
@@ -1,3 +1,8 @@
+#-- vim:sw=2:et
+#++
+#
+# :title: IRC message datastructures
+
 module Irc
   BotConfig.register BotConfigArrayValue.new('core.address_prefix',
     :default => [], :wizard => true,
@@ -150,15 +155,22 @@ module Irc
     # Access the nick of the source
     #
     def sourcenick
-      @source.nick
+      @source.nick rescue @source.to_s
     end
 
     # Access the user@host of the source
     #
     def sourceaddress
-      "#{@source.user}@#{@source.host}"
+      "#{@source.user}@#{@source.host}" rescue @source.to_s
+    end
+
+    # Access the botuser corresponding to the source, if any
+    #
+    def botuser
+      @bot.auth.autologin(@source) rescue @bot.auth.everyone
     end
 
+
     # Was the message from an identified user?
     def identified?
       return @identified
@@ -208,6 +220,12 @@ module Irc
     # channel the message was in, nil for privately addressed messages
     attr_reader :channel
 
+    # for PRIVMSGs, false unless the message was a CTCP command,
+    # in which case it evaluates to the CTCP command itself
+    # (TIME, PING, VERSION, etc). The CTCP command parameters
+    # are then stored in the message.
+    attr_reader :ctcp
+
     # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
     # will be stripped from the message)
     attr_reader :action
@@ -222,6 +240,7 @@ module Irc
       @target = target
       @private = false
       @plugin = nil
+      @ctcp = false
       @action = false
 
       if target == @bot.myself
@@ -250,9 +269,13 @@ module Irc
         @address = true
       end
 
-      if(@message =~ /^\001ACTION\s(.+)\001/)
-        @message = $1
-        @action = true
+      if(@message =~ /^\001(\S+)(\s(.+))?\001/)
+        @ctcp = $1
+       # FIXME need to support quoting of NULL and CR/LF, see
+       # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
+        @message = $3 || String.new
+        @action = @ctcp == 'ACTION'
+        debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
       end
 
       # free splitting for plugins
@@ -315,6 +338,12 @@ module Irc
       @replied = true
     end
 
+    # send a CTCP response, i.e. a private NOTICE to the sender
+    # with the same CTCP command and the reply as a parameter
+    def ctcp_reply(string, options={})
+      @bot.ctcp_notice @source, @ctcp, string, options
+    end
+
     # convenience method to reply "okay" in the current language to the
     # message
     def plainokay