#! /bin/ksh

# $Header: /usr/local/src/apc/cmd/Network/stelnet.sh,v 1.35 1994/07/16 06:40:59 peter Exp $

Usage="
Usage: $Cmdname [-r] [-l] [-N] [-p path] <host> [port]
    -r : force raw mode under X.25 (default)
    -l : leave PAD alone, under X.25 (probably leave in cooked state)
    -N : no NSFnet warning (quiet mode)
    -p path : set the gopher path; use only for gopher
   Secure telnet command -- connect to another host over the internet.
   runs gopher if run as sgopher; runs gopher on special port if
   run as snewsfind.
"

# $Log: sftp.sh, v $
# Revision 1.36  1994/07/16  06:40:59 ilya 
# Adjust ftp client call according to Glas's ftp
# client requests.
# Make reasonable for swais, irc and lynx too.

# edited by Pasha for GlasNet

#
# $Log: stelnet.sh,v $
# Revision 1.35  1994/07/16  06:40:59  peter
# make it reasonable for news and ftp too.
#
# Revision 1.34  1994/07/16  06:18:17  peter
# change terminal emulation check to use wording prefered by support.
#
# Revision 1.33  1994/07/15  22:54:39  brian
# we are merging internal & external gophers, so remove settting to port 777.
#
# Revision 1.32  1994/07/06  19:25:18  brian
# ensure any ftp path has a trailing slash.
#
# Revision 1.31  1994/07/05  20:18:39  brian
# add in secure-ftp code.
#
# Revision 1.30  1994/05/26  23:25:51  evs
# Added -p option to specify startup directory path for gopher.
# Fixed -l option parsing which was shifting too many arguments.
#
# Revision 1.29  1994/05/17  07:26:30  steve
# default to setting pad to raw mode with no user query.
#
# Revision 1.28  1994/04/04  18:28:03  steve
# fix silly bug
#
# Revision 1.27  1994/04/04  18:24:31  steve
# fix silly bug
#
# Revision 1.26  1994/04/04  18:14:57  steve
# keep track of raw/canon mode, so can avoid asking redundant question about
# switching modes.
#
# Revision 1.25  1994/03/18  20:50:26  brian
# stupid bug.
#
# Revision 1.24  1994/03/18  19:52:41  brian
# last fix was bogus for when explicit about host but not port.
#
# Revision 1.23  1994/03/16  23:14:51  brian
# last change broke being able to specify host & port on command line. fixed.
#
# Revision 1.22  1994/03/08  00:26:19  steve
# IGC's gopher server at gopher.igc.apc.org
#
# Revision 1.21  1994/01/13  07:49:59  steve
# when invoked as snewsfind, don't show nsfnet warning
#
# Revision 1.20  1994/01/07  19:01:48  steve
# add snewsfind as alias for stelnet.
#
# Revision 1.19  1993/12/14  13:24:49  saff
# added AlterNex title to gopher client
#
# Revision 1.18  1993/12/01  00:33:31  evs
# Fixed a variable assignment.
#
# Revision 1.17  1993/11/10  03:50:08  steve
# sleep after NSFNet warning, to give people time to read it.
#
# Revision 1.16  1993/11/10  03:48:21  steve
# check for emulation with 'tput clear' not 'tput home', as
# some reasonable terminals do not have 'tput clear'
#
# Revision 1.15  1993/11/08  09:33:26  steve
# internal gopher moved to port 777, for security reasons.
#
# Revision 1.14  1993/11/08  08:29:38  steve
# add code to check user has reasonable terminal emulation
#
# Revision 1.13  1993/10/27  22:16:48  brian
# it looks like we want to run gopher without the -s secure flag, but
# still with the -S noshell flag.  this will allow users to save bookmarks
# and open new sessions - not sure why the latter was considered unsecure.
#
# Revision 1.12  1993/10/06  02:04:00  steve
# when running as `sgopher', allow user to specify host and
# port on the command line.
#
# Revision 1.11  1993/10/05  04:41:42  steve
# Move gopher server that we provide to our users to
# an obscure port.  Later on, look at access lists.
#
# Revision 1.10  1993/10/01  04:02:43  brian
# add in a title for gopher client.
#
# Revision 1.9  1993/10/01  00:35:09  brian
# change to be more generic & support secure gopher.
#
# Revision 1.8  1993/09/24  06:01:59  steve
# fix another silly error.
#
# Revision 1.7  1993/09/24  05:58:26  steve
# fix silly error.
#
# Revision 1.6  1993/09/24  05:57:16  steve
# add options to force raw connection, rather than asking.
#
# Revision 1.5  1993/07/07  23:00:12  steve
# add command line option to disable NSFNet authorisation
# .,
#
# Revision 1.4  1993/03/31  06:46:08  steve
# ask user if they want raw mode.
#
# Revision 1.3  1993/02/10  00:55:33  steve
# Don't use 8 bit mode -- messes up cr-lf translations.
#
# Revision 1.2  1993/02/09  05:57:58  steve
# fix syntax error
#
# Revision 1.1  1993/02/09  05:39:11  steve
# Initial revision
#
#

