Class: Raccdoc::Connection
- Inherits:
-
Object
- Object
- Raccdoc::Connection
- Defined in:
- lib/raccdoc/connection.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#post ⇒ Object
readonly
Returns the value of attribute post.
Instance Method Summary collapse
-
#connected? ⇒ Boolean
Tests the TCP socket to make sure that it’s still connected to the remote server.
-
#forums(type = "all") ⇒ Object
Returns a hash of forum information.
-
#initialize(param_args = {:host => 'bbs.iscabbs.com', :port => '6145', :user => nil, :password => nil}) ⇒ Connection
constructor
Creates a new Raccdoc connection.
-
#jump(forum = 0) ⇒ Object
Sets the active forum on the server to the specified forum name or ID, and returns a new Raccdoc::Forum object.
-
#logout ⇒ Object
Closes the Raccdoc connection, then closes the socket.
Constructor Details
#initialize(param_args = {:host => 'bbs.iscabbs.com', :port => '6145', :user => nil, :password => nil}) ⇒ Connection
Creates a new Raccdoc connection. By default, it connects to bbs.iscabbs.com, port 6145, which is the ISCABBS server.
If a :user and :password is not supplied, a guest/anonymous login is done. Most, if not all, posting is restricted under this account.
If the TCP connection fails, or the login is rejected, and exception is raised.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/raccdoc/connection.rb', line 14 def initialize(param_args = {:host => 'bbs.iscabbs.com', :port => '6145', :user => nil, :password => nil}) args = {:host => 'bbs.iscabbs.com', :port => '6145', :user => nil, :password => nil } args.merge!(param_args) begin @socket = TCPSocket.new(args[:host],args[:port]) rescue Errno::ECONNREFUSED raise ConnectionError, "Could not connect to #{args[:host]}, port #{args[:port]}\n" end begin response = @socket.readline.chomp rescue EOFError raise ConnectionError, "Got an unexpected EOF from the remote end" end unless response.match(/^2/) raise ConnectionError, response end if (args[:user] && args[:password]) @socket.puts("LOGIN #{args[:user]}\t#{args[:password]}") response = @socket.readline.chomp unless response.match(/^2/) raise InvalidLogin, response end end @host = args[:host] @port = args[:port] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
4 5 6 |
# File 'lib/raccdoc/connection.rb', line 4 def host @host end |
#post ⇒ Object (readonly)
Returns the value of attribute post.
5 6 7 |
# File 'lib/raccdoc/connection.rb', line 5 def post @post end |
Instance Method Details
#connected? ⇒ Boolean
Tests the TCP socket to make sure that it’s still connected to the remote server.
60 61 62 |
# File 'lib/raccdoc/connection.rb', line 60 def connected? @socket.closed? ? false : true end |
#forums(type = "all") ⇒ Object
Returns a hash of forum information. The key is the forum ID number, and the value is a hash containing the following data:
-
name - The forum name
-
lastnote - The earliest post ID number in the forum
-
admin - The handle of the forum admin
The argument can be one of: ALL (default), PUBLIC, PRIVATE, TODO, JOINED, NAMED, THREADS depending on server support
Note that this does not return actual Raccdoc::Forum objects in the interest of saving resources, so you’ll need to jump to the forum that you want.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/raccdoc/connection.rb', line 81 def forums(type="all") @socket.puts "LIST #{type}" forums = Hash.new response = @socket.readline.chomp unless response.match(/^3/) raise ResponseError, response end while line = @socket.readline.chomp break if line.match(/^\.$/) tmp = Hash.new line.split(/\t/).each do |pair| (key, value) = pair.split(/:/) if key == "admin" value = value.split('/')[1] end tmp[key.to_sym] = value end id = tmp[:topic].to_i forums[id] = tmp end return forums end |
#jump(forum = 0) ⇒ Object
Sets the active forum on the server to the specified forum name or ID, and returns a new Raccdoc::Forum object.
The forum can be specified by number (0), or name (“Lobby” or “Program”)
67 68 69 |
# File 'lib/raccdoc/connection.rb', line 67 def jump(forum = 0) Raccdoc::Forum.new(@socket, forum.to_s) end |
#logout ⇒ Object
Closes the Raccdoc connection, then closes the socket.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/raccdoc/connection.rb', line 46 def logout unless @socket.closed? @socket.puts "QUIT" response = @socket.readline.chomp unless response.match(/^2/) raise ConnectionError, response end @socket.close end return true end |