Parent

Cinch::Message

Attributes

bot[R]

@return [Bot]

command[RW]

@return [String]

events[R]
params[RW]

@return [Array<String>]

prefix[RW]

@return [String]

raw[RW]

@return [String]

Public Class Methods

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

Public Instance Methods

channel() click to toggle source

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

# File lib/cinch/message.rb, line 90
def channel
  @channel ||= begin
                 case command
                 when "INVITE", RPL_CHANNELMODEIS.to_s, RPL_BANLIST.to_s
                   @bot.channel_manager.find_ensured(params[1])
                 when RPL_NAMEREPLY.to_s
                   @bot.channel_manager.find_ensured(params[2])
                 else
                   if params.first.start_with?("#")
                     @bot.channel_manager.find_ensured(params.first)
                   elsif numeric_reply? and params[1].start_with?("#")
                     @bot.channel_manager.find_ensured(params[1])
                   end
                 end
               end
end
channel?() click to toggle source

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

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

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

# File lib/cinch/message.rb, line 79
def ctcp?
  params.last =~ /\0001.+\0001/
end
ctcp_args() click to toggle source

@return [Array<String>, nil]

# File lib/cinch/message.rb, line 125
def ctcp_args
  return unless ctcp?
  ctcp_message.split(" ")[1..-1]
end
ctcp_command() click to toggle source

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

# File lib/cinch/message.rb, line 84
def ctcp_command
  return unless ctcp?
  ctcp_message.split(" ").first
end
ctcp_message() click to toggle source

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

# File lib/cinch/message.rb, line 118
def ctcp_message
  return unless ctcp?
  params.last =~ /\0001(.+)\0001/
  $1
end
ctcp_reply(answer) click to toggle source

Reply to a CTCP message

@return [void]

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

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

# File lib/cinch/message.rb, line 69
def error
  @error ||= (command.to_i if numeric_reply? && command[/[45]\d\d/])
end
error?() click to toggle source

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

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

@api private @return [MatchData]

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

@return [String, nil]

# File lib/cinch/message.rb, line 131
def message
  @message ||= begin
                 if error?
                   error.to_s
                 elsif regular_command?
                   params.last
                 end
               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 26
def numeric_reply?
  !!(@numeric_reply ||= @command.match(/^\d{3}$/))
end
parse() click to toggle source

@api private @return [void]

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

  raw_params.strip!
  if match = raw_params.match(/(?:^:| :)(.*)$/)
    @params = match.pre_match.split(" ")
    @params << match[1]
  else
    @params = raw_params.split(" ")
  end
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 149
def reply(text, prefix = false)
  text = text.to_s
  if channel && prefix
    text = text.split("\n").map {|l| "#{user.nick}: #{l}"}.join("\n")
  end

  (channel || user).send(text)
end
safe_reply(text, prefix = false) click to toggle source

Like reply, but using {Channel#safe_send}/{User#safe_send} instead

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

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

@return [String, nil]

# File lib/cinch/message.rb, line 57
def server
  return unless @prefix
  return if @prefix.match(/[@!]/)
  @server ||= @prefix[/^(\S+)/, 1]
end
to_s() click to toggle source

@return [String]

# File lib/cinch/message.rb, line 180
def to_s
  "#<Cinch::Message @raw=#{raw.chomp.inspect} @params=#{@params.inspect} channel=#{channel.inspect} user=#{user.inspect}>"
end
user() click to toggle source

@return [User] The user who sent this message

# File lib/cinch/message.rb, line 46
def user
  return unless @prefix
  nick = @prefix[/^(\S+)!/, 1]
  user = @prefix[/^\S+!(\S+)@/, 1]
  host = @prefix[/@(\S+)$/, 1]

  return nil if nick.nil?
  @user ||= @bot.user_manager.find_ensured(user, nick, host)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.