Class: Cinch::Message
- Inherits:
-
Object
- Object
- Cinch::Message
- Defined in:
- lib/cinch/message.rb
Overview
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.
Instance Attribute Summary (collapse)
-
- (String?) action_message
readonly
The action message.
-
- (Bot) bot
readonly
-
- (Channel) channel
readonly
The channel in which this message was sent.
-
- (String) command
readonly
-
- (Array<String>?) ctcp_args
readonly
-
- (String?) ctcp_command
readonly
The command part of an CTCP message.
-
- (String?) ctcp_message
readonly
The CTCP message, without \001 control characters.
-
- (Integer?) error
readonly
The numeric error code, if any.
-
- (Array<Symbol>) events
-
- (String?) message
readonly
-
- (Array<String>) params
readonly
-
- (String) prefix
readonly
-
- (String) raw
readonly
-
- (String?) server
readonly
-
- (Target) target
readonly
-
- (Time) time
readonly
-
- (User) user
readonly
The user who sent this message.
Type checking (collapse)
-
- (Boolean) action?
True if the message is an action (/me).
-
- (Boolean) channel?
True if this message was sent in a channel.
-
- (Boolean) ctcp?
True if the message is an CTCP message.
-
- (Boolean) error?
True if the message describes an error.
-
- (Boolean) numeric_reply?
True if the message is an numeric reply (as opposed to a command).
Replying (collapse)
-
- action_reply(text)
Reply to a message with an action.
-
- ctcp_reply(answer)
Reply to a CTCP message.
-
- reply(text, prefix = false)
Replies to a message, automatically determining if it was a channel or a private message.
-
- safe_action_reply(text)
Like #action_reply, but using Target#safe_action instead.
-
- safe_reply(text, prefix = false)
Like #reply, but using Target#safe_send instead.
Instance Method Summary (collapse)
-
- (Message) initialize(msg, bot)
constructor
A new instance of Message.
-
- (MatchData) match(regexp, type)
private
-
- parse
private
-
- (String) to_s
Constructor Details
- (Message) initialize(msg, bot)
A new instance of Message
68 69 70 71 72 73 74 75 |
# File 'lib/cinch/message.rb', line 68 def initialize(msg, bot) @raw = msg @bot = bot @matches = {:ctcp => {}, :action => {}, :other => {}} @events = [] @time = Time.now parse if msg end |
Instance Attribute Details
- (String?) action_message (readonly)
The action message
63 64 65 |
# File 'lib/cinch/message.rb', line 63 def @action_message end |
- (Channel) channel (readonly)
The channel in which this message was sent
50 51 52 |
# File 'lib/cinch/message.rb', line 50 def channel @channel end |
- (String) command (readonly)
19 20 21 |
# File 'lib/cinch/message.rb', line 19 def command @command end |
- (Array<String>?) ctcp_args (readonly)
56 57 58 |
# File 'lib/cinch/message.rb', line 56 def ctcp_args @ctcp_args end |
- (String?) ctcp_command (readonly)
The command part of an CTCP message
47 48 49 |
# File 'lib/cinch/message.rb', line 47 def ctcp_command @ctcp_command end |
- (String?) ctcp_message (readonly)
The CTCP message, without \001 control characters
53 54 55 |
# File 'lib/cinch/message.rb', line 53 def @ctcp_message end |
- (Integer?) error (readonly)
The numeric error code, if any
44 45 46 |
# File 'lib/cinch/message.rb', line 44 def error @error end |
- (Array<Symbol>) events
25 26 27 |
# File 'lib/cinch/message.rb', line 25 def events @events end |
- (Array<String>) params (readonly)
22 23 24 |
# File 'lib/cinch/message.rb', line 22 def params @params end |
- (String?) server (readonly)
41 42 43 |
# File 'lib/cinch/message.rb', line 41 def server @server end |
- (Time) time (readonly)
31 32 33 |
# File 'lib/cinch/message.rb', line 31 def time @time end |
- (User) user (readonly)
The user who sent this message
38 39 40 |
# File 'lib/cinch/message.rb', line 38 def user @user end |
Instance Method Details
- (Boolean) action?
True if the message is an action (/me)
130 131 132 |
# File 'lib/cinch/message.rb', line 130 def action? @ctcp_command == "ACTION" end |
- action_reply(text)
This method returns an undefined value.
Reply to a message with an action.
183 184 185 186 |
# File 'lib/cinch/message.rb', line 183 def action_reply(text) text = text.to_s @target.action(text) end |
- (Boolean) channel?
True if this message was sent in a channel
119 120 121 |
# File 'lib/cinch/message.rb', line 119 def channel? !@channel.nil? end |
- (Boolean) ctcp?
True if the message is an CTCP message
124 125 126 |
# File 'lib/cinch/message.rb', line 124 def ctcp? !!(@params.last =~ /\001.+\001/) end |
- ctcp_reply(answer)
This method returns an undefined value.
Reply to a CTCP message
200 201 202 203 |
# File 'lib/cinch/message.rb', line 200 def ctcp_reply(answer) return unless ctcp? @user.notice "\001#{@ctcp_command} #{answer}\001" end |
- (Boolean) error?
True if the message describes an error
114 115 116 |
# File 'lib/cinch/message.rb', line 114 def error? !@error.nil? end |
- (MatchData) match(regexp, type)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
138 139 140 141 142 143 144 145 146 |
# File 'lib/cinch/message.rb', line 138 def match(regexp, type) if type == :ctcp @matches[:ctcp][regexp] ||= .match(regexp) elsif type == :action @matches[:action][regexp] ||= .match(regexp) else @matches[:other][regexp] ||= .to_s.match(regexp) end end |
- (Boolean) numeric_reply?
True if the message is an numeric reply (as opposed to a command)
109 110 111 |
# File 'lib/cinch/message.rb', line 109 def numeric_reply? !!@command.match(/^\d{3}$/) end |
- parse
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cinch/message.rb', line 79 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 = @ctcp_message = @ctcp_command = parse_ctcp_command @ctcp_args = parse_ctcp_args @action_message = end |
- reply(text, prefix = false)
This method returns an undefined value.
Replies to a message, automatically determining if it was a channel or a private message.
158 159 160 161 162 163 164 165 |
# File 'lib/cinch/message.rb', line 158 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_action_reply(text)
This method returns an undefined value.
Like #action_reply, but using Target#safe_action instead
192 193 194 195 |
# File 'lib/cinch/message.rb', line 192 def safe_action_reply(text) text = text.to_s @target.safe_action(text) end |
- safe_reply(text, prefix = false)
This method returns an undefined value.
Like #reply, but using Target#safe_send instead
171 172 173 174 175 176 177 |
# File 'lib/cinch/message.rb', line 171 def safe_reply(text, prefix = false) text = text.to_s if channel && prefix text = "#{@user.nick}: #{text}" end @target.safe_send(text) end |
- (String) to_s
209 210 211 |
# File 'lib/cinch/message.rb', line 209 def to_s "#<Cinch::Message @raw=#{@raw.chomp.inspect} @params=#{@params.inspect} channel=#{@channel.inspect} user=#{@user.inspect}>" end |