;	trace on		; Debugging
;
; ----- COM-AND Scripted host mode ----------------------------------
;
;	Goals:
;	o	Must autodetect caller's baud rate
;	o	Must work correctly for modems reporting true CD and otherwise.
;	o	Must log all activity
;
;	Functions:
;	o	Passworded log-on
;	o	DIR of current directory
;	o	CHDIR command
;	o	UP and DOWNLOADS
;	o	Graphical path display (using P/D TREED program)
;	o	Passworded DOS access (dangerous!)
;	o	Passworded drop-to-DOS using a doorway function
;
;	Commenced: 10/29/87 R.McG
;	Updated:    2/--/89 R.McG
;		   10/--/89 R.McG  ZMODEM added
;	Ver 1.1:   11/--/90 R.McG  HOSETUP added
;		    6/--/91 R.McG  DROPDOS added
;	--------------------------------------------------------------
;	Data for this script are established through the HOSETUP script.
;	The drop-to-DOS requires a doorway function (such as DOORWAY,
;	by Marshsall Dudley), and the script HOSTART.  The only other
;	file requisite to this HOST script is the TREED p/d utility.
;	--------------------------------------------------------------
;
;	Initialize
;
	LEGEND "Scripted HOST mode (1.1).  Press ESC to exit."
;
;	Set default values (in case HOSTDAT does not exist)
;
	S20 = "_PARM"(11:14)*","*"_PARM"(0:3) ; Port(4),speed(4)
	S21 = "ATE0Q0V1X1S0=2 S7=30 S9=10^M"  ; Standard MINIT for HOST
	S22 = "****"                    ; Set default logon password
	S23 = "xxxx"                    ; Set default DOS password
	S24 = ""                        ; Drop to DOS command
;
;	Initialize COM related values (This is done here to allow HOSTDAT
;	... edits to override these settings)
;
	SET PARITY NONE 		; fixed no parity
	SET DATA 8			; fixed 8 data bits
	SET STOP 1			; fixed 1 stop bit
	SET MASK ON			; accept 7 or 8 bits
	SET CR_IN CR_LF 		; Display received c/rs as a cr/lf
	SET ASCII UP_LF LF		; Send LFs
	SET SOFTFLOW ON 		; Allow XON/XOFF
;
;	Replace above values from HOSTDAT, if that script exists
;
	IF ISSC "HOSTDAT" FCALL "HOSTDAT"
;
;	Initialize variables that must be constant
;
	S0 = S22			; Set to our subdirectory
	S3 = S23			; Set subdir for files
	SUBDIR S29			; Read current subdir
	DLDIR S28			; Read current download subdir
;
;	Set initial values that do not change port setting
;
	LOG MARK ON			; Timestamp logging
	CLOG "* Host script loaded"
	ON ESCAPE GOSUB End		; Exit routine
	SET INAFTER OFF 		; Turn off init after hangup
	SET ALARM OFF			; Turn off alarm
	SET ATIME 1			; Set alarm time to 1 second
;
;	If this is a restart, pickup at the main prompt
;
	SET PORT S20(0:3)		; Starting port
	IF FCALLED and ISFILE "HOSTTEMP.BAT"
	   SET RECHO ON 		; Restart - need to reenable
	   LOG OPEN "HOSTLOG"           ; .. reenable
	   CLOG "* Return from drop-to-DOS"
	   GOTO Main_Prompt
	   ENDIF
;
;	Initialize values that change port setting, and start a new call
;
	SET BAUD S20(5:8)		; Starting speed
	TRANSMIT "_MESCAPE"             ; Initialize modem (modem escape)
	GOTO Restart			; Branch around subroutines
;
; -----------------------------------------------------------------------
;	Subroutine: End of HOST
; -----------------------------------------------------------------------
;
End:
	HANGUP				; Hangup the phone
	CLOG "* HOST script terminated" ; Log completion
	SET DLDIR S28			; Reset dldir
	CHDIR S29			; Reset to default directory
	RESET				; Reset default values
	CLEAR				; Clear screen
	MESS "HOST terminated... type Alt-X to exit COM-AND^M^J^M^J"
	TRAN "_MINIT"                   ; Initialize modem from defaults
	DELETE "\HOSTTEMP.TXT"          ; Cleanup
	EXIT				; Exit
