Class: Net::NNTPClient

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

Defined Under Namespace

Classes: Version

Constant Summary collapse

VERSION =
[Version::MAJOR, Version::MINOR, Version::TINY].join('.')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(host, port) {|nntp| ... } ⇒ void

This method returns an undefined value.

A block interface for a NNTP session.

Parameters:

  • host (host)

    the hostname of the NNTP server

  • port (port)

    the port of the NNTP server

Yields:

  • (nntp)

Yield Parameters:


13
14
15
16
17
18
# File 'lib/net/nntp/client.rb', line 13

def self.start(host, port, &block)
  client = new
  client.connect(host, port)
  block.call(client) # TODO: wrap this in a begin/rescue/ensure block to ensure socket is closed
  client.close
end

Instance Method Details

#article(param = nil) ⇒ NNTPResponse

Note:

If param is nil, the current article will be selected

Select an article

Parameters:

  • param (defaults to: nil)

    the article number or message id

Returns:


167
168
169
# File 'lib/net/nntp/client.rb', line 167

def article(param = nil)
  request Net::NNTP::Article.new(param)
end

#authinfo_pass(password) ⇒ NNTPResponse

Send an AUTHINFO PASS request.

Parameters:

  • password (String)

    the password to send

Returns:


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

def authinfo_pass(password)
  request Net::NNTP::AuthInfoPass.new(password)
end

#authinfo_user(username) ⇒ NNTPResponse

Send an AUTHINFO USER request.

Parameters:

  • username (String)

    the username to send

Returns:


70
71
72
# File 'lib/net/nntp/client.rb', line 70

def authinfo_user(username)
  request Net::NNTP::AuthInfoUser.new(username)
end

#body(param = nil) ⇒ NNTPResponse

Get the body of an article.

Parameters:

  • param (Object) (defaults to: nil)

    Can be either the article number of the currently selected group, or a message-id. If nil, the latest article is chosen.

Returns:


152
153
154
# File 'lib/net/nntp/client.rb', line 152

def body(param = nil)
  request Net::NNTP::Body.new(param)
end

#capabilities(keyword = nil) ⇒ NNTPCapabilitiesResponse

Note:

they keyword argument is not used by RFC3977, but it is

Get the server's current capabilities

listed as an optional parameter of CAPABILITIES.


45
46
47
# File 'lib/net/nntp/client.rb', line 45

def capabilities(keyword = nil)
  request Net::NNTP::Capabilities.new(keyword)
end

#connect(host, port, use_ssl = false) ⇒ NNTPGreetingResponse

Connect to a NNTP session.

Parameters:

  • host (String)

    the hostname of the NNTP server

  • port (Integer)

    the port of the NNTP server

  • use_ssl (Boolean) (defaults to: false)

Returns:


28
29
30
31
32
33
34
35
36
# File 'lib/net/nntp/client.rb', line 28

def connect(host, port, use_ssl = false)
  @socket = TCPSocket.new(host, port.to_i)
  if use_ssl
    @socket = OpenSSL::SSL::SSLSocket.new(@socket)
    @socket.sync_close
    @socket.connect
  end
  read_greeting
end

#dateNNTPDateResponse

Get the current time.

Returns:


91
92
93
# File 'lib/net/nntp/client.rb', line 91

def date
  request Net::NNTP::Date.new
end

#group(group) ⇒ NNTPResponse

Select a group.

Parameters:

  • group (String)

    the group to be selected

Returns:


102
103
104
# File 'lib/net/nntp/client.rb', line 102

def group(group)
  request Net::NNTP::Group.new(group)
end

#head(param = nil) ⇒ NNTPResponse

Get the head of an article.

Parameters:

  • param (Object) (defaults to: nil)

    Can be either the article number of the currently selected group, or a message-id. If nil, the latest article is chosen.

Returns:


136
137
138
# File 'lib/net/nntp/client.rb', line 136

def head(param = nil)
  request Net::NNTP::Head.new(param)
end

#helpNNTPHelpResponse

Get the server's help info.

Returns:


215
216
217
# File 'lib/net/nntp/client.rb', line 215

def help
  request Net::NNTP::Help.new
end

#ihave(message_id, article = nil) {|response, article| ... } ⇒ NNTPResponse

Notify the server that an article is available to be sent.

This method can be used in one of two ways. Either by supplying the article via the method parameter, or by using the block interface. The block interface is intended to be used if the first first stage IHAVE response is needed. Also note that if the first IHAVE response is a Net::NNTPErrorResponse, the block will not be executed and the first response will be returned.

Parameters:

  • message_id (String)

    the message-id of the article

  • article (NNTPArticle) (defaults to: nil)

    the article to be posted

Yields:

  • (response, article)

    the (optional block interface)

Yield Parameters:

Returns:


275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/net/nntp/client.rb', line 275

def ihave(message_id, article = nil, &block)
  resp = request Net::NNTP::IHaveFirstStage.new(message_id)
  return resp if resp.is_a?(Net::NNTPErrorResponse)

  if block_given?
    article = NNTPArticle.new
    block.call(resp, article)
  end

  write_long(article)
  read_response(Net::NNTP::IHaveSecondStage.new)
end

#lastNNTPResponse

Get the stat line for the last article.


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

def last
  request Net::NNTP::Last.new
end

#listgroup(group = nil, range = nil) ⇒ NNTPResponse

Note:

range is a newer NNTP feature and may not be supported

List the articles of a group

Parameters:

  • group (String) (defaults to: nil)

    the group to be selected. If nil, the currently selected group is used.

  • range (Range) (defaults to: nil)

    the range of articles to select. If nil, all articles are selected.

Returns:


120
121
122
# File 'lib/net/nntp/client.rb', line 120

def listgroup(group = nil, range = nil)
  request Net::NNTP::ListGroup.new(group, range)
end

#mode_readerNNTPResponse

Switch mode (for use on a mode-switching server)


56
57
58
# File 'lib/net/nntp/client.rb', line 56

def mode_reader
  request Net::NNTP::ModeReader.new
end

#nextNNTPResponse

Get the stat line for the next article.


195
196
197
# File 'lib/net/nntp/client.rb', line 195

def next
  request Net::NNTP::Next.new
end

#post(article = nil) {|response, article| ... } ⇒ NNTPResponse

Post an article.

This method can be used in one of two ways, by directly supplying and article to be posted via the method parameter, or by using the block interface. The block interface is intented to be used if the user has interest in the first stage POST response. Also note that if the first POST response is a Net::NNTPErrorResponse, the block will not be executed and the first response will be returned.

Parameters:

  • article (NNTPArticle) (defaults to: nil)

    the article to be posted

Yields:

  • (response, article)

    the (optional) block interface

Yield Parameters:

Returns:


239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/net/nntp/client.rb', line 239

def post(article = nil, &block)
  resp = request Net::NNTP::Post.new
  return resp if resp.is_a?(Net::NNTPErrorResponse)

  if block_given?
    article = NNTPArticle.new
    block.call(resp, article)
  end

  write_long(article.to_s)
  read_response(Net::NNTP::Post.new)
end

#quitNNTPQuitResponse

Note:

Any subsequent requests will result in a raised Exception.

Close the connection.

Returns:


294
295
296
# File 'lib/net/nntp/client.rb', line 294

def quit
  request Net::NNTP::Quit.new
end

#stat(param = nil) ⇒ NNTPResponse

Get the stat line for an article.

Parameters:

  • param (Object) (defaults to: nil)

    Can be either the article number of the currently selected group, or a message-id. If nil, the latest article is chosen.

Returns:


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

def stat(param = nil)
  request Net::NNTP::Stat.new(param)
end