/* Mini TERM BBS (c) 1993 The Brazilian (bmaple@burner.com)
**
*/

address TERM
options results                 /* Gets info back from Term              */

password = 'SECRET'
curdir = 'Work:'
Retries = 3

call TIME('R')

say "MiniBBS (c) 1993 The Brazilian"
say "bmaple@burner.com"
say ""
say "New features added by Christian Mag Magnusson."
say "This only a test how to use the Data connection from VoiXEL"
say "VoiXELminiBBS V1.00"

ANSWER:

   TIMEOUT SEC 70               /* Set term's expect time-out to 70 sec */
   SEND 'AT^M'                  /* Replace w/setup string of choice     */
   DELAY SEC 1

CYCLE:

   CLEAR WAIT                   	/* Clear's term's expect string list    */
   ADDITEM TO WAIT NAME "CONN"		/* Get term to return when "CONNECT"    */
   ADDITEM TO WAIT NAME "NO CARRIER"/* Get term to return when "NO CARRIER" */
   SEND 'ATA^M'                 	/* Answer phone at once                 */
   WAIT
   Res = result            			/* The whole string term matched        */

   /* term will return from the WAIT after the timeout length is        */
   /* exceeded, or it gets one of the strings on the wait list.         */
   /* For this reason, we have to actualy CHECK what term returns to    */
   /* find out if it's a 'RING', or if it timed out and got noting..    */

	if rc ~= 0 then
		do
	    	QUITBBS(10);
		end
    else
    if upper( left( Res, 4 ) ) = 'CONN' then
		do
	        DELAY MIC 20              /* 20 microsecs; some modems need this   */

	        GETATTR TERM.SESSION.CONNECTMESSAGE

	        DELAY SEC 1

           /* At this point we'll set the timeout to 70 seconds,        */
           /* otherwise MiniBBS will log them off after only 20 seconds */
           /* of non-activity.                                          */

	        TIMEOUT SEC 20               /* Set 'idle logoff' time       */

           /* START mini BBS stuff */
           /************************/

	        call CLS
	        SEND '^M^JWelcome to VoiXEL''s MiniBBS V1.00!^M^J^M^J'
RETYPEPASSWORD:
	        SEND '"' || 'Password: ' || '"'
	        READ CR NOECHO   ; UsrPass = result

	       	if upper(UsrPass) = upper(Password) then
				do
			        TIMEOUT SEC 60               /* Set 'idle logoff' time       */
	       			SEND '  Welcome!^M^J^M^J'
	            	DELAY SEC 1

	            	LOOP:

	            	call CLS
	            	SEND '^M^JMiniBBS Main Menu^M^J-----------------^M^J^M^J'
	            	SEND 'U. Upload file^M^J'
	            	SEND 'D. Download file^M^J'
	            	SEND 'C. Change Directory^M^J'
	            	SEND 'S. Show Directory (Dir)^M^J'
	            	SEND 'L. List Directory (List)^M^J'
	            	SEND 'G. Loggoff^M^J^M^J'
	        		SEND 'Choose a command:  '
	              	READ CR;input = result

					if rc ~= 0 then
				    	QUITBBS (1)
				    else if input = 'U' then call Upload
	              	else if input = 'D' then call Download
	              	else if input = 'C' then call ChangeDir
	              	else if input = 'S' then call Dir
	              	else if input = 'L' then call LIST
	              	else if input = 'G' then call QUITBBS(0)
	              	else call LOOP
				end
	      	else
				do                      /* Bad password! */
	              	SEND 'Bad password.^M^J^M^J'
					Retries = Retries - 1
					if Retries > 0 then
						do
							call RETYPEPASSWORD
						end
					else
						do
							call QUITBBS(2)
						end
				end
		end
	else
	if upper( left( Res, 10 ) ) = 'NO CARRIER' then
		do
			QUITBBS(9)
		end
	else
		do
			QUITBBS(8)
		end
exit


QUITBBS: parse arg retcode
	SEND '^M^J^M^JGood bye...^M^J^M^J'
	HANGUP
say TRUNC(TIME('E'))
	ADDRESS VOIXELPORT 'SetVAR LENGTH ' || TRUNC(TIME('E'))
	if retcode=0 then
		do
			ADDRESS VOIXELPORT 'SetVAR CALLERNUMBER OK'
		end
	else
		do
			ADDRESS VOIXELPORT 'SetVAR CALLERNUMBER ' || retcode
		end
	QUIT FORCE							/* Term MUST exit after logoff */
	exit

CHANGEDIR:
	SEND '^M^J^M^JCurrent Path : ' || curdir
	SEND '^M^JChange Directory To : '
	READ CR ; NewDir = result
	if Exists(NewDir) then
		do
			curdir = NewDir
		end
	else
		do
			SEND 'File/Directory not found^M^J^M^J'
		end
	return


DIR:
	EXECTOOL COMMAND 'VoiXEL:showdir ' || curdir
	SENDFILE MODE ASCII 't:dirtmp'
	return

LIST:
	EXECTOOL COMMAND 'VoiXEL:listdir ' || curdir
	SENDFILE MODE ASCII 't:dirtmp'
	return

UPLOAD:
   SEND '^M^J^M^JReady to receive file (ZModem).  Send at will.^M^J'
   RECEIVEFILE

   DELAY SEC 5
   SEND '^M^JComplete.^M^J^M^J'

   SELECTITEM FROM DOWNLOAD TOP

   do while rc = 0
      SEND 'UL File: ' || result
      SELECTITEM FROM DOWNLOAD NEXT
   end
   CLEAR DOWNLOAD
   return


DOWNLOAD:

   SEND '^M^J^M^JCurrent Path : ' || curdir
   SEND '^M^JFilename: '
   READ CR;filename = result

   if rc ~= 0 then
		do
			SEND '^M^J^M^JYou didn''t enter any filename^M^J^M^J'
      		DELAY SEC 2
			return
		end
   else
   if Exists( curdir || filename ) then
		do
      		SAY  'DL File: 'filename
      		SEND '^M^J^M^JSending file..^M^J^M^J'
      		SENDFILE filename

      		DELAY SEC 3
      		SEND '^M^JComplete.^M^J^M^J'
      		return
   		end
   else
   if Exists( curdir || '/' || filename ) then
		do
      		SAY  'DL File: 'filename
      		SEND '^M^J^M^JSending file..^M^J^M^J'
      		SENDFILE filename

      		DELAY SEC 3
      		SEND '^M^JComplete.^M^J^M^J'
      		return
		end
   else
		do
    		SEND '^M^J^M^JFile not found.^M^J^M^J'
      		DELAY 2 SEC
      		return
		end

CLS:
   SEND d2c(12)
   return