;
;	Subroutine: Read from the caller into S9
;	.. This handles 'disconnect' and timeouts.
;
Read_Comm:
	RGET S9 80 180		; wait for a connection
	IF NOT CONNECTED	; If modem reports CD dropped
	   GOTO Disconnect	; Goto disconnect
	   ENDIF

	IF NOT SUCCESS		; If timeout on the RGET
	   GOTO Timeout 	; .. issue message and disconnect
	ENDIF

	FIND S9 "NO CARRIER"    ; Test for message from modem
	IF FOUND		; If modem didn't report 'CD' true
	   GOTO Disconnect	; Goto disconnect
	   ENDIF

	SET FLAG(0) OFF 	; Report to caller
	RETURN			; Return with text in S9
;
;	Timeout on the call
;
Timeout:
	TRAN "^M^J... autodisconnect due to timeout"
	MESSAGE "^M^J... autodisconnect due to timeout"
	GOTO RComm_Exit 	; Exit cycle in the usual manner
;
;	Disconnect was reported.
;
Disconnect:
	MESSAGE  "^M^JCaller disconnected"
;
;	Read_Comm error exit
;
RComm_Exit:
	SET FLAG(0) ON		; Report to caller
	RETURN			; Return to the caller
;
;	Usages:
;	  S0 -> main password
;	  S1 -> ID
;	  S2 -> Default drive/subdir
;	  S3 -> DOS password
;	  S8 -> File name buffer
;	  S9 -> General read buffer
;
;	Begin the sequence...
;
Restart:
	CHDIR S29		; Reset to default drive
	SET RECHO OFF		; Turn off echo for us
	SET RDISP OFF		; Disable display of rcvd chars
	CLEAR			; Clear screen
	LOCATE 0,0		; Set to home
;
;	Go into auto answer (echo off, answer on 3rd)
;	Also: Return result codes, word form, with CONNECT 1200
;
;
	HANGUP			; HANGUP and leave modem in cmd mode
	MESSAGE "^M^JWaiting...!"
	PAUSE 3 		; Wait 3 secs
	SET BAUD S20(5:8)	; Starting speed
	TRANSMIT S21		; Transmit modem initialization
;
;	Wait for a connect
;
WAIT_IT_OUT:
	RGET S9 80 180
	IF NOT SUCCESS
	   GOTO Wait_IT_Out
	   ENDIF

	FIND S9 "NO CARRIER"
	IF FOUND
	   GOTO Restart
	   ENDIF

	FIND S9 "CONNECT"
	IF NOT FOUND
	   GOTO WAIT_IT_OUT
	   ENDIF
;
;	Connection established: Adjust our linespeed if need be
;
	GOSUB AutoBaud		; Change rate according to CONNECT MSG
;
;	Issue a greeting
;
	PAUSE 2 		; Let the modem settle
	RFLUSH			; Clear junk
	TRAN "^M^JThe Flying Scotsman greets you!!^M^J"
	SET RECHO ON		; Turn on echo (back to caller)
	SET RDISP ON		; Turn on display of rcvd chars
	LOG OPEN "HOSTLOG"
;
;	Request an ID
;
ID_Query:
	MESS "^M^JID prompt: "  ; Local console indicator
	TRANSMIT "^M^JEnter your ID: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO Exit		; .. disconnect and start over
	   ENDIF		; ..

	SWITCH S9
	   CASE "_NULL"         ; Test for nothing entered
	      TRAN "You must be someone^M^J"
	      GOTO Exit 	; Don't let noone in
	  ENDCASE		; End of ridicule
	ENDSWITCH		; End of ID test

	CLOG "* Host mode logon by "*S9
;
;	Request a password
;
	TRANSMIT "^M^JEnter your password: "
	LOG SUSPEND
	SET RECHO OFF		; Turn of echo of received text
	SET RDISPLAY OFF	; Turn off echo to console too

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO Exit		; .. disconnect and start over
	   ENDIF		; ..

	LOG RESUME		; Restore logging
	SET RECHO ON		; Restore echo
	SET RDISPLAY ON 	; Turn on echo to console again
;
;	Test for the main password
;
	SWITCH S9
	   CASE S0		; Test for match with S0
	      TRANSMIT "^M^J"   ; OK - good password
	   ENDCASE		; End match with S0
	   DEFAULT		; Not one of the above
	      TRANSMIT "Sorry , but you're not authorized."
	      GOTO Exit 	; And disconnect
	   ENDCASE		; End of DEFAULT
	ENDSWITCH
