ruby-net-nntp 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -1
- data/lib/net/nntp.rb +55 -17
- data/lib/net/nntp/version.rb +1 -1
- data/lib/net/nntp_article.rb +1 -1
- data/test/functional/test_nntp.rb +7 -0
- metadata +14 -6
data/CHANGELOG
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
2007-06-28 17:
|
1
|
+
2007-06-28 17:11 +0200 Anton 'tony' Bangratz <anton.bangratz@gmail.com> (43d551bd69a3 [tip])
|
2
|
+
|
3
|
+
* Rakefile, lib/net/nntp.rb, lib/net/nntp/version.rb:
|
4
|
+
moved version to separate file
|
5
|
+
|
6
|
+
2007-06-28 17:02 +0200 Anton 'tony' Bangratz <anton.bangratz@gmail.com> (650cf04a9829)
|
2
7
|
|
3
8
|
* lib/net/nntp.rb, lib/net/nntp_article.rb:
|
4
9
|
fixed bug in article overview parser
|
data/lib/net/nntp.rb
CHANGED
@@ -6,53 +6,92 @@ require 'timeout' # :nodoc:
|
|
6
6
|
require 'net/nntp_group'
|
7
7
|
require 'net/nntp_article'
|
8
8
|
require 'net/nntp/version.rb'
|
9
|
+
require 'rubygems'
|
10
|
+
require 'log4r'
|
9
11
|
|
10
12
|
|
11
13
|
module Net
|
12
14
|
class NNTP
|
13
15
|
include Timeout # :nodoc:
|
14
16
|
|
17
|
+
# Statuses of one-line responses
|
18
|
+
ONELINE_STATUSES = %w( 111 200 201 223 281 381 411 412 422 480 500 501 ).freeze
|
19
|
+
|
20
|
+
# Statuses of multiline responses
|
21
|
+
MULTILINE_STATUSES = %w( 100 215 220 221 222 224 ).freeze
|
22
|
+
|
15
23
|
# Error to indicate that NNTP Command failed gracefully
|
16
24
|
class CommandFailedError < StandardError
|
17
25
|
end
|
18
26
|
# Error to indicate that a Protocol Error occured during NNTP command execution
|
19
27
|
class ProtocolError < StandardError
|
20
28
|
end
|
29
|
+
|
30
|
+
|
21
31
|
attr_reader :socket, :grouplist, :overview_format
|
22
32
|
|
33
|
+
def self.logger=(logger)
|
34
|
+
@@logger = logger
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.logger
|
38
|
+
@@logger
|
39
|
+
end
|
40
|
+
|
41
|
+
def logger
|
42
|
+
@@logger
|
43
|
+
end
|
44
|
+
def debug(message)
|
45
|
+
@@logger.debug(message)
|
46
|
+
end
|
47
|
+
|
23
48
|
# initializes NNTP class with host and port
|
24
49
|
def initialize(host, port = 119)
|
25
50
|
@host = host
|
26
51
|
@port = port
|
27
52
|
@socket_class = TCPSocket
|
28
53
|
@group = nil
|
54
|
+
@@logger = Log4r::Logger['net::nntp'] || Log4r::Logger.new('net::nntp')
|
29
55
|
end
|
30
56
|
|
31
57
|
# Actually connects to NNTP host and port given in new()
|
32
58
|
def connect
|
33
59
|
@socket = @socket_class.new(@host, @port)
|
34
|
-
@welcome =
|
60
|
+
@welcome = read_response()
|
61
|
+
debug "Welcome: #{@welcome[0]} "
|
62
|
+
@welcome
|
35
63
|
end
|
36
64
|
|
37
65
|
# Closes connection. If not reconnected, subsequent calls of commands raise exceptions
|
38
66
|
def close
|
67
|
+
debug 'closing connection per request'
|
39
68
|
@socket.close unless socket.closed?
|
40
69
|
end
|
41
70
|
|
71
|
+
def has_xover?
|
72
|
+
!help.select {|i| i =~ /\bxover\b/i}.empty?
|
73
|
+
end
|
74
|
+
|
75
|
+
def has_over?
|
76
|
+
!help.select {|i| i =~ /\bover\b/i}.empty?
|
77
|
+
end
|
42
78
|
# Uses authinfo commands to authenticate. Timeout for first command is set to 10 seconds.
|
43
79
|
#
|
44
80
|
# Returns true on success, false on failure.
|
45
81
|
def authenticate(user, pass)
|
46
82
|
cmd = "authinfo user #{user}"
|
83
|
+
debug "Authenticating: Sending #{cmd}"
|
47
84
|
send_cmd cmd
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
85
|
+
response_array = read_response()
|
86
|
+
response = response_array[0]
|
87
|
+
debug "Authenticating: Response #{response}"
|
52
88
|
if response[0..2] == '381' then
|
53
89
|
cmd = "authinfo pass #{pass}"
|
90
|
+
debug "Authenticating: Sending #{cmd}"
|
54
91
|
send_cmd cmd
|
55
|
-
|
92
|
+
response_array = read_response()
|
93
|
+
response = response_array[0]
|
94
|
+
debug "Authenticating: Response #{response}"
|
56
95
|
end
|
57
96
|
return response && response[0..2] == '281'
|
58
97
|
end
|
@@ -63,14 +102,14 @@ module Net
|
|
63
102
|
#
|
64
103
|
def group(group)
|
65
104
|
send_cmd "group #{group}"
|
66
|
-
response =
|
67
|
-
responsecode, cnt, first, last, name = response.split
|
105
|
+
response = read_response(true)
|
106
|
+
responsecode, cnt, first, last, name = response[0].split
|
68
107
|
if responsecode == '211'
|
69
108
|
@group = Group.new(name)
|
70
109
|
@group.article_info = [cnt, first, last]
|
71
110
|
@group
|
72
111
|
else
|
73
|
-
raise CommandFailedError, response
|
112
|
+
raise CommandFailedError, response[0]
|
74
113
|
end
|
75
114
|
end
|
76
115
|
|
@@ -222,7 +261,7 @@ module Net
|
|
222
261
|
def last_or_next(cmd)
|
223
262
|
raise ProtocolError, "No group selected" unless @group
|
224
263
|
send_cmd(cmd)
|
225
|
-
response =
|
264
|
+
response = read_response()[0]
|
226
265
|
code = response[0..2]
|
227
266
|
article = @group.articles.create
|
228
267
|
case code
|
@@ -247,8 +286,8 @@ module Net
|
|
247
286
|
|
248
287
|
def create_grouplist(response)
|
249
288
|
@grouplist = {}
|
250
|
-
response.
|
251
|
-
next if
|
289
|
+
response.each_with_index do |line, idx|
|
290
|
+
next if idx == 0
|
252
291
|
break if line =~ /^\.\r\n$/
|
253
292
|
groupinfo = line.split
|
254
293
|
group = Group.new groupinfo.shift
|
@@ -259,20 +298,20 @@ module Net
|
|
259
298
|
end
|
260
299
|
|
261
300
|
def send_cmd(cmd)
|
301
|
+
debug "Sending: '#{cmd}'"
|
262
302
|
@socket.write(cmd+"\r\n")
|
263
303
|
end
|
264
304
|
|
265
|
-
def read_response
|
305
|
+
def read_response(force_oneline=false)
|
266
306
|
response = ''
|
267
|
-
queue = Queue.new
|
268
307
|
str = ''
|
269
308
|
ra = []
|
270
309
|
loop do
|
271
310
|
str = @socket.readline
|
272
|
-
queue << str
|
273
311
|
ra << str
|
274
|
-
break if str == ".\r\n" || !str
|
312
|
+
break if force_oneline || (str == ".\r\n" || !str || ONELINE_STATUSES.include?(str[0..2]) )
|
275
313
|
end
|
314
|
+
debug "Response: '#{ra}'"
|
276
315
|
ra
|
277
316
|
end
|
278
317
|
|
@@ -291,7 +330,6 @@ module Net
|
|
291
330
|
suffix
|
292
331
|
end
|
293
332
|
|
294
|
-
|
295
333
|
private :read_response, :numbers_or_id, :send_cmd, :last_or_next, :create_grouplist
|
296
334
|
|
297
335
|
end
|
data/lib/net/nntp/version.rb
CHANGED
data/lib/net/nntp_article.rb
CHANGED
@@ -9,6 +9,13 @@ module TestNet
|
|
9
9
|
class TestNNTP < Test::Unit::TestCase
|
10
10
|
|
11
11
|
def setup
|
12
|
+
unless @log
|
13
|
+
@log = Log4r::Logger.new('net::nntp')
|
14
|
+
fileout = Log4r::FileOutputter.new('net::nntp::fileout', :filename => File.join('logs', 'autotest.log'))
|
15
|
+
fileout.formatter = Log4r::PatternFormatter.new(:pattern => '%d [%5l] : %m')
|
16
|
+
@log.add fileout
|
17
|
+
@log.level = Log4r::ALL
|
18
|
+
end
|
12
19
|
nntp_connect_and_auth('localhost', 119, 'dummy', 'test')
|
13
20
|
end
|
14
21
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ruby-net-nntp
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.0.5
|
7
|
+
date: 2007-07-02 00:00:00 +02:00
|
8
8
|
summary: Net::XXX style NNTP Library for easy access.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -30,11 +30,11 @@ authors:
|
|
30
30
|
- Anton Bangratz
|
31
31
|
files:
|
32
32
|
- lib/net
|
33
|
+
- lib/net/nntp.rb
|
33
34
|
- lib/net/nntp_group.rb
|
34
|
-
- lib/net/nntp_article.rb
|
35
35
|
- lib/net/nntp
|
36
36
|
- lib/net/nntp/version.rb
|
37
|
-
- lib/net/
|
37
|
+
- lib/net/nntp_article.rb
|
38
38
|
- test/functional
|
39
39
|
- test/functional/test_nntp.rb
|
40
40
|
- test/mock
|
@@ -68,5 +68,13 @@ extensions: []
|
|
68
68
|
|
69
69
|
requirements: []
|
70
70
|
|
71
|
-
dependencies:
|
72
|
-
|
71
|
+
dependencies:
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: log4r
|
74
|
+
version_requirement:
|
75
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.0.0
|
80
|
+
version:
|