/* FaxGate V1.0 - Electronic Mail to Fax Gateway
   using GPFax and AmigaUUCP V1.16D. 
   (C) Carlos Amezaga
   UUCP: Postmaster@megalith.miami.fl.us
   Fax:  +1 305 559 3145

   This program is hereby released into the public domain on 3/31/93.
   If you use this program on your system, drop me a note letting me
   know.  I would appreciate any comments you have on FaxGate. 
*/

if ~show('L',"rexxsupport.library") then do
   if addlib('rexxsupport.library',0,-30,0) then do
     say 'Added rexxsupport.library'
   end
   else do
     say 'Support library (LIBS:rexxsupport.library) not available.'
     say 'Aborting...'
     exit(30)
   end
end

if ~exists('t:') then do
  say 'T: has not been assigned, aborting!'
  exit(25)
end

address COMMAND "List Sys:UUMAIL:Fax.queue files quick nohead >T:FaxFiles"

call open(ToFax,'T:FaxFiles',"read")
do while ~eof(ToFax)
fax = readln(ToFax)
fax2 = fax
fax = 'UUMAIL:Fax.queue/'fax
if fax= 'UUMAIL:Fax.queue/' then exit 0

faxdate = date('NORMAL')
from = ''
replyto = ''
subject = ''
maildate = ''
mailerror = ''

/* Let's hit the mail */

call open( outfile, fax, "read")
do while ~eof(outfile)
  in = readln(outfile)
  if (from = '') then
    parse var in 'From: ' from
  if (replyto = '') then
    parse var in 'Reply-To: ' replyto
  if (subject = '') then
    parse var in 'Subject: ' subject
  if (maildate = '') then
    parse var in 'Date: ' maildate
end
call close (outfile)

/* Use 'From:' line if 'Reply-To:' wasn't supplied */

if (replyto = '') then replyto = from


subject = upper(subject)
phone = strip(subject,'L','CALL#')

/* These are the checks that Fax_Send does to any number listed after the
   call# command.  Add or delete them as you like.
*/

if phone = '' then do
	Call Sorry
	mailerror = 'TRUE'
end

if length(phone) > 7 then do
	Call Sorry
	mailerror = 'TRUE'
end

if left(phone,3) = '411' then do
	Call Sorry
	mailerror = 'TRUE'
end

if left(phone,3) = '611' then do
	Call Sorry
	mailerror = 'TRUE'
end

if left(phone,3) = '911' then do
	Call Sorry
	mailerror = 'TRUE'
end

if left(phone,3) = '976' then do
	Call Sorry
	mailerror = 'TRUE'
end

if left(subject,5) ~= 'CALL#' then do
	Phone = 'NOTHING'
	Call Sorry
	mailerror = 'TRUE'
end

if mailerror = 'TRUE' then iterate

options results

logmsg = "echo >>UUSPOOL:LOGFILE `date` "

Shell command logmsg "Starting GPFAX"

if show(ports,REXX_GPFAX) = 0 then do
/*    Shell command "run >nil: <nil: gpfax:gpfax -Q -S -NP -L" */
    Shell command 'run >nil: <nil: lockserial "gpfax:gpfax -Q -S -NP -L"'
	do while show(ports,REXX_GPFAX) = 0
		Shell COMMAND 'Wait 60'
	end
    Shell COMMAND 'Wait 10'
end

address rexx_gpfax

/*
 * Setups
*/

Modemquiet
Reportlog   T 8
Paginate    ON
Resolution  Fine
Scheduler   OFF
Stampbottom ON
NOSYSREQ    ON
COVERPAGES  ON
OPENSERIAL
INITMODEM
'sendfax 'fax ' to 'phone

if RC = 0 then do
	Call Good
end
else do
	Call Bad
end

CLOSESERIAL

QUITFAX

address COMMAND 'delete 'fax
address COMMAND 'delete T:header.#?'
address COMMAND 'delete GpFax:FAX_OUT/'fax2'#?'
end
exit 0

Good:

call open( mailout, "T:fax.result", "write")

call writeln( mailout, 'To: 'replyto )
call writeln( mailout, 'Subject: Re: 'subject )
call writeln( mailout, 'X-FaxSoftware: AmigaUUCP Faxgate V1.0' )
call writeln( mailout, '' )
call writeln( mailout, '     Your E-Mail message dated 'maildate' was' )
call writeln( mailout, 'faxed on 'faxdate' to 'phone'.' )
call writeln( mailout, '' )
call writeln( mailout, '    From all indications, your fax seems to have' )
call writeln( mailout, 'gone through OK.' )
call writeln( mailout, '' )
call writeln( mailout, '--' )
call writeln( mailout, 'Faxgate' )
call close (mailout)

address COMMAND 'sendmail <T:fax.result -f FaxGate -R Fax-Server'
address COMMAND 'delete T:fax.result'
return

Bad:

call open( mailout, "T:fax.result", "write")

call writeln( mailout, 'To: 'replyto )
call writeln( mailout, 'Subject: Re: 'subject )
call writeln( mailout, 'X-FaxSoftware: AmigaUUCP Faxgate V1.0' )
call writeln( mailout, '' )
call writeln( mailout, '     Your E-Mail message dated 'maildate' could' )
call writeln( mailout, 'not be faxed on 'faxdate' to 'phone'.' )
call writeln( mailout, '' )
call writeln( mailout, '     This could be caused by a number of things such' )
call writeln( mailout, 'as the number you entered was busy on all attempts' )
call writeln( mailout, 'to call or the number you entered was incorrect.' )
call writeln( mailout, '' )
call writeln( mailout, '     Try your call again later.  Thank you.' )
call writeln( mailout, '' )
call writeln( mailout, '--' )
call writeln( mailout, 'FaxGate' )
call close(mailout)

address COMMAND 'sendmail <T:fax.result -f FaxGate -R Fax-Server'
address COMMAND 'delete T:fax.result'
return

Sorry:

call open( mailout, "T:fax.result", "write")

call writeln( mailout, 'To: 'replyto )
call writeln( mailout, 'Subject: Re: 'subject )
call writeln( mailout, 'Cc: Postmaster')
call writeln( mailout, 'X-FaxSoftware: AmigaUUCP Faxgate V1.0' )
call writeln( mailout, '' )
call writeln( mailout, '     Your E-Mail message dated 'maildate' was not' )
call writeln( mailout, 'faxed because it contained one of the following:' )
call writeln( mailout, '' )
call writeln( mailout, 'A) The Number you included was greater than seven digits.  Only' )
call writeln( mailout, '   local calls are permitted on this system.' )
call writeln( mailout, '' )
call writeln( mailout, 'B) The command you included in the subject field was incorrectly' )
call writeln( mailout, '   entered.  It must look like this:' )
call writeln( mailout, '   Subject: call#XXXXXXX' )
call writeln( mailout, '' )
call writeln( mailout, 'C) It contained one of these prefixes:' )
call writeln( mailout, '   411,611,911,976' )
call writeln( mailout, '   These prefixes are not allowed on this system' )
call writeln( mailout, '' )
call writeln( mailout, '     Please check the number and try again.' )
call writeln( mailout, '' )
call writeln( mailout, 'You entered 'phone' as the string you wanted dialed.' )
call writeln( mailout, '' )
call writeln( mailout, '--' )
call writeln( mailout, 'FaxGate' )
call close(mailout)

address COMMAND 'delete 'fax
address COMMAND 'sendmail <T:fax.result -f FaxGate -R Fax-Server'
address COMMAND 'delete T:fax.result'
return

