/*
** $VER: MM_SuperCC.rexx 0.35 (02.05.96)
**
** (C) 1995-96 by Michael A. Bode
*/

options results
address MAILMANAGER

/*************************************/
/* Default values for some variables */
/*************************************/

zone        = 2
net         = 2448
node        = 0
point       = 0
cnt         = 0
nodelist    = ''
maillist    = ''
numconv     = 0
textdir     = 'SYS:'
zaehl       = 0
tmpfil      = 'T:SuperCC.TMP'
editor      = 'Ed T:SuperCC.TMP'
gateaddress = '2:2448/7808.999'
gatename    = 'AmiGate'
echorec     = 'Alle'
subject     = 'Betreff fuer die Nachrichten'
unknown     = 'Unbekannt'

/*********/
/* Texts */
/*********/

tx1='Bitte die Nodeliste auswaehlen.'
tx2='"Domain:"'
tx3='Welchen Modus verwenden ?'
tx4='"Suchmuster:"'
tx5='"[[<Adresse>[@<Domain>] [<Name>]]|[*<FQDA> [<Realname>]]]|[<Areatag> [<Name>]]"'
tx7='"Gefundene Adressen/Tags:"'
tx8='Loesche: '
tx9='Diese Liste speichern ?'
tx10='Text-Editor aufrufen ?'
tx11='Zu versendendes Textfile auswaehlen !'
tx12='Flow-Typ auswaehlen !'
tx13='Zeilenumbruch vornehmen ?'
tx14='Empfangsbestaetigungen anfordern ?'
tx15='Mehrfachempfaenger loeschen ?'
tx16='CC:-Header erzeugen ?'
tx17='Welchen CC:-Header-Typ verwenden ?'
tx18='"Matrix-Area auswaehlen:"'
tx19='Jetzt exportieren ?'

/***********/
/* Buttons */
/***********/

button1='*_Ok'
button2='_Wildcards|*_Liste|_Eingabe|_Abbruch'
button3='_Ok|*_Abbruch'
button4='*_Ja|_Nein'
button5='_Ja|*_Nein'
button6='*_Normal|_Crash|_Hold|_Abbruch'
button7='*_Namen|_Wildcards'

/*********************************/
/* Load config and parse options */
/*********************************/

if ~open('eingabe','MM:Config/MM_SuperCC.cfg','R') then exit 20

do forever
   zeile=strip(strip(readln('eingabe'),'B',d2c(10)),'B',d2c(13)) 
   zaehl=zaehl+1
   if (left(zeile,1)~=';') & (compress(zeile)~='') then do
      if left(zeile,1)='#' then do
         parse var zeile '#' cmd parm1 parm2
         cmd=compress(upper(cmd)) ; parm1=compress(parm1) ; parm3=parm2 ; parm2=compress(parm2)
         if pos(cmd,'UNKNOWN.NODELISTPATH.ECHOREC.MAILINGLISTS.CONVERT.TEXTDIR.EDITOR.TEMPFILE.GATEADDR.GATENAME.ZONE.NET')=0 then call ladefehler

         if cmd='ZONE' then do
            zone=parm1
            if (parm1='') | (parm2~='') then call ladefehler
         end

         if cmd='NET' then do
            net=parm1
            if (parm1='') | (parm2~='') then call ladefehler
         end

         if cmd='GATEADDR' then do
            gateaddress=parm1
            if (parm1='') | (parm2~='') then call ladefehler
         end

         if cmd='GATENAME' then do
            gatename=parm1
            if (parm1='') | (parm2~='') then call ladefehler
         end

         if cmd='ECHOREC' then do
            echorec=parm1
            if (parm2~='') | (parm1='') then call ladefehler
         end

         if cmd='NODELISTPATH' then do
            nodelist=parm1
            if (right(nodelist,1)~=':') & (right(nodelist,1)~='/') then nodelist=nodelist'/'
            if (parm2~='') | (parm1='') then call ladefehler
         end

         if cmd='MAILINGLISTS' then do
            maillist=parm1
            if (right(maillist,1)~=':') & (right(maillist,1)~='/') then maillist=maillist'/'
            if (parm2~='') | (parm1='') then call ladefehler
         end

         if cmd='CONVERT' then do
            orignet.numconv=parm1 ; destnet.numconv=parm2 ; numconv=numconv+1
            if (parm1='') | (parm2='') then call ladefehler
         end

         if cmd='TEXTDIR' then do
            textdir=parm1
            if (right(textdir,1)~=':') & (right(textdir,1)~='/') then textdir=textdir'/'
            if (parm2~='') | (parm1='') then call ladefehler
         end

         if cmd='EDITOR' then do
            editor=parm1' 'parm3
            if (parm1='') then call ladefehler
         end

         if cmd='TEMPFILE' then do
            tmpfil=parm1
            if (parm2~='') | (parm1='') then call ladefehler
         end

         if cmd='UNKNOWN' then do
            unknown=parm1' 'parm3
            if (parm1='') then call ladefehler
         end
      end
      else call ladefehler
   end
   if eof('eingabe')=1 then do
      close('eingabe')
      leave
   end
