class NNTP::Session
@!attribute [r] article
The current article pointed to by the server or nil if unset. @return [NNTP::Article, nil]
Attributes
Public Class Methods
@option options [NNTP::Connection, NNTP::SSLConnection] :connection
The connection object.
# File lib/nntp/session.rb, line 17 def initialize(options) @group = nil @article = nil @connection = options.fetch(:connection) do raise ArgumentError ":connection missing" end check_initial_status end
Public Instance Methods
Authenticate to the server.
@option args :user Username @option args :pass Password @option args :type (:standard)
Which authentication type to use. Currently only standard is supported.
@return [NNTP::Status]
# File lib/nntp/session.rb, line 34 def auth(args) auth_method = args.fetch(:type, :standard) standard_auth(args) if auth_method == :standard end
@!attribute [rw] group
Retrieves current group, or sets current group, server-side, to assigned value. @param [String] group_name The name of the group to be selected. @return [NNTP::Group] The current group.
# File lib/nntp/session.rb, line 51 def group=(group_name) connection.command(:group, group_name) if status[:code] == 211 num, low, high, name = status[:msg].split @group = group_factory(name, low.to_i, high.to_i, num.to_i) end end
Fetches and returns the list of groups from the server or, if it has already been fetched, returns the saved list. @return [Array<NNTP::Group>]
# File lib/nntp/session.rb, line 42 def groups @groups ||= fetch_groups end
Fetch list of article numbers from a given group. @param group The name of the group to list defaults to {#group group
.name} @param range (nil) If given, specifies the range of messages to retrieve @return [Array<NNTP::Article>] The list of messages
(only the article numbers will be populated).
@see tools.ietf.org/html/rfc3977#section-6.1.2
# File lib/nntp/session.rb, line 65 def listgroup(*args) messages = [] connection.query(:listgroup, *args) do |status, data| if status[:code] == 211 data.each do |line| message = Article.new message.num = line.to_i messages << message end end end messages end
(see NNTP::Connection#quit
)
# File lib/nntp/session.rb, line 124 def quit connection.quit end
Fetch ID and/or Number of an article.
If an article number is passed, the current article is changed to the selected article. If an ID is passed, the current article is NOT changed. @param id|num The article-id or article number to be selected. @return [NNTP::Article, nil] a Article struct or nil upon failure.
# File lib/nntp/session.rb, line 86 def stat(identifier = nil) params = [:stat] params += [identifier.to_s] unless identifier.nil? connection.command(*params) if status.code == 223 message = article_from_status(status.msg) if identifier.to_i != 0 @article = message end message else nil end end
The most recent status from the server.
# File lib/nntp/session.rb, line 119 def status connection.status end
Fetch list of messages from current group. @return [Array<NNTP::Article>] The list of messages
(The numbers AND the subjects will be populated).
# File lib/nntp/session.rb, line 103 def subjects subjects = [] range ="#{group[:first_message]}-" connection.query(:xhdr, "Subject", range) do |status, data| if status[:code] == 221 data.each do |line| message = Article.new message.num, message.subject = line.split(' ', 2) subjects << message end end end subjects end
Private Instance Methods
# File lib/nntp/session.rb, line 137 def article_from_status(status_msg) message = Article.new num, id = status_msg.split(' ', 2) message.num = num.to_i unless num.to_i.zero? message.id = id message end
# File lib/nntp/session.rb, line 145 def check_initial_status raise "#{status}" if [400, 502].include? connection.get_status.code end
# File lib/nntp/session.rb, line 163 def fetch_groups group_list = [] connection.query :list do |status, list| list.each do |group| group_list << group_from_list(group) end if status[:code] == 215 end group_list end
# File lib/nntp/session.rb, line 157 def group_factory(*args) name = args[0] low, high, num = args[1..-1].map { |arg| arg.to_i } NNTP::Group.new(name, low, high, num) end
# File lib/nntp/session.rb, line 149 def group_from_list(group_string) params = group_string.split name = params[0] high_water_mark = params[1].to_i low_water_mark = params[2].to_i group_factory(name, low_water_mark, high_water_mark) end
# File lib/nntp/session.rb, line 128 def standard_auth(args) connection.command(:authinfo, "USER #{args[:user]}") if status[:code] == 381 connection.command(:authinfo, "PASS #{args[:pass]}") elsif [281, 482, 502].include? status[:code] status end end