Class: Cinch::Message

Inherits:
Object
  • Object
show all
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

Type checking collapse

Replying collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, bot) ⇒ Message

Returns a new instance of Message.



89
90
91
92
93
94
95
96
97
# File 'lib/cinch/message.rb', line 89

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_messageString? (readonly)

Returns The action message.

Returns:

  • (String, nil)

    The action message

Since:

  • 2.0.0



69
70
71
# File 'lib/cinch/message.rb', line 69

def action_message
  @action_message
end

#botBot (readonly)

Returns:

Since:

  • 1.1.0



41
42
43
# File 'lib/cinch/message.rb', line 41

def bot
  @bot
end

#channelChannel (readonly)

Returns The channel in which this message was sent.

Returns:

  • (Channel)

    The channel in which this message was sent



56
57
58
# File 'lib/cinch/message.rb', line 56

def channel
  @channel
end

#commandString (readonly)

Returns:



22
23
24
# File 'lib/cinch/message.rb', line 22

def command
  @command
end

#ctcp_argsArray<String>? (readonly)

Returns:



62
63
64
# File 'lib/cinch/message.rb', line 62

def ctcp_args
  @ctcp_args
end

#ctcp_commandString? (readonly)

Returns the command part of an CTCP message.

Returns:

  • (String, nil)

    the command part of an CTCP message



53
54
55
# File 'lib/cinch/message.rb', line 53

def ctcp_command
  @ctcp_command
end

#ctcp_messageString? (readonly)

Returns the CTCP message, without 001 control characters.

Returns:

  • (String, nil)

    the CTCP message, without 001 control characters



59
60
61
# File 'lib/cinch/message.rb', line 59

def ctcp_message
  @ctcp_message
end

#errorInteger? (readonly)

Returns the numeric error code, if any.

Returns:

  • (Integer, nil)

    the numeric error code, if any



50
51
52
# File 'lib/cinch/message.rb', line 50

def error
  @error
end

#eventsArray<Symbol>

Returns:

  • (Array<Symbol>)


31
32
33
# File 'lib/cinch/message.rb', line 31

def events
  @events
end

#messageString? (readonly)

Returns:



65
66
67
# File 'lib/cinch/message.rb', line 65

def message
  @message
end

#paramsArray<String> (readonly)

Returns:



25
26
27
# File 'lib/cinch/message.rb', line 25

def params
  @params
end

#prefixString (readonly)

Returns:



19
20
21
# File 'lib/cinch/message.rb', line 19

def prefix
  @prefix
end

#rawString (readonly)

Returns:



16
17
18
# File 'lib/cinch/message.rb', line 16

def raw
  @raw
end

#serverString? (readonly)

Returns:



47
48
49
# File 'lib/cinch/message.rb', line 47

def server
  @server
end

#statusmsg_modeString? (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.

Returns:

Since:

  • 2.3.0



87
88
89
# File 'lib/cinch/message.rb', line 87

def statusmsg_mode
  @statusmsg_mode
end

#tagsHash (readonly)

Returns:

  • (Hash)


28
29
30
# File 'lib/cinch/message.rb', line 28

def tags
  @tags
end

#targetTarget (readonly)

Returns:



72
73
74
# File 'lib/cinch/message.rb', line 72

def target
  @target
end

#timeTime (readonly)

Returns:

  • (Time)

Since:

  • 2.0.0



37
38
39
# File 'lib/cinch/message.rb', line 37

def time
  @time
end

#userUser (readonly)

Returns The user who sent this message.

Returns:

  • (User)

    The user who sent this message



44
45
46
# File 'lib/cinch/message.rb', line 44

def user
  @user
end

Instance Method Details

#action?Boolean

Returns true if the message is an action (/me).

Returns:

  • (Boolean)

    true if the message is an action (/me)

Since:

  • 2.0.0



153
154
155
# File 'lib/cinch/message.rb', line 153

def action?
  @ctcp_command == "ACTION"
end

#action_reply(text) ⇒ void

This method returns an undefined value.

Reply to a message with an action.

For its behaviour with regard to STATUSMSG, see #reply.

Parameters:

  • text (String)

    the action message



222
223
224
225
# File 'lib/cinch/message.rb', line 222

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.

Returns:

  • (Boolean)

    true if this message was sent in a channel



142
143
144
# File 'lib/cinch/message.rb', line 142

def channel?
  !@channel.nil?
end

#ctcp?Boolean

Returns true if the message is an CTCP message.

Returns:

  • (Boolean)

    true if the message is an CTCP message



147
148
149
# File 'lib/cinch/message.rb', line 147

def ctcp?
  !!(@params.last =~ /\001.+\001/)
end

#ctcp_reply(answer) ⇒ void

This method returns an undefined value.

Reply to a CTCP message



239
240
241
242
# File 'lib/cinch/message.rb', line 239

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.

Returns:

  • (Boolean)

    true if the message describes an error



137
138
139
# File 'lib/cinch/message.rb', line 137

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.

Returns:

  • (MatchData)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cinch/message.rb', line 161

def match(regexp, type, strip_colors)
  text = ""
  case type
  when :ctcp
    text = ctcp_message
  when :action
    text = action_message
  else
    text = message.to_s
    type = :other
  end

  if strip_colors
    text = Cinch::Formatting.unformat(text)
  end

  @matches[type][regexp] ||= text.match(regexp)
end

#numeric_reply?Boolean

Returns true if the message is an numeric reply (as opposed to a command).

Returns:

  • (Boolean)

    true if the message is an numeric reply (as opposed to a command)



132
133
134
# File 'lib/cinch/message.rb', line 132

def numeric_reply?
  !!@command.match(/^\d{3}$/)
end

#parsevoid

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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cinch/message.rb', line 101

def parse
  match = @raw.match(/(?:^@([^:]+))?(?::?(\S+) )?(\S+)(.*)/)
  tags, @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)
  @tags = parse_tags(tags)

  @user = parse_user
  @channel, @statusmsg_mode = 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) ⇒ void

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.

Parameters:

  • text (String)

    the message

  • prefix (Boolean) (defaults to: false)

    if prefix is true and the message was in a channel, the reply will be prefixed by the nickname of whoever send the mesage



195
196
197
198
199
200
201
202
# File 'lib/cinch/message.rb', line 195

def reply(text, prefix = false)
  text = text.to_s
  if @channel && prefix
    text = text.split("\n").map { |l| "#{user.nick}: #{l}" }.join("\n")
  end

  reply_target.send(text)
end

#safe_action_reply(text) ⇒ void

This method returns an undefined value.

Like #action_reply, but using Target#safe_action instead

Parameters:

  • text (String)

    the action message



231
232
233
234
# File 'lib/cinch/message.rb', line 231

def safe_action_reply(text)
  text = text.to_s
  reply_target.safe_action(text)
end

#safe_reply(text, prefix = false) ⇒ void

This method returns an undefined value.

Like #reply, but using Target#safe_send instead

Parameters:

  • text (String)

    the message

  • prefix (Boolean) (defaults to: false)

    if prefix is true and the message was in a channel, the reply will be prefixed by the nickname of whoever send the mesage



208
209
210
211
212
213
214
# File 'lib/cinch/message.rb', line 208

def safe_reply(text, prefix = false)
  text = text.to_s
  if channel && prefix
    text = "#{@user.nick}: #{text}"
  end
  reply_target.safe_send(text)
end

#to_sString

Returns:

Since:

  • 1.1.0



248
249
250
# File 'lib/cinch/message.rb', line 248

def to_s
  "#<Cinch::Message @raw=#{@raw.chomp.inspect} @params=#{@params.inspect} channel=#{@channel.inspect} user=#{@user.inspect}>"
end