Class: NntpScrape::NNTP

Inherits:
Object
  • Object
show all
Defined in:
lib/nntp_scrape/nntp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, ssl, user, pass) ⇒ NNTP

Returns a new instance of NNTP.



10
11
12
13
14
15
16
17
18
# File 'lib/nntp_scrape/nntp.rb', line 10

def initialize(host, port, ssl, user, pass)
  @host = host
  @port = port
  @ssl  = ssl
  @user = user
  @pass = pass
  @caps = []
  open
end

Instance Attribute Details

#capsObject (readonly)

Returns the value of attribute caps.



8
9
10
# File 'lib/nntp_scrape/nntp.rb', line 8

def caps
  @caps
end

#socketObject (readonly)

Returns the value of attribute socket.



7
8
9
# File 'lib/nntp_scrape/nntp.rb', line 7

def socket
  @socket
end

Instance Method Details

#debug=(val) ⇒ Object



28
29
30
# File 'lib/nntp_scrape/nntp.rb', line 28

def debug=(val)
  @debug = val
end

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  @debug
end

#logged_in?Boolean

Returns:

  • (Boolean)


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

def logged_in?
  @logged_in
end

#openObject



32
33
34
35
# File 'lib/nntp_scrape/nntp.rb', line 32

def open
  @socket = make_socket
  
end

#run(*commands) ⇒ Boolean

Executes an array of commands until one fails

Returns:

  • (Boolean)

    true if all commands succeeded



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nntp_scrape/nntp.rb', line 93

def run(*commands)
  commands.each do |cmd|
    Timeout::timeout(cmd.timeout) do
      cmd.execute(self)
    end
    return false unless cmd.continue?
  end
  true
rescue Errno::EPIPE, Errno::ECONNRESET, OpenSSL::SSL::SSLError, Timeout::Error
  open
  retry
end

#run_long(cmd, *params) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nntp_scrape/nntp.rb', line 120

def run_long(cmd, *params)
  status_line = run_short(cmd, *params)      
  lines = []
    
  return [status_line, lines] unless ["2", "1"].include? status_line[0]  #i.e. success

  loop do
    next_line = socket.gets
    break if next_line.strip == "."
    lines << next_line.strip
  end  
  [status_line, lines]      
end

#run_short(cmd, *params) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/nntp_scrape/nntp.rb', line 107

def run_short(cmd, *params)
  command = "#{cmd} #{params.join " "}"
    
  puts command if debug?
    
  socket.print "#{command}\r\n"
  status_line = socket.gets
    
  puts status_line if debug?
    
  status_line
end

#watch(group, start_id = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nntp_scrape/nntp.rb', line 37

def watch(group, start_id=nil)
  watch = Commands::Group.new(group)
  run watch
  return false unless watch.success?
  
  puts watch.high_id
  head = Commands::Head.new(start_id || watch.high_id)
  run head
  return false unless head.success?
        
  loop do
    commands = [Commands::Next.new, Commands::Head.new]
    
    unless run *commands
      puts "pausing..."
      sleep 1
      next
    end
    puts commands[1].message_id
  end
end

#watch_new(group, start_id = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/nntp_scrape/nntp.rb', line 59

def watch_new(group, start_id=nil)
  if start_id.nil?
    watch = Commands::Group.new(group)
    run watch
    return false unless watch.success?
    start_id = watch.high_id
  end
  
  loop do
    watch = Commands::Group.new(group)
    run watch
    next unless watch.success?
    
    end_id = watch.high_id
    
    if start_id == end_id
      sleep 1
      next
    end
    
    xhdr = Commands::Xhdr.new "Message-ID", start_id...end_id
    run xhdr
    next unless xhdr.success?
    
    xhdr.results.each{|v| yield v}
    start_id = end_id
  end
  
end