Parent

Class/Module Index [+]

Quicksearch

Cinch::Message

This class serves two purposes. For one, it simply represents incoming messages and allows for querying various details (who sent the message, what kind of message it is, etc).

At the same time, it allows *responding* to messages, which means sending messages to either users or channels.

Attributes

action_message[R]

@return [String, nil] The action message @since 2.0.0

bot[R]

@return [Bot] @since 1.1.0

channel[R]

@return [Channel] The channel in which this message was sent

command[R]

@return [String]

ctcp_args[R]

@return [Array<String>, nil]

ctcp_command[R]

@return [String, nil] the command part of an CTCP message

ctcp_message[R]

@return [String, nil] the CTCP message, without 001 control characters

error[R]

@return [Integer, nil] the numeric error code, if any

events[RW]

@return [Array<Symbol>]

message[R]

@return [String, nil]

params[R]

@return [Array<String>]

prefix[R]

@return [String]

raw[R]

@return [String]

server[R]

@return [String, nil]

target[R]

@return [Target]

time[R]

@return [Time] @since 2.0.0

user[R]

@return [User] The user who sent this message

Public Class Methods

new(msg, bot) click to toggle source
# File lib/cinch/message.rb, line 67
def initialize(msg, bot)
  @raw     = msg
  @bot     = bot
  @matches = {:ctcp => {}, :action => {}, :other => {}}
  @events  = []
  @time    = Time.now
  parse if msg
end

Public Instance Methods

action?() click to toggle source

@return [Boolean] true if the message is an action (/me) @since 2.0.0

# File lib/cinch/message.rb, line 129
def action?
  @ctcp_command == "ACTION"
end
channel?() click to toggle source

@return [Boolean] true if this message was sent in a channel

# File lib/cinch/message.rb, line 118
def channel?
  !@channel.nil?
end
ctcp?() click to toggle source

@return [Boolean] true if the message is an CTCP message

# File lib/cinch/message.rb, line 123
def ctcp?
  !!(@params.last =~ /\0001.+\0001/)
end
ctcp_reply(answer) click to toggle source

Reply to a CTCP message

@return [void]

# File lib/cinch/message.rb, line 181
def ctcp_reply(answer)
  return unless ctcp?
  @user.notice "\0001#{@ctcp_command} #{answer}\0001"
end
error?() click to toggle source

@return [Boolean] true if the message describes an error

# File lib/cinch/message.rb, line 113
def error?
  !@error.nil?
end
match(regexp, type) click to toggle source

@api private @return [MatchData]

# File lib/cinch/message.rb, line 137
def match(regexp, type)
  if type == :ctcp
    @matches[:ctcp][regexp] ||= ctcp_message.match(regexp)
  elsif type == :action
    @matches[:action][regexp] ||= action_message.match(regexp)
  else
    @matches[:other][regexp] ||= message.to_s.match(regexp)
  end
end
numeric_reply?() click to toggle source

@return [Boolean] true if the message is an numeric reply (as

opposed to a command)
# File lib/cinch/message.rb, line 108
def numeric_reply?
  !!@command.match(/^\d{3}$/)
end
parse() click to toggle source

@api private @return [void]

# File lib/cinch/message.rb, line 78
def parse
  match = @raw.match(/(^:(\S+) )?(\S+)(.*)/)
  _, @prefix, @command, raw_params = match.captures

  if @bot.irc.network.ngametv?
    if @prefix != "ngame"
      @prefix = "%s!%s@%s" % [@prefix, @prefix, @prefix]
    end
  end

  @params  = parse_params(raw_params)

  @user    = parse_user
  @channel = parse_channel
  @target  = @channel || @user
  @server  = parse_server
  @error   = parse_error
  @message = parse_message

  @ctcp_message = parse_ctcp_message
  @ctcp_command = parse_ctcp_command
  @ctcp_args    = parse_ctcp_args

  @action_message = parse_action_message
end
reply(text, prefix = false) click to toggle source

Replies to a message, automatically determining if it was a channel or a private message.

@param [String] text the message @param [Boolean] prefix if prefix is true and the message was in

a channel, the reply will be prefixed by the nickname of whoever
send the mesage

@return [void]

# File lib/cinch/message.rb, line 157
def reply(text, prefix = false)
  text = text.to_s
  if @channel && prefix
    text = text.split("\n").map {|l| "#{user.nick}: #{l}"}.join("\n")
  end

  @target.send(text)
end
safe_reply(text, prefix = false) click to toggle source

Like reply, but using {Target#safe_send} instead

@param (see reply) @return (see reply)

# File lib/cinch/message.rb, line 170
def safe_reply(text, prefix = false)
  text = text.to_s
  if channel && prefix
    text = "#{@user.nick}: #{text}"
  end
  @target.safe_send(text)
end
to_s() click to toggle source

@return [String] @since 1.1.0

# File lib/cinch/message.rb, line 190
def to_s
  "#<Cinch::Message @raw=#{@raw.chomp.inspect} @params=#{@params.inspect} channel=#{@channel.inspect} user=#{@user.inspect}>"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.