# standard APC prolog (see ksh_debug.sh for using debugging features)

. /etc/apc/prolog.sh

[ -z "$uiLoadedF" ] && { . uilib.sh; }   # to get NSF net checker...


[ -z "$x25rawF" ] && { export x25rawF=1 ; }
leavePad=
host=
port=

# set raw
setRaw () {
  [ -n "$NUA" ] && {
   stty -echo
   raw
  }
}
# set cononical(cooked)
setCon () {
  [ -n "$NUA" ] && {
    stty echo
    cooked
 }
}

checkRaw() {
[ -z "$NUA" -o -n "$x25rawF" ] && { return 0; }

echo "Some sites that you may wish to connect with will operate in 
full-screen mode.  It is necessary to make a special configuration
if you will be connecting to such a site.
"
while :
  do echo "Will you be needing full-screen capability (y/n/q) ? \c"
  read ans case
  case "$ans" in
    y*|Y*) x25rawF=1; return 0
	   ;;
    n*|N*) return 0
           ;;
    q*|Q*) return 1
           ;;
    *) echo "Unknown response; try again..."
       continue
       ;;
   esac
done
}

# force user to have terminal emulation...
doEmulation () {
while :
 do x=`tput clear 2>/dev/null >/dev/null`
    [ $? -eq 0 ] && { return 0; }  # got a good terminal type
echo "Your current terminal type, '$TERM', is not compatible with
the $myName. You may choose to use the (s)etup utility from the main
command line to change your terminal type, or you may change it right now.
If you change your terminal type, make the change in your communications
software as well.
 
Would you like to change your terminal type right now (y/n/q)? \c"

	  read ans foo
    	  case "$ans" in
      	    y*|Y*) t=`setup2 TERM`
	        [ $? -ne 0 ] && { return 1; }
   	    	[ -n "$t" ] && {
	  	TERM=$t
		continue
		}
	   return 1
	   ;;
	    n*|N*|q*|Q*) echo "OK.  Please contact 'support' if you need further assistance."
      	    return 1
	    ;;
          esac
 done
}


set -f					# 
set -- `getopt rNlp: "$@"` ||		# really, no options; these are for e.g
   error "$Usage"			# 
set +f
for arg in "$@"
    do	case $arg in
	   -r)
		x25rawF=1 ; shift;;
	   -N)
		noNSF=1 ; shift;;
	   -l)
		leavePad=1 ; shift;;
	   -p)
	   	case $Cmdname in
		  *gopher* ) gopherPath=" -p $2" ; shift 2;;
		         * ) error "$Usage" ;;
		esac ;;
	   --)
		shift ; break;;
	esac
done

[ $# -eq 1 ] && { host=$1; }
[ $# -eq 2 ] && { host=$1; port=$2; }

case $Cmdname in
  *telnet* ) myName=telnet
  	     RealCmdname="/usr/ucb/telnet -s" ;;
  *gopher* ) 
	     x25rawF=1
	     myName="(i)nternet menu"
	     # this should really be in a global variable
	     case $HOSTNODE in
		cdp ) title="-t \"Institute for Global Communications\"" 
		      if [ -z "$host" ]
		      then host=gopher.igc.apc.org 
		           port=70
		      else [ -z "$port" ] && { port=70; }
		      fi;;
		ax  ) title="-t \"AlterNex\"";;
		glas ) title="-t \"GlasNet\"";;
	     esac
	     if [ -z "$host" ] 
                then host=me; port=777;
		else [ -z "$port" ] && { port=70; }
	     fi
	     RealCmdname="/usr/local/bin/gopher -S "$title$gopherPath
	     emulationF=1  # need emulation, such as vt100..
	     ;;
  *news* ) 
  	     noNSF=1
	     x25rawF=1
	     myName="(n)ewsfinder menu"
	     port=778
	     # this should really be in a global variable
	     case $HOSTNODE in
		cdp ) title="-t \"News Finder\"" 
		      [ -z "$host" ] && host=gopher.igc.apc.org;;
	     esac
	     if [ -z "$host" ] 
                then host=me; 
	     fi
	     RealCmdname="/usr/local/bin/gopher -S "$title
	     emulationF=1  # need emulation, such as vt100..
	     ;;
