Class: Net::NNTP

Inherits:
Object
  • Object
show all
Defined in:
lib/net/nntp.rb,
lib/net/nntp/request.rb,
lib/net/nntp/version.rb,
lib/net/nntp/response.rb

Overview

Base class for connecting to an NNTP server and handling requests and responses.

Defined Under Namespace

Modules: BodyBaseResponse Classes: Article, ArticleBaseResponse, ArticleNotWanted, ArticleReceived, ArticleResponse, ArticleSelected, AuthenticationAccepted, AuthenticationFailed, AuthenticationOutOfSequence, AuthenticationRequired, Authinfo, Body, BodyResponse, Capabilities, CapabilityList, ConnectionClosing, ContinueResponse, Date, DateResponse, EncodingError, FailResponse, FeatureNotProvided, FormatHeader, GenericError, Group, GroupListResponse, GroupSelected, GroupUnknown, Hdr, HdrResponse, Head, HeaderResponse, Help, HelpResponse, Ihave, InformationRequest, InformationResponse, InternalFault, InvalidArticle, InvalidNumberOrRange, Last, List, ListInformationFollows, Listgroup, Modereader, Newgroups, NewgroupsResponse, Newnews, NewnewsResponse, Next, NoGroupSelected, NoNextArticle, NoPreviousArticle, NoSuchMessageid, NotImplemented, OKResponse, Over, OverviewInformation, ParameterError, PasswordRequired, PermanentlyUnavailable, Post, PostArticle, PostingAllowed, PostingFailed, PostingNotPermitted, PostingProhibited, PostingRequest, PrivacyRequired, ProtocolError, Quit, Request, Response, RetrievalRequest, RetryResponse, SelectionRequest, Stat, SyntaxError, TemporarilyUnavailable, TransferArticle, TransferNotPossible, TransferOK, TransferRejected, UnknownResponse, Xhdr, Xover, Xpat

Constant Summary collapse

