/* V_MBXLAN.CMD - Voice/Fax Answer Script for FaxWorks/PMfax (REXX) */
/* 
                  Keller Group Inc.  September 9, 1996
   You may freely use or modify this script for use with licensed
        products which are developed by Keller Group Inc.
      For new scripts or to share your scripts with others,
                 see http://www.kellergroup.com
*/
/*******************************************************************
V_MBXLAN.CMD - Advanced voice/fax mailbox system - LAN Private Mode

For use with the following Keller software configurations:
  LAN Private Mode (retrieval from private fax logs)

Features:

  Callers can:
     select a LAN user by their "extension"
     hear both a "company" greeting and the extension's "personal" greeting
     send a fax (to an extension, or by default to the server),
     leave a voice message (to an extension, or by default to the server),
     listen to an announcement, or
     enter extension and private password to enter remote retrieval mode

  In the password-protected remote retrieval mode, you can do:
     greeting modification - remotely update your personal greeting message
     password modification - remotely modify your personal access code
     voice retrieval - get voice messages by phone
     fax retrieval - get fax messages, or send them to a fax number
     log retrieval - get a fax of your log (or the last part of it)
     "fax on demand" from log - retrieve voice and fax items from your log


DESCRIPTION:
-----------
This script plays your outgoing message (OGM).  The caller can enter
the "extension" (Route ID) of a LAN user followed by the '#' key to
hear that user's personal outgoing message and then record a message
or send a fax directly to them.  If the caller doesn't enter any
extension or enters '0#' or '#', the caller can record a message to
the "receptionist" (the administrative router of the LAN system).

The caller can press the # or * key to skip the outgoing message and
immediately record their message, otherwise the script will provide a
beep and record the caller's message if no further action is taken.

If the call is from a fax device and your fax hardware detects the
caller's fax (CNG) tones, the script automatically receives the fax.

If the caller enters '2' during playing of the outgoing message, the
script plays the "announcement" and then returns to the outgoing
message again so that the caller can leave a message on the same
call.  If a LAN user's extension is selected, the LAN user's personal
announcement is played, otherwise the system's main announcement is
played.

If the caller enters '9' during or immediately after the playing of
the OGM and then enters the user-configurable access code followed by
the # key, the script enters its "remote retrieval" mode so the
caller can hear received voice messages, have received faxes sent to
them via fax, and other features.  When in this mode, the keys on the
telephone do the following:

   1 - HELP       - Play help message which describes these commands
   2 - SET CODE   - Changes LAN user's private access code (default: 42)
   3 - GREETING   - Enter new outgoing message (change greeting)

   4 - PREV MSG   - Play previous voice message
   5 - REPLAY MSG - Play current voice message (again)
   6 - NEXT MSG   - Play next voice message

   7 - GET FAXES  - Retrieve new fax messages by fax
   8 - GET LOG    - Retrieve fax log by fax
   9 - FOD LOG    - Retrieve voice/fax msgs from log ("fax-on-demand")

   0 - TOP        - Return to top level and play outgoing message again


USAGE INSTRUCTIONS:
------------------
You must be using the program with supported voice/fax hardware to
use this script.  Consult the README.DOC file for tested voice/fax
hardware and recommended configuration settings for your hardware.
Before using this script, first test your system with our built-in
voice answering machine feature by enabling "Voice" and setting the
"Answer script" to * (an asterisk character) on the Voice page of the
Settings notebook.  If you are using supported voice/fax hardware
with the proper configuration settings, you can then use
"Fax/Receive/All calls" mode to answer calls, play the outgoing
message (OGM), and record a voice message (or receive a fax).

Then, to change to this advanced script, specify the full pathname of
this file in the "Answer script" field on the Voice page of the
settings notebook.

NOTE: The script expects to find the LIB_FOD.CMD file and its Wave
audio files in the fax program (EXE) directory.  The script also uses
Wave files from the c:\mmos2\sounds directory for special effects.

SCRIPT VARIABLES:
----------------
The following script variable is used by this script.  Script
variables are set on the Voice page of the Fax Server's Settings
Notebook.

General:

	OGM - Pathname of the main "outgoing message" which is played
	to the caller.  By default, the program sets OGM to be the
	file OGM.WAV in the fax server's program directory.
	Example: OGM = C:\FAX\OGM.WAV
	[LAN users can also place a personal OGM.WAV file in their fax
	data directory or record one using remote access command '3'.]

	CODE - Access code for retrieving messages or changing
	settings from the fax server's log.  By default, the program
	sets CODE to be 42.  You can change this to be any number of up
	to 5 digits in length.
	Example: CODE = 42
	[LAN users can also set a personal access code for their mailbox
	using the '2' command when they call in, and this personal value
	is saved in the ACCESS.COD file in their fax directory.  If no
	personal access code is set, the default value is 42.]

	CALLER - (optional, default "Person") - Name of the person/company
	who will be using the access code to retrieve messages/faxes
	by phone.  This is used for addressing faxes when the caller
	uses the access code for remote retrieval.  
	Example: CALLER = Your Company Inc.

	LOGMAXLINE - (optional, default 56) - Maximum number of log
	entries, from the end of the log, to be sent to the caller
	when they retrieve the log by fax.  If set to 0, then
	the entire log is sent.
	Example: LOGMAXLINE = 120


VOICE PROMPT FILES:
------------------
The script expects to find the following Wave audio files in the fax
program (EXE) directory.  The script also uses Wave files from the
c:\mmos2\sounds directory for special effects.

To record or modify your own voice prompt files, use your OS/2
Multimedia microphone and record voice prompts using the "Fax/New
message" command then save them to a .WAV file with the "Fax/Save
file/Wave" command.  You can also use the OS/2 Digital Audio program
to record, save, edit and modify the Wave files (be sure to use
"Type" of Mono, 8-bit, 11.025 kHz).

	OGM.WAV - The main "outgoing message" as recorded with the
	Utilities/Outgoing message command.

	ENTR_COD.WAV - Short: "Enter code."
	Long: "Please enter your access code and the # key."

	ENTR_CMD.WAV - Short: "Enter command."
	Long: "To play current message press 5, to play next message
	press 6, to play previous message press 4...."

	ENTR_MSG.WAV - Short: "Enter message, then press # key."
	Long: "Please leave your message at the beep.  Press # key to end."

	ANNOUNC.WAV - Short: "No announcement."
	Long: "Our annoucement for this week is...."

	HELP.WAV - "Press 3 to change outgoing message, 4 for previous..."

	NO_MSGS.WAV - Short: "No more messages."

And used by any fax retrieval actions (LIB_FOD.CMD procedure):

	ENTR_DOC.WAV - Short: "Enter document number and # key, or
	press the # key if done."
	Long: "Please enter the document number followed by the # key,
	or press the # key if done.  To receive an index of available
	documents, enter document 1000."

	HOW_FAX.WAV - Short: "Enter 1 if calling from fax machine, 2
	if not."
	Long: "If you are calling from your fax machine and are ready
	to receive the fax press 1, to send the fax to a different
	number press 2." 

	ENTR_FAX.WAV - Short: "Enter fax number and # key."
	Long: "Please enter your fax number followed by the # key,
	including 1 and area code if this is not a local call." 

	NO_FILE.WAV - Short: "File not found."
	Long: "That document was not found, please try again."

	WAIT.WAV - Short: "Please wait."
	Long: "Please wait while we process your request."

	THANKYOU.WAV - Short: "Thank you."

TO MODIFY THE SCRIPT:
--------------------
The script is REXX and uses calls from the Keller REXX API which are
automatically loaded into the OS/2 REXX environment by the retail
versions of Keller's fax products (version 3.0 and later).  See the
Reference Manual for documentation of the FxRx and FxLn calls.  You
may modify the script as desired, and then use it as the "Answer
script" with Keller fax software products.

Note - For voice message retrieval, this script uses the RCVD/N_RCVD
mode of FxRxFind, which means that it will report only those voice
messages which have been received but not already played at the
computer and not previously retrieved via phone.  If desired, you can
change this to use READ/N_READ mode which will report all voice
messages which have not been previously retrieved via phone (even
those that have been played at the computer).  You can also retrieve
any desired voice message, whether or not it has been played or
retrieved, using the "fax log" and "fax-on-demand" features in this
script which allow you to play any voice message by its index number.

Note - For fax message retrieval, this script uses the RCVD/N_RCVD
mode of FxRxFind, which means that it will report only those faxes
which have been received but not already viewed at the computer and
not previously retrieved via phone.  If desired, you can change this
to use READ/N_READ mode which will report all faxes which have not
been previously retrieved via phone (even those that have been viewed
at the computer).  You can also retrieve any desired fax, whether or
not it has been viewed or retrieved, using the "fax log" and
"fax-on-demand" features in this script which allow you to retrieve
any fax by its index number.  In version 3 of the fax program,
viewing a received fax changes its status from Rcvd to Read, but
printing of faxes does NOT change the status, so you can auto-print
all received faxes at the computer and still retrieve them as "Rcvd"
faxes by phone. 

HOW TO PROVIDE FORWARDING, PAGING, ETC. ON A PER-USER BASIS:
-----------------------------------------------------------
Some users may wish to provide actions which are automatically
invoked when a fax or voice message is routed to a LAN user's private
fax directory.  For example, you could have all received faxes
forwarded to a fax number or emailed.  One way of doing this is by
using the "Notify Command" field on the Option/Settings dialog of the
FxRDR program on the fax server.  This command is executed whenever a
fax is routed to a LAN user, so you can write a REXX program which is
passed the UserID and then uses the REXX API commands (including
FxRxSelect to select the LAN user's fax log using the UserID) to take
appropriate actions whenever a fax or message is routed.  The
V_MBX.CMD script shows how to do fax forwarding and email, and the
Notify Command features are documented in the LAN Guide.

TO USE WITH MULTILINE VERSIONS:
------------------------------
This script can be used with multiline versions of Keller fax
software.  All lines are handled identically, so calls can be
received on any of your "receive" lines.  If you prefer to have
different lines be handled differently you can modify the script to
test the LID variable.  For example, to have line 1 immediately jump
to the "please enter your message" prompt, you could add a line like
the following after the "call FxLnInit" line in the script: "if LID =
1 then signal voice" where "voice" is a label in the script.

*******************************************************************/

  call FxLnInit

  VOXDIR = FxRxPath( 'EXE' )	/* get voice prompts from exe dir */
  LIBDIR = FxRxPath( 'EXE' )	/* get REXX subroutines from exe dir */
  FODPROCEDURE = LIBDIR||'lib_fod.cmd'  /* use this external procedure */

  /* default values for script variables */
  if CALLER = 'CALLER' then CALLER = 'Person'
  if LOGMAXLINE = 'LOGMAXLINE' then LOGMAXLINE = 56

  DEFAULTCODE = '42'		/* default access code */