end

/********************/
/* Ask for nodelist */
/********************/

titdef='"MM_SuperCC (C) Michael A. Bode"'
MM_Requester titdef 'tx1' 'button1'
MM_FileReq 'nodelist'
if (rc=1)|(rc=2) then exit

/*****************/
/* Create domain */
/*****************/

begin=max(lastpos(':',nodelist),lastpos('/',nodelist))
domain=right(nodelist,length(nodelist)-begin)
finish=lastpos('.',domain)
domain=left(domain,finish-1)
if numconv>0 then do
   do zaehl=0 for numconv-1
      if upper(domain)=upper(orignet.zaehl) then domain=destnet.zaehl
   end
end

/********************************/
/* Present domain to user & ask */
/********************************/

MM_StringReq tx2 'domain'
if (rc=1)|(rc=2) then exit

/**************************/
/* Ask for operation mode */
/**************************/

MM_Requester titdef 'tx3' 'button2'
opmode = rc
if opmode=0 then exit

/******************************/
/* Wildcard-matching selected */
/******************************/

if opmode=1 then do
   pat='2:2448/*.0'
   MM_StringReq tx4 'pat'
   if (rc=1)|(rc=2) then exit

   parse var pat zonepattern ':' netpattern '/' nodepattern '.' pointpattern
   if (zonepattern='')|(netpattern='')|(nodepattern='')|(pointpattern='') then exit
   if ~open('eingabe',nodelist,'R') then exit 20
   do forever
      zeile=strip(strip(readln('eingabe'),'B',d2c(10)),'B',d2c(13))
      if (left(zeile,1)~=';')&(length(zeile)>5) then do
         if left(zeile,1)=',' then zeile='NODE'zeile
         parse var zeile key ',' number ',' name ',' place ',' opname ',' dummy
         if key='Zone' then do
            zone  = number
            net   = number
            node  = 0
            point = 0
         end
         if upper(key)='HOST'|upper(key)='REGION' then do
            net   = number
            node  = 0
            point = 0
         end
         if key='NODE'|upper(key)='HUB'|upper(key)='DOWN'|upper(key)='HOLD' then do
            node  = number
            point = 0
         end
         if upper(key)='POINT' then point=number
         under=pos('_',opname)  
         do while under>0
            opname = overlay(' ',opname,under,1,' ')
            under  = pos('_',opname)
         end
         if (zone=zonepattern)|zonepattern='*' then do
            if (net=netpattern)|netpattern='*' then do
               if (node=nodepattern)|nodepattern='*' then do
                  if (point=pointpattern)|pointpattern='*' then do
                     adresse.cnt  = zone':'net'/'node'.'point'@'domain
                     operator.cnt = opname
                     output.cnt   = adresse.cnt
                     output.cnt   = overlay(operator.cnt,output.cnt,40,30,' ')
                     cnt          = cnt+1
                  end
               end
            end
         end
      end
      if eof('eingabe')=1 then do
         close('eingabe')
         ask=1
         leave
      end
   end
end

/*****************/
/* Mailing lists */
/*****************/

