/*
** $VER: ParseRECENT.br 1.2 (6.9.97)
** by Eirik Nicolai Synnes
**
** See SortMail.guide for documentation
**
*/

options results
options failat 31

parse arg arguments

/*
** Initialize some variables
*/

version  = subword(sourceline(2), 4, 1)

template = 'SYSTEM/A,CONFERENCE/A,MSGNO/A/N,CHECKDUPES/S,DONTADD/S,NOSTATS/S'
args.CHECKDUPES = 0; args.DONTADD = 0; args.NOSTATS = 0

globals  = 'newmsg. logcount head. version BBSREAD.LASTERROR myerr returned globals'

curline = 1; res = 0; logcount = 0; returned = 0

/*
** Find/open BBSREAD ARexx port
*/

if ~(show('P', 'BBSREAD')) then do
	address(command)
	'Run >NIL: `GetEnv THOR/THORPath`bin/LoadBBSRead'
	if exists('SYS:RexxC/WaitForPort') then 'SYS:RexxC/WaitForPort BBSREAD'
	else 'WaitForPort BBSREAD'
	if (rc = 5) then do; say 'Could not open BBSREAD''s ARexx port.'; exit(30); end
	if (rc ~= 0) then do; say 'Could not find SYS:Rexxc/WaitForPort.'; exit(30); end
	end

/*
** Give template if arguments = '?'
*/

if (arguments = '?') then do
	say 'Usage: 'template
	say 'ParseRECENT.br is an external script for SortMail.'
	exit(5)
	end

address(bbsread)
'READARGS TEMPLATE "'template'" STEM 'args' CMDLINE 'arguments
if (rc ~= 0) then do
	say BBSREAD.LASTERROR
	say 'Template: 'template
	say 'ParseRECENT.br is an external script for SortMail.'
	exit(5)
	end

/*
** Utilize BBSRead's copyback buffer
*/

'BUFMODE COPYBACK'

/*
** Read message's header and text stems
*/

'READBRMESSAGE "'args.SYSTEM'" "'args.CONFERENCE'" 'args.MSGNO' HEADSTEM 'head' DATASTEM 'data' TEXTSTEM 'text
if (rc ~= 0) then signal error

/*
** Get system data and compensate for a bug in bbsread on OS2.x systems
*/

'GETBBSDATA "'args.SYSTEM'" STEM 'bbsdata
if (rc ~= 0) then signal error

if (right(bbsdata.BBSPATH, 1) ~= ':') & (right(bbsdata.BBSPATH, 1) ~= '/') then bbsdata.BBSPATH = bbsdata.BBSPATH || '/'

/*
** Exit if it doesn't start with "|" (then it's probably not a RECENT msg)
*/

if (left(text.TEXT.curline, 1) ~= '|') then do
	myerr = 'Not an AmiNet RECENT message.'; rc = 5; signal error
	end

/*
** Read exclude file
*/

openexcl = open(ef, bbsdata.BBSPATH || 'SortMail.excl', 'R')
if (openexcl) then do
	cnt = 0
	do until eof(ef)
		entry = readln(ef)
		if (entry ~= '') then do; cnt = cnt + 1; excldir.cnt = entry; end
		end
	excldir.count = cnt
	call close(ef)
	end

/*
** Skip all lines beginning with "|"
*/

do while(left(text.TEXT.curline, 1) = '|'); curline = curline + 1; end

/*
** See if NewFiles.txt is old by checking date. If lastadd is not set
** or was set more than 30 minutes ago, then it is old
*/

lastadd = getclip('SM_LastAdd')

if (lastadd ~= '') & (lastadd >= ((date('I') * 24 * 60) + (time('M') - 30))) then delnf = 0
else delnf = 1

/*
** Exit if there are no new files
*/

if (curline >= text.TEXT.COUNT) then do
	if (delnf) & (exists(bbsdata.BBSPATH || 'Newfiles.txt')) then do
		address(command)
		'Delete "' || bbsdata.BBSPATH || 'Newfiles.txt" QUIET'
		if (rc ~= 0) then do; myerr = 'Could not delete Newfiles.txt.'; rc = 30; signal error; end
		end
	signal cleanup
	end

