/* RRR - Reply Receipt Request */

address spot
options results

signal on Syntax
signal on failure
options failat 20


lf='0a'x

parse arg area

iname = strip(getclip('IMP_NAME'))

'gotoarea NAME ' Area
if rc=0 then do
	'MessageList'

	/* Includes all mail with subjects that fit the ISUB */

	'include "#?'iname'#?" TO UNREAD nogui noreq'

	/* Get info for Report File */

	'Getnumselected'
        total=RESULT

	'progressopen TITLE "Imp Master Receipt Request" Prompt "Getting Message Numbers"'
	progdat=result


	'firstmessage'

	current=1

	do while ~(current=total+1)
		'nextmarked'
		'getsubject'
		subj=result
		'getmessagenum'
	        findRR.current=result
		current=current+1
		'progressupdate REQ "'progdat'" CURRENT "'current'" TOTAL "'total'" PROMPT "Getting Message Numbers"'
		if rc=5 then do
			current=total+1
			'excludeflage all'
		end
	end


	'progressclose 'progdat

	/* Lose them all */

	'excludeflag all'

	current=1

	/* Look at each message in turn */

	'progressopen TITLE "Imp Master Receipt Request" Prompt "0 Replied"'
	progdat=result

        count=0

	do while ~(current=total+1)
		'gotomessage "'findrr.current'"'
		'excludeflag all'
		'markmessage'
		'getmessageflags'
                flags=result
                'getsubject'
		subject=result
		if ((substr(subject,1,3)~="RR:") & (flags>=4096)) then if bittst(x2c(d2x(flags)),12) then do
	                'getdatearrived'
			date=result
			'getfrom'
			from=result
                        'getfromaddress'
			framaddress=result

	                call open(out,'t:OUTTOI','w')

			call writeln(out,"                                    Imp"||lf||"                                   Master"||lf)
			call writeln(out,"                           Receipt Request Reply"||lf||lf)

			call writeln(out,"   "||iname||" Receieved your Message About "||subject||" on "||date)

			call close(out)

			'matrixreply TO "'from'" TOADDR "'fromaddress'" FROM "'iname'" SUBJECT "Message Recieved" FILE "T:OUTTOI" nosig noedit nogui'
			'setflags UNREAD UNREPLIED'
			'edit SUBJECT "RR: 'subject'" noedit nogui noreq'
			'markmessage clear'
			'lastmessage'
			'markmessage'
			'setflags EXPORT KILLSENT'
			'clearflags UNREAD UNREPLIED'
			'markmessage clear'
                        address command
			'c:delete t:outtoi > nil:'
			address spot
			count=count+1
		end

		'markmessage clear'

        	current=current+1

		'progressupdate REQ "'progdat'" CURRENT "'current'" TOTAL "'total'" PROMPT "'count' Replied"'
		if rc=5 then current=total+1
	end
end

'progressclose 'progdat

'excludeflag all'

exit count

syntax:
failure:

signal off failure
signal off syntax

parse source . . . program

program = subword(program,1,words(program)-2)

call 'spot/impmaster/imperror'(program,rc,sigl,sourceline(sigl))

exit

