Code archives/Networking/BlitzFTP

This code has been declared by its author to be Public Domain code.

Download source code

BlitzFTP by marksibly
(Posted 26 years ago)
A VERY simple FTP client written in BB! Will probably have trouble with 99% of the FTP servers out there...

Supports: cls, cd, lcd, cdup, dir, get, put, help, exit - type 'help' once you've logged in for parameters.

Also, if the connection is too slow BB will time out after 10 seconds and you'll be dumped.

TCP timeouts will be coming soon!
Graphics 800,600

Color 255,0,64
Print "*****************"
Print "* BlitzFTP V1.0 *"
Print "*****************"

Dim dp(6),args$(3)

Color 0,255,64
ftp$=Input$( "ftp://" ):If ftp$="" Then End

com=OpenTCPStream( ftp$,21 ):If Not com RuntimeError( "Unable to connect" )

Repeat
	;receive reply from FTP
	Color 0,192,255
	Repeat
		Repeat
			ln$=ReadLine$( com )
			n=Eof( com ):If n=1 Then End
			If n RuntimeError( "Stream Error" )
			Print ln$
		Until Len( ln$ )>3
		t=Left$( ln$,3 )
	Until t>=100 And t<600 And Mid$( ln$,4,1 )=" "
	Color 0,255,64

	;update state	
	If t<400	;not an error?
		st=t
	Else		;error! Hack 'n' kludge!
		If file CloseFile file:file=0 
		If dat CloseTCPStream dat:dat=0
		If t=530 Then st=220 Else If st<300 And st<>220 st=200
	EndIf
	
	;act on state
	Select st
	Case 220
		WriteLine com,"USER "+Input$( "Username:" )
	Case 331
		WriteLine com,"PASS "+Input$( "Password:" )
	Case 332
		WriteLine com,"ACCT "+Input$( "Account:" )
	Case 230
		WriteLine com,"TYPE I"	;binary file transfer mode!
	Case 227
		;extra ip and port from (h1,h2,h3,h4,p1,p2)
		i1=Instr( ln$,"(" )
		i2=Instr( ln$,")",i1 )
		pt$=Mid$( ln$,i1+1,i2-i1-1 )+","
		For k=1 To 6
			i=Instr( pt$,"," )
			dp(k)=Left$( pt$,i-1 )
			pt$=Mid$(pt$,i+1)
		Next
		ip$=dp(1)+"."+dp(2)+"."+dp(3)+"."+dp(4)
		port=(dp(5) Shl 8) Or dp(6)
		dat=OpenTCPStream( ip$,port )
		If Not dat RuntimeError "Failed to open data port"
		WriteLine com,dat_com$
	Case 150
		;data transfer...
		If file 
			;file transfer
			If Left$( dat_com$,4 )="STOR"
				rd=file:wt=dat:Write "Uploading "+Mid$( dat_com$,6 )
			Else
				rd=dat:wt=file:Write "Downloading "+Mid$( dat_com$,6 )
			EndIf
			b=CreateBank(16384)
			size=0:time=MilliSecs()
			Repeat
				n=ReadBytes( b,rd,0,16384 )
				WriteBytes b,wt,0,n
				size=size+n
				Write "."
			Until n<>16384
			time=MilliSecs()-time
			secs#=time/1000.0
			Print:Print size+" bytes transferred in "+secs+" seconds."
			FreeBank b:CloseFile file:file=0
		Else
			;just a directory dump...
			While Not Eof( dat )
				Print ReadLine$( dat )
			Wend
		EndIf
		CloseTCPStream dat:dat=0
	Default
		If st<200 Or st>=300 RuntimeError "Fatally confused error!"
		send$=""
		Repeat
			Repeat
				arg$=Trim$( Input$( "]" ) )
			Until arg$<>""
			For n=1 To 3
				i=Instr( arg$," " )
				If i=0 Then args$(n)=arg$:Exit
				args$(n)=Left$(arg$,i-1)
				arg$=Trim$( Mid$(arg$,i+1) )
			Next
			Select Lower$( args$(1) )
			Case "cd"
				If n=2
					send$="CWD "+args$(2)
				Else
					Print "Usage: cd remote_dir"
				EndIf
			Case "lcd"
				If n>2
					Print "Usage: lcd local_dir"
				Else
					If n=2 ChangeDir args$(2)
					Print "Current dir="+CurrentDir$()
				EndIf
			Case "cdup"
				send$="CDUP"
			Case "cls"
				Cls:Locate 0,0
			Case "dir"
				If n=1
					send$="PASV":dat_com$="LIST"
				Else If n=2
					send$="PASV":dat_com$="LIST "+args$(2)
				Else
					Print "Usage: dir [remote_dir]"
				EndIf
			Case "get"
				If n=2
					fi$=Replace$( args$(2),"\","/" )
					While Instr( fi$,"/" )
						fi$=Mid$( fi$,Instr( fi$,"/" )+1 )
					Wend
					file=WriteFile( fi$ )
					If file 
						send$="PASV":dat_com$="RETR "+args$(2)
					Else
						Print "Unable to open file for writing"
					EndIf
				Else
					Print "Usage: get filename"
				EndIf
			Case "put"
				If n=2
					fi$=Replace$( args$(2),"\","/" )
					While Instr( fi$,"/" )
						fi$=Mid$( fi$,Instr( fi$,"/" )+1 )
					Wend
					file=ReadFile( fi$ )
					If file
						send$="PASV":dat_com$="STOR "+args$(2)
					Else
						Print "Unable to open file for reading" 
					EndIf
				Else
					Print "Usage: put filename"
				EndIf
			Case "help"
				Print "Local help:"
				Print "cdup, cls, help, quit, exit"
				Print "cd remote_dir, lcd [local_dir], dir [remote_dir], get remote_file, put remote_file"
				Print "Remote help:"
				send$="HELP"
			Case "shot"
				SaveBuffer FrontBuffer(),"ftpshot.bmp"
			Case "quit","exit"
				send$="QUIT"
			Default:
				Print "Unrecognized command "+Chr$(34)+args$(1)+Chr$(34)+"."
			End Select
		Until send$<>""
		WriteLine com,send$
	End Select
Forever

Comments

markcw
(Posted 20 years ago)
This is an extension of BlitzFTP. It is basically the same program but it's now a fully-featured ftp client.

New commands are: ldir, pwd, ndir, stat, del, rename, rmdir, mkdir, type, mode, stru, size, mdtm, syst, noop.

Most of these commands don't do much, i just added them for functionality. The important ones are del, rename, rmdir, mkdir and ldir.

This was the only source code i could find that worked properly. So thanks to mark sibly.

Oh yes, it also has a read/write file feature for automatic logins.

Also, you will want to compile to executable to use it.

;BlitzFTP V 2.0, on 5/9/06

Graphics 640,480,0,2
AppTitle "BlitzFTP"

Color 255,0,64
Print "* BlitzFTP V 2.0 *"
Print ""
Color 0,255,64

Dim dp(6) ;data port: host1,host2,host3,host4,port1,port2
Dim args$(3) ;ftp command: argument1,[argument2],[argument3]

;new login prompt
logfile$="BlitzFTP.ini"
fsize=FileSize(logfile$)
If FileType(logfile$)=1 ;file exists
 login$=Input$( "Create new login? Y/N: " )
 If Left$( Lower$( login$ ),1 )="y" Then fsize=0 ;overwrite file
EndIf

;read/write log file
If FileType(logfile$)=1 And fsize>10
 ;file exists and not empty
 file=ReadFile(logfile$)
 ftp$=ReadLine(file)
 user$=ReadLine(file)
 pass$=ReadLine(file)
 account$=ReadLine(file)
 CloseFile file : file=0
Else
 ;file doesn't exist, is empty or being overwritten
 ftp$=Input$( "FTP server: " ) ;eg. ftp.yourserver.com
 user$=Input$( "Username: " ) ;eg. yourname@...
 pass$=Input$( "Password: " ) ;eg. yourpassword
 login$=Input$( "Account required? Y/N: " )
 If Left$( Lower$( login$ ),1 )="y" ;need account info
  account$=Input$( "Account: " ) ;eg. youraccount
 EndIf
 file=WriteFile(logfile$)
 WriteLine file,ftp$
 WriteLine file,user$
 WriteLine file,pass$
 WriteLine file,account$
 CloseFile file : file=0
EndIf

;login to ftp server
Print "Connecting to ftp server: "+ftp$
Delay(100) ;pause to show text
port=21 ;21=command/control port, 20=data port, not always
com=OpenTCPStream( ftp$,port ) ;ftp stream handle
If Not com Input$("Error: Unable to connect ") : End

Repeat

 ;receive reply from FTP
 Color 0,192,255
 Repeat
  Repeat
   ln$=ReadLine$( com )
   n=Eof( com ) : If n=1 Then End ;1=tcp stream has closed
   If n Input$("Error: Stream error ") : End
   Print ln$
  Until Len( ln$ )>3
  t=Left$( ln$,3 )
 Until t>=100 And t<600 And Mid$( ln$,4,1 )=" "
 Color 0,255,64

 ;update state 
 If t<400 ;not an error?
  st=t
 Else  ;error! Hack 'n' kludge!
  If file CloseFile file : file=0 
  If dat CloseTCPStream dat : dat=0
  If t=530 Then st=220 Else If st<300 And st<>220 st=200
 EndIf
 
 ;act on state
 Select st

 Case 220 ;220=service ready for new user
  Print "Verifying username: "+user$
  WriteLine com,"USER "+user$

 Case 331 ;331=username ok, need password
  Print "Verifying password: "+pass$
  WriteLine com,"PASS "+pass$

 Case 332 ;332=need account for login
  Print "Verifying account: "+account$
  WriteLine com,"ACCT "+account$

 Case 230 ;230=user logged in, proceed
  WriteLine com,"TYPE I" ;binary file transfer mode!

 Case 350 ;350=requested file action pending further information
  WriteLine com,"RNTO "+args$(3) ;rename to

 Case 227 ;227=entering passive mode
  ;extra ip and port from (h1,h2,h3,h4,p1,p2)
  i1=Instr( ln$,"(" )
  i2=Instr( ln$,")",i1 )
  pt$=Mid$( ln$,i1+1,i2-i1-1 )+","
  For k=1 To 6
   i=Instr( pt$,"," )
   dp(k)=Left$( pt$,i-1 )
   pt$=Mid$(pt$,i+1)
  Next
  ip$=dp(1)+"."+dp(2)+"."+dp(3)+"."+dp(4)
  port=(dp(5) Shl 8) Or dp(6) ;data port
  dat=OpenTCPStream( ip$,port )
  If Not dat Input$("Error: Failed to open data port ") : End
  WriteLine com,dat_com$

 Case 150 ;150=file status ok, about to open data connection
  If file
   ;file transfer
   If Left$( dat_com$,4 )="STOR"
    rd=file:wt=dat:Write "Uploading "+Mid$( dat_com$,6 )
   Else
    rd=dat:wt=file:Write "Downloading "+Mid$( dat_com$,6 )
   EndIf
   b=CreateBank(16384)
   size=0:time=MilliSecs()
   Repeat
    n=ReadBytes( b,rd,0,16384 )
    WriteBytes b,wt,0,n
    size=size+n
    Write "."
   Until n<>16384
   time=MilliSecs()-time
   secs#=time/1000.0
   Print:Print size+" bytes transferred in "+secs+" seconds."
   FreeBank b:CloseFile file:file=0
  Else
   ;just a directory dump...
   While Not Eof( dat )
    Print ReadLine$( dat )
   Wend
  EndIf
  CloseTCPStream dat:dat=0

 Default ;case not recognized

  If st<200 Or st>=300 Input$("Error: Fatally confused error! ") : End
  send$=""

  Repeat ;ftp prompt

   Repeat
    arg$=Trim$( Input$( "> " ) ) ;ftp prompt char, was ']'
   Until arg$<>""
   For n=1 To 3
    i=Instr( arg$," " )
    If i=0 Then args$(n)=arg$:Exit
    args$(n)=Left$(arg$,i-1)
    arg$=Trim$( Mid$(arg$,i+1) )
   Next

   Select Lower$( args$(1) ) ;ftp command name

   Case "lcd","lchdir" ;print or change local directory
    If n>2
     Print "Usage: lcd/lchdir [local_dir]"
    Else
     If n=2 ChangeDir args$(2)
     Print Chr$(34)+CurrentDir$()+Chr$(34)
    EndIf

   Case "ldir" ;list contents of local directory
    If n>2
     Print "Usage: ldir [local_dir]"
    Else
     lpath$=CurrentDir$()
     If Len(lpath$)=3 Then lpath$=Mid$(lpath$,1,2) ;root dir
     If n=2 Then lpath$=lpath$+""+args$(2)
     ldir=ReadDir(lpath$)
     lfile$=NextFile$(ldir)
     While lfile$<>""
      If FileType(lpath$+""+lfile$)=2 ;add "" if subdirectory
       If lfile$<>"." And lfile$<>".." Then lfile$=lfile$+""
      EndIf
      Print lfile$
      lfile$=NextFile$(ldir)
     Wend
     CloseDir ldir
    EndIf

   Case "cd","chdir","cwd" ;print or change working directory
    If n=1
     send$="PWD"
    Else If n=2
     send$="CWD "+args$(2)
    Else
     Print "Usage: cd/chdir/cwd [remote_dir]"
    EndIf

   Case "pwd" ;print working directory
    send$="PWD"

   Case "cdup" ;change directory to parent
    send$="CDUP"

   Case "dir","list" ;list contents of directory
    If n=1
     send$="PASV":dat_com$="LIST"
    Else If n=2
     send$="PASV":dat_com$="LIST "+args$(2)
    Else
     Print "Usage: dir/list [remote_dir]"
    EndIf

   Case "ndir","nlst" ;list contents of directory, no other information
    If n=1
     send$="PASV" : dat_com$="NLST"
    Else If n=2
     send$="PASV" : dat_com$="NLST "+args$(2)
    Else
     Print "Usage: ndir/nlst [remote_dir]"
    EndIf

   Case "stat" ;status of file transfer or like "list" with argument
    If n=1
     send$="STAT"
    Else If n=2
     send$="STAT "+args$(2)
    Else
     Print "Usage: stat [remote_filespec]"
    EndIf

   Case "get","retr" ;retrieve/download filename
    If n=2
     fi$=Replace$( args$(2),"","/" )
     While Instr( fi$,"/" )
      fi$=Mid$( fi$,Instr( fi$,"/" )+1 )
     Wend
     file=WriteFile( fi$ )
     If file 
      send$="PASV" : dat_com$="RETR "+args$(2)
     Else
      Print "Unable to open file for writing"
     EndIf
    Else
     Print "Usage: get/retr filename"
    EndIf

   Case "put","stor" ;store/upload filename
    If n=2
     fi$=Replace$( args$(2),"","/" )
     While Instr( fi$,"/" )
      fi$=Mid$( fi$,Instr( fi$,"/" )+1 )
     Wend
     file=ReadFile( fi$ )
     If file
      send$="PASV":dat_com$="STOR "+args$(2)
     Else
      Print "Unable to open file for reading" 
     EndIf
    Else
     Print "Usage: put/stor filename"
    EndIf

   Case "del","erase","dele" ;delete filename
    If n=2
     send$="DELE "+args$(2)
    Else
     Print "Usage: del/erase/dele filename"
    EndIf

   Case "rmdir","rd","rmd" ;remove directory
    If n=2
     send$="RMD "+args$(2)
    Else
     Print "Usage: rmdir/rd/rmd path"
    EndIf

   Case "mkdir","md","mkd" ;make new directory
    If n=2
     send$="MKD "+args$(2)
    Else
     Print "Usage: mkdir/md/mkd path"
    EndIf

   Case "rename","ren","rnfr","rnto" ;rename filename
    If n=3 ;rename has 3 arguments, see case 350
     send$="RNFR "+args$(2) ;rename from
    Else
     Print "Usage: rename/ren/rnfr/rnto fromfilename tofilename"
    EndIf

   Case "type" ;set data transfer type, default is ascii
    If n=2
     send$="TYPE "+args$(2)
    Else
     Print "Usage: type A E I" ;A=ascii,E=ebcdic,I=image/binary
    EndIf

   Case "mode" ;set data transfer mode, default is stream
    If n=2
     send$="MODE "+args$(2)
    Else
     Print "Usage: mode S B C" ;S=stream,B=block,C=compressed
    EndIf

   Case "stru" ;set data structure, default is file
    If n=2
     send$="STRU "+args$(2)
    Else
     Print "Usage: stru F R P" ;F=file,R=record,P=page
    EndIf

   Case "size" ;print size of filename, in bytes
    If n=2
     send$="SIZE "+args$(2)
    Else
     Print "Usage: size filename"
    EndIf

   Case "mdtm" ;print last-modified time of filename, as YYYYMMDDhhmmss
    If n=2
     send$="MDTM "+args$(2)
    Else
     Print "Usage: mdtm filename"
    EndIf

   Case "sys","syst" ;print system type used by server
    send$="SYST"

   Case "noop","ping" ;no operation, verifys tcp connection to server
    send$="PASV" : dat_com$="NOOP"

   Case "quit","exit" ;quit ftp connection
    send$="QUIT"

   Case "help" ;list local and remote command help
    Print "Local help:"
    Print " lcd/lchdir, ldir, cd/chdir/cwd, pwd, cdup"
    Print " dir/list, ndir/nlst, stat, get/retr, put/stor"
    Print " del/erase/dele, rename/ren/rnfr/rnto, rmdir/rd/rmd"
    Print " mkdir/md/mkd, type, mode, stru, size, mdtm"
    Print " sys/syst, noop/ping, quit/exit, help, cls, shot"
    Color 0,192,255
    Print "Remote help:"
    send$="HELP"

   Case "cls" ;clear screen
    Cls : Locate 0,0

   Case "shot" ;take a screenshot
    SaveBuffer FrontBuffer(),"ftpshot.bmp"

   Default
    Print "Unrecognized command "+Chr$(34)+args$(1)+Chr$(34)+"."

   End Select

  Until send$<>""
  WriteLine com,send$ ;send ftp prompt

 End Select

Forever


Zeachco
(Posted 19 years ago)
Wow that's good to make nice software updater =D

markcw
(Posted 19 years ago)
Zeachco: merci.

Code Archives Forum