if opmode=2 then do

   /********************************/
   /* Let user select mailing-list */
   /********************************/

   MM_FileReq 'maillist'
   if (rc=1)|(rc=2) then exit
 
   if ~open('eingabe',maillist,'R') then exit 20
   do forever
      adresse.cnt=strip(strip(strip(readln('eingabe'),'B',d2c(10)),'B',d2c(13)))
      blank=pos(' ',adresse.cnt)
      if blank>0 then do
         operator.cnt=strip(right(adresse.cnt,length(adresse.cnt)-blank))
         adresse.cnt=left(adresse.cnt,blank-1)
      end
      else operator.cnt=''
      if compress(adresse.cnt)~='' then do
         if (left(adresse.cnt,1)~='*')&(left(adresse.cnt,1)~='#') then do

            /**************************/
            /* Entry is a FTN-address */
            /**************************/

            if (pos('.',adresse.cnt)=0)&(pos('@',adresse.cnt)=0) then adresse.cnt=adresse.cnt'.0'
            if pos('@',adresse.cnt)=0 then adresse.cnt=adresse.cnt'@'domain
            MM_GetNodelistNode adresse.cnt 'info'
            if rc=5 then exit 20
            if (rc=4)&(operator.cnt='') then operator.cnt=unknown
            if (rc=0)&(operator.cnt='') then operator.cnt=info.sysop
            output.cnt = adresse.cnt
            output.cnt = overlay(operator.cnt,output.cnt,40,30,' ')
            cnt        = cnt+1
         end
         if left(adresse.cnt,1)='#' then do

            /***********************/
            /* Entry is an areatag */
            /***********************/

            output.cnt                = compress(adresse.cnt)
            adresse.cnt               = output.cnt
            if compress(operator.cnt) = '' then operator.cnt=echorec
            output.cnt                = overlay(operator.cnt,output.cnt,40,30,' ')
            cnt                       = cnt+1
         end
         if left(adresse.cnt,1)='*' then do

            /*******************/
            /* Entry is a FQDA */
            /*******************/

            output.cnt                = compress(adresse.cnt)
            adresse.cnt               = output.cnt
            if compress(operator.cnt) = '' then operator.cnt='[RFC]'
            output.cnt                = overlay(operator.cnt,output.cnt,40,30,' ')
            cnt                       = cnt+1
         end
      end
      if eof('eingabe')=1 then do
         close('eingabe')
         ask=0
         leave
      end
   end
end

/**************/
/* Input Mode */
/**************/

if opmode=3 then do
   inpres=0
   do while inpres~=1
      inp=""
      MM_StringReq tx5 'inp'
      inpres=rc
      if (inpres=0)&(compress(inp)~='') then do
         if words(inp)>1 then do
            blank=pos(' ',inp)
            operator.cnt=strip(right(inp,length(inp)-blank))
            inp=left(inp,blank-1)
         end
         else operator.cnt=''
         if (left(inp,1)~='*')&(left(inp,1)~='#') then do

            /***********************/
            /* FTN-address entered */
            /***********************/

            adresse.cnt=compress(word(inp,1))
            if (pos('.',adresse.cnt)=0)&(pos('@',adresse.cnt)=0) then adresse.cnt=adresse.cnt'.0'
            if pos('@',adresse.cnt)=0 then adresse.cnt=adresse.cnt'@'domain
            MM_GetNodelistNode adresse.cnt 'info'
            if rc=5 then exit 20
            if (rc=0)&(operator.cnt='') then operator.cnt=info.sysop
            if (rc=4)&(operator.cnt='') then operator.cnt=unknown
            output.cnt = adresse.cnt
            output.cnt = overlay(operator.cnt,output.cnt,40,30,' ')
            cnt        = cnt+1
            ask        = 1
         end
         if left(inp,1)='#' then do

            /*******************/
            /* Areatag entered */
            /*******************/

            output.cnt                = compress(inp)
            adresse.cnt               = output.cnt
            if compress(operator.cnt) = '' then operator.cnt=echorec
            output.cnt                = overlay(operator.cnt,output.cnt,40,30,' ')
            cnt                       = cnt+1
            ask                       = 1
         end
         if left(inp,1)='*' then do

            /****************/
            /* FQDA entered */
            /****************/

            output.cnt                = compress(inp)
            adresse.cnt               = output.cnt
            if compress(operator.cnt) = '' then operator.cnt='[RFC]'
            output.cnt                = overlay(operator.cnt,output.cnt,40,30,' ')
            cnt                       = cnt+1
            ask                       = 1
         end
      end
   end
end

/********************************************/
/* Search for own addresses and remove them */
/********************************************/

MM_GetAddrs 'ownaka'

do zaehl2=0 for ownaka.count-1
   do zaehl3=0 for cnt-1
      if upper(ownaka.zaehl2)=upper(adresse.zaehl3) then do
         do zaehl=0 for cnt-zaehl3
            dest1          = zaehl+zaehl3
            dest2          = dest1+1
            adresse.dest1  = adresse.dest2
            operator.dest1 = operator.dest2
            output.dest1   = output.dest2
         end
         cnt=cnt-1
      end
   end
end

/*******************************************/
/* Display list of found nodes/points/tags */
/*******************************************/

