home *** CD-ROM | disk | FTP | other *** search
- #!/bin/perl
- # Archive mail and log archived messages.
-
- ## CONFIGURATION: Target sites that require archiving.
-
- @ARCHSITES = ("sitea", "siteb", "uunet"); ## a joke, son
-
- ## CONFIGURATION: Where will I archive mail if required?
-
- $ARCHFILE = "/usr/tmp/mail-$$"; ## you need to fix this
-
- ## Collect important environment variables.
-
- $SENDER = $ENV{'SENDER'};
- $HEADER = $ENV{'HEADER'};
- $BODY = $ENV{'BODY'};
- $HOSTNAME = $ENV{'HOSTNAME'};
-
- ## Collect important header fields.
-
- $H_From = `header -nf From $HEADER`;
- $H_To = `header -nf To $HEADER`;
- $H_Subject = `header -nf Subject $HEADER`;
- $H_X_Archived = `header -nf X-Archived $HEADER`;
-
- ## Check to see if archiving is required.
-
- $archive = 0;
- foreach (@ARGV) {
- @B = split(/!/);
- next unless @B > 1;
- $dest = $B[$#B - 1];
- ++$archive if grep($_ eq $dest, @ARCHSITES);
- }
-
- ## If this message is already archived elsewhere,
- ## or if we're about to archive it, log it.
-
- if ($archive || $H_X_Archived) {
- &DEL("-r", $SENDER, "-b", "/usr/adm/del-arch.log");
- print DEL $H_From, $H_To, $H_Subject, $H_X_Archived;
- print DEL "\n";
- print DEL "Archived locally as $ARCHFILE" if $archive;
- close(DEL);
- }
-
- ## If no archiving requested, deliver normally.
-
- unless ($archive) {
- foreach (@ARGV) {
- print $_, "\n";
- }
- exit;
- }
-
- ## Send the message with an X-Archived header line added.
- ## Note the "-n" flag -- don't execute this recursively!
-
- &DEL("-n", @ARGV);
- ©MSG;
- close(DEL);
-
- ## Have Deliver write the archive copy like a mailbox.
- ## Do this last, in case the script aborts early.
-
- &DEL("-b", $ARCHFILE);
- ©MSG;
- close(DEL);
-
- ## We are outahere!
-
- exit;
-
- ## Subroutine to spawn a child Deliver process.
-
- sub DEL {
- local($pid);
- $pid = open(DEL, "|-");
- warn "Can't open child deliver: $!" unless defined($pid);
- exec "deliver", @_ if defined($pid) && $pid == 0;
- }
-
- ## Subroutine to send a copy of the message to a child Deliver.
- ## The copy includes an X-Archived header.
-
- sub COPYMSG {
- open(HEADER) || warn "Can't open $HEADER: $!";
- while (<HEADER>) {
- print DEL $_ unless /^$/;
- }
- close(HEADER);
-
- print DEL "X-Archived: host $HOSTNAME, file $ARCHFILE\n";
- print DEL "\n";
-
- open(BODY) || warn "Can't open $BODY: $!";
- while (<BODY>) {
- print DEL $_;
- }
- close(BODY);
- }
-