| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | ! 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 | 
					
						
							| 
									
										
										
										
											2008-05-19 19:58:35 -04:00
										 |  |  | continuations io io.encodings.binary io.encodings.utf8 | 
					
						
							| 
									
										
										
										
											2008-12-15 02:13:35 -05:00
										 |  |  | io.files io.pathnames io.sockets kernel io.streams.duplex math | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | math.parser sequences splitting namespaces strings fry ftp | 
					
						
							|  |  |  | ftp.client.listing-parser urls ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | IN: ftp.client | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:25:25 -04:00
										 |  |  | : (ftp-response-code) ( str -- n )
 | 
					
						
							|  |  |  |     3 head string>number ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ftp-response-code ( string -- n/f )
 | 
					
						
							|  |  |  |     dup fourth CHAR: - = [ drop f ] [ (ftp-response-code) ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | : read-response-loop ( ftp-response -- ftp-response )
 | 
					
						
							|  |  |  |     readln
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:25:25 -04:00
										 |  |  |     [ add-response-line ] [ ftp-response-code ] bi
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  |     over n>> = [ read-response-loop ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | : read-response ( -- ftp-response )
 | 
					
						
							|  |  |  |     <ftp-response> readln
 | 
					
						
							|  |  |  |     [ (ftp-response-code) >>n ] | 
					
						
							|  |  |  |     [ add-response-line ] | 
					
						
							|  |  |  |     [ fourth CHAR: - = ] tri
 | 
					
						
							|  |  |  |     [ read-response-loop ] when ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | ERROR: ftp-error got expected ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ftp-assert ( ftp-response n -- )
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     2dup [ n>> ] dip = [ 2drop ] [ ftp-error ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | : ftp-command ( string -- ftp-response )
 | 
					
						
							|  |  |  |     ftp-send read-response ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-user ( url -- ftp-response )
 | 
					
						
							|  |  |  |     username>> "USER " prepend ftp-command ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:59:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-password ( url -- ftp-response )
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  |     password>> "PASS " prepend ftp-command ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:59:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | : ftp-cwd ( directory -- ftp-response )
 | 
					
						
							|  |  |  |     "CWD " prepend ftp-command ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:59:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | : ftp-retr ( filename -- ftp-response )
 | 
					
						
							|  |  |  |     "RETR " prepend ftp-command ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-set-binary ( -- ftp-response ) "TYPE I" ftp-command ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 23:05:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-pwd ( -- ftp-response ) "PWD" ftp-command ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-list ( -- )
 | 
					
						
							|  |  |  |     "LIST" ftp-command 150 ftp-assert ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-quit ( -- ftp-response ) "QUIT" ftp-command ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ftp-epsv ( -- ftp-response )
 | 
					
						
							|  |  |  |     "EPSV" ftp-command dup 229 ftp-assert ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 15:47:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : parse-epsv ( ftp-response -- port )
 | 
					
						
							|  |  |  |     strings>> first "|" split 2 tail* first string>number ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : open-passive-client ( url protocol -- stream )
 | 
					
						
							| 
									
										
										
										
											2010-10-07 02:00:38 -04:00
										 |  |  |     [ url-addr ftp-epsv parse-epsv with-port ] dip <client> drop ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 19:26:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : list ( url -- ftp-response )
 | 
					
						
							|  |  |  |     utf8 open-passive-client | 
					
						
							|  |  |  |     ftp-list | 
					
						
							| 
									
										
										
										
											2009-05-01 11:41:27 -04:00
										 |  |  |     stream-lines
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  |     <ftp-response> swap >>strings | 
					
						
							|  |  |  |     read-response 226 ftp-assert | 
					
						
							| 
									
										
										
										
											2008-05-11 23:05:53 -04:00
										 |  |  |     parse-list ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:25:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : (ftp-get) ( url path -- )
 | 
					
						
							|  |  |  |     [ binary open-passive-client ] dip
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  |     [ ftp-retr 150 ftp-assert drop ] | 
					
						
							|  |  |  |     [ binary <file-writer> stream-copy ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  |     read-response 226 ftp-assert ;
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-login ( url -- )
 | 
					
						
							|  |  |  |     read-response 220 ftp-assert | 
					
						
							|  |  |  |     [ ftp-user 331 ftp-assert ] | 
					
						
							|  |  |  |     [ ftp-password 230 ftp-assert ] bi
 | 
					
						
							|  |  |  |     ftp-set-binary 200 ftp-assert ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ftp-connect ( url -- stream )
 | 
					
						
							| 
									
										
										
										
											2010-10-07 02:00:38 -04:00
										 |  |  |     url-addr utf8 <client> drop ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:59:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : with-ftp-client ( url quot -- )
 | 
					
						
							|  |  |  |     [ [ ftp-connect ] keep ] dip
 | 
					
						
							|  |  |  |     '[ _ [ ftp-login ] _ bi ftp-quit drop ] with-stream ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ensure-login ( url -- url )
 | 
					
						
							|  |  |  |     dup username>> [ | 
					
						
							|  |  |  |         "anonymous" >>username | 
					
						
							| 
									
										
										
										
											2009-02-18 16:29:06 -05:00
										 |  |  |         "ftp-client@factorcode.org" >>password | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  |     ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:59:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : >ftp-url ( url -- url' ) >url ensure-port ensure-login ;
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 03:56:12 -05:00
										 |  |  | : ftp-get ( url -- )
 | 
					
						
							|  |  |  |     >ftp-url [ | 
					
						
							|  |  |  |         dup path>> | 
					
						
							|  |  |  |         [ nip parent-directory ftp-cwd drop ] | 
					
						
							|  |  |  |         [ file-name (ftp-get) ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:15:22 -04:00
										 |  |  |     ] with-ftp-client ;
 |