#!/bin/bash
#
#****************************************************************
#* 
#*  This is the System Command Module for iBBS.
#*
#****************************************************************
#TERM = $(echo $TERM)
####################### User configurable variables ####################

BBSNAME="Solarflow BBS"

############################# Static Variables #########################

ETC_PATH=/usr/local/etc
BIN_PATH=/usr/local/bin
INFO_PATH=/usr/local/info

pause() 
{
	echo "Press ENTER to continue..."
	read NULL
}

##################################################################
# The main menu loop						 #
##################################################################

DONE=no
while [ $DONE = no ] 
do

# --------------------------------------------------------------------
# code to display the main menu screen

SELECTION=$(dialog 2>&1 >/dev/null \
--title     "*** SYSCOM - System Command Module ***" \
--backtitle "iBBS v1.0 - A Bulletin Board Program for UNIX" \
--menu      "\
\n\
\n\
Select your choice from the list below: \
\n\
" 20 59 9 \
"Sysop"     "Manage the Sysop Log" \
"Who"       "Who's online now" \
"Chat"      "Chat with a user" \
"Log"       "View the UNIX system log" \
"User"      "User management" \
"Show"      "Logon and user activity" \
"Shutdown"  "System shutdown" \
" "          "" \
"Quit"      "Exit SYSCOM" )

# end of code to display the main menu screen 
#-------------------------------------------------------------
# code for Sysop option

case $SELECTION in 

Sysop)  
	SELECTION=$(dialog 2>&1 >/dev/null \
	--radiolist "How to view the Sysop Log?" 10 60 4 \
	File "The whole file" on \
	10 "Last 10 commands as they accumulate" off \
	Clear "Clear the sysop log" off )

	case $SELECTION in 
	File) 
		dialog \
		--title "Sysop Log" \
		--backtitle "$BBSNAME" \
		--textbox $ETC_PATH/sysop.log 20 78 
		;;
	10) 
		dialog \
		--title "Sysop Log" \
		--backtitle "$BBSNAME" \
		--tailbox $ETC_PATH/sysop.log 20 78
		;;
	Clear)
		rm $ETC_PATH/sysop.log
		touch $ETC_PATH/sysop.log
		chmod 666 $ETC_PATH/sysop.log
		;;
	esac
	;;

# end of code for Sysop option
#----------------------------------------------------------------
# code for Who's on option

Who)  
	dialog \
	--no-collapse \
	--cr-wrap \
	--title "Users currently on:" \
	--backtitle "$BBSNAME" \
	--msgbox "$(who -H -w)" 20 78
	;;

# end of code for Who's on option
# ---------------------------------------------------------------
# code for Chat option

Chat)
	dialog \
	--no-collapse \
	--cr-wrap \
	--title "Users currently on:" \
	--backtitle "$BBSNAME" \
	--msgbox "$(who -H -w)" 20 78

	SELECTION=$(dialog 2>&1 >/dev/null \
	--inputbox "Who would you like to chat with? " 10 40 )

	if who|grep ^$SELECTION >/dev/null
	then
		ytalk $SELECTION
	else
		dialog \
		--title "Chat with user" \
		--backtitle "$BBSNAME" \
		--msgbox "\nUser '$SELECTION' is not logged on" 7 38
	fi
	;;
	
# end of code for the Chat option
#---------------------------------------------------------------
# code for View the UNIX log

Log)
	SELECTION=$(dialog 2>&1 >/dev/null \
	--inputbox "View the last # of lines" 10 30 100 )
    	
	tail -$SELECTION /var/log/messages > tmp
	dialog \
	--title "/var/log/messages" \
	--backtitle "$BBSNAME" \
	--textbox tmp 20 78
	rm tmp
	;;

# end of code for the View UNIX log option 
# ---------------------------------------------------------------	
# code for Users who logged on today

