/* $VER: ReturnReceipt.br 1.0 (13.5.97)
**
** Author: Oliver Roberts
** E-Mail: Oliver@poboxes.com
**    WWW: http://www.nanunanu.org/~oliver/
**
** Brief Description:
**
** Processes messages with a "Return-Receipt-To: <address>" header line
** and creates an event to the sender confirming the message has been
** received successfully
**
** Should be called via SortMail
*/

/********************* DO NOT MODIFY BELOW HERE *************************/
Usage = 'Usage: ReturnReceipt.br BBSName Conference MessageNo'
options results

/* Strip "'s from arg string and parse into variables */
parse arg CmdLine
CmdLine = compress(CmdLine,'"')
parse var CmdLine bbs conf msgnum
if bbs = '?' | conf = '' | msgnum = '' then do
	say 'ReturnReceipt.br: ' || Usage
	exit
end

if ~show('p', 'BBSREAD') then do
	address command
		"run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
		"WaitForPort BBSREAD"
end

address(bbsread)

/* Read the data on the message that is to be forwarded */

READBRMESSAGE bbsname '"'bbs'"' confname '"'conf'"' msgnr msgnum headstem HEADTAGS textstem TEXTTAGS  datastem DATATAGS
if(rc ~= 0) then goto quit

toaddress = ''
do n=1 to TEXTTAGS.COMMENT.COUNT
	if (upper(left(TEXTTAGS.COMMENT.n,18)) = upper('Return-Receipt-To:')) then
		toaddress = word(TEXTTAGS.COMMENT.n,2)
end

if (toaddress = '') then exit

/* Get a unique message for the event */

UNIQUEMSGFILE bbsname '"'bbs'"' stem UNIQUEFILE
if(rc ~= 0) then goto quit

/* Create message file */

if(~open(fh, UNIQUEFILE.NAME, W)) then ExitMsg '"Unable to open file:' UNIQUEFILE.NAME

/* Build the message */

TB_WDAYS = 'Monday Tuesday Wednesday Thursday Friday Saturday Sunday'
TB_MONTH = 'January February March April May June July August September October November December'

AMIGA2DATE HEADTAGS.CREATIONDATE stem '"'date'"'

if(date.WDAY == '0') then
	WKDays = 'Sunday'
else
	WKDays = word(TB_WDAYS, date.WDAY)

MODays = word(TB_MONTH, date.MONTH)

call writeln(fh,'Machine-generated automatic response:')
call writeln(fh,'')
call writeln(fh,'Your message to ' || HEADTAGS.TONAME || ' (' || HEADTAGS.TOADDR || ')')
call writeln(fh,'dated' WKDays date.MDAY MODays date.YEAR 'at' right('0' || date.HOUR,2) || ':' || right('0' || date.MIN,2) || ':' || right('0' || date.SEC,2))
call writeln(fh,'with message-id ' || HEADTAGS.MSGID)
call writeln(fh,'and subject "' || HEADTAGS.SUBJECT || '"')
call writeln(fh,'has been received successfully.')
call writeln(fh,'')
call writeln(fh,'Note: This receipt doesn''t imply that the message has been read.')
call writeln(fh,'')
call writeln(fh,'--')
if ~(open(tv,'ENV:THOR/THORVERSION',R)) then do
	call writeln(fh,'THOR - Amiga TCP/IP')
end
else do
	call writeln(fh,readln(tv) || ' - Amiga TCP/IP')
	call close(tv)
end
call close(fh)

/* Post it */

drop EVENT.

EVENT.SUBJECT = 'RECEIPT: ' || HEADTAGS.SUBJECT
EVENT.TOADDR = toaddress
EVENT.CONFERENCE = conf
EVENT.MSGFILE = UNIQUEFILE.FILEPART

WRITEBREVENT bbsname '"'bbs'"' event 0 stem EVENT
if(rc ~= 0) then goto quit

exit

ExitMsg:
    parse arg msgstr
    say ''
    say 'ReturnReceipt.br:' msgstr
    say ''
    exit
