#!/usr/sbin/dip
#
# Connection script for SLIP from east.alma-ata.su to itpm.alma-ata.su
#
# This script calls to two different phones on the same machine
# On first and second pass we calls PHONE1, on third - PHONE2.
# If we connected at some phone, next try to connect starts with
# this phone number.
#
# By Paul Cadach (Alma-Ata)
#
# note, that some of the features used in this script, are
# deemed dangerous security-wise by me (Uri) and are thus
# disabled in the DIP code. In order to enable them, look
# through the source, mainly of "command.c"...

  chatkey	NO\sCARRIER	4
  chatkey	NO\sDIALTONE	5
  chatkey	BUSY		6

# Fetch the IP address of our target host.
main:
  print Initializing networking parameters...
  get $local east
  get $remote itpm
  network

  # Set the desired serial port and speed
  get $phone 0
redial:
  print Initializing serial port and modem...
  port cua0
  if $errlvl != 0 goto badtty
#  speed 9600

  # Reset the modem and terminal line.
#  reset

  # Prepare for dialing.
  init AT&FQ0V1X4B0E0\K5\Q3\J\V3%C1%E1S10=14S7=60
  print Dialing to $remote [ $rmtip ]...
  inc $phone
  if $phone != 4 goto phone_ok
  get $phone 1
phone_ok:
  if $phone == 3 goto phone2
phone1:
  dial PHONE1 90
  goto check_dial
phone2:
  dial PHONE2 60
check_dial:
  if $errlvl == 1 goto login
  if $errlvl == 3 goto dialtimeout
  if $errlvl == 4 goto nocarrier
  if $errlvl == 5 goto nodialtone
  if $errlvl == 6 goto busy
  goto error

# We are connected. Login to the system.
login:
  print Login to the system...
  sleep 1
#  send \r\n\r\n
  wait ogin: 30
  if $errlvl == 3 goto chattimeout
  if $errlvl != 0 goto error
  sleep 1
  send YOUR-LOGIN\n
  wait ord: 10
  if $errlvl == 3 goto chattimeout
  if $errlvl != 0 goto error
  sleep 1
  send YOUR-PASSWORD\n
  wait SLIP 30
  if $errlvl == 3 goto chattimeout
  if $errlvl != 0 goto error

done:
  config pre  routing del east.alma-ata.su
  config pre  interface dummy down
  config up   routing add irbis.alma-ata.su gw itpm.alma-ata.su
  config down routing del irbis.alma-ata.su
  config post interface dummy east.alma-ata.su up
  config post routing add east.alma-ata.su
  print CONNECTED to $remote [ $rmtip ]
  set $mtu 576
  mode SLIP
  sleep 10
  dec $phone
  goto redial

nocarrier:
  print Dialing failed (NO CARRIER), repeating...
  goto redial

nodialtone:
  print Dialing failed (NO DIALTONE), repeating after delay (20 sec)...
  sleep 20
  goto redial

busy:
  print Dialing failed (BUSY), repeating after delay (20 sec)...
  sleep 20
  goto redial

chattimeout:
  print Timeout in chat script, repeating...
  goto redial

dialtimeout:
  print SLIP to $remote [ $rmtip ] failed (TIMEOUT while dialing)
  goto exit

badtty:
  print Cannot open TTY line
  goto exit

error:
  print SLIP to $remote [ $rmtip ] failed
#  reset

exit:

