! 3 Oct 88
!
! 01/11/90 Changed voice phone number
! 10/3/88 v6.0E1 xprot
! 5/15/87 v6.0E unix version
!
!	Written by Terry R. Lambert
!
!  bbs.cmd		Full featured bbs for distribution with TERM
!			new version.
!
emulate tty
set abort off
set error off
break off

! Definition of access mask bits
setvar M_regis 1			! User has registered
setvar M_email 2			! User has access to email
setvar M_uload 4			! User can upload
setvar M_dload 8			! User can download
setvar M_ukill 16			! User can kill any message
setvar M_user1 32			! User defineable #1
setvar M_user2 64			! User defineable #2
setvar M_sysop 128			! User is a sysop
setvar M_admin 256			! User is the system administrator

setvar revno	"2.0"			! current revision number
setvar lcase	true			! Yes lowercase is default
setvar nl	true			! Weather or not he needs LF (Y=def)
setvar quiet	false			! Beep when something wrong is typed
setvar syname	"the Century Software BBS"	! System id (change this)
setvar vphone	"(000)000-0000"		! Voice phone number (change this)
setvar lcount	20			! # of lines to display before waiting
setvar trys	4			! the number of trys on critical stuff
setvar access	M_email			! default access mask
setvar passwd	""			! default password (none)
setvar alias	""			! default alias is set to first name
setvar expert	false			! not in expert mode by default
setvar dlimit	78			! default input length limit
setvar llimit	dlimit			! current limit is default
setvar maxbase	20			! maximum message bases supported
setvar mbase	"message/general"	! The default message base

! Find out if he can use lowercase and if he needs linefeeds
! uncomment following lines only if absoultely necessary

! ? "\r\n\r\nCAN YOUR TERMINAL DISPLAY LOWER CASE LETTERS? ";
! do bbsio@yesno lcase
! ? "Do you need linefeeds?"
! ? "(if there is a blank line above this one, say no)"
! do bbsio@yesno nl

! Give the local startup message and ID and take credit
? "\r\n\r\nWelcome to $(syname)"
?

! If we have a voice communications line, tell it
if vphone!=""
	? "Our number for voice communications is $(vphone)"
	?
endif

! Credits --- do not remove, please
? "Century Software TERM Grapevine V$(revno)"
? "System independent Bulletin Board"
? "Author: Terry R. Lambert"
?

do bbsio@paginate "startup"

setvar trycount 0
setvar correct false
repeat							! main login loop
	repeat						! insist on a name
		?
		? "What is your first name? ";
		setvar llimit 14
		do bbsio@echoin fname true true
		?
		? "What is your last name? ";
		setvar llimit 15
		do bbsio@echoin lname true true
		setvar llimit dlimit
		?
		if "$(fname)"="" | "$(lname)"=""
			setvar trycount trycount+1
			? "You must enter both your first and last name"
			if !quiet
				? "\007";
			endif
		endif
	until ("$(fname)"!="" & "$(lname)"!="") | trycount>=trys
	if trycount < trys
		? "$(fname) $(lname)... is this correct? ";
		do bbsio@yesno correct
		?
		setvar trycount trycount+1
	endif
	if trycount>=trys & !correct
		? "Please call the system operator at $(vphone)"
		? "if you are having trouble logging into this system."
		quit
	endif
until correct

? "Hold on a sec while I see if I know you..."
do bbsio@wlock "userlist.lck" inuseby
! just in case the userlist file is locked a long time
if inuseby!=""
	? "This BBS is undergoing maintenance or adding a new user."
	? "Please try again at a later time.  Thank you."
	wait 5
	quit
endif

do bbsio@userlkup "$(fname) $(lname)"
if newuser
	setvar alias left(fname,1)+"."+lname
	? "Hello there $(fname) $(lname)!"
	?
	? "This is your first time on!"
	?
	? "In order to protect your account, you need a password."
	do bbsio@chpasswd false
	?
	? "You may want to set an alias for mail messages... this is"
	? "defaulted to your first initial and your last name."
	do bbsio@alias
	do bbsio@fixuser "$(fname) $(lname)" passwd alias lcount access

* special placement of following line is to avoid locking longer than necessary
	do bbsio@unlock "userlist.lck"
