percy 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +1 -1
  2. data/VERSION +1 -1
  3. data/lib/percy.rb +56 -15
  4. metadata +2 -2
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Percy 1.1.1
1
+ # Percy 1.2.0
2
2
 
3
3
  ## Configuring and starting the bot
4
4
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.2.0
data/lib/percy.rb CHANGED
@@ -42,7 +42,7 @@ class Percy
42
42
  attr_accessor :traffic_logger, :connected
43
43
  end
44
44
 
45
- VERSION = 'Percy 1.1.1 (http://github.com/tbuehlmann/percy)'
45
+ VERSION = 'Percy 1.2.0 (http://github.com/tbuehlmann/percy)'
46
46
 
47
47
  Config = Struct.new(:server, :port, :password, :nick, :username, :verbose, :logging, :reconnect, :reconnect_interval)
48
48
 
@@ -143,20 +143,25 @@ class Percy
143
143
  @config.nick
144
144
  end
145
145
 
146
- # returns all users on a specific channel
146
+ # returns all users on a specific channel as array: ['Foo', 'bar', 'The_Librarian']
147
147
  def self.users_on(channel)
148
- self.add_observer
148
+ actual_length = self.add_observer
149
149
  self.raw "NAMES #{channel}"
150
+ channel = Regexp.escape(channel)
150
151
 
151
152
  begin
152
- Timeout::timeout(10) do # try 10 seconds to retrieve the users of <channel>
153
- start = 0
153
+ Timeout::timeout(30) do # try 30 seconds to retrieve the users of <channel>
154
+ start = actual_length
154
155
  ending = @temp_socket.length
156
+ users = []
155
157
 
156
158
  loop do
157
159
  for line in start..ending do
158
- if @temp_socket[line] =~ /^:\S+ 353 \S+ = #{channel} :/
159
- return $'.split(' ')
160
+ case @temp_socket[line]
161
+ when /^:\S+ 353 .+ #{channel} :/i
162
+ users << $'.split(' ')
163
+ when /^:\S+ 366 .+ #{channel}/i
164
+ return users.flatten.uniq.map { |element| element.gsub(/[!@%+]/, '') } # removing all modes
160
165
  end
161
166
  end
162
167
 
@@ -166,7 +171,41 @@ class Percy
166
171
  end
167
172
  end
168
173
  rescue Timeout::Error
169
- return false
174
+ return []
175
+ ensure
176
+ self.remove_observer
177
+ end
178
+ end
179
+
180
+ # returns all users on a specific channel as array (with status): ['@Foo', '+bar', 'The_Librarian', '!Frank']
181
+ def self.users_with_status_on(channel)
182
+ actual_length = self.add_observer
183
+ self.raw "NAMES #{channel}"
184
+ channel = Regexp.escape(channel)
185
+
186
+ begin
187
+ Timeout::timeout(30) do # try 30 seconds to retrieve the users of <channel>
188
+ start = actual_length
189
+ ending = @temp_socket.length
190
+ users = []
191
+
192
+ loop do
193
+ for line in start..ending do
194
+ case @temp_socket[line]
195
+ when /^:\S+ 353 .+ #{channel} :/i
196
+ users << $'.split(' ')
197
+ when /^:\S+ 366 .+ #{channel}/i
198
+ return users.flatten.uniq
199
+ end
200
+ end
201
+
202
+ sleep 0.25
203
+ start = ending
204
+ ending = @temp_socket.length
205
+ end
206
+ end
207
+ rescue Timeout::Error
208
+ return []
170
209
  ensure
171
210
  self.remove_observer
172
211
  end
@@ -174,17 +213,17 @@ class Percy
174
213
 
175
214
  # get the channel limit of a channel
176
215
  def self.channel_limit(channel)
177
- self.add_observer
216
+ actual_length = self.add_observer
178
217
  self.raw "MODE #{channel}"
179
218
 
180
219
  begin
181
220
  Timeout::timeout(10) do # try 10 seconds to retrieve l mode of <channel>
182
- start = 0
221
+ start = actual_length
183
222
  ending = @temp_socket.length
184
223
 
185
224
  loop do
186
225
  for line in start..ending do
187
- if @temp_socket[line] =~ /^:\S+ 324 \S+ #{channel} .*l.* (\d+)/
226
+ if @temp_socket[line] =~ /^:\S+ 324 \S+ #{Regexp.escape(channel)} .*l.* (\d+)/
188
227
  return $1.to_i
189
228
  end
190
229
  end
@@ -203,19 +242,19 @@ class Percy
203
242
 
204
243
  # check whether an user is online
205
244
  def self.is_online(nick)
206
- self.add_observer
245
+ actual_length = self.add_observer
207
246
  self.raw "WHOIS #{nick}"
208
247
 
209
248
  begin
210
249
  Timeout::timeout(10) do
211
- start = 0
250
+ start = actual_length
212
251
  ending = @temp_socket.length
213
252
 
214
253
  loop do
215
254
  for line in start..ending do
216
- if @temp_socket[line] =~ /^:\S+ 311 \S+ (#{nick}) /i
255
+ if @temp_socket[line] =~ /^:\S+ 311 \S+ (#{Regexp.escape(nick)}) /i
217
256
  return $1
218
- elsif line =~ /^:\S+ 401 \S+ #{nick} /i
257
+ elsif line =~ /^:\S+ 401 \S+ #{Regexp.escape(nick)} /i
219
258
  return false
220
259
  end
221
260
  end
@@ -252,6 +291,8 @@ class Percy
252
291
  @mutex_observer.synchronize do
253
292
  @observers += 1
254
293
  end
294
+
295
+ return @temp_socket.length - 1 # so the loop knows where to begin to search for patterns
255
296
  end
256
297
 
257
298
  # remove observer
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Tobias B\xC3\xBChlmann"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-29 00:00:00 +01:00
12
+ date: 2010-01-05 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency