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

- (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

Returns:

  • (String, nil)

    The action message

Since:

  • 2.0.0



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

def action_message
  @action_message
end

- (Bot) bot (readonly)

Returns:

Since:

  • 1.1.0



35
36
37
# File 'lib/cinch/message.rb', line 35

def bot
  @bot
end

- (Channel) channel (readonly)

The channel in which this message was sent

Returns:

  • (Channel)

    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)

Returns:



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

def command
  @command
end

- (Array<String>?) ctcp_args (readonly)

Returns:



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

Returns:

  • (String, nil)

    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

Returns:

  • (String, nil)

    the CTCP message, without \001 control characters



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

def ctcp_message
  @ctcp_message
end

- (Integer?) error (readonly)

The numeric error code, if any

Returns:

  • (Integer, nil)

    the numeric error code, if any



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

def error
  @error
end

- (Array<Symbol>) events

Returns:

  • (Array<Symbol>)


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

def events
  @events
end

- (String?) message (readonly)

Returns:



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

def message
  @message
end

- (Array<String>) params (readonly)

Returns:



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

def params
  @params
end

- (String) prefix (readonly)

Returns:



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

def prefix
  @prefix
end

- (String) raw (readonly)

Returns:



13
14
15
# File 'lib/cinch/message.rb', line 13

def raw
  @raw
end

- (String?) server (readonly)

Returns:



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

def server
  @server
end

- (Target) target (readonly)

Returns:



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

def target
  @target
end

- (Time) time (readonly)

Returns:

  • (Time)

Since:

  • 2.0.0



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

def time
  @time
end

- (User) user (readonly)

The user who sent this message

Returns:

  • (User)

    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)

Returns:

  • (Boolean)

    true if the message is an action (/me)

Since:

  • 2.0.0



130
131
132
# File 'lib/cinch/message.rb', line 130

def action?
  @ctcp_command == "ACTION"
end

- (Boolean) channel?

True if this message was sent in a channel

Returns:

  • (Boolean)

    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

Returns:

  • (Boolean)

    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



182
183
184
185
# File 'lib/cinch/message.rb', line 182

def ctcp_reply(answer)
  return unless ctcp?
  @user.notice "\001#{@ctcp_command} #{answer}\001"
end

- (Boolean) error?

True if the message describes an error

Returns:

  • (Boolean)

    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.

Returns:

  • (MatchData)


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] ||= ctcp_message.match(regexp)
  elsif type == :action
    @matches[:action][regexp] ||= action_message.match(regexp)
  else
    @matches[:other][regexp] ||= message.to_s.match(regexp)
  end
end

- (Boolean) numeric_reply?

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)



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 = 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)

This method returns an undefined value.

Replies to a message, automatically determining if it was a channel or a private message.

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



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_reply(text, prefix = false)

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



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

Returns:

Since:

  • 1.1.0



191
192
193
# File 'lib/cinch/message.rb', line 191

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