/* Find New Mail */

address spot

options results

parse arg area

/* Use clip list to prevent needing to search more than once, also stops opening files again and again */

mnum=getclip('IMP_NEW_'||area)

if mnum='' then do

	maxsearch=400

	'gotoarea'area
	'messagelist'

	'getnumselected'

	isel=result

	/* Prevents Searching Mail already Checked By using Pointers to last message checked */
	/* This Part opens the Area.IMP file or creates it. */

	if ~open(SF,'Mail:areas/'area'.IMP','r') then do
		'firstmessage'
		'getmessagenum'
		mnum=result
		'getfrom'
		from=strip(substr(compress(result,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
		'getto'
		to=strip(substr(compress(result,"-!:&%^$/\[]*#?()"||'27'x||'22'x),1,13))
		'getsubject'
		subj=result
		if upper(substr(subj,1,3))="RE:" Then subj=strip(substr(subj,4),'B')
		subj=strip(substr(compress(subj,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
		address command
		'c:echo "'mnum'*n'from'*n'to'*n'subj'" > "mail:areas/'area'.IMP"'
		address spot
		call open(SF,'Mail:areas/'area'.IMP','r')
	end

	/* Read Data from Area.Imp */

	MNum=readln(SF)
	From=strip(readln(SF))
	To=strip(Readln(SF))
	Subj=strip(Readln(SF))
	call close(SF)

	/* Then Update it with the Last Message */

	if ~exists('mail:areas/'area'.NIMP') then do
		'lastmessage'
		'getmessagenum'
		nmnum=result
		'getfrom'
		nfrom=strip(substr(compress(result,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
		'getto'
		nto=strip(substr(compress(result,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
		'getsubject'
		nsubj=result
		if upper(substr(nsubj,1,3))="RE:" Then nsubj=strip(substr(subj,4),'B')
		nsubj=strip(substr(compress(nsubj,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
		address command
		'c:echo "'nmnum'*n'nfrom'*n'nto'*n'nsubj'" > "mail:areas/'area'.NIMP"'
		address spot
	end

	/* And Move to the correct Message With A Search routine */

	'getnummsgs'
	maxmnum=result
	if mnum>maxmnum then mnum=maxmnum
	'gotomessage ' mnum

		/* Check I'm at the right place */

	'getfrom'
	afrom=strip(substr(compress(result,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
	'getsubject'
	asubj=result
	if upper(substr(asubj,1,3))="RE:" then asubj=strip(substr(asubj,4),'B')
	asubj=strip(substr(compress(asubj,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
	'getto'
	Ato=strip(substr(compress(result,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))

	moved=0
	firsto=0


	if ~((afrom=from) & (asubj=subj) & (ato=to)) then do
		'gotomessage'mnum
		if isel=0 then do
			'excludeflag all'
			'include from "'from'#?" noreq nogui'
		        sel=result
			if sel=0 then moved=maxsearch
		end
		do until (((asubj=subj) & (ato=to)) | (moved>=maxsearch))
			if isel=0 then 'prevmarked'
			if isel>0 then 'prevmessage'
			if rc>0 then moved=maxsearch
			moved=moved+1
			'getsubject'
			asubj=result
			if upper(substr(asubj,1,3))="RE:" then asubj=strip(substr(asubj,4),'B')
			asubj=strip(substr(compress(asubj,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
			'getto'
			Ato=strip(substr(compress(result,"-!:&%^$[]/\*#?()"||'27'x||'22'x),1,13))
		end
		if moved>=maxsearch then do
			'firstmessage'
	                firsto=1
		end
	end
	if isel=0 then 'excludeflag all'

	'getmessagenum'
	mnum=result

	if firsto=1 then mnum=1

	call setclip('IMP_NEW_'||area,mnum)

end

exit mnum
