/* Newsgroups.awebrx - REXX exec to get list of all newsgroups */
/*

                             Description 

Short : Add 'List all newsgroups' option to AWebNews
Author: Menno Tjoelker, M.Tjoelker@mpn.cp.philips.com
Date  : 25 December 1996.

This exec is an enhancement to AwebNews.
It adds the option to select a newsgroup from all available newsgroups.
I preferred to create a separate exec, rather than modifying Bill
Parker's excellent AWebNews exec. I leave it to him to integrate this
function into his exec, if he wants. I've copied some routines from his
exec and the program structure is very similar, so this would not be too
difficult.

  
                             Requirements

In order to have use of this exec, you need the AWeb webbrowser and
the News plugin AWebNews.

                             Installation

Newgroups.awebrx should be put in directory AWEB-II:plugins/awebnews,
the directory where AWebNews resides. 

There are two options to call Newsgroups.awebrx, either from the ARexx
menu of AWeb and/or from the confignews.awebrx exec. In the latter case
you have to change one line within the confignews.awebrx exec.

ARexx Menu:
-----------

title           Show All Newsgroups
macro           AWeb-II:plugins/awebnews/Newsgroups.awebrx

Confignews.awebrx:
------------------

Add this line

  call writeln(1,' - <a href="x-aweb:rexx/'current_dir'newsgroups.awebrx">List All Newsgroups </a>')

after the line saying

  call writeln(1,' - <a href="x-aweb:rexx/'current_dir'news.awebrx">Read News </a>')

(The easiest way to find this line is to search for the word CAUTION.) 

*/

/*Show Host first response at top of page use 'on' or 'off'*/
showstartinfo='off'

/*Show calling parameters at top of page use 'on' or 'off'*/
showcomand='off'

/*Show message info at top of page use 'on' or 'off'*/
showmesinfo='off'                           /* MT 25-12-1996 */

options results
signal on ioerr
newsgroupsfile="newsgroups"
allgroupsfile="allgroups.html"              /* MT 25-12-1996 */
lterm='0d'x
term='.'||'0d'x
fterm='.'||'0d'x||'0a'x
bterm='0d'x||'0a'x||'.'||'0d'x||'0a'x
ports = show('P')
parse var ports dummy 'AWEB.' portnr .
address value 'AWEB.' || portnr
'GET ACTIVEPORT'
awebhost = result
if ~show('L','rexxsupport.library') then
if ~addlib('rexxsupport.library',0,-30,0) then
exit(20)

parse arg addr
say addr
parse var addr . 'rfrgrp="' rfrgrp '"' .    /* MT 25-12-1996 */

parse source prog_type result_flag called resolved ext host .

last_slash = lastpos('/',called)
last_colon = lastpos(':',called)
dir_pos = max(last_slash,last_colon)
if dir_pos > 0 then
current_dir = left(called,dir_pos)
else
current_dir = ''


address value awebhost
address command 'delete T:configawebnews.#?.html >NIL:'

fname='T:awebnews.'time(S)'.html'

call firstinfo

if NewsHost='' then do
  call open(1,fname,w)
  call writeln(1,'<html><head><title>AWebNews Error</title></head><body>')
  call writeln(1,'<h1>AWebNews Error</h1>')
  call writeln(1,'<b>You have not configured your NewsHost.</b><p>')
  call writeln(1,'Please run ConfigNews script <br>')
  call writeln(1,'<a href="x-aweb:rexx/'current_dir'confignews.awebrx">Configuration </a>')
  call close(1)
  'OPEN file://localhost/'fname
  'SCREENTOFRONT'
  exit
end


call pragma(w,n)
if ~showlist(H,'TCP') then do
  call open(1,fname,w)
  call writeln(1,'<html><head><title>AWebNews Error</title></head><body>')
  call writeln(1,'<h1>AWebNews Error</h1>')
  call writeln(1,'<b>Can not find TCP: on your system.</b><p>')
  call close(1)
  'OPEN file://localhost/'fname
  'SCREENTOFRONT'
  exit
end

/* MT 25-12-1996 */

if ~exists(current_dir||allgroupsfile) | rfrgrp="Refresh List" then do
  call opennews
  call getgroups
  call closenews
end

else NOP

'OPEN file://localhost/'current_dir||allgroupsfile
'SCREENTOFRONT'
'ACTIVATEWINDOW'
'ALLOWCMD'

exit

/***************************** ROUTINES *********************************/

getgroups:                              /* MT 25-12-1996 */
trace off
call open(1,current_dir||allgroupsfile,w)
call htmltop
call writeln(1,'<h1>Newsgroups available on server <p>' NewsHost '</h1><br>')
call writeln(1,'<h2>Retrieved' date("e") time()||'.</h2>')
call writeln(1,'<i>AWebNews enhancement by Menno Tjoelker, 25-12-1996</i>') 
call writeln(1,'<br>')
call writeln(1,'<hr>')
call writeln(1,'<br>')


call writeln(8,'LIST')
mesinfo=readln(8)
if showmesinfo='on' then call writeln(1,mesinfo '<br>')
parse var mesinfo errcode mesinfo
if errcode='215' then do
  lineinfo=readln(8)
  do while lineinfo~=term 
	parse var lineinfo group last first p
	call writeln(1,'<a href=news:'group'>'group '</a> (Msgs: 'first' - 'last')<br>')
	lineinfo=readln(8)
  end
trace on
end

else NOP

call writeln(1,'<br><hr><br>')
call writeln(1,'<form action="x-aweb:rexx/'||called||'">')
call writeln(1,'<input type="submit" value="Refresh List" name="rfrgrp">')
call writeln(1,'</form><p>')
call writeln(1,'<b>Warning:</b> Getting all groups might take very much time and memory!')
call writeln(1,'<p>')
call writeln(1,'<a href="x-aweb:rexx/'current_dir'confignews.awebrx">AWebNews Configuration </a>')
call htmlbottom
call close(1)
trace results
return         


htmltop:
call writeln(1,'<html><head><title>')
call writeln(1,'AWebNews Reader')
call writeln(1,'</title></head>')
if colo='on' then  call writeln(1,'<body bgcolor="'bacc'" text="'texc'">')
if showcomand='on' then call writeln(1,addr '<br>')
if showstartinfo='on' then call writeln(1,startinfo)
return


htmlbottom:
call writeln(1, '<hr></body></html>')
call close(1)
return


opennews:
if showlist(H,'TCP') then do
  if ~ open(8,'TCP:'NewsHost'/nntp',w) then do
    address command 'RequestChoice "AWebNews " "Can Not Open Host 'NewsHost'" "Ok" >NIL:'
    exit
  end
  startinfo = readln(8)
  parse var startinfo errcode startinfo
end
return

closenews:
call writeln(8,'QUIT')
call close(8)
return


firstinfo:
if  open(2,current_dir||'newsconfig',r) then do
  configinfo = readch(2,3000)

  parse var configinfo . 'colo ' colo '0a'x
  parse var configinfo . 'texc ' texc '0a'x
  parse var configinfo . 'bacc ' bacc '0a'x
  parse var configinfo . 'bfile ' bfile '0a'x
  parse var configinfo . 'host ' NewsHost '0a'x
  parse var configinfo . 'many ' many '0a'x
  parse var configinfo . 'sho ' sho '0a'x
  parse var configinfo . 'lho ' lho '0a'x
  parse var configinfo . 'scan ' scan '0a'x
  call close(2)
end
return


ioerr:
    address command 'RequestChoice "AWebNews " "'NewsHost' not responding" "Ok" >NIL:'
    exit