output.count=cnt
do forever
   MM_SingleSelReq 'output' 'choice' tx7 'Num'
   pre=rc
   if (pre=1)|(pre=2) then exit
   sel=choice.0
   if choice.count=1 then do
      txt=tx8||adresse.sel' ?'
      MM_Requester titdef 'txt' 'button3'
      if rc=1 then do
         if cnt=1 then exit
         do zaehl=0 for cnt-sel
            dest1          = zaehl+sel
            dest2          = dest1+1
            adresse.dest1  = adresse.dest2
            operator.dest1 = operator.dest2
            output.dest1   = output.dest2
         end
         cnt          = cnt-1
         output.count = output.count-1
         ask          = 1
         sel          = 0
      end
   end
   if (pre=0)&(choice.count=0) then leave
end

/***************************************************/
/* If list has changed: ask if list shall be saved */
/***************************************************/

if ask=1 then do
   MM_Requester titdef 'tx9' 'button4'
   if rc=1 then do
      MM_FileReq 'maillist'
      if rc=2 then exit
      if rc=0 then do
         if ~open('ausgabe',maillist,'W') then exit 20
         do zaehl=0 for cnt
            wrtstr=adresse.zaehl
            if (left(adresse.zaehl,1)~='*')&(left(adresse.zaehl,1)~='#') then do
               if pos('.0',adresse.zaehl)=0 then wrtstr=wrtstr' 'operator.zaehl
            end
            writeln('ausgabe',wrtstr)
         end
         close('ausgabe')
      end
   end
end

/*******************/
/* Invoke editor ? */
/*******************/

MM_Requester titdef 'tx10' 'button5'
if rc=1 then do
   address COMMAND editor
   textdir=tmpfil
   einv=4711
end

/********************************************/
/* Ask for several still missing parameters */
/********************************************/

MESSI.TEAR='SuperCC v0.35'
flg='PVT'
flg2=''

if einv~=4711 then do
    MM_Requester titdef 'tx11' 'button1'
    MM_FileReq 'textdir'
    if (rc=1)|(rc=2) then exit
end

MM_StringReq titdef 'subject'
MESSI.SUBJ=subject
if (rc=1)|(rc=2) then exit

MM_Requester titdef 'tx12' 'button6'
if rc=0 then exit
if rc=2 then flg=flg' CRASH'
if rc=3 then flg=flg' HOLD'

MM_Requester titdef 'tx13' 'button4'
if rc=1 then do
   flg=flg' REFLOW'
   flg2='REFLOW'
end

MM_Requester titdef 'tx14' 'button4'
if rc=1 then flg=flg' RRR'

MM_Requester titdef 'tx15' 'button4'
krec=rc

MM_Requester titdef 'tx16' 'button5'

/************************/
/* Construct CC:-header */
/************************/

if rc=1 then do
   if ~open('ausgabe','T:SuperCC.TMP','W') then exit 20
   ccstyle=1
   if opmode=1 then do
      MM_Requester titdef 'tx17' 'button7'
      ccstyle=rc
   end
   if ccstyle=1 then do
      cstr='CC: '
      laenge = length(adresse.0)
      if left(adresse.0,1) = '*' then do
         if operator.0 = '[RFC]' then cstr=(cstr||right(adresse.0,laenge-1))
         else cstr=(cstr||operator.0)
      end
      if left(adresse.0,1) = '#' then cstr=cstr||right(adresse.0,laenge-1)
      if left(adresse.0,1) ~= '*' & left(adresse.0,1) ~= '#' then do
         if operator.0=unknown then cstr=cstr||adresse.0
         else cstr=cstr||operator.0
      end
      if cnt>1 then do
         do zaehl=1 for cnt-1
            dup=0
            laenge = length(adresse.zaehl)
            do zaehl2=0 for zaehl
               if left(adresse.zaehl,1)~='*'&left(adresse.zaehl,1)~='#' then do
                  if (upper(operator.zaehl)=upper(operator.zaehl2))&(operator.zaehl~=unknown) then dup=1
                  addme=operator.zaehl
               end
               if left(adresse.zaehl,1)='*'|left(adresse.zaehl,1)='#' then do
                  if upper(adresse.zaehl)=upper(adresse.zaehl2) then dup=1
                  if left(adresse.zaehl,1)='*' then do
                     if operator.zaehl='[RFC]' then addme=right(adresse.zaehl,laenge-1)
                     else addme=operator.zaehl
                  end
                  if left(adresse.zaehl,1)='#' then addme=right(adresse.zaehl,laenge-1)
               end
            end
            if ((krec=1)&(dup=0))|krec=0 then do
               if (length(cstr)+2+length(addme))<80 then cstr=cstr', 'addme
               else do
                  writeln('ausgabe',cstr',')
                  cstr='    'addme
               end
            end
         end
         writeln('ausgabe',cstr||d2c(13))
      end
   end
   else do
      writeln('ausgabe','CC: 'pat||d2c(13))
   end