else
* special placement of following line is to avoid locking longer than necessary
	do bbsio@unlock "userlist.lck"

	setvar trycount 0
	repeat
		? "What is your password? (it will not echo) "
		setvar llimit 6
		do bbsio@echoin passts false false
		setvar llimit dlimit
		?
		setvar trycount trycount+1
		if left(passts+"      ",6)!=passwd
		 if trycount>=trys
	  ? "Please call the system operator at $(vphone)"
	  ? "if you are having trouble logging into the system."
		  quit
		 else
		 	? "Invalid password..."
			if !quiet
				? "\007";
			endif
		 endif
		endif
	until left(passts+"      ",6)=passwd
	? "Welcome back, $(fname) $(lname) aka $(alias)"
endif
?
? "You have the following priviledges:"
?
if (access & M_email) > 0
	? " - Use of the electronic mail system"
endif
if (access & M_ukill) > 0
	? " - The ability to kill messages other than your own"
endif
if (access & M_uload) > 0
	? " - The ability to upload files"
endif
if (access & M_dload) > 0
	? " - The ability to download files"
endif
if (access & M_user1) > 0
	? " - User priviledge # 1"
endif
if (access & M_user2) > 0
	? " - User priviledge # 2"
endif
if (access & M_sysop) > 0
	? " - Sysop access"
endif
if (access & M_admin) > 0
	? " - Administrator access"
endif

repeat
if !expert
 ?
 ? "            [ Grapevine $(revno)                       Main menu ]"
 ?
 ? "            <C> Change password        <D> Download files"
 ? "            <G> Goodbye                <L> List files avaialable"
 ? "            <M> Mail subsystem         <P> Private sysop message"
 ? "            <U> Upload files           <X> Expert mode on/off"
 ? "            <?> System statistics"
! Next several lines are special case and must be accounted for in proc chkper
! as they allow for fully hidden commands.  Unless the user has permission, the
! following lines should not be executed.  Chkper should be called prior to the
! function title being displayed, and NONE of the function should be executed
! if auth or whatever boolean used is not true.  Current implimentation beeps,
! although this is easily forced by not allowing the "goodcmd" to be set, it
! may cause a user noticible lag, so best to do it this way if the bbs gets
! large enough for that to happen.
 if (access & (M_sysop|M_admin)) > 0
   ?
   ? "              Sysop functions only:"
   ?
   ? "            <A> Administrative menu    <!> Unix shell"
 endif
endif
?
if (access & (M_sysop|M_admin)) > 0
	? "C,D,G,L,M,P,U,X,?,  A,!: ";
else
	? "C,D,G,L,M,P,U,X,?: ";
