Andrew Bedno    Andrew@Bedno.com • 773-213-4578
 History   Kudos   Samples 

Shell
1994 - Ongoing  (30 Years 3 Months)
Shell

Bulletin Board System

Comprehensive and easy to use bulletin board program for guest dial ins to a Unix system.
Also download my library of Shell utility routines.

# bbs.sh - Andrew Bedno - 1994.05.01
# Bourne Shell BBS menu system.
# Provides file transfer and mail service to guest logins.

title()
{
  echo ''
  echo '**********  '$*'  **********'
}

datetime()
{
  echo `date '+%y.%m.%d %H:%M:%S'`' \c'
}

BASEDIR='/u/guest'
FADIR='public'
PROTOCOL="K"
PRONAME="Kermit"
FALOG=$BASEDIR'/guest.log'
CHOICE=' '
FUNC=_none
DEV=`who am i | awk '{ print $2 }'`
echo `datetime`'Login: dev '$DEV >>$FALOG
XRESULT=0

while [ $FUNC != _exit ]
  do

    title "MAIN MENU"
    if [ ! -d $BASEDIR/$FADIR ]
      then
        CURRDIR=''
      else
        cd $BASEDIR/$FADIR >/dev/null 2>&1
        CURRDIR=`pwd`
      fi
    if [ "$CURRDIR" != "$BASEDIR/$FADIR" ]
      then
        echo ''
        echo 'Directory access error!'
        echo 'Contact system administrator.'
        echo 'Logging off.'
        echo `datetime`'Directory access error.' >$FALOG
        sleep 5
        exit
      fi
    if [ "$XRESULT" -ne 0 ]
      then
        echo ''
        echo 'WARNING! Most recent file transfer may have failed!'
        echo $PRONAME' reported error '$XRESULT''
      fi
    echo ''
    echo 'Operations available to guest user:'
    echo ''
    echo '  [N] News from system administrator.'
    echo '  [F] File areas.   (Current area: '$FADIR')'
    echo '  [L] List files.'
    echo '  [P] Select file transfer protocol.  (Currently '$PRONAME')'
    echo '  [D] Download a file.'
    echo '  [U] Upload a file.'
    echo '  [S] Send mail.'
    echo '  [X] Exit (logoff).'
    echo ''
    echo -n 'Enter letter for desired operation: '
    read CHOICE
    CHOICE=`echo $CHOICE | tr "[a-z]" "[A-Z]"`
    FUNC=_none
    case $CHOICE in
      "F") FUNC=_filearea ;;
      "L") FUNC=_listfiles ;;
      "P") FUNC=_protocol ;;
      "D") FUNC=_download ;;
      "U") FUNC=_upload ;;
      "S") FUNC=_sendmail ;;
      "X") FUNC=_logoff ;;
      "N") FUNC=_news ;;
      * ) FUNC=_redraw ;;
    esac

    if [ $FUNC = _logoff ]
      then
        title LOGOFF
        echo ''
        FUNC=_exit
      fi

    if [ $FUNC = _news ]
      then
        ANYNEWS=FALSE
        title NEWS
        if [ -s $BASEDIR/news -a -s $BASEDIR/$FADIR/.news ]
          then
            echo ''
            echo 'Which news do you wish to read:'
            echo ''
            echo '  [1] General news for all guests.'
            echo '  [2] News specific to this file area.'
            echo '  [0] Return to main menu.'
            echo ''
            echo -n 'Enter a number to select: '
            read WHATNEWS
          else
            WHATNEWS="X"
            if [ -s $BASEDIR/news ]
              then
                WHATNEWS=1
              fi
            if [ -s $BASEDIR/$FADIR/.news ]
              then
                WHATNEWS=2
              fi
            if [ "$WHATNEWS" = "X" ]
              then
                echo ''
                echo 'There is no news.'
                echo ''
                echo -n 'Press <ENTER> for main menu:'
                read CONT
                echo ''
                echo `datetime`'No news.' >>$FALOG
              fi
          fi
        if [ "$WHATNEWS" = "1" ]
          then
            title "GENERAL NEWS"
            echo ''
            more $BASEDIR/news
            echo ''
            echo -n 'Press <ENTER> for main menu:'
            read CONT
            echo ''
            echo `datetime`'General news.' >>$FALOG
          fi
        if [ "$WHATNEWS" = "2" ]
          then
            title "FILE AREA NEWS"
            echo ''
            more $BASEDIR/$FADIR/.news
            echo ''
            echo -n 'Press <ENTER> for main menu:'
            read CONT
            echo ''
            echo `datetime`'Area news.' >>$FALOG
          fi
        if [ "$WHATNEWS" = "0" ]
          then
            echo `datetime`'News not viewed.' >>$FALOG
          fi
      fi

    if [ $FUNC = _protocol ]
      then
        title "FILE TRANSFER PROTOCOL"
        echo ''
        echo 'This system supports four common file transfer protocols.'
        echo ''
        echo '  [X] Xmodem'
        echo '       Fast, poor error checking, no file info.'
        echo '  [Y] Ymodem.'
        echo '       Fast, better error checking, good file info.'
        echo '  [Z] Zmodem.'
        echo '       Fast, good error recovery, full file info.'
        echo '  [K] Kermit.'
        echo '       Slower, best features, supported on large systems.'
        echo '  [0] Return to main menu.'
        echo ''
        echo -n 'Enter a letter to select: '
        read PROIN
        PROIN=`echo $PROIN | tr "[a-z]" "[A-Z]"`
        if [ "$PROIN" != "0" ]
          then
            PROSAVE=$PROTOCOL
            if [ "$PROIN" = "X" ]
              then
                PROTOCOL="X"
                PRONAME="Xmodem"
              fi
            if [ "$PROIN" = "Y" ]
              then
                PROTOCOL="Y"
                PRONAME="Ymodem"
              fi
            if [ "$PROIN" = "Z" ]
              then
                PROTOCOL="Z"
                PRONAME="Zmodem"
              fi
            if [ "$PROIN" = "K" ]
              then
                PROTOCOL="K"
                PRONAME="Kermit"
              fi
            if [ "$PROSAVE" != "$PROTOCOL" ]
              then
                echo `datetime`'New protocol selected: '$PRONAME >>$FALOG
              fi
          fi
      fi
            
    if [ $FUNC = _filearea ]
      then
        title "FILE AREA SELECTION"
        echo ''
        echo 'This system has a public area containing files'
        echo 'accessible to all users.  There are also several'
        echo 'special interest areas available to authorized'
        echo 'users.  To access the public area, enter a blank'
        echo 'name when prompted for a file area login.'
        echo 'To enter any other area, enter the name given'
        echo 'to you by the system administrator for access'
        echo 'to the desired area.'
        echo ''
        echo 'Enter file area login: \c'
        read FALOGIN
        if [ "$FALOGIN" = "" ]
          then
            echo ''
            echo 'Returning to public file area.'
            echo `datetime`'Return to public file area.' >>$FALOG
            FADIR='public'
          else
            grep '^'$FALOGIN',' $BASEDIR/passwd >/dev/null
            if [ $? -ne 0 ]
              then
                echo ''
                echo 'There is no such file area login on record.'
                echo 'You have been returned to the public area.'
                FADIR='public'
                echo `datetime`'File area login failed: '$FALOGIN >>$FALOG
              else
                echo ''
                echo 'Enter password for area: \c'
                stty -echo
                read FAPW
                stty echo
                echo ''
                if [ "$FAPW" = "" ]
                  then
                    echo ''
                    echo 'File area password cancelled.'
                    echo `datetime`'File area password cancelled.' >>$FALOG
                    echo ''
                    echo 'Returning to public file area.'
                    FADIR='public'
                  else
                    echo ''
                    grep '^'$FALOGIN','$FAPW',' /u/guest/passwd >/dev/null
                    if [ $? -ne 0 ]
                      then
                        echo ''
                        echo 'Password is incorrect for login.'
                        echo 'You have been returned to the public area.'
                        FADIR='public'
                        echo `datetime`'File area password failed: ' \
                             $FALOGIN', '$FAPW >$FALOG
                      else
                        FADIR=`grep '^'$FALOGIN','$FAPW',' /u/guest/passwd | \
                               cut -d',' -f3`
                        echo `datetime`'File area login OK: '$FALOGIN >>$FALOG
                        echo ''
                        echo 'Login to "'$FALOGIN'" area accepted.'
                      fi
                  fi
              fi
          fi
      fi

    if [ $FUNC = _listfiles ]
      then
        title "LIST FILES"
        LFILES=`ls | grep -v '^\.' | wc -l`
        LFILES=`echo $LFILES`
        if [ "$LFILES" -eq 0 ]
          then
            echo ''
            echo 'There are no files in this area.'
            sleep 4
          else
            echo ''
            echo `datetime`'List files.' >>$FALOG
            echo ' Submitter    Size Date         Name'
            echo '----------------------------------------------'
            ls -l | grep -v ' \.' | grep -v '^total' | \
              cut -c15-23,32-255 | more
            echo ''
            echo -n 'Press <ENTER> for main menu:'
            read CONT
            echo ''
          fi
      fi

    if [ $FUNC = _download ]
      then
        title "FILE DOWNLOAD"
        XRESULT=0
        echo ''
        echo 'Note that file names on this system ARE case sensitive!'
        echo ''
        echo 'Enter name of file to download: \c'
        read FNAME
        echo ''
        if [ "$FNAME" != "" ]
          then
            echo
            FNAME=`basename $FNAME`
            if [ -s $FNAME ]
              then
                echo 'Commencing '$PRONAME' send . . .'
                echo 'Activate '$PRONAME' file download on your computer now!'
                echo 'Press  Ctrl-C  a few times to terminate if necessary.'
                echo 'If Ctrl-C does not terminate, just be patient.'
                echo ''
                sleep 15
                echo `datetime`'Downloading: '$FNAME >>$FALOG
                if [ "$PROTOCOL" = "X" ]
                  then
                    sx -q $FNAME 2>/dev/null
                    XRESULT=$?
                  fi
                if [ "$PROTOCOL" = "Y" ]
                  then
                    sb -q $FNAME 2>/dev/null
                    XRESULT=$?
                  fi
                if [ "$PROTOCOL" = "Z" ]
                  then
                    sz -q $FNAME 2>/dev/null
                    XRESULT=$?
                  fi
                if [ "$PROTOCOL" = "K" ]
                  then
                    kermit -q -i -s $FNAME 2>/dev/null
                    XRESULT=$?
                  fi
                echo ''
                echo 'File transfer result code: '$XRESULT
                echo `datetime`'Download result: '$XRESULT >>$FALOG
              else
                echo ''
                echo 'Cannot transfer file "'$FNAME'"'
                echo 'File either does not exist, is zero size,'
                echo 'or is not accessible by guest user.'
                echo `datetime`'Empty or not found: '$FNAME >>$FALOG
              fi
          fi
      fi

    if [ $FUNC = _upload ]
      then
        title "FILE UPLOAD"
        XRESULT=0
        if [ "$PROTOCOL" = "X" -o "$PROTOCOL" = "K" ]
          then
            echo ''
            echo 'Do not include path ("/", "\" or "." characters)'
            echo 'or drive ("A:", "B:" or "C:") specifications'
            echo 'in the file name.  Such information pertains only'
            echo 'to your computer, not to this receiving host.'
            echo ''
            echo 'Enter name of file to upload: \c'
            read FNAME
          else
            FNAME=""
          fi
        if [ "$FNAME" != "" -o "$PROTOCOL" = "Y" -o "$PROTOCOL" = "Z" ]
          then
            UPOK=TRUE
            if [ "$PROTOCOL" = "X" -o "$PROTOCOL" = "K" ]
              then
                # For protocols requiring names, check for existing file.
                FNAME=`basename $FNAME`
                if [ -s $FNAME ]
                  then
                    echo ''
                    echo 'File "'$FNAME'" already exists!'
                    echo 'Please enter a different name.'
                    echo 'This system does not permit overwriting'
                    echo 'previously uploaded files.'
                    echo `datetime`'Exists: '$FNAME >>$FALOG
                    UPOK=FALSE
                  fi
              fi
            if [ "$PROTOCOL" = "Y" -o "$PROTOCOL" = "Z" ]
              then
                # For name carrying protocols, load to temp dir.
                cd $BASEDIR/tmp
                rm -f * >/dev/null 2>&1
              fi
            if [ "$UPOK" = "TRUE" ]
              then
                echo `datetime`'Uploading: '$FNAME >>$FALOG
                echo ''
                echo 'Commencing '$PRONAME' receive . . .'
                echo 'Activate '$PRONAME' file upload on your computer now!'
                if [ "$PROTOCOL" = "Y" -o "$PROTOCOL" = "Z" ]
                  then
                    echo ''
                    echo 'NOTE! Do not include path or drive information'
                    echo 'when specifying, on your system, the name of'
                    echo 'the file to send.  This may require that the'
                    echo 'file be located in the present working'
                    echo 'directory on your system.  Transfers of'
                    echo 'files whose names include "/", "\", or "."'
                    echo 'characters, or a drive specification such as'
                    echo '"A:", "B:" or "C:" will fail.'
                    echo ''
                    sleep 2
                  fi
                echo 'Press  Ctrl-C  a few times to terminate if necessary.'
                echo 'If Ctrl-C does not terminate, just be patient.'
                echo ''
                sleep 15
                if [ "$PROTOCOL" = "X" ]
                  then
                    rx $FNAME 2>/dev/null
                    XRESULT=$?
                    if [ ! -s "$FNAME" ]
                      then
                        # Remove file and set error code if zero bytes.
                        rm -f $FNAME >/dev/null 2>&1
                        echo ''
                        echo 'File "'$FNAME'" is empty'
                        echo 'and has been removed.'
                        sleep 4 
                        echo `datetime`'Empty: '$FNAME >>$FALOG
                        if [ "$XRESULT" -eq 0 ]
                          then
                            XRESULT=2
                          fi
                      fi
                  fi
                if [ "$PROTOCOL" = "Y" ]
                  then
                    rb 2>/dev/null
                    XRESULT=$?
                  fi
                if [ "$PROTOCOL" = "Z" ]
                  then
                    rz 2>/dev/null
                    XRESULT=$?
                  fi
                if [ "$PROTOCOL" = "K" ]
                  then
                    kermit -q -i -k >$FNAME 2>/dev/null
                    XRESULT=$?
                    if [ ! -s "$FNAME" ]
                      then
                        # Remove file and set error code if zero bytes.
                        rm -f $FNAME >/dev/null 2>&1
                        echo ''
                        echo 'File "'$FNAME'" is empty'
                        echo 'and has been removed.'
                        sleep 4 
                        echo `datetime`'Empty: '$FNAME >>$FALOG
                        if [ "$XRESULT" -eq 0 ]
                          then
                            XRESULT=2
                          fi
                      fi
                  fi
                if [ "$PROTOCOL" = "Y" -o "$PROTOCOL" = "Z" ]
                  then
                    cd $BASEDIR/tmp
                    # Check newly uploaded named files.
                    XFILES=`ls | wc -l`
                    XFILES=`echo $XFILES`
                    if [ "$XFILES" -eq 0 ]
                      then
                        if [ "$XRESULT" -eq 0 ]
                          then
                            XRESULT=2
                          fi
                      else
                        for XFNAME in *
                          do
                            if [ -s $BASEDIR/$FADIR/$XFNAME ]
                              then
                                echo ''
                                echo 'File "'$XFNAME'" already exists!'
                                echo 'Please rename this file on your'
                                echo 'system and send it again.'
                                echo 'This system does not'
                                echo 'permit the overwrite of'
                                echo 'previously uploaded files.'
                                echo `datetime`'Exists: '$XFNAME >>$FALOG
                                if [ "$XRESULT" -eq 0 ]
                                  then
                                    XRESULT=13
                                  fi
                              else
                                if [ ! -s "$XFNAME" ]
                                  then
                                    # Don't copy if zero bytes.
                                    echo ''
                                    echo 'File "'$XFNAME'" is empty'
                                    echo 'and has been removed.'
                                    sleep 4 
                                    echo `datetime`'Empty: '$XFNAME >>$FALOG
                                    if [ "$XRESULT" -eq 0 ]
                                      then
                                        XRESULT=2
                                      fi
                                  else
                                    cp $XFNAME $BASEDIR/$FADIR/$XFNAME
                                  fi
                              fi
                          done
                      fi
                    rm -f * >/dev/null 2>&1
                  fi
                echo ''
                echo 'File transfer result code: '$XRESULT
                echo `datetime`'Upload result: '$XRESULT >>$FALOG
              fi
          fi
      fi

    if [ $FUNC = _sendmail ]
      then
        title "SEND MAIL"
        echo ''
        echo 'Users available to mail: '
        cat $BASEDIR/users.mail | grep -v '^#' | \
          sort | pr -7 -o2 -w70 -l23 -t
        echo ''
        echo 'Enter addressee name: \c'
        read NAME 
        if [ "$NAME" != "" ]
          then
            grep '^'$NAME'$' $BASEDIR/users.mail >/dev/null
            if [ $? -ne 0 ]
              then
                echo ''
                echo 'User "'$NAME'" does not exist.'
              else
                echo `datetime`'Sending mail: '$NAME >>$FALOG
                echo ''
                echo 'Press  Ctrl-D  to finish creating mail.'
                echo ''
                mail $NAME
              fi
          fi
      fi

  done

title "DONE"
echo ''
sleep 4
echo `datetime`'Logoff.' >>$FALOG


 DOWNLOAD         < NEWER    OLDER >