Class | Irc::UserMessage |
In: |
lib/rbot/message.rb
|
Parent: | BasicUserMessage |
class for handling IRC user messages. Includes some utilities for handling the message, for example in plugins. The message member will have any bot addressing "^bot: " removed (address? will return true in this case)
action | [R] | for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff will be stripped from the message) |
channel | [R] | channel the message was in, nil for privately addressed messages |
ctcp | [R] | 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. |
params | [R] | for plugin messages, the rest of the message, with the plugin name removed |
plugin | [R] | for plugin messages, the name of the plugin invoked by the message |
replyto | [R] | convenience member. Who to reply to (i.e. would be sourcenick for a privately addressed message, or target (the channel) for a publicly addressed message |
instantiate a new UserMessage
bot: | associated bot class |
source: | hostmask of the message source |
target: | nick/channel message is destined for |
message: | message part |
# File lib/rbot/message.rb, line 333 333: def initialize(bot, server, source, target, message) 334: super(bot, server, source, target, message) 335: @target = target 336: @private = false 337: @plugin = nil 338: @ctcp = false 339: @action = false 340: 341: if target == @bot.myself 342: @private = true 343: @address = true 344: @channel = nil 345: @replyto = source 346: else 347: @replyto = @target 348: @channel = @target 349: end 350: 351: # check for option extra addressing prefixes, e.g "|search foo", or 352: # "!version" - first match wins 353: bot.config['core.address_prefix'].each {|mprefix| 354: if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "") 355: @address = true 356: @prefixed = true 357: break 358: end 359: } 360: 361: # even if they used above prefixes, we allow for silly people who 362: # combine all possible types, e.g. "|rbot: hello", or 363: # "/msg rbot rbot: hello", etc 364: if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "") 365: @address = true 366: end 367: 368: if(@message =~ /^\001(\S+)(\s(.+))?\001/) 369: @ctcp = $1 370: # FIXME need to support quoting of NULL and CR/LF, see 371: # http://www.irchelp.org/irchelp/rfc/ctcpspec.html 372: @message = $3 || String.new 373: @action = @ctcp == 'ACTION' 374: debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})" 375: @logmessage = @message.dup 376: @plainmessage = BasicUserMessage.strip_formatting(@message) 377: @message = BasicUserMessage.strip_initial_formatting(@message) 378: end 379: 380: # free splitting for plugins 381: @params = @message.dup 382: # Created messges (such as by fake_message) can contain multiple lines 383: if @params.gsub!(/\A\s*(\S+)[\s$]*/m, "") 384: @plugin = $1.downcase 385: @params = nil unless @params.length > 0 386: end 387: end
convenience method to reply to a message with an action. It‘s the same as doing: @bot.action m.replyto, string So if the message is private, it will reply to the user. If it was in a channel, it will reply in the channel.
# File lib/rbot/message.rb, line 466 466: def act(string, options={}) 467: @bot.action @replyto, string, options 468: @replied = true 469: end
# File lib/rbot/message.rb, line 285 285: def inspect 286: fields = ' plugin=' << plugin.inspect 287: fields << ' params=' << params.inspect 288: fields << ' channel=' << channel.to_s if channel 289: fields << ' (reply to ' << replyto.to_s << ')' 290: if self.private? 291: fields << ' (private)' 292: else 293: fields << ' (public)' 294: end 295: if self.action? 296: fields << ' (action)' 297: elsif ctcp 298: fields << ' (CTCP ' << ctcp << ')' 299: end 300: super(fields) 301: end
Like the above, but append the username
# File lib/rbot/message.rb, line 484 484: def nickokay 485: str = @bot.lang.get("okay").dup 486: if self.public? 487: # remove final punctuation 488: str.gsub!(/[!,.]$/,"") 489: str += ", #{@source}" 490: end 491: self.reply str, :nick => false 492: end
send a NOTICE to the message source
# File lib/rbot/message.rb, line 502 502: def notify(msg,opts={}) 503: @bot.notice(sourcenick, msg, opts) 504: end
convenience method to reply to a message, useful in plugins. It‘s the same as doing: @bot.say m.replyto, string So if the message is private, it will reply to the user. If it was in a channel, it will reply in the channel.
# File lib/rbot/message.rb, line 408 408: def plainreply(string, options={}) 409: reply string, {:nick => false}.merge(options) 410: end
returns true for private messages, e.g. "/msg bot hello"
# File lib/rbot/message.rb, line 390 390: def private? 391: return @private 392: end
returns true if the message was in a channel
# File lib/rbot/message.rb, line 395 395: def public? 396: return !@private 397: end
The general way to reply to a command. The following options are available: :nick [false, true, :auto]
state if the nick of the user calling the command should be prepended :auto uses core.reply_with_nick
:forcenick [false, true]
if :nick is true, always prepend the target's nick, even if the nick already appears in the reply. Defaults to false.
:to [:private, :public, :auto]
where should the bot reply? :private always reply to the nick :public reply to the channel (if available) :auto uses core.private_replies
# File lib/rbot/message.rb, line 437 437: def reply(string, options={}) 438: opts = {:nick => :auto, :forcenick => false, :to => :auto}.merge options 439: 440: if opts[:nick] == :auto 441: opts[:nick] = @bot.config['core.reply_with_nick'] 442: end 443: 444: if !self.public? 445: opts[:to] = :private 446: elsif opts[:to] == :auto 447: opts[:to] = @bot.config['core.private_replies'] ? :private : :public 448: end 449: 450: if (opts[:nick] && 451: opts[:to] != :private && 452: (string !~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/ || 453: opts[:forcenick])) 454: string = "#{@source}#{@bot.config['core.nick_postfix']} #{string}" 455: end 456: to = (opts[:to] == :private) ? source : @channel 457: @bot.say to, string, options 458: @replied = true 459: end