/*
View Html Mails From Yam 1.35!
HTMail.rexx B1.35
Arexx programmers: please look at the code and criticise!!
Author Emrah Baskaya
This version requires rexxreqtools.library found on aminet
*/

/*program settings (not active now)*/
Voyager = "Work:Internet/Voyager/V"

/*library settings*/
IF ~SHOW(L,'rexxreqtools.library') THEN
CALL ADDLIB('rexxreqtools.library',0,-30,0)
IF ~SHOW(L,'rexxsupport.library') THEN
CALL ADDLIB('rexxsupport.library',0,-30,0)


/*boolean settings*/
l = 0
ff = 0
reachbound = 0
sov = 0
detect = 0
choys = 0

/*variable settings*/
a = -1
fid = 0
m = 0
htmfile = 0

/*constant settings*/
path = "T:"
LF = '0a'x
DA = '0c'x
MU = '0b'x
JB = '0d'x
DE = '1b'x
pg.1 = '/'
pg.2 = '-'
pg.3 = '\'
pg.4 = '|'


IF ~SHOW('P','VOYAGER.1') & ~SHOW('P','VOYAGER') & ~SHOW('P','IBROWSE') & ~SHOW('P','AWEB.1') THEN DO
	CALL RTEZREQUEST('Cannot Locate Browser Port'||LF||,
	'You must run a browser'||LF||,
	'before viewing the mail.'||LF||,
	'Currently supported are:'||LF||,
	'VOYAGER, IBrowse, AWeb.',
	,'Oh, I see..',
	,'Html-EMail',
	,'rt_reqpos = reqpos_centerscr' 'rtez_flags = ezreqf_centertext')
	EXIT
END


OPTIONS RESULTS
ADDRESS YAM
IF ADDRESS() ~= YAM THEN EXIT

getmailinfo file
org = RESULT

CALL OPEN('1win','con:10/20/320/160/Html-EMail/NOCLOSE/NOSIZE','W')


WRITELN('1win','After this window is closed,')
WRITELN('1win','you may need to hit the')
WRITELN('1win','Reload button if new html')
WRITELN('1win','is not shown...')

WRITELN('1win','Processing Mail file:')


/*this mighty nested loops learn the <id>*/
/*   names of possible html components   */
/*it also figures out which decoded part */
/*      of mail will be e mail html      */
IF OPEN('1input',org,'R') THEN DO

	DO WHILE ~EOF('1input')
		parsed = READLN('1input')
		mmt = FIND(upper(parsed),'CONTENT-TYPE:')
		IF mmt > 0 & (reachbound | ~ff) THEN DO
			a = a + 1
			stop = 0
			mime = strip(word(substr(parsed,pos('/',parsed) + 1),1),B,';')
			IF UPPER(mime) = 'HTML' THEN DO
				htmfile = 'Yam.msg.'a
				htmrenamed = 'H!T!M'a||right(boundparser,5,'!')'.html'
				END


			DO FOREVER
m = m + 1
IF m = 5 THEN m = 1
WRITECH('1win',JB pg.m)
				IF EOF('1input') THEN LEAVE 
				findsta = 0
				IF POS('boundary',parsed) > 0 THEN DO
					boundparser = STRIP(STRIP(SUBSTR(parsed,POS('boundary',parsed) + 9),B,';'),B,'"')
					end	

					reachbound = 1

					IF stop THEN LEAVE

					reachbound = 0
				parsed = READLN('1input')
				IF parsed = "" THEN DO FOREVER
					IF EOF('1input') THEN LEAVE 
					parsed = READLN('1input')
					IF POS(boundparser,parsed) > 0 THEN reachbound = 1
					IF parsed ~= "" & ~reachbound THEN findsta = 1
					IF reachbound THEN LEAVE
					END
				IF reachbound & ~findsta & ff THEN a = a - 1
				IF reachbound THEN LEAVE		

				p = FIND(parsed,'Content-ID:')

				IF p > 0 THEN DO
          fid = fid + 1
					stop = 1
					attached.fid = SUBSTR(parsed,p + 13,LASTPOS('>',parsed) - p - 13)
					attnewname = 'H!T!M'a||LEFT(attached.fid,5,'!')'.'mime
					attachdd.fid = 'Yam.msg.'a
					CALL RENAME(path||attachdd.fid,path||attnewname)
					attachdd.fid = attnewname


					END
	 		END
		ff = 1
	END
END

CALL CLOSE('1input')

END

IF htmfile = 0 THEN DO
	CALL RTEZREQUEST('This file doesn''t seem to be'||LF||,
	'an HTML-Email file',
 	,'No kidding!',
	,'Html-EMail',
	,'rt_reqpos = reqpos_centerscr' 'rtez_flags = ezreqf_centertext')
	CALL CLOSE('1win')
	CALL CLOSE('1input')
	dlist = SHOWDIR(path)
	no = WORDS(dlist)
	DO f = 1 TO no
   fname = WORD(dlist,f)
	hfile = 0
   IF LEFT(fname,5) = 'H!T!M' then
	hfile = 1
	IF ~detect & hfile THEN DO
		detect = 1
		choys = RTEZREQUEST('Detected decoded HTML email'||LF||,
		'and its components. You may'||LF||,
		'remove them to gain memory',
	 	,'Remove Them|Leave Them',
		,'Html-EMail',
		,'rt_reqpos = reqpos_centerscr' 'rtez_flags = ezreqf_centertext')
		END
	IF choys & hfile THEN
	DELETE(path||fname)
	END
	EXIT
END

/* this mighty loop redirects the image sources to the appropriate place*/

WRITELN('1win',LF||'Processing Html file:')

IF OPEN('1input',path||htmfile,'R') THEN DO
	CALL OPEN('1output',path||htmrenamed,'W')
	DO FOREVER
		IF EOF('1input') THEN LEAVE
		satir = READLN('1input')
    IF POS('cid:',satir) > 0 THEN
		DO t = 1 TO fid
			y = POS(attached.t,satir)
			IF y > 0 THEN DO
m = m + 1
IF m = 5 THEN m = 1
WRITECH('1win',JB pg.m)
				satir = INSERT('file:///'||path||attachdd.t,DELSTR(satir,y - 4,LENGTH(attached.fid) + 4),y - 5)
				end
		END
		CALL WRITELN('1output',satir)
	END

CALL CLOSE('1output')
CALL CLOSE('1input')

htmfile = htmrenamed

local = '"'||'file:///'||path||htmfile||'"'


IF SHOW('P','VOYAGER') & ~sov THEN DO
	ADDRESS 'VOYAGER'
	show
	openurl local
	WRITELN('1win',LF||'Voyager is loading the page..')
	screentofront
	sov = 1
	END
IF SHOW('P','VOYAGER.1') & ~sov THEN DO
	ADDRESS 'VOYAGER.1'
	show
	openurl local
	WRITELN('1win',LF||'Voyager is loading the page..')
	screentofront
	sov = 1
	END
IF SHOW('P','IBROWSE') & ~sov THEN DO
	ADDRESS 'IBROWSE'
	show
	gotourl local
	WRITELN('1win',LF||'IBrowse is loading the page..')
	sov = 1
	END

IF SHOW('P','AWEB.1') & ~sov THEN DO
	ADDRESS 'AWEB.1'
	open local
	WRITELN('1win',LF||'AWeb is loading the page..')
	sov = 1
	END

END

CALL CLOSE('1win')

