/* Anti-Spam v1.0 */

parse arg mode
if mode='' then mode = 'CHECK'

mode=upper(mode)

/* Check I have the necessary files available */

if ~open(newshistory,"etc:newshistory",a) then
	if ~open(newshistory,"etc:NewsHistory",w) then
		do
			say('0A'X || 'etc:NewsHistory is not available' || '0A'X)
                	if ~show(ports,'AMITCP') then
				do
					say('AmiTCP not started')
                              		say('Please start it and try again' || '0A'X)
				end
        		exit 20
		end

if ~open(nntp_getcmds,"t:nntp-getcmds",r) then
	do
		say('0A'X || 't:nntp-getcmds is not available' || '0A'X)
		say('Please set t: to where it is, or run NNTPtransfer again' || '0A'X)
                say()
		exit 20
	end

if ~show('L',"rexxsupport.library") then do
	if ~exists("LIBS:rexxsupport.library") then do
			say('rexxsupport.library is not available.')
			exit 20
	end
	addlib('rexxsupport.library',0,-30,0)
end

/* Find out if it's likely that the newshistory file will be unusable */
/* Since a message-id is of the form <sr46teg@a.b.c> the smallest     */
/* possible id is assumed to be <a@a> (although this is unlikely)     */

history_fileattr=statef('etc:NewsHistory')
parse var history_fileattr type " " size " " notused

if size < 5 then
	do
		say('0A'X || 'Newshistory is very small, assuming deleted, or new file' || '0A'X)
                call get_Spam_msgid
		call add_spammer
		exit 0
	end

if size > 80000 then
		say('0A'X || 'Your Newshistory file is a little large, you really ought to trim it!' || '0A'X)

/* Since I couldn't find a better way here is my method of reading   */
/* the last line from a file, better solutions appreciated           */
/* NOTE: no eof checking, since small files should be trapped ^      */

say('Finding last good article' || '0A'X)
offset=-5
do until (readch(newshistory) ='0A'X )
	writech(stdout,'/' || '08'X)
	offset=offset-1
	seek(newshistory,offset,END)
	writech(stdout,'\' || '08'X)
end

last_msgid = readln(newshistory)

say('Got last good article' last_msgid || '0A'X)
say('Searching for Spam article' || '0A'X)

/* eof checking here, as if nntptransfer has been run again, the */
/* last article in the newshistory file _won't_ be in the        */
/* nntp-getcmds file                                             */

do until (msgid=last_msgid | eof(nntp_getcmds))
	writech(stdout,'\' || '08'X)
	linein=readln(nntp_getcmds)
       	parse var linein cmd " " msgid
	writech(stdout,'/' || '08'X)
end

/* we've hit eof - therefore it's the first or non-existent item */
if msgid='' then
do
    seek(nntp_getcmds,0,'BEGIN')
end
call get_Spam_msgid
call add_spammer

close(newshistory)
close(offenders)

say('Now re-run NNTPtransfer')
say('If you have further problems, just rerun this script')

exit 0

/* Handles final processing, ie adding msgid to 		     */
/* etc:newshistory, adding to offenders file, and grabbing of duff   */
/* article if I decide to do it                                      */

add_spammer:

if spammer=' ' | spammer = last_msgid then
	do
		say('Last good article, and last article downloaded are the same, exiting' || '0A'X)
		exit 5
	end

say(spammer 'is the guilty one' || '0A'X)
say('Adding to offenders file')

if open(offenders,'etc:offenders',a) then
	writeln(offenders, spammer 'Added on ' Left(Date(w),3)', 'Date()' 'Time())
	else
	if open(offenders,'etc:offenders',w) then
		writeln(offenders, spammer 'Added on ' Left(Date(w),3)', 'Date()' 'Time())
	else
	do
		say('0A'X || 'Could not open/create offenders file')
		say('Skipping' || '0A'X)
	end

if upper(mode) = 'check' then
do
    say('0A'X || 'Do you wish to add this to your killfile?')
    parse pull reply
end
if (reply='Y' | reply='y' | mode = 'auto') then call add_to_killfile

say ('0A'X || 'Adding to NewsHistory' || '0A'X)
seek(newshistory,0,END)
writeln(newshistory, spammer)
address command 'AmiTCP:bin/nntpart.rx "'spammer'"'
return

/* Pulls the message id of the spam */

get_Spam_msgid:

get_Spam_cmd=readln(nntp_getcmds)
parse var get_Spam_cmd cmd " " spammer
close(nntp_getcmds)

return

/* Strange as it may seem, this adds */
/* msg-id patterns to the killfile   */

add_to_killfile:

parse var spammer unique '@' static
part. =''
do index=0 to 100 until part.index=''
	parse var static part.index '.' static
end
index=index-1

do i=0 to index
	pattern='<#?'
	do j=i to index
		pattern=pattern || '.' || part.j
	end
	say('0A'X || 'Add' pattern 'to nntpkillfile?')
	parse pull reply
        if (reply='Y' | reply='y') then do
		if (index-j)<3 then do
			say('0A'X || 'This pattern may kill a lot of articles, are you really sure?')
			parse pull reply
			if (reply='Y' | reply='y') then do
				if open(killfile,"etc:nntpkillfile",a) then do
					writeln(killfile, pattern)
					close(killfile)
					end
					else
					if open(killfile,"etc:killfile",w) then do
						writeln(killfile, pattern)
						close(killfile)
					end
				else
				say('Could not write to killfile')
        			return
				end
			end
		end
        end
return
