factor/extra/ftp/client/client.factor

205 lines
5.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2008-05-11 23:05:53 -04:00
USING: accessors arrays classes.singleton combinators
continuations io io.encodings.binary io.encodings.ascii
io.files io.sockets kernel math math.parser sequences
splitting namespaces strings ;
IN: ftp.client
TUPLE: ftp-client host port stream user password mode ;
2008-05-11 23:05:53 -04:00
TUPLE: ftp-response n strings parsed ;
SINGLETON: active
SINGLETON: passive
: <ftp-response> ( -- ftp-response )
ftp-response new
V{ } clone >>strings ;
: <ftp-client> ( host -- ftp-client )
ftp-client new
swap >>host
21 >>port
"anonymous" >>user
"factor-ftp@factorcode.org" >>password ;
2008-05-11 18:25:25 -04:00
: add-response-line ( ftp-response string -- ftp-response )
over strings>> push ;
: (ftp-response-code) ( str -- n )
3 head string>number ;
: ftp-response-code ( string -- n/f )
dup fourth CHAR: - = [ drop f ] [ (ftp-response-code) ] if ;
: last-code ( ftp-response -- n )
strings>> peek (ftp-response-code) ;
2008-05-11 18:25:25 -04:00
: read-response-until ( stream ftp-response n -- ftp-response )
>r over stream-readln
[ add-response-line ] [ ftp-response-code ] bi
r> tuck = [ drop nip ] [ read-response-until ] if ;
: read-response ( stream -- ftp-response )
<ftp-response>
over stream-readln
2008-05-11 18:25:25 -04:00
[ add-response-line ] [ fourth CHAR: - = ] bi
[ dup last-code read-response-until ]
[ nip ] if dup last-code >>n ;
: ftp-read ( ftp-client -- ftp-response )
2008-05-11 18:25:25 -04:00
stream>> read-response ;
: ftp-send ( str ftp-client -- )
stream>>
[ stream-write ]
[ "\r\n" swap stream-write ]
[ stream-flush ] tri ;
2008-05-11 19:26:59 -04:00
: ftp-command ( string ftp-client -- ftp-response )
[ ftp-send ] [ ftp-read ] bi ;
: ftp-user ( ftp-client -- ftp-response )
2008-05-11 19:26:59 -04:00
[ user>> "USER " prepend ] [ ftp-command ] bi ;
: ftp-password ( ftp-client -- ftp-response )
2008-05-11 19:26:59 -04:00
[ password>> "PASS " prepend ] [ ftp-command ] bi ;
2008-05-11 19:26:59 -04:00
: ftp-set-binary ( ftp-client -- ftp-response )
>r "TYPE I" r> ftp-command ;
: ftp-pwd ( ftp-client -- ftp-response )
2008-05-11 19:26:59 -04:00
>r "PWD" r> ftp-command ;
: ftp-list ( ftp-client -- ftp-response )
2008-05-11 19:26:59 -04:00
>r "LIST" r> ftp-command ;
: ftp-quit ( ftp-client -- ftp-response )
2008-05-11 19:26:59 -04:00
>r "QUIT" r> ftp-command ;
2008-05-11 19:26:59 -04:00
: ftp-cwd ( directory ftp-client -- ftp-response )
>r "CWD " prepend r> ftp-command ;
2008-05-11 19:26:59 -04:00
: ftp-retr ( filename ftp-client -- ftp-response )
>r "RETR " prepend r> ftp-command ;
2008-05-11 18:25:25 -04:00
: parse-epsv ( ftp-response -- port )
strings>> first
"|" split 2 tail* first string>number ;
2008-05-11 23:05:53 -04:00
: ch>attribute ( ch -- symbol )
{
{ CHAR: d [ +directory+ ] }
{ CHAR: l [ +symbolic-link+ ] }
{ CHAR: - [ +regular-file+ ] }
[ drop +unknown+ ]
} case ;
TUPLE: remote-file
type permissions links owner group size month day time year name ;
: <remote-file> ( -- remote-file ) remote-file new ;
: parse-permissions ( remote-file str -- remote-file )
[ first ch>attribute >>type ] [ rest >>permissions ] bi ;
: parse-list-9 ( lines -- seq )
[
<remote-file> swap {
[ 0 swap nth parse-permissions ]
[ 1 swap nth string>number >>links ]
[ 2 swap nth >>owner ]
[ 3 swap nth >>group ]
[ 4 swap nth string>number >>size ]
[ 5 swap nth >>month ]
[ 6 swap nth >>day ]
[ 7 swap nth >>time ]
[ 8 swap nth >>name ]
} cleave
] map ;
: parse-list-8 ( lines -- seq )
[
<remote-file> swap {
[ 0 swap nth parse-permissions ]
[ 1 swap nth string>number >>links ]
[ 2 swap nth >>owner ]
[ 3 swap nth >>size ]
[ 4 swap nth >>month ]
[ 5 swap nth >>day ]
[ 6 swap nth >>time ]
[ 7 swap nth >>name ]
} cleave
] map ;
: parse-list-3 ( lines -- seq )
[
<remote-file> swap {
[ 0 swap nth parse-permissions ]
[ 1 swap nth string>number >>links ]
[ 2 swap nth >>name ]
} cleave
] map ;
: parse-list ( ftp-response -- ftp-response )
dup strings>>
[ " " split [ empty? not ] filter ] map
dup length {
{ 9 [ parse-list-9 ] }
{ 8 [ parse-list-8 ] }
{ 3 [ parse-list-3 ] }
[ drop ]
} case >>parsed ;
2008-05-11 19:26:59 -04:00
: ftp-epsv ( ftp-client -- ftp-response )
>r "EPSV" r> ftp-command ;
2008-05-11 18:25:25 -04:00
M: ftp-client dispose ( ftp-client -- )
2008-05-11 19:26:59 -04:00
[ ftp-quit drop ] [ stream>> dispose ] bi ;
ERROR: ftp-error got expected ;
2008-05-11 18:25:25 -04:00
: ftp-assert ( ftp-response n -- )
2dup >r n>> r> = [ 2drop ] [ ftp-error ] if ;
2008-05-11 18:25:25 -04:00
: ftp-connect ( ftp-client -- )
dup
[ host>> ] [ port>> ] bi <inet> ascii <client>
>>stream drop ;
: ftp-login ( ftp-client -- )
{
[ ftp-connect ]
[ ftp-read 220 ftp-assert ]
[ ftp-user 331 ftp-assert ]
[ ftp-password 230 ftp-assert ]
[ ftp-set-binary 200 ftp-assert ]
} cleave ;
2008-05-11 19:26:59 -04:00
: start-2nd ( ftp-client -- port )
ftp-epsv [ 229 ftp-assert ] [ parse-epsv ] bi ;
2008-05-11 18:25:25 -04:00
: list ( ftp-client -- ftp-response )
2008-05-11 19:26:59 -04:00
dup [ host>> ] [ start-2nd ] bi <inet> ascii <client>
2008-05-11 18:25:25 -04:00
over ftp-list 150 ftp-assert
lines <ftp-response> swap >>strings
2008-05-11 23:05:53 -04:00
>r ftp-read 226 ftp-assert r>
parse-list ;
2008-05-11 18:25:25 -04:00
2008-05-11 19:26:59 -04:00
: ftp-get ( filename ftp-client -- ftp-response )
dup [ host>> ] [ start-2nd ] bi <inet> binary <client>
rot tuck
[ over ftp-retr 150 ftp-assert ]
2008-05-11 18:25:25 -04:00
[ binary <file-writer> stream-copy ] 2bi*
ftp-read dup 226 ftp-assert ;
2008-05-11 19:26:59 -04:00
GENERIC: ftp-download ( path obj -- )
2008-05-11 19:26:59 -04:00
M: ftp-client ftp-download ( path ftp-client -- )
dup ftp-login
[ >r parent-directory r> ftp-cwd drop ]
[ >r file-name r> ftp-get drop ]
[ dispose drop ] 2tri ;
2008-05-11 19:26:59 -04:00
M: string ftp-download ( path string -- )
<ftp-client> ftp-download ;