X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fmessage.rb;h=35100802b8d640db409b90d876fc54a2ed147900;hb=fdf1bc954352f19818f5f9f1c86643a2f8ef40c6;hp=c671e00a672e7bdce7213a0baa86a4bf22f4cc73;hpb=37b0d5ebb084e79f9555bb4e391cfda382c46356;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/message.rb b/lib/rbot/message.rb index c671e00a..35100802 100644 --- a/lib/rbot/message.rb +++ b/lib/rbot/message.rb @@ -1,18 +1,30 @@ +#-- vim:sw=2:et +#++ +# +# :title: IRC message datastructures + module Irc - BotConfig.register BotConfigArrayValue.new('core.address_prefix', - :default => [], :wizard => true, - :desc => "what non nick-matching prefixes should the bot respond to as if addressed (e.g !, so that '!foo' is treated like 'rbot: foo')" - ) - BotConfig.register BotConfigBooleanValue.new('core.reply_with_nick', - :default => false, :wizard => true, - :desc => "if true, the bot will prepend the nick to what he has to say when replying (e.g. 'markey: you can't do that!')" - ) - BotConfig.register BotConfigStringValue.new('core.nick_postfix', - :default => ':', :wizard => true, - :desc => "when replying with nick put this character after the nick of the user the bot is replying to" - ) + class Bot + module Config + Config.register ArrayValue.new('core.address_prefix', + :default => [], :wizard => true, + :desc => "what non nick-matching prefixes should the bot respond to as if addressed (e.g !, so that '!foo' is treated like 'rbot: foo')" + ) + + Config.register BooleanValue.new('core.reply_with_nick', + :default => false, :wizard => true, + :desc => "if true, the bot will prepend the nick to what he has to say when replying (e.g. 'markey: you can't do that!')" + ) + + Config.register StringValue.new('core.nick_postfix', + :default => ':', :wizard => true, + :desc => "when replying with nick put this character after the nick of the user the bot is replying to" + ) + end + end + # Define standard IRC attriubtes (not so standard actually, # but the closest thing we have ...) @@ -110,6 +122,9 @@ module Irc # contents of the message attr_accessor :message + # contents of the message (for logging purposes) + attr_accessor :logmessage + # has the message been replied to/handled by a plugin? attr_accessor :replied @@ -140,6 +155,7 @@ module Irc warning "Message does not have identification" end end + @logmessage = @message.dup if target && target == @bot.myself @address = true @@ -150,26 +166,22 @@ module Irc # Access the nick of the source # def sourcenick - if @source.reply_to?(:nick) - @source.nick - else - @source.to_s - end + @source.nick rescue @source.to_s end # Access the user@host of the source # def sourceaddress - str = String.new - if @source.reply_to?(:user) - str << @source.user.to_s - end - if @source.reply_to?(:host) - str << @source.host.to_s - end - str + "#{@source.user}@#{@source.host}" rescue @source.to_s end + # Access the botuser corresponding to the source, if any + # + def botuser + source.botuser rescue @bot.auth.everyone + end + + # Was the message from an identified user? def identified? return @identified @@ -219,6 +231,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 @@ -233,6 +251,7 @@ module Irc @target = target @private = false @plugin = nil + @ctcp = false @action = false if target == @bot.myself @@ -261,9 +280,14 @@ 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})" + @logmessage = @message.dup end # free splitting for plugins @@ -310,7 +334,7 @@ module Irc # the nick or core.reply_with_nick is set to false # def reply(string, options={}) - if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/ + if @bot.config['core.reply_with_nick'] and not string =~ /\b#{Regexp.escape(@source.to_s)}\b/ return nickreply(string, options) end plainreply(string, options) @@ -326,6 +350,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 @@ -352,6 +382,12 @@ module Irc plainokay end + # send a NOTICE to the message source + # + def notify(msg,opts={}) + @bot.notice(sourcenick, msg, opts) + end + end # class to manage IRC PRIVMSGs