/*
  BaudBandit V1.30 remote server      Needs ARexx 1.10

  'F10 \mAnswer local' for local login.
*/

arg option
signal on break_c
Options Results

Remote U ; UPath=Result
Remote D ; DPath=Result
Remote W ; WelcomeFile=Result
Remote P ; Password=Upper(Result)
Remote S ; SPassword=Upper(Result)

if Right(UPath,1)=':' then
	MsgFile=UPath'BBMsgs'
else	MsgFile=UPath'/BBMsgs'

maxidle=3*60		/* Idle time = 3 minutes */

/** remote login **/

if(option='') then do
	Send 'ATA\r'
	Timeout 20
	Wait 'CONNECT'		/* wait 20 seconds for connect */
	if rc~=0 then exit	/* timed out? */
	call delay 120
	Remote ON
end
else do /* local */
	Set H
	Quiet on
end

Timeout maxidle 		/* max idle time at prompts */


/** Welcome message */
address command 'Type' WelcomeFile

tries=0
options prompt 'Password: '
say
do forever
	pull pass
	if(pass=password | pass=spassword) then break;	/* correct password */
	else do
		tries=tries+1
		if(tries=3) then do		/* 3 tries, hang up */
			say 'Access terminated.'
			Remote OFF
			Quiet off
			Set F
			Send '\ah'
			exit
		 end
		options prompt 'Incorrect.  Password: ' /* ask again */
	 end
 end
hilevel=0
if(pass=spassword) then hilevel=1

Status 'D'
startdir=result
call pragma('D',DPath)
directory=pragma('D')
Data directory

/* Get current protocol */
Status Trans
protocol = RESULT

/*-------------------------- MAIN --------------------------------------------*/


do while(opt~='G')
	go=0
	do while(~go)
			say
			say '                 ___________'
			say '     ___________/ Main Menu \__________'
			say '    |                                  |'
			say '    |  [L]ist        [P]rotocol        |'
			say '    |  [D]ownload    [U]pload          |'
if hilevel~=0 then	say '    |  [S]hell                         |'
			say '    |  [M]essage     [R]ead text file  |'
			say '    |  [G]oodbye     [H]elp            |'
			MSG '    |  [\] Chat                        |'
			say '    |__________________________________|'
			say

		options prompt time(c)'> '
		parse upper pull string' 'arg
		opt=left(string,1)

		go=1	/* check for access */
		if(hilevel=0) then if(opt='S') then go=0
	end

	options prompt 'Filename: '
	select
		when(opt='L') then call list();
		when(opt='P') then call chpro();
		when(opt='D') then call dload();
		when(opt='U') then call uload();
		when(opt='H') then call help();
		when(opt='R') then call read();
		when(opt='M') then call edit();
		when(opt='S') then call shell();
		when(opt='\') then call chat();
		otherwise call page();
	end
end

say 'Goodbye, thank you for calling.'

if(option='') then do
	Remote off
	dcd
	if(rc~=0) then Send '\ah'
end

Data startdir
Quiet off
Set f
exit

/*------------------------- FUNCTIONS ---------------------------------------*/

help:	say
	say ' Command summary:'
	say ' L    List files in directory'
	say ' P    Protocol menu.  Or give option at main prompt (P Z)'
	say ' U    Upload file.        Optional ( U filename )'
	say ' D    Download file.      Optional ( D filename )'
 if(hilevel~=0) then
	say ' S    Enter command shell.  CD will change D/L dir.'

	say ' R    Read text file.     Optional ( R filename )'
	MSG ' \    Enter chat mode.  \ on a blank line to end.'
	say ' G    Goodbye. (Hangup!)'
	say
	options prompt 'Continue:'
	pull arg
	return;

shell:	say
	Say 'Dropping to DOS.  Type EXIT to end.'
	do while(upper(opt)~='EXIT')
		options prompt '1B'x'[33m'pragma('D')'>'||'1B'x'[0m'
		parse pull opt' 'arg
		if(upper(opt)='CD') then do
			call pragma('D',arg)
			directory=pragma('D')
			Data directory		/* Keeps BB in sync w/shell */
			iterate
		end
		if exists(opt)~=0 then do	/* Implicit CD */
			if left(statef(opt),3)='DIR' then do
				call pragma('D',opt)
				directory=pragma('D')
				Data directory
				iterate
			end
		end
		if(opt~='' & upper(opt)~='EXIT') then
			address command opt '<* >*' arg
	 end
	return

uload:	if(arg='') then pull arg	/* no filename given */
	if(arg~='') then do		/* check for filename */
		Say 'Starting' protocol 'Upload'
		call pragma('D',UPath)
		directory=pragma('D')
		Data directory
		DownLoad arg
		call pragma('D',DPath)
		directory=pragma('D')
		Data directory
	end
	return

dload:	if(arg='') then pull arg	/* no filename given */
	if(arg~='') then do		/* check for filename */
		Say 'Starting' protocol 'Download'
		UpLoad arg
	end
	return

edit:	if ~Open(f,MsgFile,'A') then
		if ~Open(f,MsgFile,'W') then do
			say 'Unable to open message file.'
			return
		end
	Say
	Say 'Enter your message.  [Return] on a blank line to end.'
	do while(line~='')
		options prompt ': '
		parse pull line
		call writeln(f,line)
	end
	call writeln(f,'')
	call Close(f)
	Say 'Message saved.'
	return

chpro:	if(arg='') then do	/* show protocol menu */
		say
		say '[W]- WXModem'
		say '[X]- XModem-CRC'
		say '[K]- XModem-1K'
		say '[Y]- YModem'
		say '[G]- YModem-G'
		say '[Z]- ZModem'
		say '[A]- ZModem (Upload at Menus)'
		say
		options prompt protocol '> '
		pull arg
	 end
	Set arg
	Status Transfer
	protocol = RESULT
	say protocol
	return

read:	if(arg=='') then pull arg	/* no filename given */
	say
	if(arg~='') then address command 'CD' directory'0A'x 'Type >* <*' arg
	options prompt 'Continue:'
	pull arg
	return

list:	address command 'list >* <*' directory
	options prompt 'Continue:'
	pull arg
	return

chat:	options prompt ''
	string=''
	do while(string~='\')
		pull string
	end
	return

page:	Beep 320;Beep 390;Beep 360;Beep 500;Beep 240
	return

break_c:
	Say 'Timeout/Carrier loss -- Disconnecting.'
	Remote OFF
	dcd
	if(rc~=0) then Send '\ah'
	Data startdir
	Quiet off
	Set F
	exit