#  *ftp* ) 
#	     x25rawF=1
#	     myName="ftp command"
#	     # this should really be in a global variable
#	     [ -z "$host" ] &&
#		error Must specify the site you want to FTP to
#	     [ -n "$port" ] &&
#		case $port in
#		  */ ) ;;
#		  * ) port="$port/" ;;
#		esac
#	     gopherPath=" -p ftp:$host@/$port"
#	     host=me
#	     port=70
#	     case $HOSTNODE in
#		cdp ) title="-t \"Institute for Global Communications\"" 
#		      host=gopher.igc.apc.org ;;
#		ax  ) title="-t \"AlterNex\"";;
#		glas ) title="-t \"GlasNet\"";;
#	     esac
#	     RealCmdname="/usr/local/bin/gopher -S "$title$gopherPath
#	     emulationF=1  # need emulation, such as vt100..
#	     ;;
   *ftp* )  
#Usage: ftp
#        -v  verbose mode
#        -t  trace mode
#        -d  debug mode
#        -i  interactive mode
#        -n  autologin mode
#        -g  globing mode
#        -r  remote terminal mode
#        -l# user's degree level [0..3]
 
             cd /tmp || { echo Can\'t cd to the spool directory
                          exit 1
             }
             Log=~accting/ftp.log
             myName="FTP client"
             RealCmdname="/usr/local/bin/ftp -r"
             ;;
   *swais* ) 
#Usage: swais
#  [-s sourcname]          select sourcename for search
#  [-S sourcedir]          defaults to ~/wais-sources
#  [-C common_sourcedir]   defaults to /usr/lib/wais-sources
#  [-R]                    set 'remote terminal' mode
#  [-t translation_table]  select table for chararacter translation
#  [-h]                    this help message
#  [keywords]
#
# List of currently supported tables:
#               CP866           - Alternative Russian
#               CP1251          - Russian Windows
#               KOI8            - KOI-8
#               ASCII           - IBM PC character set
#               DEC_MCS         - DEC Multinational
#               LATIN_1         - ISO Latin 1
#               and some other... (see $APCLIB/swais/translation/*)

             if [ 0$ALT_CODE_F -eq 01 ]
              then
               APC_CHAR_SET="cp866"
              else
               APC_CHAR_SET=koi8
             fi
             WAISCOMMONSOURCEDIR=/f/wais-sources
             export WAISCOMMONSOURCEDIR
             Log=~accting/swais.log
             myName="SWAIS client"
             RealCmdname="/usr/local/bin/swais -R -t $APC_CHAR_SET 2> /dev/null"
             ;;
   *irc* )   
#Usage: irc [switches] [nickname] [server list]
#  The [nickname] can be at most 9 characters long
#  The [server list] is a whitespace separate list of server name
#  The [switches] may be any or all of the following
#   -c <channel> joins <channel> o startup
#   -p <port>    default server connection port (usually 6667)
#   -f           your terminal uses flow controls (^S/^Q), so IRCII shouldn't
#   -F           your terminal doesn't use flow control (default)
#   -s           don't use separate server processes (ircserv)
#   -S           use separate server processes (ircserv)
#   -d           runs IRCII in "dumb" terminal mode
#   -a           adds default servers and command line servers to server list
#   -r=          set up a restrictions (-r= for more help)
#   -R           assumes that IRCII starts from a remote terminal without an
#                access to the local filesystem

             IRCDCCFILES=/tmp
             export IRCDCCFILES
             Log=~accting/irc.log
             myname="IRC client"
             RealCmdname="/usr/local/bin/irc -R -r=cd,exec,server"
             ;;
   *lynx* )
#   USAGE: lynx -char_set=[number]
#   List of currently supported character sets:
#       [0]     ISO Latin 1
#       [1]     DEC Multinational
#       [2]     IBM PC character set
#       [3]     7 bit approximations
#       [4]     KOI-8
#       [5]     Russian Windows
#       [6]     Alternative Russian

             if [ $ALT_CODE_F -eq 01 ]
              then
               APC_CHAR_SET=6
              else
               APC_CHAR_SET=4
             fi

             Log=~accting/www.log
             myname="WWW client"
             RealCmdname="/usr/local/bin/lynx -noprint -restrictions=shell,editor,bookmark,exec,exec_frozen,news_post,print -char_set=$APC_CHAR_SET -term=$TERM $*"
             ;;	
esac


[ -z "$noNSF" ] && { checkNSFNET || exit 0; }
sleep 1

[ -n "$emulationF" ] && { doEmulation || exit 0; }

# used to say:
#   [ -z "$x25rawF$leavePad" ] && { checkRaw || exit 0; }  
#  now we always assume raw...

[ -n "$x25rawF" -a -z "$leavePad" ] && { setRaw; }

trap 'echo "`date +%y%m%d%H%M` $LOGNAME closed" >>$Log' EXIT INT QUIT TERM HUP TSTP
echo "`date +%y%m%d%H%M` $LOGNAME opened" >>$Log

eval $RealCmdname $host $port
 
[ -n "$x25rawF" -a -z "$leavePad" ]  && { setCon; }