endif
repeat
	setvar goodcmd false
	setvar cmd toupper(chrin())
	switch
	case cmd="!"
		if (access & (M_admin)) > 0
			? "Shell"
			? "Type 'exit' to return to the BBS"
			?
			run sh
			do bbsio@hitret
			setvar goodcmd true
		elseif (access & (M_sysop)) > 0
			? "Shell"
			?
			? "This option available to Administrator only"
			?
			do bbsio@hitret
			setvar goodcmd true
		endif
	case cmd="A"
		if (access & (M_sysop|M_admin)) > 0
			? "Administrative menu"
			do bbsadmin
			setvar goodcmd true
		endif
	case cmd="C"
		? "Change password"
		do bbsio@chkper M_regis auth
		if auth

		do bbsio@wlock "userlist.lck" inuseby
		  if inuseby!=""
		    ? "Unable to lock userlist for password change."
		    ? "Please try again at a later time.  Thank you."
		    ?
		    do bbsio@hitret
		  else
		    do bbsio@chpasswd true
		    do bbsio@unlock "userlist.lck"
		    do bbsio@hitret
		  endif
		else
		  ? "You can not change your password at this time"
		  ?
		  do bbsio@hitret
		endif
		setvar goodcmd true
	case cmd="D"
		? "Download files"
		do bbsio@chkper M_dload auth
		if auth
			? "What is the filename? (8 chars + extension) "
			setvar llimit 12
			do bbsio@echoin fname true false
			setvar llimit dlimit
			?
			if fname = ""
				? "Transfer canceled"
			elseif !exists("dnload/"+fname)
				? "File does not exist: Transfer canceled"
			else
				run term -x bbs@dload "$(fname)"
			endif
		else
		  ? "You must send a message to the sysop for download privileges"
		  ?
		  do bbsio@hitret
		endif
		setvar goodcmd true
	case cmd="G"
		? "Goodbye"
		? " ...Are you sure? ";
		do bbsio@yesno correct
		?
		if correct
			? "Goodbye ...Thanks for calling... remember,"
			? "you heard it through the Grapevine."
			quit
		else
			wait 3
		endif
		setvar goodcmd true
	case cmd="L"
		? "List files available"
		do bbsio@chkper M_dload auth
		?
		if auth
			? "Files available for download are:"
			?
			setvar count 0
			for x in (dnload/*) do
				setvar count count+1
				if count>lcount-2
					setvar count 0
					do bbsio@hitret
				endif
				setvar x right(x,len(x)-7)
				setvar x left(x+"               ",16)
				? "$x"
			endfor
			?
			? "End of files."
		else
		  ? "You must send a message to the sysop for download privileges"
		  ?
		endif
		do bbsio@hitret
		setvar goodcmd true
	case cmd="M"
		? "Mail subsystem"
		do bbsio@chkper M_email auth
		if auth
			do bbsmail
		else
		  ? "Your mail privileges have been suspended."
		  ? "Contact the sysop in a prive message to see about re-instating them"
		  ? "...be sure to include your phone number"
		  ?
		  do bbsio@hitret
		endif
		setvar goodcmd true
	case cmd="P"
		? "Private sysop message"
		do bbsmail@send "SYSOP"
		setvar goodcmd true
	case cmd="U"
		? "Upload files"
		do bbsio@chkper M_uload auth
		if auth
			? "What is the filename? (8 chars + extension) "
			setvar llimit 12
			do bbsio@echoin fname true false
			setvar llimit dlimit
			?
			if fname = ""
				? "Transfer canceled"
			elseif exists("upload/"+fname)
				? "File exists: Transfer canceled"
			else
				run term -x bbs@uload "$(fname)"
			endif
		else
		  ? "You must send a message to the sysop for upload privileges"
		  ?
		  do bbsio@hitret
		endif
		setvar goodcmd true
	case cmd="X"
		if !expert
			? "Expert mode on"
			setvar expert true
		else
			? "Expert mode off"
			setvar expert false
		endif
		setvar goodcmd true
	case cmd="?"
		? "System statistics"
		?
		do bbsio@paginate "stats"
		?
		do bbsio@hitret
		setvar goodcmd true
	endswitch
	if !goodcmd & !quiet
		? "\007";
	endif
until goodcmd
until false
quit


! -x mode scripts that are here in lieu of a better place to put them


! Name:		uload
! Parameters:	filename to upload
! Purpose:	-x mode support script procedure for upload of files
proc uload
	do bbs@pickprot
	xmit "Hit return, then begin sending:\r\n"
	repeat
	until comin()="\r"
	xmit "30 seconds to start sending:\r\n"
	htime 30
	iferror rcv upload/$1
		xmit "Upload failed.\r\n"
	else
		xmit "File received.\r\n"
	endif
	quit
endproc uload

! Name:		dload
! Parameters:	filename to download
! Purpose:	s-x mode support script procedure for download of files
proc dload
	do bbs@pickprot
	xmit "Hit return, then begin receiving:\r\n"
	repeat
	until comin()="\r"
	xmit "30 seconds to start receiving:\r\n"
	htime 30
	iferror send dnload/$1
		xmit "Download failed.\r\n"
	else
		xmit "Transfer complete.\r\n"
	endif
	quit
endproc dload

! Name:		pickprot
! Parameters:	none
! Purpose:	set file transfer protocol for up or download
!		(uses "protocol" command to achieve)
proc pickprot
	xmit "Protocol: one of the following:\r\n\r\n"
	xmit " <1> TERM            <2> Old TERM\r\n"
	xmit " <3> Kermit          <4> Xmodem\r\n\r\n"
	setvar gdc false
	repeat
		setvar x comin()
		switch
		case x="1"
			xmit "TERM\r\n"
			xprot termcrc
			setvar gdc true
		case x="2"
			xmit "Old TERM\r\n"
			xprot oldterm
			setvar gdc true
		case x="3"
			xmit "Kermit\r\n"
			xprot kermit
			setvar gdc true
		case x="4"
			xmit "Xmodem\r\n"
			xprot modem7
			setvar gdc true
		default
			xmit "Transfer aborted.\r\n"
			quit
		endswitch	
	until gdc=true
endproc
