1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
2000-01-21 07:52:24 -05:00
|
|
|
= net/session.rb
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
This library is distributed under the terms of the Ruby license.
|
|
|
|
You can freely distribute/modify this library.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
module Net
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-01-21 07:52:24 -05:00
|
|
|
Version = '1.1.4'
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
== Net::Protocol
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
the abstruct class for Internet protocol
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
=== Constants
|
|
|
|
|
|
|
|
: Version
|
2000-01-21 07:52:24 -05:00
|
|
|
The version of Session class. It is a string like "1.1.4".
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = nil )
|
1999-12-29 06:14:04 -05:00
|
|
|
This method Creates a new Session object.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
: start( address = 'localhost', port = nil, *args )
|
|
|
|
: start( address = 'localhost', port = nil, *args ){|session| .... }
|
1999-12-29 06:14:04 -05:00
|
|
|
This method creates a new Session object and start session.
|
1999-10-13 03:29:15 -04:00
|
|
|
If you call this method with block, Session object give itself
|
|
|
|
to block and finish session when block returns.
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
: Proxy( address, port )
|
|
|
|
This method creates a proxy class of its protocol.
|
|
|
|
Arguments are address/port of proxy host.
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: address
|
|
|
|
the address of connecting server (FQDN).
|
|
|
|
|
|
|
|
: port
|
|
|
|
connecting port number
|
|
|
|
|
|
|
|
: start( *args )
|
1999-12-29 06:14:04 -05:00
|
|
|
This method start protocol. If you call this method when the protocol
|
1999-10-13 03:29:15 -04:00
|
|
|
is already started, this only returns false without doing anything.
|
|
|
|
|
|
|
|
'*args' are specified in subclasses.
|
|
|
|
|
|
|
|
: finish
|
1999-12-29 06:14:04 -05:00
|
|
|
This method ends protocol. If you call this method before protocol starts,
|
1999-10-13 03:29:15 -04:00
|
|
|
it only return false without doing anything.
|
|
|
|
|
|
|
|
: active?
|
|
|
|
true if session have been started
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
class Protocol
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
def start( address = 'localhost', port = nil, *args )
|
1999-12-29 06:14:04 -05:00
|
|
|
instance = new( address, port )
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
if iterator? then
|
1999-12-29 06:14:04 -05:00
|
|
|
instance.start( *args ) { yield instance }
|
1999-12-17 10:00:13 -05:00
|
|
|
else
|
1999-12-29 06:14:04 -05:00
|
|
|
instance.start *args
|
|
|
|
instance
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-12-22 08:49:13 -05:00
|
|
|
def Proxy( p_addr, p_port )
|
|
|
|
klass = Class.new( self )
|
|
|
|
klass.module_eval %-
|
|
|
|
|
|
|
|
def initialize( addr, port )
|
|
|
|
@proxyaddr = '#{p_addr}'
|
|
|
|
@proxyport = '#{p_port}'
|
|
|
|
super @proxyaddr, @proxyport
|
|
|
|
@address = addr
|
|
|
|
@port = port
|
|
|
|
end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
def connect( addr, port )
|
|
|
|
super @proxyaddr, @proxyport
|
1999-12-22 08:49:13 -05:00
|
|
|
end
|
|
|
|
private :connect
|
|
|
|
|
|
|
|
attr :proxyaddr
|
|
|
|
attr :proxyport
|
|
|
|
-
|
|
|
|
def klass.proxy?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
private
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
def protocol_param( name, val )
|
1999-12-17 10:00:13 -05:00
|
|
|
module_eval %-
|
|
|
|
def self.#{name.id2name}
|
|
|
|
#{val}
|
|
|
|
end
|
|
|
|
-
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# sub-class requirements
|
|
|
|
#
|
1999-12-29 06:14:04 -05:00
|
|
|
# protocol_param command_type
|
|
|
|
# protocol_param port
|
1999-12-17 10:00:13 -05:00
|
|
|
#
|
|
|
|
# private method do_start (optional)
|
|
|
|
# private method do_finish (optional)
|
|
|
|
#
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
protocol_param :port, 'nil'
|
|
|
|
protocol_param :command_type, 'nil'
|
|
|
|
protocol_param :socket_type, '::Net::ProtocolSocket'
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
def initialize( addr = nil, port = nil )
|
|
|
|
@address = addr || 'localhost'
|
1999-12-17 10:00:13 -05:00
|
|
|
@port = port || self.type.port
|
|
|
|
|
|
|
|
@active = false
|
|
|
|
@pipe = nil
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
@command = nil
|
1999-12-17 10:00:13 -05:00
|
|
|
@socket = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
attr :address
|
|
|
|
attr :port
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
attr :command
|
1999-12-17 10:00:13 -05:00
|
|
|
attr :socket
|
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def start( *args )
|
|
|
|
return false if active?
|
1999-10-13 03:29:15 -04:00
|
|
|
@active = true
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
begin
|
1999-12-29 06:14:04 -05:00
|
|
|
connect @address, @port
|
1999-12-17 10:00:13 -05:00
|
|
|
do_start *args
|
|
|
|
yield if iterator?
|
|
|
|
ensure
|
|
|
|
finish if iterator?
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
1999-12-29 06:14:04 -05:00
|
|
|
if @command then
|
1999-09-22 03:32:33 -04:00
|
|
|
do_finish
|
1999-12-17 10:00:13 -05:00
|
|
|
disconnect
|
|
|
|
end
|
|
|
|
|
|
|
|
if @socket and not @socket.closed? then
|
|
|
|
@socket.close
|
|
|
|
@socket = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if active? then
|
|
|
|
@active = false
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def active?
|
|
|
|
@active
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def set_pipe( arg ) # un-documented
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe = arg
|
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def do_start
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_finish
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
def connect( addr, port )
|
|
|
|
@socket = self.type.socket_type.open( addr, port, @pipe )
|
|
|
|
@command = self.type.command_type.new( @socket )
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def disconnect
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.quit
|
|
|
|
@command = nil
|
|
|
|
@socket = nil
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
Session = Protocol
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
|
|
|
== Net::Command
|
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( socket )
|
|
|
|
This method create new Command object. 'socket' must be ProtocolSocket.
|
|
|
|
This method is abstract class.
|
|
|
|
|
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: quit
|
1999-12-29 06:14:04 -05:00
|
|
|
This method dispatch command which ends the protocol.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
class Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
@socket = sock
|
1999-12-22 08:49:13 -05:00
|
|
|
@error_occured = false
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr :socket, true
|
1999-12-22 08:49:13 -05:00
|
|
|
attr :error_occured
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
def quit
|
|
|
|
if @socket and not @socket.closed? then
|
|
|
|
begin
|
|
|
|
do_quit
|
|
|
|
ensure
|
|
|
|
@socket.close unless @socket.closed?
|
|
|
|
@socket = nil
|
|
|
|
end
|
1999-12-22 08:49:13 -05:00
|
|
|
@error_occured = false
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_reply( *oks )
|
1999-12-22 08:49:13 -05:00
|
|
|
reply_must( get_reply, *oks )
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_must( rep, *oks )
|
1999-09-22 03:32:33 -04:00
|
|
|
oks.each do |i|
|
|
|
|
if i === rep then
|
|
|
|
return rep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-12-22 08:49:13 -05:00
|
|
|
@error_occured = true
|
1999-09-22 03:32:33 -04:00
|
|
|
rep.error! @socket.sending
|
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
|
|
|
|
def getok( line, ok = SuccessCode )
|
|
|
|
@socket.writeline line
|
|
|
|
check_reply ok
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class ProtocolError < StandardError ; end
|
|
|
|
class ProtoSyntaxError < ProtocolError ; end
|
|
|
|
class ProtoFatalError < ProtocolError ; end
|
|
|
|
class ProtoUnknownError < ProtocolError ; end
|
|
|
|
class ProtoServerError < ProtocolError ; end
|
|
|
|
class ProtoAuthError < ProtocolError ; end
|
|
|
|
class ProtoCommandError < ProtocolError ; end
|
1999-12-22 08:49:13 -05:00
|
|
|
class ProtoRetryError < ProtocolError ; end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
class ReplyCode
|
|
|
|
|
|
|
|
def initialize( cod, mes )
|
|
|
|
@code = cod
|
|
|
|
@msg = mes
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :code
|
|
|
|
attr :msg
|
|
|
|
|
|
|
|
def error!( sending )
|
1999-12-17 10:00:13 -05:00
|
|
|
mes = <<MES
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
status %s
|
1999-09-22 03:32:33 -04:00
|
|
|
writing string is:
|
|
|
|
%s
|
|
|
|
|
|
|
|
error message from server is:
|
|
|
|
%s
|
|
|
|
MES
|
1999-12-17 10:00:13 -05:00
|
|
|
raise self.type::Error,
|
|
|
|
sprintf( mes, @code, Net.quote(sending), Net.quote(@msg) )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
class SuccessCode < ReplyCode
|
|
|
|
Error = ProtoUnknownError
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
class ContinueCode < SuccessCode
|
|
|
|
Error = ProtoUnknownError
|
|
|
|
end
|
|
|
|
|
|
|
|
class ErrorCode < ReplyCode
|
|
|
|
Error = ProtocolError
|
|
|
|
end
|
|
|
|
|
|
|
|
class SyntaxErrorCode < ErrorCode
|
|
|
|
Error = ProtoSyntaxError
|
|
|
|
end
|
|
|
|
|
|
|
|
class FatalErrorCode < ErrorCode
|
|
|
|
Error = ProtoFatalError
|
|
|
|
end
|
|
|
|
|
|
|
|
class ServerBusyCode < ErrorCode
|
|
|
|
Error = ProtoServerError
|
|
|
|
end
|
|
|
|
|
1999-12-22 08:49:13 -05:00
|
|
|
class RetryCode < ReplyCode
|
|
|
|
Error = ProtoRetryError
|
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
class UnknownCode < ReplyCode
|
|
|
|
Error = ProtoUnknownError
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
|
|
|
== Net::ProtocolSocket
|
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = nil )
|
|
|
|
This create new ProtocolSocket object, and connect to server.
|
|
|
|
|
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: close
|
|
|
|
This method closes socket.
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
: address, addr
|
1999-10-13 03:29:15 -04:00
|
|
|
a FQDN address of server
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
: ip_address, ipaddr
|
1999-10-13 03:29:15 -04:00
|
|
|
an IP address of server
|
|
|
|
|
|
|
|
: port
|
|
|
|
connecting port number.
|
|
|
|
|
|
|
|
: closed?
|
|
|
|
true if ProtocolSokcet have been closed already
|
|
|
|
|
|
|
|
|
|
|
|
: read( length )
|
|
|
|
This method read 'length' bytes and return the string.
|
|
|
|
|
|
|
|
: readuntil( target )
|
|
|
|
This method read until find 'target'. Returns read string.
|
|
|
|
|
|
|
|
: readline
|
|
|
|
read until "\r\n" and returns it without "\r\n".
|
|
|
|
|
|
|
|
: read_pendstr
|
|
|
|
This method read until "\r\n.\r\n".
|
|
|
|
At the same time, delete period at line head and final line ("\r\n.\r\n").
|
|
|
|
|
|
|
|
: read_pendlist
|
|
|
|
: read_pendlist{|line| .... }
|
|
|
|
This method read until "\r\n.\r\n". This method resembles to 'read_pendstr',
|
|
|
|
but 'read_pendlist' don't check period at line head, and returns array which
|
|
|
|
each element is one line.
|
|
|
|
|
|
|
|
When this method was called with block, evaluate it for each reading a line.
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
: write( src )
|
|
|
|
This method send 'src'. ProtocolSocket read strings from 'src' by 'each'
|
|
|
|
iterator. This method returns written bytes.
|
|
|
|
|
|
|
|
: writebin( src )
|
|
|
|
This method send 'src'. ProtocolSokcet read string from 'src' by 'each'
|
|
|
|
iterator. This method returns written bytes.
|
|
|
|
|
|
|
|
: writeline( str )
|
|
|
|
This method writes 'str'. There has not to be bare "\r" or "\n" in 'str'.
|
|
|
|
|
|
|
|
: write_pendstr( src )
|
|
|
|
This method writes 'src' as a mail.
|
|
|
|
ProtocolSocket reads strings from 'src' by 'each' iterator.
|
|
|
|
This returns written bytes.
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
class ProtocolSocket
|
|
|
|
|
|
|
|
def initialize( addr, port, pipe = nil )
|
|
|
|
@addr = addr
|
|
|
|
@port = port
|
|
|
|
@pipe = pipe
|
|
|
|
|
|
|
|
@closed = true
|
|
|
|
@ipaddr = ''
|
|
|
|
@sending = ''
|
|
|
|
@buffer = ''
|
|
|
|
|
|
|
|
@socket = TCPsocket.new( addr, port )
|
|
|
|
@closed = false
|
|
|
|
@ipaddr = @socket.addr[3]
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :pipe, true
|
|
|
|
|
|
|
|
class << self
|
|
|
|
alias open new
|
|
|
|
end
|
|
|
|
|
|
|
|
def reopen
|
|
|
|
unless closed? then
|
|
|
|
@socket.close
|
2000-01-21 07:52:24 -05:00
|
|
|
@buffer = ''
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
@socket = TCPsocket.new( @addr, @port )
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
attr :socket, true
|
|
|
|
|
|
|
|
def close
|
|
|
|
@socket.close
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
|
|
|
|
|
|
|
def address
|
|
|
|
@addr.dup
|
|
|
|
end
|
|
|
|
alias addr address
|
|
|
|
|
|
|
|
attr :port
|
|
|
|
|
|
|
|
def ip_address
|
|
|
|
@ipaddr.dup
|
|
|
|
end
|
|
|
|
alias ipaddr ip_address
|
|
|
|
|
|
|
|
attr :sending
|
|
|
|
|
|
|
|
|
|
|
|
CRLF = "\r\n"
|
|
|
|
D_CRLF = ".\r\n"
|
|
|
|
TERMEXP = /\n|\r\n|\r/o
|
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def read( len, ret = '' )
|
1999-12-17 10:00:13 -05:00
|
|
|
@pipe << "reading #{len} bytes...\n" if pre = @pipe ; @pipe = nil
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
rsize = 0
|
|
|
|
while rsize + @buffer.size < len do
|
2000-01-21 07:52:24 -05:00
|
|
|
rsize += writeinto( ret, @buffer.size )
|
1999-09-22 03:32:33 -04:00
|
|
|
fill_rbuf
|
|
|
|
end
|
2000-01-21 07:52:24 -05:00
|
|
|
writeinto( ret, len - rsize )
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
@pipe << "read #{len} bytes\n" if @pipe = pre
|
|
|
|
ret
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-01-21 07:52:24 -05:00
|
|
|
def read_all( ret = '' )
|
|
|
|
@pipe << "reading all...\n" if pre = @pipe; @pipe = nil
|
|
|
|
|
|
|
|
rsize = 0
|
|
|
|
begin
|
|
|
|
while true do
|
|
|
|
rsize += writeinto( ret, @buffer.size )
|
|
|
|
fill_rbuf
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
;
|
|
|
|
end
|
|
|
|
|
|
|
|
@pipe << "read #{rsize} bytes\n" if @pipe = pre
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def readuntil( target )
|
|
|
|
until idx = @buffer.index( target ) do
|
|
|
|
fill_rbuf
|
|
|
|
end
|
|
|
|
|
2000-01-21 07:52:24 -05:00
|
|
|
ret = ''
|
|
|
|
writeinto( ret, idx + target.size )
|
|
|
|
ret
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def readline
|
|
|
|
ret = readuntil( CRLF )
|
|
|
|
ret.chop!
|
1999-12-17 10:00:13 -05:00
|
|
|
ret
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def read_pendstr( dest = '' )
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe << "reading text...\n" if pre = @pipe ; @pipe = nil
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
rsize = 0
|
|
|
|
|
|
|
|
while (str = readuntil( CRLF )) != D_CRLF do
|
|
|
|
rsize += str.size
|
|
|
|
str.gsub!( /\A\./o, '' )
|
|
|
|
dest << str
|
|
|
|
end
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe << "read #{rsize} bytes\n" if @pipe = pre
|
1999-12-17 10:00:13 -05:00
|
|
|
dest
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def read_pendlist
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe << "reading list...\n" if pre = @pipe ; @pipe = nil
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
arr = []
|
|
|
|
str = nil
|
|
|
|
call = iterator?
|
|
|
|
|
|
|
|
while (str = readuntil( CRLF )) != D_CRLF do
|
|
|
|
str.chop!
|
|
|
|
arr.push str
|
|
|
|
yield str if iterator?
|
|
|
|
end
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe << "read #{arr.size} lines\n" if @pipe = pre
|
1999-12-17 10:00:13 -05:00
|
|
|
arr
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
READ_BLOCK = 1024 * 8
|
|
|
|
|
|
|
|
def fill_rbuf
|
|
|
|
@buffer << @socket.sysread( READ_BLOCK )
|
|
|
|
end
|
|
|
|
|
2000-01-21 07:52:24 -05:00
|
|
|
def writeinto( ret, len )
|
1999-09-22 03:32:33 -04:00
|
|
|
bsi = @buffer.size
|
2000-01-21 07:52:24 -05:00
|
|
|
ret << @buffer[ 0, len ]
|
1999-09-22 03:32:33 -04:00
|
|
|
@buffer = @buffer[ len, bsi - len ]
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
@pipe << %{read "#{Net.quote ret}"\n} if @pipe
|
2000-01-21 07:52:24 -05:00
|
|
|
len
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
|
|
|
|
def write( src )
|
|
|
|
do_write_beg
|
|
|
|
each_crlf_line( src ) do |line|
|
|
|
|
do_write_do line
|
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
do_write_fin
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def writebin( src )
|
|
|
|
do_write_beg
|
|
|
|
src.each do |bin|
|
|
|
|
do_write_do bin
|
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
do_write_fin
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def writeline( str )
|
|
|
|
do_write_beg
|
|
|
|
do_write_do str
|
|
|
|
do_write_do CRLF
|
1999-12-17 10:00:13 -05:00
|
|
|
do_write_fin
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def write_pendstr( src )
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe << "writing text from #{src.type}" if pre = @pipe ; @pipe = nil
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
do_write_beg
|
|
|
|
each_crlf_line( src ) do |line|
|
|
|
|
do_write_do '.' if line[0] == ?.
|
|
|
|
do_write_do line
|
|
|
|
end
|
|
|
|
do_write_do D_CRLF
|
|
|
|
wsize = do_write_fin
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
@pipe << "wrote #{wsize} bytes text" if @pipe = pre
|
1999-12-17 10:00:13 -05:00
|
|
|
wsize
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def each_crlf_line( src )
|
|
|
|
buf = ''
|
|
|
|
beg = 0
|
1999-12-22 08:49:13 -05:00
|
|
|
pos = s = bin = nil
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-22 08:49:13 -05:00
|
|
|
src.each do |bin|
|
|
|
|
buf << bin
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
beg = 0
|
1999-12-22 08:49:13 -05:00
|
|
|
while pos = buf.index( TERMEXP, beg ) do
|
|
|
|
s = $&.size
|
|
|
|
break if pos + s == buf.size - 1 and buf[-1] == ?\r
|
|
|
|
|
|
|
|
yield buf[ beg, pos - beg ] << CRLF
|
|
|
|
beg = pos + s
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
buf = buf[ beg, buf.size - beg ] if beg != 0
|
|
|
|
end
|
|
|
|
|
|
|
|
buf << "\n" unless /\n|\r/o === buf[-1,1]
|
|
|
|
|
|
|
|
beg = 0
|
1999-10-13 03:29:15 -04:00
|
|
|
while pos = buf.index( TERMEXP, beg ) do
|
1999-12-22 08:49:13 -05:00
|
|
|
yield buf[ beg, pos - beg ] << CRLF
|
|
|
|
beg = pos + $&.size
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def do_write_beg
|
|
|
|
@writtensize = 0
|
|
|
|
@sending = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_write_do( arg )
|
1999-12-17 10:00:13 -05:00
|
|
|
if @pipe or @sending.size < 128 then
|
|
|
|
@sending << Net.quote( arg )
|
1999-09-22 03:32:33 -04:00
|
|
|
else
|
|
|
|
@sending << '...' unless @sending[-1] == ?.
|
|
|
|
end
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
s = @socket.write( arg )
|
|
|
|
@writtensize += s
|
1999-12-17 10:00:13 -05:00
|
|
|
s
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def do_write_fin
|
1999-10-13 03:29:15 -04:00
|
|
|
if @pipe then
|
1999-12-17 10:00:13 -05:00
|
|
|
@pipe << 'write "'
|
|
|
|
@pipe << @sending
|
|
|
|
@pipe << "\"\n"
|
1999-10-13 03:29:15 -04:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
@socket.flush
|
1999-12-17 10:00:13 -05:00
|
|
|
@writtensize
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def Net.quote( str )
|
|
|
|
str = str.gsub( "\n", '\\n' )
|
|
|
|
str.gsub!( "\r", '\\r' )
|
|
|
|
str.gsub!( "\t", '\\t' )
|
|
|
|
str
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
end # module Net
|