end
else do
   if ~open('ausgabe','T:SuperCC.TMP','W') then exit 20
end

/************************/
/* Read text to be sent */
/************************/

if ~open('eingabe',textdir,'R') then exit 20
lof=readch('eingabe',32768)
writech('ausgabe',lof)
close('eingabe')
close('ausgabe')
address COMMAND 'copy T:SuperCC.TMP T:SuperCC.TAG'
if ~open('ausgabe','T:SuperCC.TMP','A') then exit 20
writeln('ausgabe', d2c(13)'--- MM_SuperCC v0.35')
close('ausgabe')

/**********************************/
/* Ask for matrix-area to be used */
/**********************************/

MM_GetAreas 'area' 'MAIL'
MM_SingleSelReq 'area' 'choice2' tx18 'Str'
if (rc=1)|(rc=2) then exit
if choice2.count~=1 then exit
tag=choice2.0
cnt2=0

/*****************/
/* Send messages */
/*****************/

dup=0
do zaehl=0 for cnt
   if krec=1 then do
      if zaehl>0 then do
         dup=0
         do zaehl2=0 for zaehl
            if left(adresse.zaehl,1)~='*'&left(adresse.zaehl,1)~='#' then do
               if (upper(operator.zaehl)=upper(operator.zaehl2))&(operator.zaehl~=unknown) then dup=1
            end
            if left(adresse.zaehl,1)='*'|left(adresse.zaehl,1)='#' then do
               if upper(adresse.zaehl)=upper(adresse.zaehl2) then dup=1
            end
         end
      end
   end
   if ((krec=1)&(dup=0))|krec=0 then do
      if left(adresse.zaehl,1)~='*' then do
         if left(adresse.zaehl,1)~='#' then do

            /***********************/
            /* Send message as FTN */
            /***********************/

            MESSI.FILE   = 'T:SuperCC.TMP'
            MESSI.TO     = operator.zaehl
            MESSI.TOADDR = adresse.zaehl
            MESSI.FLAGS  = flg
            MM_WriteMsg tag 'MESSI'
            cnt2=cnt2+1
         end
         else do

            /************************/
            /* Send message in echo */
            /************************/

            MESSI.FILE   = 'T:SuperCC.TAG'
            MESSI.TO     = operator.zaehl
            MESSI.TOADDR = ''
            MESSI.FLAGS  = flg2
            MM_WriteMsg right(adresse.zaehl,length(adresse.zaehl)-1) 'MESSI'
            cnt2=cnt2+1
         end
      end
      else do

         /***********************/
         /* Send message as RFC */
         /***********************/

         if ~open('ausgabe','T:SuperCC.RFC','W') then exit 20
         if ~open('eingabe','T:SuperCC.TMP','R') then exit 20
         writeln('ausgabe', 'TO: 'right(adresse.zaehl,length(adresse.zaehl)-1)||d2c(13))
         lof=readch('eingabe',32768)
         writech('ausgabe',lof)
         close('eingabe')
         close('ausgabe')
         MESSI.FILE   = 'T:SuperCC.RFC'
         MESSI.TO     = gatename
         MESSI.TOADDR = gateaddress
         MESSI.FLAGS  = flg
         MM_WriteMsg tag 'MESSI'
         cnt2=cnt2+1
      end
   end
end

/*************************/
/* Export if user wishes */
/*************************/

MM_Requester titdef 'tx19' 'button4'
if rc=1 then do
   MM_Scan tag
   MM_Export tag
   do zaehl=0 for cnt
      if left(adresse.zaehl,1)='#' then do
         MM_Scan operator.zaehl
         MM_Export operator.zaehl
      end
   end
end

/********************/
/* Delete tempfiles */
/********************/

address COMMAND "Delete >Nil: <Nil: "tmpfil
address COMMAND "Delete >Nil: <Nil: T:SuperCC#?"

exit

/***********************/
/* End of main program */
/***********************/

ladefehler:
say 'Error in config file line 'zaehl
say '*'zeile'*'
close('eingabe')
exit

/******************/
/* End of program */
/******************/

