/* NNTPtransfer.rexx v1.0 */

/* Set up default values */

NNTPSERVER='news'
GROUPS='UULIB:NewsGroups'
HISTORY='AmiTCP:db/NewsHistory'
STAMP='AmiTCP:db/NewsStamp'
KILL='AmiTCP:db/nntpKillFile'
BLIP=1
PANIC=200000
BATCH=16
TRACE=3
RETRIES=50
BLIPMSG='"*e[M *e[3D%5ld articles received *e[1A"'
NEWACTIVE='UULib:NewNewsGroups'
UNBATCH='NOUNBATCH'

parse arg arguments

do while arguments ~=''
	parse var arguments arg arguments
	flag=upper(arg)
	call HandleArgs(flag)
end

if ~exists(Groups) then
do
	say 'You must set up a '||Groups||' file, or give the name'
	say 'of another file containing the news groups you want'
	exit 10
end

options failat 21
address command 'amitcp:bin/connectstate'
connectstate=GetVar('CONNECTSTATE')
if connectstate ~= 'ONLINE' then
do
    say 'NNTPTransfer: Not connected.'
    exit 30
end

address command 'amitcp:bin/checkhost 'NNTPSERVER
if RC=5 then
do
    say 'NNTPTransfer: 'NNTPSERVER' unknown host or down'
    exit 30
end

nntptransfer=GetVar('NNTPTRANSFER')
if nntptransfer=0 | nntptransfer='' then
do
    error=SetVar('NNTPTRANSFER','1','GLOBAL')
end
else if nntptransfer=1 then
do
    say 'NNTPTransfer already running ?'
    say 'if not unset/unsetenv NNTPTRANSFER'
    exit 30
end
say 'Pulling News...'

nntpcmds='T:NNTPcmds'
nntpopts='COMPLEX BATCH 'batch' BLIP 'blip 'BLIPMSG 'blipmsg' HISTORYFILE 'history' STAMPFILE' stamp 'TRACE' trace 'KILLFILE' kill

call namefiles

options failat 21
done=false
nntpfail=0
do count=0 to retries while done=false
	say 'Trying' nntpserver
    address command 'nntpclient < 'nntpcmds' 'nntpserver' 'nntpopts
    nntpfail=RC
    if nntpfail = 0 then
    do
        done=true
    end 
    else if nntpfail = 10 then
    do
        call nntpfail10
    end 
    else if nntpfail = 15 then
    do
        call nntpfail15
    end
    else do
        say 'NNTPClient failed 'nntpfail
        done=true
    end
end
error=SetVar('NNTPTRANSFER','0','GLOBAL')
options failat 10 
call DoNewGroups
call UnBatchNews
delete(nntpcmds)

exit nntpfail

/* Function Definitions */

nntpfail10:

call DoNewGroups
say 'Something odd, may be a bad article'
failat 21
address command 'Amitcp:bin/AntiSpam NOCHECK'
if RC ~= 0 then
do
    done=true
end
else do
    call NameFiles
end
return

nntpfail15:

/*
options failat 21
address command 'amitcp:bin/connectstate'
*/
connectstate=GetVar('CONNECTSTATE')
if connectstate ~= 'ONLINE' then
do
    say 'NNTPTransfer: Not connected.'
    done=true
end 
return

DoNewGroups:

address command 'type >AmiTCP:log/incoming.news t:nntp-getcmds'
if exists(newgroups) then
do
    say '----- New News Groups -----'
    address command 'type 'newgroups' >> 'NEWACTIVE
    say nntpserver' has the following new news groups:'
    say
    address command 'type 'newgroups
    /*delete(newgroups)*/
end
return

UnBatchNews:

if UNBATCH = 'UNBATCH' then
do
    if exists(newsbatch) then
    do
       'amitcp:bin/unbatch_news.sh'
    end
    else do
       say 'Sorry, no news transferred'
    end
end
return

NameFiles:

UNIQUE=time(s)
newsbatch='UUSpool:Newsbatch.'|| UNIQUE
newgroups='T:NewGroups.'|| UNIQUE
address command 'echo > 'nntpcmds' "NEWGROUPS 'newgroups'*NNEWNEWS' Groups ' 'newsbatch'"'
return

HandleArgs:

parse var flag
	select
		when abbrev('HOST',flag,2) then
			parse var arguments nntpserver arguments
		when abbrev('GROUPS',flag,1) then
			parse var arguments groups arguments
		when abbrev('HISTORY',flag,2) then
			parse var arguments history arguments
		when abbrev('STAMP',flag,1) then
			parse var arguments stamp arguments
		when abbrev('KILL',flag,1) then
			parse var arguments kill arguments
		when abbrev('BLIP',flag,1) then
			parse var arguments blip arguments
		when abbrev('PANIC',flag,1) then
			parse var arguments panic arguments
		when abbrev('BATCH',flag,1) then
			parse var arguments batch arguments
		when abbrev('TRACE',flag,1) then
			parse var arguments trace arguments
		when abbrev('RETRIES',flag,1) then
			parse var arguments retries arguments
		when abbrev('BLIPMSG',flag,1) then
			parse var arguments blipmsg arguments
		when abbrev('NEWACTIVE',flag,1) then
			parse var arguments newactive arguments
		when abbrev('UNBATCH',flag,1) then
			parse var arguments unbatch arguments
		otherwise do
			say('Unknown Argument' flag)
			say('Exiting...')
			exit 20
			end
	end
return