/*
** Update NewFiles.txt, delete it first if there's a old one there already
*/

if (delnf) & (exists(bbsdata.BBSPATH || 'Newfiles.txt')) then do
	address(command)
	'Delete "'bbsdata.BBSPATH || 'Newfiles.txt" QUIET'
	if (rc ~= 0) then do; myerr = 'Could not update Newfiles.txt.'; rc = 30; signal error; end
	address(bbsread)
	end

if (exists(bbsdata.BBSPATH || 'Newfiles.txt')) then openar = open(ar, bbsdata.BBSPATH || 'Newfiles.txt', 'A')
else openar = open(ar, bbsdata.BBSPATH || 'Newfiles.txt', 'W')

if ~(openar) then do; myerr = 'Could not open Newfiles.txt for writing.'; rc = 30; signal error; end

/*
** Process the RECENT message
*/

do until curline > text.TEXT.COUNT
	aline = text.TEXT.curline

	if (left(aline, 1) = '|') then signal writestats
	if (aline = 'Message of the day:') then signal writemotd

	if (aline ~= '') then do
		delfile = 0; drop found. filetags.

		if (args.DONTADD) then addfile = 0; else addfile = 1

		farea = word(aline, 2)

		if (addfile) & (openexcl) then do i = 1 to excldir.count
			if (index(excldir.i, '/') > 0) & (farea = excldir.i) then addfile = 0
			else if left(farea, length(excldir.i)) = excldir.i then addfile = 0
			end

		if (addfile) then do
			fname = word(aline, 1)
			fdesc = right(aline, length(aline) - 35)

			fsize = right(left(aline, 34), 5)
			if (right(fsize, 1) = 'M') then mega = 1
			else mega = 0
			fsize = compress(fsize, 'KM .')

			if ~(datatype(fsize, 'W')) then fsize = 0
			fsize = fsize * 1024
			if (mega) then fsize = trunc((fsize * 1024) / 10)

			/*
			** Because of a problem in bbsread.library, CHECKDUPES is
			** currently disabled

			if (args.CHECKDUPES) then do
				'SEARCHBRFILE BBSNAME "'args.SYSTEM'" FAREANAME "'farea'" SEARCH "'fname'" NAME STEM 'found
				if (rc = 6) then drop BBSREAD.LASTERROR
				else if (rc ~= 0) then signal error
				else if (result > 0) then do i = 1 to found.FILE.1.COUNT
					'READBRFILE BBSNAME "'args.SYSTEM'" FAREANAME "'farea'" FILENR 'found.FILE.1.i' TAGSSTEM 'filetags
					if (rc ~= 0) then signal error
					else if (filetags.DESCRIPTION.1 ~= fdesc) & (fsize > filetags.SIZE + 1023 | fsize < filetags.SIZE - 1024) then do
						'WRITEBRFILE BBSNAME "'args.SYSTEM'" FAREANAME "'farea'" UPDATEFILENR 'found.FILE.1.1' DELETEFILE'
						if (rc ~= 0) then signal error
						delfile = 1
						end
					if (delfile = 0) then addfile = 0
					end
				end

			**
			*/
			end

		if (addfile) & (fdesc ~= '') then do
			'CONFIGFAREA "'args.SYSTEM'" "'farea'"'
			if (rc ~= 0) then signal error

			drop brfile.
			brfile.NAME = fname
			brfile.SIZE = fsize
			brfile.DATE = head.CREATIONDATE
			brfile.DESCRIPTION.COUNT = 1
			brfile.DESCRIPTION.1 = strip(fdesc)

			'WRITEBRFILE "'args.SYSTEM'" "'farea'" STEM 'brfile
			if (rc ~= 0) then signal error
			end

		if (writeln(ar, aline) = 0) then do; myerr = 'Failed to add data to Newfiles.txt.'; rc = 30; signal error; end
		end
	curline = curline + 1
	end

signal amifini

/*
** Write statistics from AmiNet
*/

writestats:

if (args.NOSTATS) then signal amifini

logcount = logcount + 1; newmsg.text.logcount = '*AmiNet* *Statistics*'
logcount = logcount + 1; newmsg.text.logcount = ''