;
;	Now - do something
;
Main_Prompt:
	MESS "^M^JMain prompt: "; Local console indicator
	TRAN "^M^JC)hdir F)ilelist, P)athlist, U)pload, D)ownload, or E)xit: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO EXIT		; .. disconnect and start over
	   ENDIF		; ..

	SWITCH S9		; Test the entry
	   CASE "C"
	      GOTO CHDIR
	   ENDCASE

	   CASE "D"
	      GOTO DOWNLOAD
	   ENDCASE

	   CASE "E"
	      TRAN "Ok... bye^M^J"
	      GOTO EXIT
	   ENDCASE

	   CASE "F"
	      GOTO FILELIST
	   ENDCASE

	   CASE "P"
	      GOTO PATHLIST
	   ENDCASE

	   CASE "U"
	      GOTO UPLOAD
	   ENDCASE

	   CASE "X"
	      GOTO DOS
	   ENDCASE

	   CASE "Y"
	      GOTO DROPDOS
	   ENDCASE

	   CASE "OFF"
	      TRAN "Ok... bye^M^J"
	      GOTO EXIT
	   ENDCASE
	;
	;	Default case for typists
	;
	   DEFAULT
	      FIND S9 "CHDIR"   ; Try for larger
	      IF FOUND		; If entry contained "CHDIR"
		 GOTO CHDIR
		 ENDIF

	      FIND S9 "DOWN"    ; Try for larger
	      IF FOUND		; If entry contained 'Down"
		 GOTO DOWNLOAD
		 ENDIF

	      FIND S9 "FILE"    ; Try for larger
	      IF FOUND		; If entry contained 'FILE"
		 GOTO FILELIST
		 ENDIF

	      FIND S9 "PATH"    ; Try for larger
	      IF FOUND		; If entry contained 'PATH"
		 GOTO PATHLIST
		 ENDIF

	      FIND S9 "UP"      ; Try for larger
	      IF FOUND		; If entry contained 'up"
		 GOTO UPLOAD
		 ENDIF

	      FIND S9 "DOS"     ; Try for larger
	      IF FOUND		; If entry contained 'DOS"
		 GOTO DOS
		 ENDIF

	      TRAN "^M^JCommand not recognized... try again"
	      GOTO Main_Prompt	; If none of the above
	   ENDCASE		; End of DEFAULT
	ENDSWITCH
;
;	Can't get here because of the DEFAULT in the SWITCH above
;
	TRAN "^M^JThank you veddy much.^M^J"
	GOTO Main_Prompt
;
;	General exit routine - don't GOTO from within a subroutine!!!
;
EXIT:
	CLOG "* Host mode exit"
	LOG CLOSE		; Turn off logging
	MESS "^G"               ; Beep to indicate completion
	GOTO Restart		; And start over
;
;	Subroutine: Query for a file name - return in S8
;
File_Query:
	MESS "^M^JFname query: "; Local console indicator
	TRAN "^M^JEnter the file name: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   RETURN		; .. disconnect and start over
	   ENDIF		; ..

	ASSIGN S8 S9		; Move fname into another variable
	SWITCH S8
	   CASE "_NULL"         ; Test for nothing entered
	      SET FLAG(1) ON	; Report to caller
	      RETURN		; Return right here w/ flag set
	   ENDCASE
	ENDSWITCH		; End of ID test

	SET FLAG(1) OFF 	; Report to caller
	RETURN			; Return to caller
;
;	XMODEM Upload (up from caller)
;
UPLOAD:
	MESS "^M^JUpload from caller"

	GOSUB File_Query	; Ask for a file name
	IF FLAG(0)		; If first flag rtns set
	   GOTO EXIT		; .. disconnect and start over
	   ENDIF		; ..

	IF FLAG(1)		; If no file returned
	   GOTO Main_Prompt	; .. start over
	   ENDIF		; ..


	ISFILE S8		; Test for file already
	IF SUCCESS		; If file exists
	   TRAN "^M^JFile already exists"
	   GOTO UPLOAD		; Ask again
	   ENDIF

	MESS "!Method prompt: " ; Local console indicator
	TRAN "^M^JW)xmodem, X)modem, Y)modem (X1k), Z)modem, or K)ermit: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO Main_Prompt	; .. disconnect and start over
	   ENDIF		; ..

	SWITCH S9		; Test the entry
	   CASE "W"
	      GETFILE WXMODEM S8
	   ENDCASE
	   CASE "X"
	      GETFILE XMODEM S8
	   ENDCASE
	   CASE "Y"
	      GETFILE YMODEM S8
	   ENDCASE
	   CASE "Z"
	      GETFILE ZMODEM
	   ENDCASE
	   CASE "K"
	      GETFILE KERMIT	; FIle name supplied by caller
	   ENDCASE
	   DEFAULT
	      TRAN "^M^JInvalid transfer selection"
	      GOTO Main_Prompt
	   ENDCASE
	ENDSWITCH

	GOTO EOTransfer 		; Report success/failure
