cinch 2.0.0.pre.2 → 2.0.0.pre.3

Sign up to get free protection for your applications and to get access to all the features.
data/docs/readme.md CHANGED
@@ -16,4 +16,5 @@ The following is a list of important documents to read.
16
16
  - {file:docs/encodings.md Encodings}
17
17
  - {file:docs/logging.md Logging}
18
18
  - {Cinch::SASL SASL}
19
+ - {Cinch::Formatting Formatting messages}
19
20
  - {file:docs/events.md Events}
@@ -1,5 +1,11 @@
1
1
  module Cinch
2
- # @since 2.0.0
2
+ # This module can be used for adding colors and formatting to
3
+ # messages.
4
+ #
5
+ # The format codes used are those defined by mIRC, which are also
6
+ # the ones supported by most clients.
7
+ #
8
+ # For usage instructions and examples, see {.format}.
3
9
  #
4
10
  # List of valid colors
5
11
  # =========================
@@ -30,6 +36,8 @@ module Cinch
30
36
  # Other
31
37
  # =====
32
38
  # - reset (Resets all formatting to the client's defaults)
39
+ #
40
+ # @since 2.0.0
33
41
  module Formatting
34
42
  # @private
35
43
  Colors = {
data/lib/cinch/message.rb CHANGED
@@ -8,29 +8,18 @@ module Cinch
8
8
  #
9
9
  # At the same time, it allows **responding** to messages, which
10
10
  # means sending messages to either users or channels.
11
- #
12
- # @attr_reader user
13
- # @attr_reader error
14
- # @attr_reader message
15
- # @attr_reader server
16
- # @attr_reader target
17
- # @attr_reader channel
18
- # @attr_reader action_message
19
- # @attr_reader ctcp_args
20
- # @attr_reader ctcp_command
21
- # @attr_reader ctcp_message
22
11
  class Message
23
12
  # @return [String]
24
- attr_accessor :raw
13
+ attr_reader :raw
25
14
 
26
15
  # @return [String]
27
- attr_accessor :prefix
16
+ attr_reader :prefix
28
17
 
29
18
  # @return [String]
30
- attr_accessor :command
19
+ attr_reader :command
31
20
 
32
21
  # @return [Array<String>]
33
- attr_accessor :params
22
+ attr_reader :params
34
23
 
35
24
  # @return [Array<Symbol>]
36
25
  attr_reader :events
@@ -45,6 +34,37 @@ module Cinch
45
34
  # @since 1.1.0
46
35
  attr_reader :bot
47
36
 
37
+ # @return [User] The user who sent this message
38
+ attr_reader :user
39
+
40
+ # @return [String, nil]
41
+ attr_reader :server
42
+
43
+ # @return [Integer, nil] the numeric error code, if any
44
+ attr_reader :error
45
+
46
+ # @return [String, nil] the command part of an CTCP message
47
+ attr_reader :ctcp_command
48
+
49
+ # @return [Channel] The channel in which this message was sent
50
+ attr_reader :channel
51
+
52
+ # @return [String, nil] the CTCP message, without \001 control characters
53
+ attr_reader :ctcp_message
54
+
55
+ # @return [Array<String>, nil]
56
+ attr_reader :ctcp_args
57
+
58
+ # @return [String, nil]
59
+ attr_reader :message
60
+
61
+ # @return [String, nil] The action message
62
+ # @since 2.0.0
63
+ attr_reader :action_message
64
+
65
+ # @return [Target]
66
+ attr_reader :target
67
+
48
68
  def initialize(msg, bot)
49
69
  @raw = msg
50
70
  @bot = bot
@@ -62,40 +82,24 @@ module Cinch
62
82
 
63
83
  if @bot.irc.network.ngametv?
64
84
  if @prefix != "ngame"
65
- @prefix = "%s!user@host" % [@prefix, @prefix, @prefix]
85
+ @prefix = "%s!%s@%s" % [@prefix, @prefix, @prefix]
66
86
  end
67
87
  end
68
88
 
69
- raw_params.strip!
70
- if match = raw_params.match(/(?:^:| :)(.*)$/)
71
- @params = match.pre_match.split(" ")
72
- @params << match[1]
73
- else
74
- @params = raw_params.split(" ")
75
- end
76
- end
89
+ @params = parse_params(raw_params)
77
90
 
78
- # @return [User] The user who sent this message
79
- def user
80
- return unless @prefix
81
- nick = @prefix[/^(\S+)!/, 1]
82
- user = @prefix[/^\S+!(\S+)@/, 1]
83
- host = @prefix[/@(\S+)$/, 1]
91
+ @user = parse_user
92
+ @channel = parse_channel
93
+ @target = @user || @channel
94
+ @server = parse_server
95
+ @error = parse_error
96
+ @message = parse_message
84
97
 
85
- return nil if nick.nil?
86
- @user ||= @bot.user_list.find_ensured(user, nick, host)
87
- end
88
-
89
- # @return [String, nil]
90
- def server
91
- return unless @prefix
92
- return if @prefix.match(/[@!]/)
93
- @server ||= @prefix[/^(\S+)/, 1]
94
- end
98
+ @ctcp_message = parse_ctcp_message
99
+ @ctcp_command = parse_ctcp_command
100
+ @ctcp_args = parse_ctcp_args
95
101
 
96
- # @return [Integer, nil] the numeric error code, if any
97
- def error
98
- @error ||= (command.to_i if numeric_reply? && command[/[45]\d\d/])
102
+ @action_message = parse_action_message
99
103
  end
100
104
 
101
105
  # @group Type checking
@@ -103,68 +107,32 @@ module Cinch
103
107
  # @return [Boolean] true if the message is an numeric reply (as
104
108
  # opposed to a command)
105
109
  def numeric_reply?
106
- !!(@numeric_reply ||= @command.match(/^\d{3}$/))
110
+ !!@command.match(/^\d{3}$/)
107
111
  end
108
112
 
109
113
  # @return [Boolean] true if the message describes an error
110
114
  def error?
111
- !!error
115
+ !@error.nil?
112
116
  end
113
117
 
114
118
  # @return [Boolean] true if this message was sent in a channel
115
119
  def channel?
116
- !!channel
120
+ !@channel.nil?
117
121
  end
118
122
 
119
123
  # @return [Boolean] true if the message is an CTCP message
120
124
  def ctcp?
121
- !!(params.last =~ /\001.+\001/)
125
+ !!(@params.last =~ /\001.+\001/)
122
126
  end
123
127
 
124
128
  # @return [Boolean] true if the message is an action (/me)
125
129
  # @since 2.0.0
126
130
  def action?
127
- ctcp_command == "ACTION"
131
+ @ctcp_command == "ACTION"
128
132
  end
129
133
 
130
134
  # @endgroup
131
135
 
132
- # @return [String, nil] The action message
133
- # @since 2.0.0
134
- def action_message
135
- return nil unless action?
136
- ctcp_message.split(" ", 2).last
137
- end
138
-
139
- # @return [String, nil] the command part of an CTCP message
140
- def ctcp_command
141
- return unless ctcp?
142
- ctcp_message.split(" ").first
143
- end
144
-
145
- # @return [Channel] The channel in which this message was sent
146
- def channel
147
- @channel ||= begin
148
- case command
149
- when "INVITE", Constants::RPL_CHANNELMODEIS.to_s, Constants::RPL_BANLIST.to_s
150
- @bot.channel_list.find_ensured(params[1])
151
- when Constants::RPL_NAMEREPLY.to_s
152
- @bot.channel_list.find_ensured(params[2])
153
- else
154
- if params.first.start_with?("#")
155
- @bot.channel_list.find_ensured(params.first)
156
- elsif numeric_reply? and params[1].start_with?("#")
157
- @bot.channel_list.find_ensured(params[1])
158
- end
159
- end
160
- end
161
- end
162
-
163
- # @return [Target]
164
- def target
165
- channel || user
166
- end
167
-
168
136
  # @api private
169
137
  # @return [MatchData]
170
138
  def match(regexp, type)
@@ -177,30 +145,6 @@ module Cinch
177
145
  end
178
146
  end
179
147
 
180
- # @return [String, nil] the CTCP message, without \001 control characters
181
- def ctcp_message
182
- return unless ctcp?
183
- params.last =~ /\001(.+)\001/
184
- $1
185
- end
186
-
187
- # @return [Array<String>, nil]
188
- def ctcp_args
189
- return unless ctcp?
190
- ctcp_message.split(" ")[1..-1]
191
- end
192
-
193
- # @return [String, nil]
194
- def message
195
- @message ||= begin
196
- if error?
197
- error.to_s
198
- elsif regular_command?
199
- params.last
200
- end
201
- end
202
- end
203
-
204
148
  # @group Replying
205
149
 
206
150
  # Replies to a message, automatically determining if it was a
@@ -213,11 +157,11 @@ module Cinch
213
157
  # @return [void]
214
158
  def reply(text, prefix = false)
215
159
  text = text.to_s
216
- if channel && prefix
160
+ if @channel && prefix
217
161
  text = text.split("\n").map {|l| "#{user.nick}: #{l}"}.join("\n")
218
162
  end
219
163
 
220
- target.send(text)
164
+ @target.send(text)
221
165
  end
222
166
 
223
167
  # Like #reply, but using {Target#safe_send} instead
@@ -227,9 +171,9 @@ module Cinch
227
171
  def safe_reply(text, prefix = false)
228
172
  text = text.to_s
229
173
  if channel && prefix
230
- text = "#{user.nick}: #{text}"
174
+ text = "#{@user.nick}: #{text}"
231
175
  end
232
- target.safe_send(text)
176
+ @target.safe_send(text)
233
177
  end
234
178
 
235
179
  # Reply to a CTCP message
@@ -237,7 +181,7 @@ module Cinch
237
181
  # @return [void]
238
182
  def ctcp_reply(answer)
239
183
  return unless ctcp?
240
- user.notice "\001#{ctcp_command} #{answer}\001"
184
+ @user.notice "\001#{@ctcp_command} #{answer}\001"
241
185
  end
242
186
 
243
187
  # @endgroup
@@ -245,12 +189,96 @@ module Cinch
245
189
  # @return [String]
246
190
  # @since 1.1.0
247
191
  def to_s
248
- "#<Cinch::Message @raw=#{raw.chomp.inspect} @params=#{@params.inspect} channel=#{channel.inspect} user=#{user.inspect}>"
192
+ "#<Cinch::Message @raw=#{@raw.chomp.inspect} @params=#{@params.inspect} channel=#{@channel.inspect} user=#{@user.inspect}>"
249
193
  end
250
194
 
251
195
  private
252
196
  def regular_command?
253
197
  !numeric_reply? # a command can only be numeric or "regular"…
254
198
  end
199
+
200
+ def parse_params(raw_params)
201
+ raw_params = raw_params.strip
202
+ params = []
203
+ if match = raw_params.match(/(?:^:| :)(.*)$/)
204
+ params = match.pre_match.split(" ")
205
+ params << match[1]
206
+ else
207
+ params = raw_params.split(" ")
208
+ end
209
+
210
+ return params
211
+ end
212
+
213
+ def parse_user
214
+ return unless @prefix
215
+ nick = @prefix[/^(\S+)!/, 1]
216
+ user = @prefix[/^\S+!(\S+)@/, 1]
217
+ host = @prefix[/@(\S+)$/, 1]
218
+
219
+ return nil if nick.nil?
220
+ return @bot.user_list.find_ensured(user, nick, host)
221
+ end
222
+
223
+ def parse_channel
224
+ # has to be called after parse_params
225
+ case @command
226
+ when "INVITE", Constants::RPL_CHANNELMODEIS.to_s, Constants::RPL_BANLIST.to_s
227
+ @bot.channel_list.find_ensured(@params[1])
228
+ when Constants::RPL_NAMEREPLY.to_s
229
+ @bot.channel_list.find_ensured(@params[2])
230
+ else
231
+ if @params.first.start_with?("#")
232
+ @bot.channel_list.find_ensured(@params.first)
233
+ elsif numeric_reply? and @params[1].start_with?("#")
234
+ @bot.channel_list.find_ensured(@params[1])
235
+ end
236
+ end
237
+ end
238
+
239
+ def parse_server
240
+ return unless @prefix
241
+ return if @prefix.match(/[@!]/)
242
+ return @prefix[/^(\S+)/, 1]
243
+ end
244
+
245
+ def parse_error
246
+ return @command.to_i if numeric_reply? && @command[/[45]\d\d/]
247
+ end
248
+
249
+ def parse_message
250
+ # has to be called after parse_params
251
+ if error?
252
+ @error.to_s
253
+ elsif regular_command?
254
+ @params.last
255
+ end
256
+ end
257
+
258
+ def parse_ctcp_message
259
+ # has to be called after parse_params
260
+ return unless ctcp?
261
+ @params.last =~ /\001(.+)\001/
262
+ $1
263
+ end
264
+
265
+ def parse_ctcp_command
266
+ # has to be called after parse_ctcp_message
267
+ return unless ctcp?
268
+ @ctcp_message.split(" ").first
269
+ end
270
+
271
+ def parse_ctcp_args
272
+ # has to be called after parse_ctcp_message
273
+ return unless ctcp?
274
+ @ctcp_message.split(" ")[1..-1]
275
+ end
276
+
277
+ def parse_action_message
278
+ # has to be called after parse_ctcp_message
279
+ return nil unless action?
280
+ @ctcp_message.split(" ", 2).last
281
+ end
282
+
255
283
  end
256
284
  end
data/lib/cinch/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Cinch
2
2
  # Version of the library
3
- VERSION = '2.0.0-pre.2'
3
+ VERSION = '2.0.0-pre.3'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.2
4
+ version: 2.0.0.pre.3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-05 00:00:00.000000000 Z
12
+ date: 2012-03-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple, friendly DSL for creating IRC bots
15
15
  email: