class Irc::UserMessage

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)

Attributes

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

Public Class Methods

new(bot, server, source, target, message) click to toggle source

instantiate a new UserMessage

bot

associated bot class

source

hostmask of the message source

target

nick/channel message is destined for

message

message part

Calls superclass method Irc::BasicUserMessage.new
# File lib/rbot/message.rb, line 333
def initialize(bot, server, source, target, message)
  super(bot, server, source, target, message)
  @target = target
  @private = false
  @plugin = nil
  @ctcp = false
  @action = false

  if target == @bot.myself
    @private = true
    @address = true
    @channel = nil
    @replyto = source
  else
    @replyto = @target
    @channel = @target
  end

  # check for option extra addressing prefixes, e.g "|search foo", or
  # "!version" - first match wins
  bot.config['core.address_prefix'].each {|mprefix|
    if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
      @address = true
      @prefixed = true
      break
    end
  }

  # even if they used above prefixes, we allow for silly people who
  # combine all possible types, e.g. "|rbot: hello", or
  # "/msg rbot rbot: hello", etc
  if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
    @address = true
  end

  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
    @plainmessage = BasicUserMessage.strip_formatting(@message)
    @message = BasicUserMessage.strip_initial_formatting(@message)
  end

  # free splitting for plugins
  @params = @message.dup
  # Created messges (such as by fake_message) can contain multiple lines
  if @params.gsub!(/\A\s*(\S+)[\s$]*/m, "")
    @plugin = $1.downcase
    @params = nil unless @params.length > 0
  end
end

Public Instance Methods

act(string, options={}) click to toggle source

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
def act(string, options={})
  @bot.action @replyto, string, options
  @replied = true
end
action?() click to toggle source
# File lib/rbot/message.rb, line 399
def action?
  return @action
end
ctcp_reply(string, options={}) click to toggle source

send a CTCP response, i.e. a private NOTICE to the sender with the same CTCP command and the reply as a parameter

# File lib/rbot/message.rb, line 473
def ctcp_reply(string, options={})
  @bot.ctcp_notice @source, @ctcp, string, options
end
inspect() click to toggle source
Calls superclass method Irc::BasicUserMessage#inspect
# File lib/rbot/message.rb, line 285
def inspect
  fields = ' plugin=' << plugin.inspect
  fields << ' params=' << params.inspect
  fields << ' channel=' << channel.to_s if channel
  fields << ' (reply to ' << replyto.to_s << ')'
  if self.private?
    fields << ' (private)'
  else
    fields << ' (public)'
  end
  if self.action?
    fields << ' (action)'
  elsif ctcp
    fields << ' (CTCP ' << ctcp << ')'
  end
  super(fields)
end
nickokay() click to toggle source

Like the above, but append the username

# File lib/rbot/message.rb, line 484
def nickokay
  str = @bot.lang.get("okay").dup
  if self.public?
    # remove final punctuation
    str.gsub!(/[!,.]$/,"")
    str += ", #{@source}"
  end
  self.reply str, :nick => false
end
nickreply(string, options={}) click to toggle source

Same as reply, but when replying in public it adds the nick of the user the bot is replying to

# File lib/rbot/message.rb, line 414
def nickreply(string, options={})
  reply string, {:nick => true}.merge(options)
end
nickreply!(string, options={}) click to toggle source

Same as nickreply, but always prepend the target's nick.

# File lib/rbot/message.rb, line 419
def nickreply!(string, options={})
  reply string, {:nick => true, :forcenick => true}.merge(options)
end
notify(msg,opts={}) click to toggle source

send a NOTICE to the message source

# File lib/rbot/message.rb, line 502
def notify(msg,opts={})
  @bot.notice(sourcenick, msg, opts)
end
okay() click to toggle source

the default okay style is the same as the default reply style

# File lib/rbot/message.rb, line 496
def okay
  @bot.config['core.reply_with_nick'] ? nickokay : plainokay
end
plainokay() click to toggle source

convenience method to reply “okay” in the current language to the message

# File lib/rbot/message.rb, line 479
def plainokay
  self.reply @bot.lang.get("okay"), :nick => false
end
plainreply(string, options={}) click to toggle source

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
def plainreply(string, options={})
  reply string, {:nick => false}.merge(options)
end
private?() click to toggle source

returns true for private messages, e.g. “/msg bot hello”

# File lib/rbot/message.rb, line 390
def private?
  return @private
end
public?() click to toggle source

returns true if the message was in a channel

# File lib/rbot/message.rb, line 395
def public?
  return !@private
end
reply(string, options={}) click to toggle source

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
def reply(string, options={})
  opts = {:nick => :auto, :forcenick => false, :to => :auto}.merge options

  if opts[:nick] == :auto
    opts[:nick] = @bot.config['core.reply_with_nick']
  end

  if !self.public?
    opts[:to] = :private
  elsif opts[:to] == :auto
    opts[:to] = @bot.config['core.private_replies'] ? :private : :public
  end

  if (opts[:nick] &&
      opts[:to] != :private &&
      (string !~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/ ||
        opts[:forcenick]))
    string = "#{@source}#{@bot.config['core.nick_postfix']} #{string}"
  end
  to = (opts[:to] == :private) ? source : @channel
  @bot.say to, string, options
  @replied = true
end