/* Announcer version 19931205 */
/* AREXX program to scan AmigaNOS smtp mail and act on it */
/* Announcer.rx is Copyright © 1993 by Dan Roman */

/* Edit the following lines for your system */
/********************************************/

mailfile='uumail:robot.txt'					/* user mail file to use */
modemport='SER:'							/* port modem is connect to */
modemstring='ATDT5551212'					/* dial string for modem */
modemringdelay=300							/* delay before hang up */
beeperstring='ATDT5551234@44**64**0**158##'	/* dial and beeper string for modem */
beeperdelay=1200							/* delay before hang up */

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

if ~show('L',"rexxsupport.library") then do
	call addlib('rexxsupport.library',0,-30,0)
end

signal on BREAK_C

if ~open(mailin,mailfile,'R') then exit 0
if ~open(log,'TCPIP:Announcer.log','A') then call open(log,'TCPIP:Announcer.log','W')

do while ~eof(mailin)
	line=readln(mailin)
	if upper(left(line,6))=='FROM: ' then do
		writech(log,date()||' '||time()||' -- ')
		writeln(log,line)
		from=right(line,length(line)-6)
	end
	if upper(left(line,9))=='SUBJECT: ' then do
		line=right(line,length(line)-9)
		writech(log,'   Command: '||line||' ** Status: ')
		select
			when upper(left(strip(line,'L'),3))=='RUN' then do
				if open(cmdfile,'TCPIP:Announcer.commands','R') then do
					parse var line cmd pgm .
					do while ~eof(cmdfile)
						if upper(pgm)==upper(readln(cmdfile)) then do
							address command line
							writeln(log,'Ok')
							break
						end
					end
				end
				else writeln(log,'Error opening command file.')
			end

			when upper(strip(line))=='PHONEHOME' then do
				if ~open(mdm,modemport,'W') then do
					writeln(log,'Error opening modem port '||modemport)
					end
				else do
					call delay(60)
					writech(mdm,modemstring||d2c(13))
					call delay(modemringdelay)
					writech(mdm,d2c(13))
					call delay(60)
					close(mdm)
					writeln(log,'Ok')
					end
			end

			when upper(strip(line))=='BEEPER' then do
				if ~open(mdm,modemport,'W') then do
					writeln(log,'Error opening modem port '||modemport)
					end
				else do
					call delay(60)
					writech(mdm,beeperstring||d2c(13))
					call delay(beeperdelay)
					writech(mdm,d2c(13))
					call delay(60)
					writech(mdm,'ATH0'||d2c(13))
					call delay(60)
					close(mdm)
					writeln(log,'Ok')
					end
			end

			when upper(strip(line))=='PAGEMESSAGE' then do
				do while ~eof(mailin)&length(strip(line))>0
					line=readln(mailin)
				end
				if open(talker,'SPEAK:','W') then do
					i=0
					do while ~eof(mailin)&upper(line)~='[START]'
						line=readln(mailin)
					end
					do while ~eof(mailin)&upper(line)~='[END]'
						line=readln(mailin)
						writeln(talker,line)
						i=i+1
					end
					writeln(log,i||' lines processed')
					close(talker)
				end
				else writeln(log,'Error opening SPEAK:')
			end

			when upper(left(strip(line,'L'),4))=='PAGE' then do
				if open(talker,'SPEAK:','W') then do
					writeln(log,'Ok')
					if upper(line)=='PAGE' then do
						writeln(talker,'Page from')
						do i=1 to length(from) until substr(from,i+1,1)=='@'
							writech(talker,substr(from,i,1)||' ')
						end
					end
					else writeln(talker,line)
					close(talker)
				end
				else writeln(log,'Error opening SPEAK:')
			end

			otherwise
				writeln(log,'Invalid Command')
		end
	end
end

close(mailin)
call delete(mailfile)
exit 0

BREAK_C:
	say "*** User break"
	exit 10