;
;	XMODEM Download (down to caller)
;
DOWNLOAD:
	MESS "^M^JDownload to caller"

	GOSUB File_Query	; Ask for a file name
	IF FLAG(0)		; If first flag rtns set
	   GOTO EXIT		; .. disconnect and start over
	   ENDIF		; ..

	IF FLAG(1)		; If no file returned,
	   GOTO Main_Prompt	; .. start over
	   ENDIF		; ..

	ISFILE S8		; Test for file already
	IF NOT SUCCESS		; If file doesn't exist
	   TRAN "^M^JFile doesn't exist"
	   GOTO DOWNLOAD	; Ask again
	   ENDIF

	MESS "^M^JMethod prompt "
	TRAN "^MW)xmodem, X)modem, Y)modem (X1k), Z)modem, K)ermit, or A)scii: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO Main_Prompt	; .. disconnect and start over
	   ENDIF		; ..

	SWITCH S9		; Test the entry
	   CASE "A"
	      SENDFILE ASCII S8
	   ENDCASE
	   CASE "W"
	      SENDFILE WXMODEM S8
	   ENDCASE
	   CASE "X"
	      SENDFILE XMODEM S8
	   ENDCASE
	   CASE "Y"
	      SENDFILE YMODEM S8
	   ENDCASE
	   CASE "Z"
	      SENDFILE ZMODEM S8
	   ENDCASE
	   CASE "K"
	      SENDFILE KERMIT S8
	   ENDCASE
	   DEFAULT
	      TRAN "^M^JInvalid transfer selection"
	      GOTO Main_Prompt
	   ENDCASE
	ENDSWITCH

	GOTO EOTransfer 		; Report success/failure
;
;	End of transfer... note result on local console
;
EOTRANSFER:
	IF NOT SUCCESS
	   MESS "^M^JTransfer failed"
	ELSE
	   MESS "^M^JTransfer OK"
	   ENDIF
	GOTO Main_Prompt
;
;	Filelist... awkward... but it works
;
FILELIST:
	MESS "^M^JFilelist command: "   ; Local console indicator
	TRAN "^M^J Working..."          ; May take a moment

	DOS "DIR >HOSTTEMP.TXT"         ; To a temp file
	TRAN "^M^J"                     ; Send a c/r
	SENDFILE ASCII "HOSTTEMP.TXT"
	TRAN "^M^J"                     ; Send a c/r

	DOS "DEL HOSTTEMP.TXT"          ; Clean up after us
	GOTO Main_Prompt		; And continue
;
;	CHDIR... Query for a path.
;
CHDIR:
	MESS "^M^JCHDIR Command: "      ; Local console indicator
	TRAN "^M^JEnter the drive:subdirectory: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO Main_Prompt	; .. disconnect and start over
	   ENDIF		; ..

	CHDIR S9		; Do it.
	GOTO Main_Prompt	; And continue
;
;	Path tree... awkward... but it works
;
PATHLIST:
	MESS "^M^JPathlist command: "; Local console indicator
	TRAN "^M^J Working..."  ; May take a moment

	DOS "Treed >HOSTTEMP.TXT" ; To a temp file
	TRAN "^M^J"             ; Send a c/r
	SENDFILE ASCII "HOSTTEMP.TXT"
	TRAN "^M^J"             ; Send a c/r

	DOS "DEL HOSTTEMP.TXT"  ; Clean up after us
	GOTO Main_Prompt	; And continue
;
;	DOS command: Accept a command and execute it
;
DOS:
	GOSUB DOSPSW		; Request a password
	IF FAILED GOTO Main_Prompt
