/* WORKSRV.CMD                                                              */
/* Ez a program létesíti a kapcsolatot az otthonról hívó géppel;            */ 
/* elvégzi a beléptetést (login) és a "route-olást".                        */
/* Másold be ezt a fájlt a \TCPIP\BIN vagy az \MPTN\BIN könyvtárba a        */
/* munkahelyi gépen.                                                        */
/* Ezt a programot a WORKSRV.CFG file hívja meg.                            */

/* Konfiguráció: Állítsd be az alábbi REXX változókat:                      */

/* Soros port konfigurációja és a modem parancsok                           */
comport   = 'COM2'           /* a modem által használt soros port           */
comparms  = '57600,n,8,1'    /* a soros port inicializációs paraméterei     */
initcmd   = 'ATZ'	     /* a modemet reszetelô parancs                 */
anscmd	  = 'ATS0=1'	     /* automatikus hívásválaszolás                 */
connectstring='CONNECT'      /* a modem által kapcsolódáskor kiadott szó    */

/* Internet címek, számítógép nevek                                         */
ipaddress  = '130.161.41.99'	 /* A munkahelyi PC IP címe (sl0 interfész) */
ipdest	   = '130.161.41.18'	 /* Az otthoni PC IP címe (sl0 interfész)   */
netmask    = '255.255.255.0'

/* Az alábbi paramétereket csak akkor kell megadni, ha a munkahelyi gép     */
/* az Interneten van.                                                       */
desthostname = 'home'      /* Az otthoni PC neve. A DNS-nek ismernie kell!  */
macaddress   = '00:c0:55:66:77:88'/* A munkahelyi PC MAC (adapter) címe.    */

/* A felhasználó adatai.                                                    */
username = 'xxxx'	    /* a login név a munkahelyi gépen               */
password = 'yyyy'	    /* a jelszó a munkahelyi gépen                  */

/* Ha a munkahelyi gép nincs az Interneten, akkor cseréld ki az             */
/* Ethercard='T' sort Ethercard='F'-re!                                     */
Ethercard = 'T'             /* a munkahelyi gép az Interneten van           */

/******  Az alábbi REXX programrészleten elvileg NEM kell módosítani! *******/

/* Define routines for errorhandling */
signal on error name errorhandler
signal on failure name errorhandler
signal on halt name errorhandler

/* signal on novalue name errorhandler */
signal on syntax name errorhandler
signal on notready name errorhandler

/* Load RexxUtility functions */
call RxFuncAdd 'SysSleep', 'RexxUtil', 'SysSleep'

parse arg interface

/*--------------------------------------------------------------------------*/
/*                   Initialization and Main Script Code                    */
/*--------------------------------------------------------------------------*/

/* Set some definitions for easier COM strings */
cr='0d'x
crlf='0d0a'x
timeout = 0

say ''
say ' OS/2 Slip Szerver (interface 'interface')'

/* soros port inicializálás:     mode com2 57600,n,8,1,buffer=on            */
'mode 'comport comparms

/* Do this until SLIP command was sent                                      */
do until connect

   /* Flush any stuff left over from previous COM activity                  */
   call flush_receive

   /* modem inicializálás                                                   */
   call lineout , 'Reszetelem a modemet...'
   call send initcmd || cr
   call waitfor 'OK', 5 ; call flush_receive 'echo'
     if RC = 1 then do
       call lineout , 'A modem nem reszetelôdik... Ujra próbálom...'
       call send '+++'
       exit 1
     end

   /* beállítja a modemet automatikus válaszolásra                         */
   call lineout , 'Beállítom az automatikus válaszolást...'
   call send anscmd || cr
   call waitfor 'OK', 40 ; call waitfor crlf , 2
    if RC = 1 then do
       call lineout , 'A modem nem válaszol... Ujra próbálom... '
       exit 2
     end

   /* Flush anything else */
   call flush_receive 'echo'

   /* Vár a hívásra.                                                        */
   say 'Várom a hívást: ' date() ', ' time()'.'
   do while waitfor(connectstring,1)\=0
      call SysSleep 3
   end
   call waitfor crlf , 2
   say 'Login folyamat kezdete: ' date() ', ' time()'.'

   call waitfor crlf , 2

   /* Send login */
   call send 'OS/2 Slip Szerver '||cr
   do 2
      call send crlf
   end

/* Kéri a User nevet.                                                       */
   call send 'User:'
   call waitfor username, 30, 1
   if rc\=1 then call waitfor cr, 30
   if rc=1 then do
      say 'Sikertelen kísérlet: ' date() ', ' time()'.'
      call SysSleep 1
      call send '+++'
      call SysSleep 2
      call send 'ATH'||cr
      connect = 0
   end

/* Ha a User OK, kéri a jelszót.                                            */
   call send crlf
   call send 'Password:'
   call waitfor password, 30, 1

   if rc\=1 then call waitfor cr, 30

   if rc=1 then do
      say 'Sikertelen kísérlet: ' date() ', ' time()'.'
      call SysSleep 1
      call send '+++'
      call SysSleep 2
      call send 'ATH'||cr
      connect = 0
   end
   else do
      say 'Sikeres kapcsolat: ' date() ', ' time()'.'
      connect = 1
   end

   call flush_receive 'echo'