Show)
	SELECTION=$(dialog 2>&1 >/dev/null \
	--radiolist "What would you like to see?" 10 60 4 \
	Last "Users who logged on" on \
	Lastb "Bad logon attempts" off \
	Time "Connect time" off )

	case $SELECTION in
	Lastb)
		lastb > tmp
		dialog \
		--title "Bad logon attempts" \
		--backtitle "$BBSNAME" \
		--textbox tmp 20 78
		rm tmp
	;; 
	Last)
		last > tmp
		dialog \
		--title "Users who logged on" \
		--backtitle "$BBSNAME" \
		--textbox tmp 20 78
		rm tmp
	;;
	
	Time)
		ac -d -p > tmp
		dialog \
		--title "User Connect time" \
		--backtitle "$BBSNAME" \
		--textbox tmp 20 78
		rm tmp
	;;
	esac
;;


# end of code for Users who logged on today
# ------------------------------------------------------------
# code for the User Management option

User)
	SELECTION=$(dialog 2>&1 >/dev/null \
	--backtitle "$BBSNAME" \
	--radiolist "Choose an item from the list" 11 60 4 \
	Lookup "Lookup a user" on \
	Full "View the user file" off \
	Names "View all user names" off \
	Delete "Delete a user" off )
	
	case $SELECTION in
	Lookup)
		SELECTION=$(dialog 2>&1 >/dev/null \
		--inputbox "Which user to look up? " 10 40 )

		LINE=$(grep ^$SELECTION $INFO_PATH/user.list)
		if [ $? = 0 ]
		then
			LOGINNAME=$(echo $LINE | cut -f1 -d";")
			REALNAME=$(echo $LINE | cut -f2 -d";")
			FROM=$(echo $LINE | cut -f3 -d";")
			PROCESSOR=$(echo $LINE | cut -f4 -d";")
			OS=$(echo $LINE | cut -f5 -d";")
			TER=$(echo $LINE | cut -f6 -d";")
			LOOKFOR=$(echo $LINE | cut -f7 -d";")
			HEAR=$(echo $LINE | cut -f8 -d";")
			WHEN=$(echo $LINE | cut -f9 -d";")
			CONNECT=$(tail -1 /home/$SELECTION/user.log | awk '{print $6}')

			dialog \
			--title "User information for $SELECTION" \
			--backtitle "$BBSNAME" \
			--msgbox " \
			User is:			$LOGINNAME\n
			Real name is:			$REALNAME\n
			Calling from:			$FROM\n 
			Processor type:			$PROCESSOR\n
			Operating System:		$OS\n
			Terminal type is:		$TER\n
			What they look for:		$LOOKFOR\n
			Where they heard of this bbs:	$HEAR\n
			User created on:		$WHEN\n
			Last connected from:		$CONNECT" 20 75
		else
			dialog \
			--title "Lookup a User" \
			--backtitle "$BBSNAME" \
			--msgbox "\nUser '$SELECTION' does not exist" 7 35
		fi
		;;
	Full)
		dialog \
		--textbox $INFO_PATH/user.list 25 78
		;;
	Names)
		cat $INFO_PATH/user.list | sed s/-*// | cut -f1 -d";" | sort -r >tmp
		dialog \
		--textbox tmp 25 30
		rm tmp
		;;
esac
;;

# end of code for the User Management option
# ----------------------------------------------------------------
# code for the Shutdown option

Shutdown)
	dialog \
	--defaultno \
	--yesno "Do you really want to shut the system down?" 5 50

	if [ $? = 0 ]
	then
 		DONE=yes
		if [ -f tmp ]
		then
			rm tmp
		fi

		wall "The system is going down NOW!"
		shutdown -r now
	fi
	;;

# end of code for the Shutdown option
# ---------------------------------------------------------------
# code to Quit Syscom

Quit) 
 	DONE=yes
	if [ -f tmp ]
	then
		rm tmp
	fi
	;;

# end of code for Quit option
# ---------------------------------------------------------------

esac
done