;
;	DOS... Query for a command
;
	MESS "^M^JDOS Command: "; Local console indicator
	TRAN "^M^JEnter the command: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO Main_Prompt	; .. disconnect and start over
	   ENDIF		; ..

	TRAN "^M^J Working..."  ; May take a moment

	CONCAT S9 ">HOSTTEMP.TXT"
	DOS   S9		; Do it.

	TRAN "^M^J"             ; Send a c/r
	SENDFILE ASCII "HOSTTEMP.TXT"
	TRAN "^M^J"             ; Send a c/r

	DOS "DEL HOSTTEMP.TXT"  ; Clean up after us
	GOTO Main_Prompt	; And continue
;
;	DOSPSW	Request a password
;
DOSPSW:
	MESSAGE "^M^JRequesting DOS password"
	LOG SUSPEND		; Turn off logging
	SET RECHO OFF		; .. and don't echo passsword
	SET RDISPLAY OFF	; Turn off echo to console too

	TRANSMIT "^M^JEnter the DOS password: "

	GOSUB Read_Comm 	; Read into S9
	IF FLAG(0)		; If first flag rtns set
	   GOTO DOSPERR 	; Exit, failed
	   ENDIF		; ..

	LOG RESUME		; Turn on logging again
	SET RECHO ON		; .. and begin echoing again
	SET RDISPLAY ON 	; Turn on echo to console again
;
;	Test for the our password
;
	SWITCH S9
	   CASE S3		; Test for match with S3
	      TRANSMIT "^M^J"   ; OK - good password
	   ENDCASE		; End match with S3
	   DEFAULT		; Not one of the above
	      TRANSMIT "Sorry , but you're not authorized."
	      GOTO DOSPERR	; Exit, failed
	   ENDCASE		; End of DEFAULT
	ENDSWITCH
;
;	Return with success
;
	LOG RESUME		; Restart log
	SET SUCCESS ON		; Set indicator for caller
	RETURN
;
;	Return with success
;
DOSPERR:
	LOG RESUME		; Restart log
	SET SUCCESS OFF 	; Set indicator for caller
	RETURN
;
;	DROPDOS command: Request a password
;
DROPDOS:
	IF NULL S24
	   TRAN "^M^JCommand not recognized... try again"
	   GOTO Main_Prompt	; Can't do it
	   ENDIF
	GOSUB DOSPSW		; Request a password
	IF FAILED GOTO Main_Prompt
;
;	DROPDOS... Build a batch file
;
	FOPENO "HOSTTEMP.BAT" TEXT
	IF NOT SUCCESS
	   TRAN "File error - cannot drop to DOS^M^J"
	   GOTO Main_Prompt
	   ENDIF

	WRITE "ECHO OFF!"       ; Start the batch file
	S0 = S24		; Setup up drop to DOS command
	PRESERVE S0		; Make it printable
	WRITE S0		; Write the Drop to DOS command
	WRITE "!"               ; And a terminating cr

	WRITE "CD "*S29*"!"     ; Change a changed directory
	WRITE "COM-AND /q/p/fHOSTART!" ; inhibit COM-AND.CMD; take modem as set
	WRITE "^Z"
	FCLOSEO 		; And we're done with it

	CLOG "* Drop-to-DOS"

	SET TTHRU OFF		; Disable type through
	STACK CLEAR		; Place invocation of the batch file
	STACK "HOSTTEMP.BAT!"   ; .. into BIOS's area
	BYE			; Do it.
;
;	Auto baudrate detect (according to message in S9)
;
AutoBaud:
	IF FIND S9 "1200"
	   SET BAUD 1200		; Set to new rate
	   RETURN			; We're done.
	   ENDIF

	IF FIND S9 "2400"
	   SET BAUD 2400		; Set to new rate
	   RETURN			; We're done.
	   ENDIF

	IF FIND S9 "4800"
	   SET BAUD 4800		; Set to new rate
	   RETURN			; We're done.
	   ENDIF

	IF FIND S9 "9600"
	   SET BAUD 9600		; Set to new rate
	   RETURN			; We're done.
	   ENDIF

	IF FIND S9 "19200"
	   SET BAUD 19.2		; Set to new rate
	   RETURN			; We're done.
	   ENDIF

	IF FIND S9 "19.2"
	   SET BAUD 19.2		; Set to new rate
	   RETURN			; We're done.
	   ENDIF
;
;	None of the above... set to 300
;
	SET BAUD 300		; Set to 1200 baud
	RETURN			; We're done.