end

/* Now configure this machine for the appropriate address,                  */
'ifconfig sl0 'ipaddress ipdest' netmask 'netmask
'route add host 'ipdest ipaddress' 1'

/* If Work machine is on Ethernet, proxy arp handles packets going to       */
/* Home machine if Ethercard='T' then                                       */
   'arp -s 'desthostname macaddress 'pub'

/* Tell client what to do                                                   */
call send crlf
call send 'SLIP mód.'||crlf
call send 'Az IP címed: 'ipdest'.  MTU méret 1500 bytes'||crlf
call send "VJ tömörítés bekapcsolva."||crlf

call flush_receive 'echo'

/* Wait to avoid sending of garbage while client is in command mode         */
call SysSleep 5

exit 0

/*--------------------------------------------------------------------------*/

/* Errorhandler */
errorhandler:
say copies('=',79)
say 'HIBA T™RTÉNT:'
say '   Condition:' condition('c')
say '   Instruction:' condition('i')
say '   Description:' condition('d')
say '   Status:' condition('s')
say '   Line:' sigl
say copies('=',79)
return 0

/*--------------------------------------------------------------------------*/
/*                            send ( sendstring)                            */
/*..........................................................................*/
/*                                                                          */
/* Routine to send a character string off to the modem.                     */
/*                                                                          */
/*--------------------------------------------------------------------------*/

send:

   parse arg sendstring
   call slip_com_output interface , sendstring

   return

/*--------------------------------------------------------------------------*/
/*                    waitfor ( waitstring , [timeout] )                    */
/*..........................................................................*/
/*                                                                          */
/* Waits for the supplied string to show up in the COM input.  All input    */
/* from the time this function is called until the string shows up in the   */
/* input is accumulated in the "waitfor_buffer" variable.                   */
/*                                                                          */
/* If timeout is specified, it says how long to wait if data stops showing  */
/* up on the COM port (in seconds).                                       */
/*                                                                          */
/*--------------------------------------------------------------------------*/

waitfor:

   parse arg waitstring , timeout, doecho .                /* Changed by MW */

   if doecho\=1 then doecho=0                              /* Changed by MW */

   if timeout = '' then
     timeout = 5000    /* L O N G   delay if not specified */
   waitfor_buffer = '' ; done = -1; curpos = 1
   ORI_TIME=TIME('R')                                      /* Changed by MW */

   if (remain_buffer = 'REMAIN_BUFFER') then do
      remain_buffer = ''
   end

   do while (done = -1)
      if (remain_buffer \= '') then do
         line = remain_buffer
         remain_buffer = ''
       end
       else do
         line = slip_com_input(interface,,10)
         if doecho=1 then call send line                   /* Changed by MW */
      end
      waitfor_buffer = waitfor_buffer || line
      index = pos(waitstring,waitfor_buffer)
      if (index > 0) then do
         remain_buffer = substr(waitfor_buffer,index+length(waitstring))
         waitfor_buffer = delstr(waitfor_buffer,index+length(waitstring))
         done = 0
      end
      call charout , substr(waitfor_buffer,curpos)
      curpos = length(waitfor_buffer)+1
      if ((done \= 0) & (TIME('E')>timeout)) then do
/*        call lineout , ' WAITFOR: timed out ' */         /* Changed by MW */
        done = 1
       end
   end
/*   timeout=0 */                                          /* Changed by MW */
   RC=done
   drop timeout doecho                                     /* Changed by MW */
 return RC


/*--------------------------------------------------------------------------*/
/*                               readpass ()                                */
/*..........................................................................*/
/*                                                                          */
/* Routine used to read a password from the user without echoing the        */
/* password to the screen.                                                  */
/*                                                                          */
/*--------------------------------------------------------------------------*/

readpass:

  answer = ''
  do until key = cr
    key = slip_getch()
    if key \= cr then do
      answer = answer || key
    end
  end
  say ''
  return answer


/*--------------------------------------------------------------------------*/
/*                             flush_receive ()                             */
/*..........................................................................*/
/*                                                                          */
/* Routine to flush any pending characters to be read from the COM port.    */
/* Reads everything it can until nothing new shows up for 100ms, at which   */
/* point it returns.                                                        */
/*                                                                          */
/* The optional echo argument, if 1, says to echo flushed information.      */
/*                                                                          */
/*--------------------------------------------------------------------------*/

flush_receive:

   parse arg echo

   /* If echoing the flush - take care of waitfor remaining buffer */
   if (echo \= '') & (length(remain_buffer) > 0) then do
      call charout , remain_buffer
      remain_buffer = ''
   end

   /* Eat anything left in the modem or COM buffers */
   /* Stop when nothing new appears for 100ms.      */

   do until line = ''
     line = slip_com_input(interface,,100)
     if echo \= '' then
        call charout , line
   end

   return