Class: Cinch::Message
- Inherits:
-
Object
- Object
- Cinch::Message
- Defined in:
- lib/cinch/message.rb
Instance Attribute Summary collapse
- #bot ⇒ Bot readonly
- #command ⇒ String
-
#events ⇒ Object
readonly
Returns the value of attribute events.
- #params ⇒ Array<String>
- #prefix ⇒ String
- #raw ⇒ String
Instance Method Summary collapse
-
#channel ⇒ Channel
The channel in which this message was sent.
-
#channel? ⇒ Boolean
True if this message was sent in a channel.
-
#ctcp? ⇒ Boolean
True if the message is an CTCP message.
- #ctcp_args ⇒ Array<String>?
-
#ctcp_command ⇒ String?
The command part of an CTCP message.
-
#ctcp_message ⇒ String?
The CTCP message, without 001 control characters.
-
#ctcp_reply(answer) ⇒ void
Reply to a CTCP message.
-
#error ⇒ Number?
The numeric error code, if any.
-
#error? ⇒ Boolean
True if the message describes an error.
-
#initialize(msg, bot) ⇒ Message
constructor
A new instance of Message.
- #match(regexp, type) ⇒ MatchData private
- #message ⇒ String?
-
#numeric_reply? ⇒ Boolean
opposed to a command).
- #parse ⇒ void private
-
#reply(text, prefix = false) ⇒ void
Replies to a message, automatically determining if it was a channel or a private message.
-
#safe_reply(text, prefix = false) ⇒ void
Like #reply, but using Channel#safe_send/User#safe_send instead.
- #server ⇒ String?
- #to_s ⇒ String
-
#user ⇒ User
The user who sent this message.
Constructor Details
#initialize(msg, bot) ⇒ Message
Returns a new instance of Message.
15 16 17 18 19 20 21 |
# File 'lib/cinch/message.rb', line 15 def initialize(msg, bot) @raw = msg @bot = bot @matches = {:ctcp => {}, :other => {}} @events = [] parse if msg end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
12 13 14 |
# File 'lib/cinch/message.rb', line 12 def events @events end |
Instance Method Details
#channel ⇒ Channel
Returns The channel in which this message was sent.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cinch/message.rb', line 91 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? ⇒ Boolean
Returns true if this message was sent in a channel.
75 76 77 |
# File 'lib/cinch/message.rb', line 75 def channel? !!channel end |
#ctcp? ⇒ Boolean
Returns true if the message is an CTCP message.
80 81 82 |
# File 'lib/cinch/message.rb', line 80 def ctcp? params.last =~ /\001.+\001/ end |
#ctcp_args ⇒ Array<String>?
126 127 128 129 |
# File 'lib/cinch/message.rb', line 126 def ctcp_args return unless ctcp? .split(" ")[1..-1] end |
#ctcp_command ⇒ String?
Returns the command part of an CTCP message.
85 86 87 88 |
# File 'lib/cinch/message.rb', line 85 def ctcp_command return unless ctcp? .split(" ").first end |
#ctcp_message ⇒ String?
Returns the CTCP message, without 001 control characters.
119 120 121 122 123 |
# File 'lib/cinch/message.rb', line 119 def return unless ctcp? params.last =~ /\001(.+)\001/ $1 end |
#ctcp_reply(answer) ⇒ void
This method returns an undefined value.
Reply to a CTCP message
175 176 177 178 |
# File 'lib/cinch/message.rb', line 175 def ctcp_reply(answer) return unless ctcp? user.notice "\001#{ctcp_command} #{answer}\001" end |
#error ⇒ Number?
Returns the numeric error code, if any.
70 71 72 |
# File 'lib/cinch/message.rb', line 70 def error @error ||= (command.to_i if numeric_reply? && command[/[45]\d\d/]) end |
#error? ⇒ Boolean
Returns true if the message describes an error.
65 66 67 |
# File 'lib/cinch/message.rb', line 65 def error? !!error end |
#match(regexp, type) ⇒ 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.
110 111 112 113 114 115 116 |
# File 'lib/cinch/message.rb', line 110 def match(regexp, type) if type == :ctcp @matches[:ctcp][regexp] ||= .match(regexp) else @matches[:other][regexp] ||= .to_s.match(regexp) end end |
#message ⇒ String?
132 133 134 135 136 137 138 139 140 |
# File 'lib/cinch/message.rb', line 132 def @message ||= begin if error? error.to_s elsif regular_command? params.last end end end |
#numeric_reply? ⇒ Boolean
opposed to a command)
27 28 29 |
# File 'lib/cinch/message.rb', line 27 def numeric_reply? !!(@numeric_reply ||= @command.match(/^\d{3}$/)) end |
#parse ⇒ void
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.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cinch/message.rb', line 33 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) ⇒ void
This method returns an undefined value.
Replies to a message, automatically determining if it was a channel or a private message.
a channel, the reply will be prefixed by the nickname of whoever send the mesage
150 151 152 153 154 155 156 157 |
# File 'lib/cinch/message.rb', line 150 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) ⇒ void
This method returns an undefined value.
Like #reply, but using Channel#safe_send/User#safe_send instead
164 165 166 167 168 169 170 |
# File 'lib/cinch/message.rb', line 164 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 ⇒ String?
58 59 60 61 62 |
# File 'lib/cinch/message.rb', line 58 def server return unless @prefix return if @prefix.match(/[@!]/) @server ||= @prefix[/^(\S+)/, 1] end |
#to_s ⇒ String
181 182 183 |
# File 'lib/cinch/message.rb', line 181 def to_s "#<Cinch::Message @raw=#{raw.chomp.inspect} @params=#{@params.inspect} channel=#{channel.inspect} user=#{user.inspect}>" end |
#user ⇒ User
Returns The user who sent this message.
47 48 49 50 51 52 53 54 55 |
# File 'lib/cinch/message.rb', line 47 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 |