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
-
#action_message ⇒ String?
readonly
The action message.
-
#bot ⇒ Bot
readonly
-
#channel ⇒ Channel
readonly
The channel in which this message was sent.
-
#command ⇒ String
readonly
-
#ctcp_args ⇒ Array<String>?
readonly
-
#ctcp_command ⇒ String?
readonly
The command part of an CTCP message.
-
#ctcp_message ⇒ String?
readonly
The CTCP message, without \001 control characters.
-
#error ⇒ Integer?
readonly
The numeric error code, if any.
-
#events ⇒ Array<Symbol>
-
#message ⇒ String?
readonly
-
#params ⇒ Array<String>
readonly
-
#prefix ⇒ String
readonly
-
#raw ⇒ String
readonly
-
#server ⇒ String?
readonly
-
#statusmsg_mode ⇒ String?
readonly
The STATUSMSG mode a channel message was sent to.
-
#tags ⇒ Hash
readonly
-
#target ⇒ Target
readonly
-
#time ⇒ Time
readonly
-
#user ⇒ User
readonly
The user who sent this message.
Type checking collapse
-
#action? ⇒ Boolean
True if the message is an action (/me).
-
#channel? ⇒ Boolean
True if this message was sent in a channel.
-
#ctcp? ⇒ Boolean
True if the message is an CTCP message.
-
#error? ⇒ Boolean
True if the message describes an error.
-
#numeric_reply? ⇒ Boolean
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
-
#initialize(msg, bot) ⇒ Message
constructor
A new instance of Message.
-
#match(regexp, type, strip_colors) ⇒ MatchData
private
-
#parse
private
-
#to_s ⇒ String
-
#to_symbol(string) ⇒ Object
Constructor Details
#initialize(msg, bot) ⇒ Message
Returns a new instance of Message.
88 89 90 91 92 93 94 95 96 |
# File 'lib/cinch/message.rb', line 88 def initialize(msg, bot) @raw = msg @bot = bot @matches = { ctcp: {}, action: {}, other: {} } @events = [] @time = Time.now @statusmsg_mode = nil parse if msg end |
Instance Attribute Details
#action_message ⇒ String? (readonly)
Returns The action message.
68 69 70 |
# File 'lib/cinch/message.rb', line 68 def @action_message end |
#channel ⇒ Channel (readonly)
Returns The channel in which this message was sent.
55 56 57 |
# File 'lib/cinch/message.rb', line 55 def channel @channel end |
#command ⇒ String (readonly)
21 22 23 |
# File 'lib/cinch/message.rb', line 21 def command @command end |
#ctcp_args ⇒ Array<String>? (readonly)
61 62 63 |
# File 'lib/cinch/message.rb', line 61 def ctcp_args @ctcp_args end |
#ctcp_command ⇒ String? (readonly)
Returns the command part of an CTCP message.
52 53 54 |
# File 'lib/cinch/message.rb', line 52 def ctcp_command @ctcp_command end |
#ctcp_message ⇒ String? (readonly)
Returns the CTCP message, without \001 control characters.
58 59 60 |
# File 'lib/cinch/message.rb', line 58 def @ctcp_message end |
#error ⇒ Integer? (readonly)
Returns the numeric error code, if any.
49 50 51 |
# File 'lib/cinch/message.rb', line 49 def error @error end |
#events ⇒ Array<Symbol>
30 31 32 |
# File 'lib/cinch/message.rb', line 30 def events @events end |
#params ⇒ Array<String> (readonly)
24 25 26 |
# File 'lib/cinch/message.rb', line 24 def params @params end |
#statusmsg_mode ⇒ String? (readonly)
The STATUSMSG mode a channel message was sent to.
Some IRC servers allow sending messages limited to people in a
channel who have a certain mode. For example, by sending a
message to +#channel
, only people who are voiced, or have a
higher mode (op) will receive the message.
This attribute contains the mode character the message was sent
to, or nil if it was a normal message. For the previous example,
this attribute would be set to "v"
, for voiced.
86 87 88 |
# File 'lib/cinch/message.rb', line 86 def statusmsg_mode @statusmsg_mode end |
#tags ⇒ Hash (readonly)
27 28 29 |
# File 'lib/cinch/message.rb', line 27 def @tags end |
#time ⇒ Time (readonly)
36 37 38 |
# File 'lib/cinch/message.rb', line 36 def time @time end |
#user ⇒ User (readonly)
Returns The user who sent this message.
43 44 45 |
# File 'lib/cinch/message.rb', line 43 def user @user end |
Instance Method Details
#action? ⇒ Boolean
Returns true if the message is an action (/me).
150 151 152 |
# File 'lib/cinch/message.rb', line 150 def action? @ctcp_command == "ACTION" end |
#action_reply(text)
This method returns an undefined value.
Reply to a message with an action.
For its behaviour with regard to STATUSMSG, see #reply.
213 214 215 216 |
# File 'lib/cinch/message.rb', line 213 def action_reply(text) text = text.to_s reply_target.action(text) end |
#channel? ⇒ Boolean
Returns true if this message was sent in a channel.
139 140 141 |
# File 'lib/cinch/message.rb', line 139 def channel? !@channel.nil? end |
#ctcp? ⇒ Boolean
Returns true if the message is an CTCP message.
144 145 146 |
# File 'lib/cinch/message.rb', line 144 def ctcp? !!(@params.last =~ /\001.+\001/) end |
#ctcp_reply(answer)
This method returns an undefined value.
Reply to a CTCP message
230 231 232 233 234 |
# File 'lib/cinch/message.rb', line 230 def ctcp_reply(answer) return unless ctcp? @user.notice "\001#{@ctcp_command} #{answer}\001" end |
#error? ⇒ Boolean
Returns true if the message describes an error.
134 135 136 |
# File 'lib/cinch/message.rb', line 134 def error? !@error.nil? end |
#match(regexp, type, strip_colors) ⇒ MatchData
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.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cinch/message.rb', line 158 def match(regexp, type, strip_colors) text = "" case type when :ctcp text = when :action text = else text = .to_s type = :other end text = Cinch::Formatting.unformat(text) if strip_colors @matches[type][regexp] ||= text.match(regexp) end |
#numeric_reply? ⇒ Boolean
Returns true if the message is an numeric reply (as opposed to a command).
129 130 131 |
# File 'lib/cinch/message.rb', line 129 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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cinch/message.rb', line 100 def parse match = @raw.match(/\A(?:@([^ ]+) )?(?::(\S+) )?(\S+)(.*)/) , @prefix, @command, raw_params = match.captures if @bot.irc.network.ngametv? @prefix = "%s!%s@%s" % [@prefix, @prefix, @prefix] if @prefix != "ngame" end @params = parse_params(raw_params) @tags = () @user = parse_user @channel, @statusmsg_mode = 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.
If the message is a STATUSMSG, i.e. it was send to +#channel
or @#channel
instead of #channel
, the reply will be sent as
the same kind of STATUSMSG. See #statusmsg_mode for more
information on STATUSMSG.
190 191 192 193 194 195 |
# File 'lib/cinch/message.rb', line 190 def reply(text, prefix = false) text = text.to_s text = text.split("\n").map { |l| "#{user.nick}: #{l}" }.join("\n") if @channel && prefix reply_target.send(text) end |
#safe_action_reply(text)
This method returns an undefined value.
Like #action_reply, but using Target#safe_action instead
222 223 224 225 |
# File 'lib/cinch/message.rb', line 222 def safe_action_reply(text) text = text.to_s reply_target.safe_action(text) end |
#safe_reply(text, prefix = false)
This method returns an undefined value.
Like #reply, but using Target#safe_send instead
201 202 203 204 205 |
# File 'lib/cinch/message.rb', line 201 def safe_reply(text, prefix = false) text = text.to_s text = "#{@user.nick}: #{text}" if channel && prefix reply_target.safe_send(text) end |
#to_s ⇒ String
240 241 242 |
# File 'lib/cinch/message.rb', line 240 def to_s "#<Cinch::Message @raw=#{@raw.chomp.inspect} @params=#{@params.inspect} channel=#{@channel.inspect} user=#{@user.inspect}>" end |
#to_symbol(string) ⇒ Object
272 273 274 |
# File 'lib/cinch/message.rb', line 272 def to_symbol(string) string.tr("-", "_").downcase.to_sym end |