/* $VER: SOM.thor 1.3.1 (17.02.97)
 *
 * Saves the current message, and forwards it to several "interested" parties.
 *
 * PLEASE don't use this as a toy!
 * The only way our concerns will be taken seriously is if WE take them seriously.
 *
 * Concept & Original Script by: Kirk Strauser <kstrauser@gxl.com>
 * Re-written by: Adrian Knight <ajk@dial.pipex.com>
 *                Kirk Strauser <kstrauser@gxl.com>
 */

options results
options failat 31

thorport = address()
if left(thorport, 5) ~= 'THOR.' then do
  say 'Cannot find thorport.'
  exit
end

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

IF ~SHOW('L','rexxdossupport.library') THEN DO
  IF ~ADDLIB('rexxdossupport.library',10,-30,0) THEN DO
    SAY 'rexxdossupport.library, not found!'
    EXIT(10)
  END
END

thorrexx = GetVar('THOR/THORPATH',Global) || "Rexx/"
somcfg = thorrexx || 'som.cfg'
tmp.NAME = 'T:spam.temp2'
yamrun = 0
mailer = 3

address(thorport)

CURRENTMSG stem MSG
if (rc ~= 0) then call disreq("No current message.")

SAVEMESSAGE CURRENT FILE "T:Spam.temp1"
if(rc ~= 0) then call disreq("Unable to save current message.")

address BBSREAD
READBRMESSAGE bbsname '"'MSG.BBSNAME'"' confname '"'MSG.CONFNAME'"' msgnr MSG.MSGNR headstem HEADTAGS textstem TEXTTAGS
if (rc ~= 0) then call disreq

parse var HEADTAGS.FROMADDR j"@"domain
if domain='' then call disreq("Unable to find senders domain name")

if exists(somcfg) then do
  open(cfg,somcfg,"R")
  tname = HEADTAGS.FROMNAME || ',Postmaster'
  taddr = HEADTAGS.FROMADDR || ',postmaster@' || domain
  msgtit = '"Usenet spam/mail fraud"'
  do until EOF(cfg)
    line = readln(cfg)
    if (left(line,1) ~= '#') & (line ~= '') & (word(line,1)) ~= 'Mailer' then do
      parse var line ttname","ttaddr
      tname = tname || ',' || ttname
      taddr = taddr || ',' || ttaddr
    end
    else if (word(line,1)) = 'Mailer' then mailer = (word(line,3))
  end
  close(cfg)
end
else call disreq("Unable to locate config file.")

if mailer = 1 then do
  UNIQUEMSGFILE bbsname '"'MSG.BBSNAME'"' stem tmp
  if (rc ~= 0) then call disreq
end

address command 'Join 'thorrexx'SpamHeader T:Spam.temp1 'thorrexx'SpamFooter as 'tmp.NAME
if (rc ~= 0) then call disreq("Unable to build message file.")

address(thorport)

if mailer > 2 then do
  REQUESTNOTIFY TEXT '"Post messages using?"' BT '"_Thor|_YAM"'
  if (rc ~= 0) then call disreq
  mailer = result
end

if mailer = 1 then do
  REQUESTNOTIFY TEXT '"Mark message as urgent?"' BT '"_Yes|_No"'
  if (rc ~= 0) then call disreq
  flag = result

  address BBSREAD
  EVE_ENTERMSG = 0
  drop EVENT.
  EVENT.TONAME = tname
  EVENT.TOADDR = taddr
  EVENT.SUBJECT = msgtit
  EVENT.CONFERENCE = 'EMail'
  say tmp.NAME
  EVENT.MSGFILE = tmp.NAME
  EVENT.URGENT = flag
  WRITEBREVENT bbsname '"'MSG.BBSNAME'"' event EVE_ENTERMSG stem EVENT
  if (rc ~= 0) then do
    address command 'delete >NIL:' tmp.NAME
    call disreq
  end
end
else do
  if ~show('p','YAM') then do
    address command 'Run <>NIL: YAM:YAM'
    address command 'WaitForPort "YAM"'
    yamrun=1
  end

  address YAM
  mailwrite
  writemailto taddr
  writesubject msgtit
  writeletter 'T:Spam.temp2'

  request '"Should this be?" "Sent|Queued"'
  action=result
  if action=1 then writesend
  else writequeue
end
call cleanup

disreq:
  PARSE ARG distext
  if distext = '' then do
    if address() = "BBSREAD" then distext=BBSREAD.LASTERROR
    else distext=THOR.LASTERROR
  end
  address(thorport)
  REQUESTNOTIFY TEXT '"' distext '"' BT '"_Abort"'
  call cleanup
return

cleanup:
  if yamrun = 1 then address YAM 'quit'
  address command 'delete >nil: T:Spam.temp?'
exit
