/*
 $VER: GetHTTP.Thor 2.0b2 by Remco van Hooff
*/

hotlist_amosaic = 'envarc:mosaic/.mosaic-hotlist-default'
amosaic = 1
hotlist_ibrowse = 'IBrowse:prefs/hotlist'
ibrowse = 1
hotlist_html    = 'path_to_hotlist:hotlist.html'
html    = 0

/* don't edit these */
cr = '0d'x
lf = '0a'x

/* filter chars, expand if you want */
filter.1 = cr
filter.2 = lf
filter.3 = ')'
filter.4 = ','
filter.5 = "'"
filter.6 = '"'
filter.7 = ']'
filter.8 = '>'
filter.9 = '}'
filter.count = 9     /* number of chars */

/* --- no need to edit anything below this line --- */
tempfile  = 't:http.tmp'
space   = '                    '
drop name.

options failat 31
p = address() || ' ' || show('P',,)
thorport = pos('THOR.',p)
if thorport > 0 then thorport = word(substr(p,thorport),1)
else do
  say 'THOR port not found!'
  exit 10
end

if ~show('p', 'BBSREAD') then do
  address command
    "run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
    "WaitForPort BBSREAD"
end

address(thorport)
options results

CURRENTMSG stem MSG
if(rc ~= 0) then do
  REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
  exit
end
msgnum = MSG.MSGNR
bbs    = MSG.BBSNAME
conf   = MSG.CONFNAME

address bbsread READBRMESSAGE BBSNAME '"'bbs'"' CONFNAME '"'conf'"' MSGNR msgnum HEADSTEM headtags
if(rc ~= 0) then do
  REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
  exit
end
subj = HEADTAGS.SUBJECT
if pos('RE:',UPPER(subj)) ~=0 then subj = substr(subj,5)

SAVEMESSAGE CURRENT FILENAME tempfile NOHEADER NOANSI OVERWRITE
if(rc ~= 0) then do
  REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
  exit
end

else do
  call open(tmp, tempfile, 'r')
    num = 0
    do while ~eof(tmp)
      msg = readln(tmp)
      msg = translate(msg, 'hpt', 'HPT')
      parse var msg . 'ttp://' httpadres .
      if httpadres ~= '' then do
        num = num + 1
        lengte = length(httpadres)   
        call filter(httpadres, lengte)
        found.num = httpadres
        found.count = num
      end
    end
  call close(tmp)
  address command 'delete >nil: 'tempfile
  if found.0 = 0 then 'requestnotify text "No URL found." bt "_OK"'
end

call listfound

if ok = 1 then do
  listsave:
      do i = 1 to save.count
        if name.i = 'NAME.'i then name.i = space
        show.i = name.i' - 'save.i
      end
      sep = save.count +1
      but = save.count +2
      show.sep = ''
      show.but = 'SAVE'
      show.count = save.count+2
    REQUESTLIST INSTEM show TITLE '"Select to enter a name"' SIZEGADGET
    if (rc = 30) then do
      REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
      exit
    end
    if rc ~= 5 then do
      selected = result
      do n = 1 to save.count
        if selected = show.n then do
          if name.n = space then name.n = ''
          REQUESTSTRING title '"Enter a name"' BT '"_OK"' BODY '"'save.n'"' MAXCHARS 20 INITIALSTRING '"'name.n'"'
          name.n = LEFT(result,20,' ')
        end
        if selected = 'SAVE' then signal savehotlist
      end
    signal listsave
    end
end
EXIT

filter:
  adres = arg(1)
  lngth = arg(2)
  do i = 1 to filter.count
    check = pos(filter.i, adres)
    if check ~=0 then do
      adres = delstr(adres, check)
    end
  end
  punt = lastpos('.', adres)
  if punt ~=0 then lngth = length(adres)
  if (punt = lngth) then do
    adres = delstr(adres, punt)
  end
  httpadres = 'http://'||adres
return

listfound:
  if found.COUNT > 0 then do
    REQUESTLIST instem found outstem save title '"Select URL(s) to save"' MULTISELECT SIZEGADGET
    if (rc = 30) then do
      REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
      exit
    end
  end
  if rc ~= 5 then ok = 1
  if found.COUNT = 0 then do
    REQUESTNOTIFY '"No URLs found in this message"' '"_Ok"'
    exit
  end
return

savehotlist:
  DO i = 1 TO save.count
    IF name.i = '' THEN name.i = 'GetHTTP -' subj '('i')'
  END
  IF amosaic = 1 then call save_amosaic
  IF ibrowse = 1 then call save_ibrowse
  IF html    = 1 then call save_html
exit

save_amosaic:
  IF ~EXISTS(hotlist_amosaic) THEN DO
    Requestnotify '"Amosaic hotlist not found!"' '"_OK"'
    RETURN
  END
  ELSE DO
    dat = date()
    parse var dat dagnr maand jaar
    dag = left(date('W', date(S), 'S'), 3)
    datum = dag maand dagnr time()jaar
    CALL OPEN(htlst,hotlist_amosaic,'a')
    DO i = 1 TO save.count
      CALL WRITELN(htlst,save.i||' '||datum)
      CALL WRITELN(htlst,STRIP(name.i))
    end
    CALL CLOSE(htlst)
  END
  ADDRESS COMMAND 'copy' hotlist_amosaic 'env:mosaic/'
RETURN

save_ibrowse:
  IF ~EXISTS(hotlist_ibrowse) THEN DO
    Requestnotify '"IBrowse hotlist not found!"' '"_OK"'
    RETURN
  END
  ELSE DO
    CALL OPEN(htlst,hotlist_ibrowse,'a')
    DO i = 1 TO save.count
      CALL WRITELN(htlst,save.i)
      CALL WRITELN(htlst,STRIP(name.i))
    end
    CALL CLOSE(htlst)
  END
RETURN

save_html:
  IF ~EXISTS(hotlist_html) THEN DO
    Requestnotify '"HTML hotlist not found!"' '"_OK"'
    RETURN
  END
  ELSE DO
    CALL OPEN(htlst,hotlist_html,'a')
    DO i = 1 TO save.count
      CALL WRITELN(htlst,'<LI><A HREF="'save.i'">'STRIP(name.i)'</A><br>')
    end
    CALL CLOSE(htlst)
  END
RETRUN