do i = curline to text.TEXT.COUNT
	aline = text.TEXT.i
	if (aline = 'Message of the day:') then do
		curline = i
		signal writemotd
		end
	logcount = logcount + 1; newmsg.text.logcount = aline
	end
logcount = logcount + 1; newmsg.text.logcount = ''

signal amifini

/*
** Write AmiNet Message of the Day
*/

writemotd:

if (args.NOSTATS) then signal amifini

'AMIGA2DATE SECONDS 'head.CREATIONDATE' STEM 'time
if (rc ~= 0) then signal error

logcount = logcount + 1; newmsg.text.logcount = '*AmiNet* *Message* *of* *the* *Day*: 'time.MDAY'.'time.MONTH'.'time.YEAR
logcount = logcount + 1; newmsg.text.logcount = ''

do i = curline to text.TEXT.COUNT
	aline = text.TEXT.i
	if (left(aline, 1) = '|') then do
		curline = i
		signal writestats
		end
	logcount = logcount + 1; newmsg.text.logcount = aline
	end
logcount = logcount + 1; newmsg.text.logcount = ''

/*
** Clean up and return
*/

amifini:
call close(ar)

if (exists('C:Sort')) then do
	address(command)
	'C:Sort FROM 'bbsdata.BBSPATH'Newfiles.txt TO 'bbsdata.BBSPATH'Newfiles.txt COLSTART 20'
	end

if logcount > 0 then call writemessage(args.SYSTEM, 'Information')

signal cleanup

/*
** Some error detection stuff
*/

error:
syntax:

returned = rc

select
	when symbol('BBSREAD.LASTERROR') = 'VAR' then say 'Line 'sigl' returned 'returned': 'BBSREAD.LASTERROR
	when symbol('myerr') = 'VAR' then say 'Line 'sigl' returned 'returned': 'myerr
	otherwise say 'Line 'sigl' returned 'returned': 'errortext(returned)
	end

break_c:
halt:
cleanup:

/*
** Turn off copyback buffer
*/

address(bbsread)
'BUFMODE ENDCOPYBACK'

if (returned = 0) then do
	lastadd = date('I') * 24 * 60 + time('M')
	call setclip('SM_LastAdd', lastadd)
	exit(0)
	end
else exit(returned)

 /****************************************************************************
************************** Write message to database **************************
 ****************************************************************************/

writemessage: interpret 'procedure expose 'globals
              parse arg system, conference

CDF_NOT_ON_BBS         = '00008000'x  /* This conference is not on the bbs. */

CDNT_INFO              = 4            /* This conference is an information conference */

/*
** See if the conference the msg will be written to exists
*/

address(bbsread)
'GETCONFLIST BBSNAME "'system'" STEM 'conflist
if (rc ~= 0) then signal error

do n = 1 to conflist.COUNT + 1 while upper(conference) ~= upper(conflist.n)
	if (n = conflist.COUNT + 1) then do
		/* Create the new conference */
		'CONFIGCONF "'system'" "'conference'" SET 'c2x(CDF_NOT_ON_BBS)' CONFNETTYPE 'CDNT_INFO
		if (rc ~= 0) then signal error
		conflist.n = toconf
		conflist.COUNT = conflist.COUNT + 1
		end
	end

/*
** If Show own messages isn't activated in conference mark then don't
** mark message as unread.
*/

'GETCONFDATA "'system'" "'conference'" STEM 'confdata
if (rc ~= 0) then signal error

if (confdata.CONFTYPE ~= CDNT_MAILFOLDER) then do
	'CONFIGCONF "'system'" "'conference'" SET 'c2x(CDF_NOT_ON_BBS)' CONFNETTYPE 'CDNT_INFO
	if (rc ~= 0) then signal error		
	end

/*
** Write the message
*/

newmsg.FROMNAME   = head.FROMNAME
newmsg.SUBJECT    = 'Aminet log message'
newmsg.TEXT.COUNT = logcount

'WRITEBRMESSAGE "'system'" "'conference'" STEM 'newmsg
if (rc ~= 0) then signal error

return(0)
