/*
 * Script to send mail to an IXO paging service via ixo.
 *
 * Copyright 1992, Mike W. Meyer
 * All Rights Reserved
 *
 * Modified by Christopher A. Wichura for use with spoolpage.
 *
 * Usage: mailpage [dest] [format]
 *
 * Format specifies what format the page should take. It is a REXX
 * expression that is evaluated to determine the string to send to
 * the pager. If the string is "", then no page is sent. When format
 * is evaluated, the following variables are defined:
 *
 * message.field- the value of all the header line <field>.
 * message.body	- the body of the message, with newlines and tabs
 *		  replaced by spaces.
 * message.	- is "" for any other indices.
 * who		- the user name (stripped of machine and routing info)
 *		  of the sender.
 * what		- the subject, stripped of all initial "Re:"'s
 * text		- the text of the body, with lines starting with ">"
 *		  deleted.
 *
 * Note: By using external functions, this can be a *very* powerfull facility!
 */
debug = 0			/* set to 1 to enable debugging messages */
if ~debug then trace Background

/* Arrange things so signals send us somewhere reasonable */
signal on ioerr
signal on halt
signal on syntax

/* Argument processing */
parse arg dest format
if dest = "" then do
	say "no destination specified"
	exit
	end

if format = "" then format = "who'/'what'/'text"

/* Read in the header */
message. = ""	
field = ""
do until eof(stdin)
	line = translate(readln(stdin), ' ', '09'x)
	/* Save field information for possible future use */
	select
		when line = "" then leave
		when left(line, 1) ~= " " then do
			parse var line field ':' data
			field = upper(field)
			message.field = strip(data)
			end
		when field = "" then do
			if debug then say "Broken header line:" line", ignored"
			end
		otherwise message.field = message.field strip(line)
		end
	end

/* Skip to the body. */
do while line = "" & ~eof(stdin)
	line = readln(stdin)
	end

/* Read in the body */
line = strip(translate(line, ' ', '09'x))
text = ""
message.body = ""
do while ~eof(stdin)
	if line ~= "" then do
		message.body = message.body || line || d2c(10)
		if left(line, 1) ~= '>' then text = text || line || d2c(10)
		end
	line = strip(translate(readln(stdin), ' ', '09'x))
	end
message.body = message.body || line || d2c(10)
if left(line, 1) ~= '>' then text = text || line || d2c(10)
text = strip(text)

/* Set up the magic variables: inverse of text */
message.body = text

/* Who, stripped down to the user name */
who = message.from
parse var who '<' mail '>'
if mail = "" then do
	firstpar = pos('(', who)
	lastpar = lastpos(')', who)
	if firstpar = 0 | lastpar = 0 then mail = who
	else mail = delstr(who, firstpar, lastpar - firstpar + 1)
	end
parse var mail who '@'
parse var who who '%'
bangdex = lastpos('!', who)
if bangdex ~= 0 then who = substr(who, bangdex + 1)
who = strip(who)

/* subject, stripped of Re:'s */
what = message.subject
do while upper(left(what, 3)) = "RE:"
	what = strip(substr(what, 4), 'Leading')
	end

/* Get the date the user wants, in a clean room */
data = builddata(format)
if data = "" then exit 0

/* Run the command, exiting properly if it fails */
options failat 21
rc = 0
if debug then say data
else do
	tempname = 't:mailpage.' || time('s')

	if open(dataout, tempname, 'W') then do
		call writech(dataout, data)
		call close(dataout)

		address command 'pager:bin/spoolpage' dest '<' || tempname
		address command 'delete' tempname 'quiet force'
		end
exit rc

/* Catch errors and exit in that mode */
ioerr:
halt:
syntax:
exit 20

/* create a lexical scope with only the advertized variables visible. */
builddata: procedure expose message. who what text
	interpret "return" arg(1)