hello:
  USERID = ''			/* if blank, use the fax server's log */
  USERDIR = ''

  /* If using Brooktrout, you may need to use certain lines for fax since */
  /* it has trouble hearing calling fax CNG tones when you are using      */
  /* voice and DTMF commands. For example... */
  /* if LID = 1 then signal fax */

  call FxRxSelect ''
  call FxLnMsg 'OGM'				/* show in status window */
	if result = 'NOTOK' then signal byebye
  call FxLnPlay OGM				/* play greeting prompt */
	if result = 'NOTOK' then signal byebye
	if result = 'FAX' then signal fax
	if result = 'DATA' then signal data

  digits = GetExtension( 5, 10, 5 )		/* get extension or number */
	if digits = 'NOTOK' then signal byebye
	if digits = 'FAX' then signal fax
	if digits = 'DATA' then signal data

  if digits = '' | digits = '0' then signal voice  
  if digits = 2 then signal announce		/* use key value... */
  if digits = 9 then signal accesscode

  /* else they must have entered an extension... */
  call FxRxSelect digits

  if result = 'NOTOK' then signal hello

  /* else it is 'OK', meaning it is a valid LAN user */
  USERID = digits
  USERDIR = FxRxPath( 'LOG' )
  if stream(USERDIR||'OGM.WAV', 'C', 'QUERY EXISTS' ) <> ''
  then call FxLnPlay USERDIR||'OGM.WAV'
  call FxRxSelect ''  

  call FxLnDtmf 1, 3, 'digits'			/* get key, if any... */
	if result = 'NOTOK' then signal byebye
	if result = 'FAX' then signal fax
	if result = 'DATA' then signal data
	if result <> 'DTMF' then signal voice

  if digits = 2 then signal announce		/* use key value... */
  if digits = 3 then signal fax
  if digits = 9 then signal accesscode


voice:					/* take voice message, hangup */
  call FxLnMsg "Record Msg"
  call FxLnTone 1850
	if result = 'NOTOK' then signal byebye
  call FxLnDtmf 	/* flush buffer */
  call FxLnRecord
	if result = 'NOTOK' then signal byebye
	if result = 'FAX' then signal fax
	if result = 'DATA' then signal data
  /* caller can hang up or press a key to end recording */
  call FxLnTone 1850, 1850, 1850
  if USERID <> '' then do
    LREC.!Owner_ID = '#'||USERID	/* route to extension, if any */
  end
  signal out


fax:					/* receive fax and hangup */
  call FxLnReceive
  if USERID <> '' then do
    LREC.!Owner_ID = '#'||USERID	/* route to extension, if any */
  end
  signal out


data:					/* report data call, hangup */
  call FxLnMsg 'Data call, oh well...'
  LREC.!Status = '*Data!'   /* log status of call */
  signal out


announce:				/* play annoucement, restart */
  call FxLnMsg "Announcement"
  call FxRxSelect USERID
  if stream(FxRxPath('LOG')||'ANNOUNC.WAV', 'C', 'QUERY EXISTS' ) <> ''
  then call FxLnPlay FxRxPath('LOG')||'ANNOUNC.WAV'
  else call FxLnPlay VOXDIR||'ANNOUNC.WAV'
  call FxRxSelect ''
  signal hello


accesscode:
  if USERID <> '' then do
    call FxRxSelect USERID	/* work from the private log, if found */
  end
  call FxLnMsg 'Code ???'		/* get & check access code */
  call FxLnPlay VOXDIR||'ENTR_COD.WAV'
	if result = 'NOTOK' then signal byebye
  digits = GetNumber( 6, 10 )

  if USERID = '' then do
    if digits <> CODE then signal evil
  end
  else do
    if digits <> FetchCode() then signal evil
  end

  call FxLnMsg 'Code Mode'
  call FxLnVout 'Valid code entered'
  call FxRxBeep 354, 393, 295, 354, 0, 393

  call FxRxFind 'MSG', 'N_RCVD'		/* get # of new voice msgs */
  if result = 'NOTOK' then do
	call FxLnPlay 'c:\mmos2\sounds\BOING.wav'
	signal byebye
  end
  n = result

  /* if no messages... stay in command mode for other (fax) commands */
  if n = 0 then do
        call FxLnPlay VOXDIR||'NO_MSGS.WAV'
	    if result = 'NOTOK' then signal byebye
  end

  i = n				/* Play 1 beep for each voice message */
  call FxLnMsg 'Code Mode' n 'messages'
  call FxLnVout 'Beep' n 'times'
  do while i > 0
    call FxLnTone 2100
    i = i - 1
  end

  call FxLnVout 'Enter message loop'
  i = 0
  j = 0
  do forever
    call FxLnMsg 'Waiting for next command'
    call FxLnPlay VOXDIR||'ENTR_CMD.WAV'
    call FxLnDtmf 1, 30, 'digits'
    if result <> 'DTMF' then do
      signal goodbye
    end
    else do
      call FxLnMsg 'Code Mode command' digits
      call FxLnVout 'Command' digits 'entered'


      if digits = '1' then do			/* 1 = Play help msg */
        call FxLnVout 'Help message'
        call FxLnPlay VOXDIR||'HELP.WAV'
	  if result = 'NOTOK' then signal byebye
        iterate
      end


      if digits = '2' & USERID <> '' then do	/* 2 = change access code */
        /* Only access codes for LAN users are changed through this. */
        /* For the fax server, use the CODE script variable on Voice page. */
        call FxLnVout 'Change access code for' USERID
        call FxLnMsg 'Code Mode command' digits
        call FxLnPlay VOXDIR||'ENTR_COD.WAV'
          if result = 'NOTOK' then signal byebye
        code1 = GetNumber( 6, 10 )
        call FxLnPlay VOXDIR||'ENTR_COD.WAV'
          if result = 'NOTOK' then signal byebye
        code2 = GetNumber( 6, 10 )
        if code1 = code2 then do
          call SaveCode code1
          call FxLnPlay VOXDIR||'THANKYOU.WAV'
        end
        else do
          call FxLnVout 'New code mismatch:' code1 'vs' code2
          call FxLnPlay 'c:\mmos2\sounds\BOO.wav'
        end
        iterate
      end


      if digits = '3' then do			/* 3 = Enter outgoing msg */
        call FxLnVout 'Enter outgoing message for' USERID
        call FxLnPlay VOXDIR||'ENTR_MSG.WAV'
	  if result = 'NOTOK' then signal byebye
        call FxLnTone 1850
	  if result = 'NOTOK' then signal byebye
        call FxLnDtmf		/* flush buffer */
        if USERID = ''
        then call FxLnRecord OGM
        else call FxLnRecord USERDIR||'OGM.WAV'
	  if result = 'NOTOK' then signal byebye
        call FxLnTone 1850, 1850, 1850
        call FxLnDtmf		/* flush buffer */
        if USERID = ''
        then call FxLnPlay OGM
        else call FxLnPlay USERDIR||'OGM.WAV'
	  if result = 'NOTOK' then signal byebye
        iterate
      end


      if digits = '4' then do			/* 4 = Previous msg */
        call FxLnVout 'Play previous message' i
        if i <= 1
	then call FxLnPlay VOXDIR||'NO_MSGS.WAV'
	else do
	  i = i - 1
          call FxLnMsg 'Code Mode command' digits 'Msg' list.i
  	  call FxLnPlay list.i
  	end
        iterate
      end


      if digits = '5' then do			/* 5 = Replay msg */
        call FxLnVout 'Replay message' i
        if i <= 0
	then digits = '6'	/* replay = next if just starting */
	else do
          call FxLnMsg 'Code Mode command' digits 'Msg' list.i
	  call FxLnPlay list.i
          iterate
	end
      end


      if digits = '6' then do			/* 6 = Next message */
        call FxLnVout 'Play next message' i j
        if i = n 
	then call FxLnPlay VOXDIR||'NO_MSGS.WAV'
	else do
  	  i = i + 1
  	  if i > j then do
	    call FxRxFind 'MSG', 'RCVD', 'LREC'
	    call FxLnVout 'Found' LREC.!ID
	    list.i = FxRxIndexToFax( LREC.!ID )	/* use pathname! */
            /* Need path rather than index since FxRxSelect switch */
            /* applies to FxRx calls, but not FxLn calls. */
	    j = i
	  end
          call FxLnMsg 'Code Mode command' digits 'Msg' list.i
  	  call FxLnPlay list.i
        end
        iterate
      end


      if digits = '7' then do			/* 7 = send rcvd faxes */
        call FxLnVout 'Send rcvd faxes'
        call FxLnMsg 'Code Mode command' digits
        call FxRxFind 'FAX', 'N_RCVD'		/* get # of new faxes */
        if result = 'NOTOK' then do
          call FxLnPlay 'c:\mmos2\sounds\BOING.wav'
          signal byebye
        end
        nfax = result
        if nfax = 0 then do
          call FxLnPlay VOXDIR||'NO_MSGS.WAV'
          if result = 'NOTOK' then signal byebye
        end
        else do
          ifax = nfax		/* Get faxes into list and beep per fax */
          faxlist = ''
          flist = ''
          call FxLnMsg 'Code Mode' nfax 'faxes'
          call FxLnVout 'FaxBeep' nfax 'times'
          do while ifax > 0
            call FxLnTone 2100
            ifax = ifax - 1
            call FxRxFind 'FAX', 'RCVD', 'LREC'
            call FxLnVout 'Found' LREC.!ID
            /* Note use of filenames rather than ID values.  This is done */
            /* because FxLnSend, which could be used in the FOD procedure,*/
            /* cannot be given IDs from an FxRxSelect LAN user directory. */
            faxlist = AddToCsvList( FxRxIndexToFax( LREC.!ID ), faxlist )
            flist = flist LREC.!ID
          end
          /* call our FOD procedure to do the faxing */
          FODPARAMS = "FxRxPath( 'LOG' ), VOXDIR, , CALLER, faxlist," ,
            "'As you requested, your received faxes are attached:' flist," ,
            "'Remote fax retrieval...'"
          interpret 'call' "'"||FODPROCEDURE||"'" FODPARAMS
          if result = 'BYEBYE' then signal BYEBYE
          if result = 'OUT' then signal OUT
        end
        iterate
      end


      if digits = '8' then do			/* 8 = send log */
        call FxLnVout 'Send log'
        call FxLnMsg 'Code Mode command' digits
        call FxLnPlay VOXDIR||'WAIT.WAV'
	  if result = 'NOTOK' then signal byebye
        call SendLog
          if result = 'BYEBYE' then signal BYEBYE
          if result = 'OUT' then signal OUT
        iterate
      end


      if digits = '9' then do			/* 9 = FOD from log */
        call FxLnVout 'FOD from log'
        call FxLnMsg 'Code Mode command' digits
        FODPARAMS = "FxRxPath( 'LOG' ), VOXDIR, , CALLER, , ," ,
               "'FOD Retrieval...'"
        interpret 'call' "'"||FODPROCEDURE||"'" FODPARAMS
          if result = 'BYEBYE' then signal BYEBYE
          if result = 'OUT' then signal OUT
        iterate
      end


      if digits = '0' then do			/* 0 = back to top level */
        call FxLnVout 'Return to HELLO'
	signal hello
      end


      if digits = '*' | digits = '#' then do
        signal goodbye
      end


      /* otherwise, if none were matched... */
      call FxLnVout 'Unknown command' i
      call FxLnPlay 'c:\mmos2\sounds\EEERRUPP.wav'
      iterate

    end
  end


goodbye:
  call FxLnVout 'Exit Code Mode'
  call FxLnPlay VOXDIR||'THANKYOU.WAV'
  call FxLnPlay 'c:\mmos2\sounds\DRUMROLL.wav'
  signal byebye


evil:
  call FxLnVout 'Bad code entered:' digits 'vs' CODE
  call FxLnPlay 'c:\mmos2\sounds\BOO.wav'
  call FxRxBeep 1318, 1396, 1318, 1090, 1318, 1396, 1318, 1090
  signal byebye


byebye:					/* called for error cases */
  LREC.!Status = ''			/* don't log anything */
  signal out


out:					/* clean up and quit */
  call FxLnTerm
  exit


/************************ Procedures ************************/


GetNumber: procedure
  /* Gets DTMF input until # or * key or "digits" are entered. */
  /* TimeOut is seconds to wait for each digit (default 15).       */
  parse arg digits, TimeOut
  number = ''
  if TimeOut = '' then TimeOut = 15  /* default to 15 seconds */
  do digits
    call FxLnDtmf 1, TimeOut, 'newkey'
      if result <> 'DTMF' | newkey = '#' | newkey = '*' then leave
    number = number||newkey
  end
  return number


GetExtension: procedure
  /* Gets DTMF input until # or * key or "digits" are entered. */
  /* TimeOut is seconds to wait for first digit (default 15).   */
  /* NextTime is seconds to wait for subsequent digits (default TimeOut)*/
  /* Returns 'FAX' or 'DATA' or 'NOTOK' results or the number. */
  parse arg digits, TimeOut, NextTime
  number = ''
  if TimeOut = '' then TimeOut = 15  /* default to 15 seconds */
  if NextTime = '' then NextTime = TimeOut
  do digits
    call FxLnDtmf 1, TimeOut, 'newkey'
      if result = 'FAX' | result = 'DATA' | result = 'NOTOK' then return result
      if result <> 'DTMF' | newkey = '#' | newkey = '*' then leave
    number = number||newkey
    TimeOut = NextTime
  end
  return number


TmpFile: procedure
  /* Generate a unique base file name using seconds since midnight */
  return 'TMP'||time( S )


AddToCsvList: procedure
  /* returns a comma-separated value list with item added */
  parse arg item, list
  if list = ''
    then list = item
    else list = list||','||item
  return list


SendLog:	/* sends the log file by fax */
  tmp1 = VOXDIR||TmpFile()||'.TXT'
  tmp2 = VOXDIR||TmpFile()||'.FAX'
  log = FxRxPath( 'LOG' )||'fax.log'

  call LineOut tmp1, 'Fax Log   ' Date('W') ' '||Date('N') ' ' Time('C')
  call LineOut tmp1, ''
  call LineOut tmp1, ,
       'Type ID      Date    Time  Pg Status Elpsd  Name/RemoteID/CallerID'
  call LineOut tmp1, ''

  if LOGMAXLINE <= 0 then do /* send the entire fax log */
    do while Lines( log ) > 0
      record = LineIn( log )		/* read a record from log */
      if FxRxParseLog( record, 'lrec' ) = OK then do
        call LogRpt tmp1
      end
    end
  end
  else do /* send the last x lines from the log */
    head = 1
    tail = 1
    do while Lines( log ) > 0	/* collect last x lines */
      buf.head = LineIn( log )
      head = head + 1
      if head > LOGMAXLINE then head = 1
      if head = tail then tail = tail + 1
      if tail > LOGMAXLINE then tail = 1
    end
    if tail <> head & FxRxParseLog( buf.head, 'lrec' ) = OK then do
      call LogRpt tmp1
    end
    do while tail <> head
      if FxRxParseLog( buf.tail, 'lrec' ) = OK then do
        call LogRpt tmp1
      end
      tail = tail + 1
      if tail > LOGMAXLINE then tail = 1
    end
  end
  rc = LineOut( log )			/* close the files */
  rc = LineOut( tmp1 )
  call FxRxTextToFax tmp1, tmp2		/* convert text to fax */
  /* call our FOD procedure to do the faxing */
  FODPARAMS = "FxRxPath( 'LOG' ), VOXDIR, , CALLER, tmp2," ,
          "'As you requested, the log is attached.', 'Log retrieval...'"
  interpret 'call' "'"||FODPROCEDURE||"'" FODPARAMS
  fodresult = result
  call FxLnVout 'Deleting:' tmp1
  'DEL "'||tmp1||'"'
  call FxLnVout 'Deleting:' tmp2
  'DEL "'||tmp2||'"'
  return fodresult


LogRpt:
  /* Using global lrec., write a report line to a file */
  parse arg Rfile

  if BitTest( lrec.!Flags, 256 ) then mtype = 'MSG'
  else if BitTest( lrec.!Flags, 512 ) then mtype = 'Txt'
  else if BitTest( lrec.!Flags, 1024 ) then mtype = 'Dat'
  else mtype = 'Fax'

  if lrec.!Company = ''
  then namestr = lrec.!Name
  else namestr = lrec.!Name||', '||lrec.!Company

  if lrec.!Pages = 0
  then pagestr = ''    /* don't show 0 pages */
  else pagestr = lrec.!Pages
      
  call LineOut Rfile, mtype left( lrec.!ID, 6 ) ,
         left( lrec.!Date, 9 ) left( lrec.!Time, 5 ) ,
         right( pagestr, 2 ) left( lrec.!Status, 6 ) ,
         right( lrec.!Elapsed, 5) ' '||strip( left( namestr, 46 ) )
  return


BitTest: procedure
  /* Given a number and a bit number, return 1 if bit is set */
  parse arg value, bit
  return (value % bit) // 2

SaveCode:
  /* Save access code to file ACCESS.COD in user's log directory */
  parse arg newcode
  call LineOut USERDIR||'access.cod', newcode, 1
  call LineOut USERDIR||'access.cod'		/* close file */
  return

FetchCode:
  /* Returns the access code for the current USERID */
  newcode = LineIn( USERDIR||'access.cod', 1 )
  call LineOut USERDIR||'access.cod'		/* close file */
  if newcode = '' then newcode = DEFAULTCODE
  return newcode