VERSION =
"1.0.0"
RESPONSES =
{
    '100' => HelpResponse,
    '101' => CapabilityList,
    '111' => DateResponse,

    '200' => PostingAllowed,
    '201' => PostingProhibited,
    '205' => ConnectionClosing,
    '211' => GroupSelected,
    '215' => ListInformationFollows,
    '220' => ArticleResponse,
    '221' => HeaderResponse,
    '222' => BodyResponse,
    '223' => ArticleSelected,
    '224' => OverviewInformation,
    '225' => HdrResponse,
    '230' => NewnewsResponse,
    '231' => NewgroupsResponse,
    '235' => TransferOK,
    '240' => ArticleReceived,
    '281' => AuthenticationAccepted,

    '335' => TransferArticle,
    '340' => PostArticle,
    '381' => PasswordRequired,

    '400' => TemporarilyUnavailable,
    '403' => InternalFault,
    '411' => GroupUnknown,
    '412' => NoGroupSelected,
    '420' => InvalidArticle,
    '421' => NoNextArticle,
    '422' => NoPreviousArticle,
    '423' => InvalidNumberOrRange,
    '430' => NoSuchMessageid,
    '435' => ArticleNotWanted,
    '436' => TransferNotPossible,
    '437' => TransferRejected,
    '440' => PostingNotPermitted,
    '441' => PostingFailed,
    '480' => AuthenticationRequired,
    '481' => AuthenticationFailed,
    '482' => AuthenticationOutOfSequence,
    '483' => PrivacyRequired,

    '500' => NotImplemented,
    '501' => SyntaxError,
    '502' => PermanentlyUnavailable,
    '503' => FeatureNotProvided,
    '504' => EncodingError
}
CLASSES =
{ 
  '1' => InformationResponse, 
  '2' => OKResponse,
  '3' => ContinueResponse,
  '4' => RetryResponse,
  '5' => FailResponse
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ NNTP

Initializes an NNTP instance.

Takes an option hash as argument. Keys being processed are :host, :port and :timeout



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/net/nntp.rb', line 41

def initialize(args={})
  @host = args[:host] || default_host()
  @port = args[:port] || default_port()
  @timeout = args[:timeout] || default_timeout()
  @current_request = nil
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Attribute Details

#current_articleObject (readonly)

Returns the value of attribute current_article.



20
21
22
# File 'lib/net/nntp.rb', line 20

def current_article
  @current_article
end

#current_groupObject (readonly)

Returns the value of attribute current_group.



20
21
22
# File 'lib/net/nntp.rb', line 20

def current_group
  @current_group
end

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/net/nntp.rb', line 19

def host
  @host
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



20
21
22
# File 'lib/net/nntp.rb', line 20

def last_response
  @last_response
end

#portObject

Returns the value of attribute port.



19
20
21
# File 'lib/net/nntp.rb', line 19

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



19
20
21
# File 'lib/net/nntp.rb', line 19

def timeout
  @timeout
end

Class Method Details

.loggerObject

Accessor for the Log4r style logger.



29
30
31
# File 'lib/net/nntp.rb', line 29

def self.logger
  @@logger
end

.logger=(logger) ⇒ Object

Setter for a Log4r style logger.



24
25
26
# File 'lib/net/nntp.rb', line 24

def self.logger=(logger)
  @@logger = logger
end

Instance Method Details

#article(opts = nil) ⇒ Object

Proxy for Net::NNTP::Article requests and processing.

See Net::NNTP::Article for parameters

Returns the response.



172
173
174
175
# File 'lib/net/nntp.rb', line 172

def article(opts=nil)
  request = Article.new(opts)
  process(request)
end

#authenticate(user, pass) ⇒ Object

Proxy for authentication request/response cycles.

See Net::NNTP::Authinfo for parameters.

Returns true if the authentication has been accepted.



156
157
158
159
160
161
162
163
164
165
# File 'lib/net/nntp.rb', line 156

def authenticate(user, pass)
  @credentials = [user, pass]
  auth_request = Authinfo.new('user', user)
  if PasswordRequired === process(auth_request)
    auth_request = Authinfo.new('pass', pass)
    AuthenticationAccepted === process(auth_request)
  else
    false
  end
end

#body(opts) ⇒ Object

Proxy for Net::NNTP::Body requests and processing.

See Net::NNTP::Body for parameters

Returns the response.



182
183
184
185
# File 'lib/net/nntp.rb', line 182

def body(opts)
  request = Body.new(opts)
  process(request)
end

#closeObject



84
85
86
# File 'lib/net/nntp.rb', line 84

def close
  @socket.close unless @socket.closed?
end

#connectObject

Sets up the connection to a TCPSocket, using #host and #port accessors.

To use different server and port values, use the accessors.

Reads and returns the response (should be a PostingOK or PostingProhibited response, or a generic response).

Usage Example

nntp = Net::NNTP.new
nntp.server = 'my.server.example.com'
nntp.port = 22119
if nntp.connect
  # ... do stuff ...
end


69
70
71
72
73
74
# File 'lib/net/nntp.rb', line 69

def connect 
  @socket = TCPSocket.new(host(), port());
  @connected = true
  @current_request = nil
  @last_response = read_response()
end

#connected?Boolean

Returns true if #connect was successful and the socket is connected.

Returns:

  • (Boolean)


89
90
91
# File 'lib/net/nntp.rb', line 89

def connected?
  @socket && !@socket.closed? && @connected
end

#debug(message) ⇒ Object

Shortcut for Net::NNTP.logger.debug(message)



34
35
36
# File 'lib/net/nntp.rb', line 34

def debug(message)
  Net::NNTP.logger.debug(message)
end

#group(name) ⇒ Object

Proxy for Net::NNTP::Group requests and processing.

See Net::NNTP::Group for parameters

Returns the response.



222
223
224
# File 'lib/net/nntp.rb', line 222

def group(name)
  process Group.new(name)
end

#hdr(field, opts) ⇒ Object

Proxy for Net::NNTP::Hdr requests and processing.

See Net::NNTP::Hdr for parameters

Returns the response.



265
266
267
268
# File 'lib/net/nntp.rb', line 265

def hdr(field, opts)
  request = Hdr.new(field, opts)
  process(request)
end

#head(opts) ⇒ Object

Proxy for Net::NNTP::Head requests and processing.

See Net::NNTP::Head for parameters

Returns the response.



192
193
194
195
# File 'lib/net/nntp.rb', line 192

def head(opts)
  request = Head.new(opts)
  process(request)
end

#helpObject

Proxy for Net::NNTP::Help requests and processing.

Returns the response.



200
201
202
# File 'lib/net/nntp.rb', line 200

def help
  process Help.new
end

#ihave(id, body) ⇒ Object

Proxy for Net::NNTP::Ihave requests and processing.

See Net::NNTP::Ihave for parameters

Returns the response.



319
320
321
322
323
# File 'lib/net/nntp.rb', line 319

def ihave(id, body)
  request = Ihave.new id
  request.body = body
  process(request)
end

#lastObject

Proxy for Net::NNTP::Last requests and processing.

Returns the response.



207
208
209
# File 'lib/net/nntp.rb', line 207

def last
  process Last.new
end

#list(keyword = nil, rest = {}) ⇒ Object

Proxy for Net::NNTP::List requests and processing.

See Net::NNTP::List for parameters

Returns the response.



231
232
233
# File 'lib/net/nntp.rb', line 231

def list(keyword=nil, rest={})
  process List.new(keyword, rest)
end

#listgroup(groupname = nil, range = {}) ⇒ Object

Proxy for Net::NNTP::Listgroup request and processing

See Net::NNTP::Listgroup for parameters

Returns the response.



240
241
242
# File 'lib/net/nntp.rb', line 240

def listgroup(groupname=nil, range={})
  process Listgroup.new(groupname, range)
end

#nextObject

Proxy for Net::NNTP::Next requests and processing.



213
214
215
# File 'lib/net/nntp.rb', line 213

def next
  process Next.new
end

#over(range = nil) ⇒ Object

Proxy for Net::NNTP::Over requests and processing.

See Net::NNTP::Over for parameters

Returns the response.



286
287
288
289
# File 'lib/net/nntp.rb', line 286

def over(range=nil)
  request = Over.new(range)
  process(request)
end

#post(body) ⇒ Object

Proxy for Net::NNTP::Post requests and processing.

See Net::NNTP::Post for parameters

Returns the response.



307
308
309
310
311
# File 'lib/net/nntp.rb', line 307

def post(body)
  request = Post.new
  request.body = body
  process(request)
end

#process(request) ⇒ Object

Processes a request.

request must be a Net::NNTP::Request subclass. Certain requests (Authinfo and requests that require a two-stage response/request cycle will try to process these two stages gracefully. For multiline responses, the body will be processed, too.

Net::NNTP::GroupSelected responses will be recorded in current_group, Net::NNTP::ArticleSelected responses in current_article.

Returns the last response.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/net/nntp.rb', line 120

def process(request)
  if connected?
    @current_request = request
    debug("Sending #{request.command}")
    @socket.write(request.command)
    @last_response = read_response()
    if @last_response.needs_article?
      debug "Sending article: #{request.dotstuff}"
      request.dotstuff.split(/\r?\n/).each do |line|
        line << "\r\n"
        @socket.write line
      end
      @socket.write ".\r\n"
      @last_response = read_response()
    end
    if (@last_response.multiline?)
      debug "Response is multiline"
      @last_response.body = @socket
    end
    @socket.close if @last_response.force_close?
    @last_response
    if (GroupSelected === @last_response)
      @current_group = @last_response
    elsif (ArticleSelected === @last_response)
      @current_article = @last_response
    else 
      @last_response
    end
  end
end

#quitObject

Proxy for Net::NNTP::Quit requests and processing.



246
247
248
# File 'lib/net/nntp.rb', line 246

def quit
  process Quit.new
end

#reconnectObject



76
77
78
79
80
81
82
# File 'lib/net/nntp.rb', line 76

def reconnect
  close unless @socket.closed?
  connect
  authenticate *@credentials if @credentials
  process @current_group.request if @current_group
  process @current_article.request if @current_article
end

#stat(opts = nil) ⇒ Object

Proxy for Net::NNTP::Stat requests and processing.

See Net::NNTP::Stat for parameters

Returns the response.



255
256
257
258
# File 'lib/net/nntp.rb', line 255

def stat(opts=nil)
  request = Stat.new(opts)
  process(request)
end

#xhdr(field, opts) ⇒ Object

Proxy for Net::NNTP::Xhdr requests and processing.

See Net::NNTP::Xhdr for parameters

Returns the response.



275
276
277
278
# File 'lib/net/nntp.rb', line 275

def xhdr(field, opts)
  request = Xhdr.new(field, opts)
  process(request)
end

#xover(range = nil) ⇒ Object

Proxy for Net::NNTP::Xover requests and processing.

See Net::NNTP::Xover for parameters

Returns the response.



296
297
298
299
# File 'lib/net/nntp.rb', line 296

def xover(range=nil)
  request = Xover.new(range)
  process(request)
end