 io.server io.sockets kernel math.parser namespaces sequences
 ftp io.unix.launcher.parser unicode.case splitting assocs
 classes io.server destructors calendar io.timeouts
-io.streams.duplex threads continuations
+io.streams.duplex threads continuations math
 concurrency.promises byte-arrays ;
 IN: ftp.server
 


ERROR: type-error type ;

: handle-TYPE ( obj -- )
    [
        tokenized>> second >upper {
            { "IMAGE" [ "Binary" ] }
            { "I" [ "Binary" ] }
            [ type-error ]
        } case
        200 "Switching to " rot " mode" 3append server-response
    ] [
        2drop "TYPE is binary only" ftp-error
    ] recover ;

: handle-PWD ( obj -- )
    drop
    257 current-directory get "\"" swap "\"" 3append server-response ;

: random-local-server ( -- server )
    remote-address get class new 0 >>port binary <server> ;

: handle-STOR ( obj -- )
    [

        { "QUIT" [ handle-QUIT f ] }

        ! { "PORT" [ ] }
        { "PASV" [ handle-PASV t ] }
        ! { "MODE" [ ] }
        { "TYPE" [ handle-TYPE t ] }
        ! { "STRU" [ ] }
