Class: Cinch::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#botBot (readonly)

Returns:



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

def bot
  @bot
end

#commandString

Returns:



9
10
11
# File 'lib/cinch/message.rb', line 9

def command
  @command
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#paramsArray<String>

Returns:



11
12
13
# File 'lib/cinch/message.rb', line 11

def params
  @params
end

#prefixString

Returns:



7
8
9
# File 'lib/cinch/message.rb', line 7

def prefix
  @prefix
end

#rawString

Returns:



5
6
7
# File 'lib/cinch/message.rb', line 5

def raw
  @raw
end

Instance Method Details

#channelChannel

Returns The channel in which this message was sent.

Returns:

  • (Channel)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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_argsArray<String>?

Returns:



126
127
128
129
# File 'lib/cinch/message.rb', line 126

def ctcp_args
  return unless ctcp?
  ctcp_message.split(" ")[1..-1]
end

#ctcp_commandString?

Returns the command part of an CTCP message.

Returns:

  • (String, nil)

    the command part of an CTCP message



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

def ctcp_command
  return unless ctcp?
  ctcp_message.split(" ").first
end

#ctcp_messageString?

Returns the CTCP message, without 001 control characters.

Returns:

  • (String, nil)

    the CTCP message, without 001 control characters



119
120
121
122
123
# File 'lib/cinch/message.rb', line 119

def ctcp_message
  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

#errorNumber?

Returns the numeric error code, if any.

Returns:

  • (Number, nil)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (MatchData)


110
111
112
113
114
115
116
# File 'lib/cinch/message.rb', line 110

def match(regexp, type)
  if type == :ctcp
    @matches[:ctcp][regexp] ||= ctcp_message.match(regexp)
  else
    @matches[:other][regexp] ||= message.to_s.match(regexp)
  end
end

#messageString?

Returns:



132
133
134
135
136
137
138
139
140
# File 'lib/cinch/message.rb', line 132

def message
  @message ||= begin
                 if error?
                   error.to_s
                 elsif regular_command?
                   params.last
                 end
               end
end

#numeric_reply?Boolean

opposed to a command)

Returns:

  • (Boolean)

    true if the message is an numeric reply (as



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

def numeric_reply?
  !!(@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.



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

Parameters:

  • text (String)

    the message

  • prefix (Boolean) (defaults to: false)

    if prefix is true and the message was in



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

Parameters:

  • text (String)

    the message

  • prefix (Boolean) (defaults to: false)

    if prefix is true and the message was in



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

#serverString?

Returns:



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_sString

Returns:



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

#userUser

Returns The user who sent this message.

Returns:

  • (User)

    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