home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / wUUCP_0.0.29.lha / bin / uuclean.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1994-05-12  |  18.9 KB  |  466 lines

  1. /* rexx:uuclean.rexx, (c) Tue, 10 May 1994 16:51:36 +0200 by "Kai 'wusel' Siering" <wusel@3jean[.uucp|.hanse.de]> */
  2. /*
  3.  * $Header: rexx:uuclean.rexx,v 1.6 1994/05/12 01:38:43 wusel Exp wusel $
  4.  * $Log: uuclean.rexx,v $
  5. # Revision 1.6  1994/05/12  01:38:43  wusel
  6. # On `Precedence: junk'áor `bulk', no warning message is sent.
  7. #
  8. # Revision 1.5  1994/05/11  03:22:23  wusel
  9. # Final fixes; now honours `Precedence:' header (does not include body if
  10. # `junk'áor `bulk'). Installed at hactar.
  11. #
  12. # Revision 1.4  1994/05/11  02:38:06  wusel
  13. # Testversion; now includes warning message (can be switched off).
  14. #
  15. # Revision 1.3  1994/05/11  00:55:21  wusel
  16. # Added args argument, `ccpostmaster' will sent a copy of all bounces to the
  17. # local postmaster.
  18. #
  19. # Revision 1.2  1994/05/11  00:42:33  wusel
  20. # Now uses new getreturnaddr() instead of relying on `R'áentry in X-file.
  21. #
  22. # Revision 1.1  1994/05/10  19:27:21  wusel
  23. # Minor changes; installed at hactar.
  24. #
  25. # Revision 1.0  1994/05/10  19:09:19  wusel
  26. # Initial revision
  27. #
  28.  */
  29.  
  30. parse arg spooldir wdays xdays args
  31.  
  32. mailfile = 't:uuclean.'pragma('ID')'.tmp'
  33.  
  34. if((right(spooldir, 1)~=":") & (right(spooldir, 1)~="/")) then spooldir=spooldir||"/"
  35.  
  36. say "Cleaning UUCP spool directory "spooldir"."
  37.  
  38. ccpostmaster= 0
  39. dowarn      = 1
  40.  
  41. if(index(upper(args), "NOWARN")>0) then dowarn = 0
  42. if(index(upper(args), "CCPOSTMASTER")>0) then ccpostmaster = 1
  43.  
  44. if(dowarn) then say "Sending warnings after "wdays" days and wiping jobs older than "xdays" days."
  45. else            say "Wiping jobs older than "xdays" days."
  46.  
  47. if(dowarn) then
  48. do
  49.    if(xdays <= wdays) then
  50.    do
  51.       say "Error: xdays must be greater than wdays, aborting."
  52.       exit 21
  53.    end
  54. end
  55.  
  56. if(ccpostmaster) then say "Will send copies to local postmaster."
  57.  
  58. /* open the Rexx support library */
  59.  
  60. if ~show('L',"rexxsupport.library") then do
  61.    if addlib('rexxsupport.library',0,-30,0) then do
  62.      say 'Added rexxsupport.library'
  63.    end
  64.    else do
  65.      say 'Support library (LIBS:rexxsupport.library) not available.'
  66.      say 'Aborting...'
  67.      exit(30)
  68.    end
  69. end
  70.  
  71. HostName    = ''
  72. DomainName  = ''
  73.  
  74. if(open(conf, "uulib:config", r)) then do
  75.    do while ~eof(conf)
  76.       in=readln(conf)
  77.  
  78.       in2 = Translate(in, ' ', D2C(9))
  79.       parse var in2 key cont .
  80.  
  81.       select
  82.          when upper(key)="NODENAME"     then HostName   = strip(cont)
  83.          when upper(key)="DOMAINNAME"   then DomainName = strip(cont)
  84.          otherwise nop
  85.       end
  86.    end
  87.    err=close(conf)
  88. end
  89. else do
  90.    say 'UUCP configuration file (UUlib:config) not found, aborting.'
  91.    exit(20)
  92. end
  93.  
  94. ThisSite   = HostName||DomainName
  95. dirs       = showdir(spooldir, "dir")
  96. numdirs    = words(dirs)
  97. xdate      = date('i') - xdays
  98. wdate      = date('i') - wdays
  99.  
  100. do dir=1 to numdirs
  101.    currdir=spooldir||word(dirs, dir)
  102.  
  103.    say "Processing files in "currdir" ..."
  104.  
  105.    list = showdir(currdir)
  106.    number = words(list)
  107.    fdate = 0
  108.  
  109.    do i=1 to number
  110.       filename = word(list, i)
  111.       if(left(filename, 2) = "C.") then
  112.       do
  113.          fullfilename=currdir||"/"||filename
  114.          fstat = statef(fullfilename)
  115.          fdate = word(fstat, 5)
  116.  
  117.          if(word(fstat, 1) = 'FILE') then
  118.          do
  119.             select
  120.                when(dowarn & (fdate = wdate)) then /* send warning message only */
  121.                do
  122.                   files2process=parsecfile(fullfilename)
  123.                   if(words(files2process)==2) then
  124.                   do
  125.                      /*say "Processing "word(files2process, 2)" ..."*/
  126.                      xfiledata=parsexfile(currdir||"/"word(files2process, 2))
  127.                      if(words(xfiledata)>=1) then
  128.                      do
  129.                         if(compare(word(xfiledata, 1), "rmail")==0) then
  130.                         do
  131.                            isjunkmail=0
  132.                            precedence=""
  133.                            numrcpts=words(xfiledata)-2
  134.                            msgboundary=date('i')||pragma('ID')||time('s')
  135.                            retaddr=getreturnaddress(currdir||"/"word(files2process, 1))
  136.                            daysleft=xdays-wdays
  137.  
  138.                            say "Send warning message (`"word(files2process, 1)"'): `"xfiledata"'."
  139.                            if (open(mail, mailfile, w)) then
  140.                            do
  141.                               call writeln(mail, "From: uucp@"ThisSite" (UUCP Administrator)")
  142.                               call writeln(mail, "To: "retaddr)
  143.                               if(ccpostmaster) then call writeln(mail, "Cc: postmaster@"ThisSite" (PostMaster of "hostname")")
  144.                               call writeln(mail, "Subject: Delivery delayed: cannot connect to host "word(dirs, dir)" for "wdays" days.")
  145.                               call writeln(mail, "MIME-Version: 1.0")
  146.                               call writeln(mail, "X-Version: $Id: uuclean.rexx,v 1.6 1994/05/12 01:38:43 wusel Exp wusel $")
  147.                               call writeln(mail, "Message-ID: <"msgboundary"@"ThisSite">")
  148.                               call writeln(mail, 'Content-Type: multipart/mixed; boundary="Boundary (ID 'msgboundary')"')
  149.                               call writeln(mail, "Content-Transfer-Encoding: 8bit")
  150.                               call writeln(mail, "")
  151.                               call writeln(mail, "This is a MIME encapsulated message. If you read this, you don't have a")
  152.                               call writeln(mail, "MIME compliant mail user agent, you therefore might have trouble reading")
  153.                               call writeln(mail, "this message. You have been warned ;)")
  154.                               call writeln(mail, "")
  155.                               call writeln(mail, "--Boundary (ID "msgboundary")")
  156.                               call writeln(mail, "")
  157.                               call writeln(mail, "                     *** THIS IS A WARNING MESSAGE ONLY ***")
  158.                               call writeln(mail, "")
  159.                               call writeln(mail, "We can't connect to host "word(dirs, dir)" for "wdays" days, your message will remain en-")
  160.                               call writeln(mail, "queued and attempts to deliver it will continue for "daysleft" more days. If the")
  161.                               call writeln(mail, "situation won't change until then, your message will be returned to you")
  162.                               call writeln(mail, "as being undeliverable. Sorry for any inconvenience.")
  163.                               call writeln(mail, "")
  164.                               call writeln(mail, "Your message is on hold for delivery to:")
  165.                               do rcpt=1 to numrcpts
  166.                                  call writeln(mail, "   "word(xfiledata, rcpt+2)" (via "word(dirs, dir)")")
  167.                               end
  168.                               call writeln(mail, "")
  169.                               call writeln(mail, "--Boundary (ID "msgboundary")")
  170.                               call writeln(mail, "Content-Type: message/RFC822")
  171.                               call writeln(mail, "Content-Transfer-Encoding: 8bit")
  172.                               call writeln(mail, "")
  173.                               if(open(datafile, currdir||"/"word(files2process, 1), r)) then
  174.                               do
  175.                                  /* Skip `From ' line ... */
  176.                                  in = readln(datafile)
  177.                                  /* Copy message */
  178.                                  do while ~eof(datafile)
  179.                                     in = readln(datafile)
  180.                                     call writeln(mail, in)
  181.                                     if(left(upper(in), 11)="PRECEDENCE:") then
  182.                                     do
  183.                                        parse var in precedence ' ' precedence
  184.                                        if((precedence="junk") | (precedence="bulk")) then isjunkmail=1
  185.                                     end
  186.                                     if(in = "") then break
  187.                                  end
  188.                                  call close(datafile)
  189.                                  call writeln(mail, "[Body not included, this is just a warning message.]")
  190.                               end
  191.                               else
  192.                               do
  193.                                  call writeln(mail, "[OOPS?! Can't access "currdir||"/"word(files2process, 1)"?]")
  194.                               end
  195.                               call writeln(mail, "")
  196.                               call writeln(mail, "--Boundary (ID "msgboundary")--")
  197.                               call close(mail)
  198.                               if(isjunkmail) then say "`Precedence: "precedence"', no mail sent."
  199.                               else                address COMMAND "sendmail >nil: <"mailfile
  200.                               address COMMAND "delete >nil: "mailfile
  201.                            end
  202.                         end
  203.                      end
  204.                      else
  205.                      do
  206.                         say "Skipping: numargs<1 ("words(xfiledata)"); xfiledata: `"xfiledata"'."
  207.                      end
  208.                   end
  209.                end   /* when */
  210.  
  211.                when(fdate < xdate) then   /* bounce message */
  212.                do
  213.                   files2process=parsecfile(fullfilename)
  214.                   if(words(files2process)==2) then
  215.                   do
  216.                      /*say "Processing "word(files2process, 2)" ..."*/
  217.                      xfiledata=parsexfile(currdir||"/"word(files2process, 2))
  218.                      if(words(xfiledata)>=1) then
  219.                      do
  220.                         if(compare(word(xfiledata, 1), "rmail")==0) then
  221.                         do
  222.                            isjunkmail=0
  223.                            precedence=""
  224.                            numrcpts=words(xfiledata)-2
  225.                            msgboundary=date('i')||pragma('ID')||time('s')
  226.                            retaddr=getreturnaddress(currdir||"/"word(files2process, 1))
  227.  
  228.                            say "Bounce email (`"word(files2process, 1)"'): `"xfiledata"'."
  229.                            if (open(mail, mailfile, w)) then
  230.                            do
  231.                               call writeln(mail, "From: uucp@"ThisSite" (UUCP Administrator)")
  232.                               call writeln(mail, "To: "retaddr)
  233.                               if(ccpostmaster) then call writeln(mail, "Cc: postmaster@"ThisSite" (PostMaster of "hostname")")
  234.                               call writeln(mail, "Subject: Returned mail: cannot connect to host "word(dirs, dir)" for "xdays" days.")
  235.                               call writeln(mail, "MIME-Version: 1.0")
  236.                               call writeln(mail, "X-Version: $Id: uuclean.rexx,v 1.6 1994/05/12 01:38:43 wusel Exp wusel $")
  237.                               call writeln(mail, "Message-ID: <"msgboundary"@"ThisSite">")
  238.                               call writeln(mail, 'Content-Type: multipart/mixed; boundary="Boundary (ID 'msgboundary')"')
  239.                               call writeln(mail, "Content-Transfer-Encoding: 8bit")
  240.                               call writeln(mail, "")
  241.                               call writeln(mail, "This is a MIME encapsulated message. If you read this, you don't have a")
  242.                               call writeln(mail, "MIME compliant mail user agent, you therefore might have trouble reading")
  243.                               call writeln(mail, "this message. You have been warned ;)")
  244.                               call writeln(mail, "")
  245.                               call writeln(mail, "--Boundary (ID "msgboundary")")
  246.                               call writeln(mail, "")
  247.                               call writeln(mail, "Can't connect to host "word(dirs, dir)" for "xdays" days, your message is therefore")
  248.                               call writeln(mail, "considered undeliverable and returned to you. Sorry for any incon-")
  249.                               call writeln(mail, "venience.")
  250.                               call writeln(mail, "")
  251.                               call writeln(mail, "Your message could not be delivered to:")
  252.                               do rcpt=1 to numrcpts
  253.                                  call writeln(mail, "   "word(xfiledata, rcpt+2)" (via "word(dirs, dir)")")
  254.                               end
  255.                               call writeln(mail, "")
  256.                               call writeln(mail, "--Boundary (ID "msgboundary")")
  257.                               call writeln(mail, "Content-Type: message/RFC822")
  258.                               call writeln(mail, "Content-Transfer-Encoding: 8bit")
  259.                               call writeln(mail, "")
  260.                               if(open(datafile, currdir||"/"word(files2process, 1), r)) then
  261.                               do
  262.                                  /* Skip `From ' line ... */
  263.                                  in = readln(datafile)
  264.                                  /* Copy message */
  265.                                  do while ~eof(datafile)
  266.                                     in = readln(datafile)
  267.                                     call writeln(mail, in)
  268.                                     if(left(upper(in), 11)="PRECEDENCE:") then
  269.                                     do
  270.                                        parse var in precedence ' ' precedence
  271.                                        if((precedence="junk") | (precedence="bulk")) then isjunkmail=1
  272.                                     end
  273.                                     if(isjunkmail & in="") then break
  274.                                  end
  275.                                  call close(datafile)
  276.                                  if(isjunkmail) then call writeln(mail, "[Low priority message, body suppressed.]")
  277.                               end
  278.                               else
  279.                               do
  280.                                  call writeln(mail, "[OOPS?! Can't access "currdir||"/"word(files2process, 1)"?]")
  281.                               end
  282.                               call writeln(mail, "")
  283.                               call writeln(mail, "--Boundary (ID "msgboundary")--")
  284.                               call close(mail)
  285.                               address COMMAND "sendmail >nil: <"mailfile
  286.                               address COMMAND "delete >nil: "mailfile
  287.                               address COMMAND "delete >nil: "currdir||"/"word(files2process, 1)" "currdir||"/"word(files2process, 2)" "fullfilename
  288.                            end
  289.                         end
  290.  
  291.                         if(compare(word(xfiledata, 1), "rnews")==0) then
  292.                         do
  293.                            say "Delete newsbatch (`"word(files2process, 1)"'): `"xfiledata"'."
  294.                            address COMMAND "delete >nil: "currdir||"/"word(files2process, 1)" "currdir||"/"word(files2process, 2)" "fullfilename
  295.                         end
  296.                      end
  297.                      else
  298.                      do
  299.                         say "Skipping: numargs<1 ("words(xfiledata)"); xfiledata: `"xfiledata"'."
  300.                      end
  301.                   end
  302.                end   /* when */
  303.  
  304.                otherwise nop
  305.             end /* select */
  306.          end /* if FILE */
  307.       end /* if C.* */
  308.    end /* do ... end */
  309. end
  310. exit 0
  311.  
  312.  
  313. /* parsexfile(file)
  314.  *
  315.  * Parses a given uucp remote execution file and returns a string con-
  316.  * sisting of the command and, in terms of `rmail' jobs, a blank, the
  317.  * notification address according to the `R' line, and the receipient(s)
  318.  * of this message. Returns an empty string if it isn't sure what kind
  319.  * of file it deals with.
  320.  *
  321.  * (c) 10 May 1994 by Kai 'wusel'áSiering <wusel@hactar.hanse.de>
  322.  */
  323. parsexfile: procedure
  324.    arg filename
  325.  
  326.    out   = ""
  327.    cmd   = ""
  328.    sender= ""
  329.    rcpt  = ""
  330.  
  331.    if(open(file, filename, r)) then
  332.    do
  333.       do forever
  334.          in = readln(file)
  335.          if(eof(file)) then break
  336.  
  337.          parse var in type ' ' rest
  338.  
  339.          select
  340.             when type="R" then sender=rest
  341.             when type="C" then parse var rest cmd ' ' rcpt
  342.             otherwise nop
  343.          end
  344.       end
  345.  
  346.       if(cmd="rmail") then out=cmd sender rcpt
  347.       else out=cmd
  348.  
  349.       close(file)
  350.    end
  351.    else
  352.    do
  353.       say "Can not read from "filename"!"
  354.    end
  355. return out
  356.  
  357.  
  358. /* parsecfile(file)
  359.  *
  360.  * Parses a given uucp control file and returns a string consisting
  361.  * of the datafile, a blank, and the remote execution file. Returns
  362.  * an empty string if it isn't sure what kind of command file it
  363.  * deals with. This routine WILL NOT work with control files for
  364.  * less or more than exactly two files.
  365.  *
  366.  * (c) 10 May 1994 by Kai 'wusel'áSiering <wusel@hactar.hanse.de>
  367.  */
  368. parsecfile: procedure
  369.    arg filename
  370.  
  371.    out = ""
  372.    if(open(file, filename, r)) then
  373.    do
  374.       in = readln(file)
  375.       parse var in direction1 srcfile1 destfile1 user1 flgs1 tmpfile1 mode1
  376.       in = readln(file)
  377.       if ~eof(file) then
  378.       do
  379.          parse var in direction2 srcfile2 destfile2 user2 flgs2 tmpfile2 mode2
  380.          in = readln(file)
  381.          if eof(file) then
  382.          do
  383.             if(left(destfile2, 2) = "X.") then
  384.             do
  385.                if((compare(direction1, direction2)=0) & (compare(direction1, "S")=0)) then
  386.                do
  387.                   out = srcfile1 srcfile2
  388.                end
  389.                else
  390.                do
  391.                   say "Expected two 'S' requests, got '"direction1"'/'"direction2"'."
  392.                end
  393.             end
  394.             else
  395.             do
  396.                say "Unexpected destfile2: "destfile2"."
  397.             end
  398.          end
  399.          else
  400.          do
  401.             say filename" has more than two files!"
  402.          end
  403.       end
  404.       else
  405.       do
  406.          say "Can not read second line in "filename"!"
  407.       end
  408.       close(file)
  409.    end
  410.    else
  411.    do
  412.       say "Can not read from "filename"!"
  413.    end
  414. return out
  415.  
  416.  
  417. /* getreturnaddress(file)
  418.  *
  419.  * Return envelope return address of given file; standard uucp mail format
  420.  * is expected, i. e. the first line MUST read:
  421.  *
  422.  *    "From " address " " date " remote from " remote
  423.  *
  424.  * `address'áis checked to contain `!', `%' or `@', if it lacks all of
  425.  * them, the returnaddress reads `address||"@"||remote', otherwise it's
  426.  * simply `address'. This is both simply and somewhat secure in what we
  427.  * might find in `address'. At least we don't make the addresse worse ;)
  428.  *
  429.  * In case of problems, the returned argument is set to `POSTMASTER',
  430.  * so there need to be no error checking, the returned argument is
  431.  * always a valid address.
  432.  *
  433.  * (c) 11 May 1994 by Kai 'wusel'áSiering <wusel@hactar.hanse.de>
  434.  */
  435.  
  436. getreturnaddress: procedure
  437.    arg filename
  438.  
  439.    addr = "POSTMASTER"
  440.    if(open(file, filename, r)) then
  441.    do
  442.       in = readln(file)
  443.       parse var in first second rest 'remote from 'remote
  444.  
  445.       if(first="From") then
  446.       do
  447.          if((index(second, "!")>0) | (index(second, "%")>0) | (index(second, "@")>0)) then do
  448.             addr=second
  449.          end
  450.          else
  451.          do
  452.             addr=second||"@"||remote
  453.          end
  454.       end
  455.       else
  456.       do
  457.          say "Illegal format, this is no mail?! line=`"in"'"
  458.       end
  459.       close(file)
  460.    end
  461.    else
  462.    do
  463.       say "Can not read from "filename"!"
  464.    end
  465. return addr
  466.