/*
 *            Imp Quick List Reader
 * The Ultimate Accessory to Spot Imp Master
 *
 *         (C)1995 Steve Pearson
 *
 *          This is Tailor-Ware
 *         Edit it as YOU NEED it
 *
 *    $VER : Imp Quick List Reader V1 (10.8.95)
 *
 */

address spot
options results

parse arg opt

upper opt

maxsearch=400
fname=""
lid=""

if (open('LLID','ENV:LASTLIST.IMP','r')) then do
	lid=readln('LLID')
	call close('LLID')
end

select
	when substr(opt,1,3)="GUI" then do
                if subword(opt,2,1)~="" then lid= subword(opt,2,1)
		'requestfile title "Imp Quick List" path MAIL:QUICKLISTS/'lid
		fname=result
	end

	when opt="" then do
		if lid="" then do
			'requestfile title "Imp Quick List" path MAIL:QUICKLISTS/'lid
			fname=result
		end
	end
	otherwise lid=opt

end


if fname="" then do

	address command

	'c:list "mail:quicklists/'lid'" lformat "%p%n" > t:lidlist'
	lsiz=word(statef("t:lidlist"),2);
	if lsiz~="0" then 'c:sort from t:lidlist to pipe:wobble'
	'c:delete t:lidlist > nil:'


	address spot

	if open(temp,"pipe:wobble",'r') then do
		fname=readln(temp)
		tempo=""
		tempo=readln(temp)
		if tempo="" then
			if exists('ENV:LASTLIST.IMP') then do
			address command
			'c:delete "ENV:LASTLIST.IMP" >nil:'
			address spot

		end

		call close(temp)


	end


end


if ~(open('gm',fname,'r')) then do
	if exists('ENV:LASTLIST.IMP') then do
		address command
		'c:delete "ENV:LASTLIST.IMP" >nil:'
		address spot
	end
	exit
end

area=readln('gm')
mnum=readln('gm')
call close('gm')

/* Find Name and Subject from filename */

doname=substr(fname,lastpos('/',fname))

parse var doname "-" from "-" subj

/* Jump into the areas messages view */
/* And jump to the right message */

'messageadvance off'
'messages'
'gotoarea 'area
'getnummsgs'
maxmnum=result
if mnum>maxmnum then mnum=maxmnum
'gotomessage ' mnum



/* Check I'm at the right message! */

'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))

moved=0


if ~((afrom=from) & (asubj=subj)) then do
	'messagelist'
	if rc>0 then moved=maxsearch
	'lastmessage'
	'excludeflag all'
	'include from "'from'#?" noreq nogui'
        sel=result
	if sel=0 then moved=maxsearch
	do until (((afrom=from) & (asubj=subj))) | (moved>=maxsearch)
		'prevmarked'
		if rc>0 then moved=maxsearch
		moved=moved+1
		'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))
	end

	if moved>=maxsearch then do
		'requestnotify TITLE "Imp Quick List" Prompt "Unable To Find Message!!"'
		'gotomessage' mnum
		address command
		'c:delete "'fname'" >nil:'
		address spot
		'messages'
		exit
	end

	'messages'
end

'excludeflag all'


'messageadvance on'


rlid=substr(fname,pos('/',fname)+1,lastpos('/',fname)-pos('/',fname))

call open('LLID','ENV:LASTLIST.IMP','W')
call writeln('LLID',rlid)
call close('LLID')

'getmessageflags'
flags=result

/* I'm not to sure aboutt his next line

According to whati can gather about the bittst command ir should tell me if the mesage
has the unreplied flag set but I'm not too sure it's working!

*/

/* If the message is Unreplied ask if the user wants that flag clearing */

wibble=0

if flags>=65536 then if bittst(x2c(d2x(flags)),16) then wibble=1

if wibble=1 then do
	'requestresponse TITLE "Imp Quick List" Prompt "Remove List Entry?" Gadgets "_Remove|_Forget|_Keep"'
end

if wibble~=1 then do
	'requestresponse TITLE "Imp Quick List" Prompt "Remove List Entry?" Gadgets "_Remove|_Keep"'
end

press=rc
if press~=0 then do
	address command
	'c:delete "'fname'" >nil:'
	address spot
end
if press=2 then do
	'excludeflag all'
	'MARKMESSAGE'
	'toggleunreply'
	'excludeflag all'
end
if press=0 then do
	address command
	nfn=substr(fname,1,lastpos('/',fname))||'_'||substr(fname,pos('-',fname,lastpos('/',fname)))
	'c:rename "'fname'" "'nfn'"'
        address spot
end
