/* MLH - Mailing List Handler */

address spot
options results

signal on Syntax
signal on failure
options failat 20



parse arg area,mlhid

LF='0a'x

addtot=0
remtot=0
infotot=0


ADDT='spot/readprefs'("MAIL:MAILINGLISTS/"||MLHID||".MLP","ADDTEXT")
REMT='spot/readprefs'("MAIL:MAILINGLISTS/"||MLHID||".MLP","REMOVETEXT")
INFOT='spot/readprefs'("MAIL:MAILINGLISTS/"||MLHID||".MLP","INFOTEXT")

yname=getclip('IMP_NAME')

'gotoarea NAME ' Area
if rc=0 then do
	'MessageList'
	'include "'MLHID'" to UNREAD NOGUI NOREQ'

	'getnumselected'
	selected=result

	'firstmessage'

	current=1


	'progressopen TITLE "Imp Mailing List Handler" Prompt "0 Added 0 Removed 0 Info"'
	progdat=result


	/* Move through each selected message and Matrix reply the selected text file to the person =) */

	do while ~(current=selected+1)

	/* Generate the Imp Master Header for the List Reply */

	call open('addrep','t:mlhtemprep','w')
        call writeln('addrep',"                              _ _|"||lf||"                                |  __ `__ \  __ \"||lf||"                                |  |   |   | |   |"||lf||"                              ___|_|  _|  _| .__/"||lf||"                                            _|"||lf||"                           \  |            |"||lf||"                          |\/ |  _` |  __| __| _ \  __|"||lf||"                          |   | (   |\__ \ |   __/ |"||lf||"                         _|  _|\__,_|____/\__\___|_|"||lf||lf)
	CALL WRITELN('addrep',lf||lf||'                              Mailing List Report'||lf||lf)
	call close('addrep')

	'nextmarked'
	'getfrom'
	from=result
	'GetFromAddress'
	FromAddress=RESULT
	'GetSubject'
	mact=upper(result)
	if mact="ADD" then do
		addtot=addtot+1
		address command
		'c:echo "'from' @ 'fromaddress'" >> Mail:mailinglists/'mlhid'.MLIST'
		'c:type 'addt' >> t:mlhtemprep'
		address spot
		'matrixreply TO "'from'" TOADDR "'fromaddress'" FROM "'yname'" SUBJECT "Imp Mailing List Addition" ORIGIN "Imp Master V1.0 (C) Steve Pearson" FILE "t:mlhtemprep" nosig noedit nogui'
	end
	if mact="REMOVE" then do
		remtot=remtot+1
		call open('mlr','mail:mailinglists/'||mlhid||'.MLIST','r')
		call open('mlw','t:TEMPML','w')
		do until eof('mlr')
			mline=strip(readln('mlr'),'B')
			if mline~=strip(from,'B')||' @ '||fromaddress then if strip(mline,'B')~='' then do
				call writeln('mlw',mline);
				call writeln('mlw',readln('mlr'))
			end
			if mline=strip(from,'B')||' @ '||fromaddress then do
				call readln('mlr')
			end
		end

		call close('mlr')
		call close('mlw')
		address command
		'c:copy t:tempml to mail:mailinglists/'mlhid'.MLIST'
		'c:delete t:tempml >nil:'
		'c:type 'remt' >> t:mlhtemprep'
		address spot
		'matrixreply TO "'from'" TOADDR "'fromaddress'" FROM "'yname'" SUBJECT "Imp Mailing List Removal" ORIGIN "Imp Master V1.0 (C) Steve Pearson" FILE "t:mlhtemprep" nosig noedit nogui'
	end
	if mact="INFO" then do
		infotot=infotot+1
		address command
		'c:type 'infot' >> t:mlhtemprep'
		address SPOT
		'matrixreply TO "'from'" TOADDR "'fromaddress'" FROM "'yname'" SUBJECT "Imp Mailing List Info" ORIGIN "Imp Master V1.0 (C) Steve Pearson" FILE "t:mlhtemprep" nosig noedit nogui'
	end

	current=current+1
	'progressupdate REQ "'progdat'" CURRENT "'current'" TOTAL "'selected'" Prompt "'addtot' Added 'remtot' Removed 'Infotot' Info"'
	if rc=5 then current=selected+1
end

'progressclose ' progdat

/* Mark them as read and replied so they don't bother you */
/* The report tells you the files were requested */

'clearflags UNREAD UNREPLIED'
'excludeflag ALL'

reto=addtot||"#"||remtot||"#"||infotot

exit reto

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

