Mailing List CGatePro@mail.stalker.com Message #92816
From: James Reynolds <james123@scl.utah.edu>
Subject: Re: Approval perl script
Date: Tue, 2 Oct 2007 16:59:04 -0600
To: CommuniGate Pro Discussions <CGatePro@mail.stalker.com>
I've modified the script again.  This version will only send one email when something shows up in the approval folder so that the inbox isn't full of messages after vacation.

Enjoy.

James

#!/usr/bin/perl

###################################################
# Program Name: approval_check
# Created: Nov 16, 2001
# Modified: February 5, 2002 (documentation added)
# Modified: October 1, 2007 (supported multiple lists) by James Reynolds james@scl.utah.edu
# Modified: October 2, 2007 (sends only one email) by James Reynolds james@scl.utah.edu
# Author: Russ Pagenkopf (jnrp@uas.alaska.edu)
# Purpose: This program is used to check if there are messages awaiting
#          list-mom approval. If so it sends an email to an external
#          account with notification.
#
# Special notes: All paths and addresses are hardcoded. Be careful to check
#          and make sure they match actual paths on your local machine
#          and that the remote email account is added correctly.
#
#          Also note that this script will only work against a mail folder
#          that is in the mbox format,
#
#          Last, note that this script must be triggered externally. I use
#          cron with the following entry.
#         #          * 9,17 * * * /root/scripts/approval_check
#
#          You should be able to run this program from anywhere you want
#          with the appropriate privileges. I'm a little lazy since the box
#          has only two accounts, mine and root. ;)
#
# Yes, I believe in quick, dirty and ugly if it gets the job done! :-)
#
# Freely released to the public domain, use at your own risk.
#
###################################################

my $debug = 0;

my $logfile = "/var/log/approval_check";

# If each list pretty much has the same info, you can set it all here.

my $account_name = 'someone';
my $email_to = 'you@example.com'; # For multiple emails, just comma separate them.
my $email_from = 'no-reply@example.com';
my $account_path = '/var/CommuniGate/Accounts';

# Or if each list requires different settings, you can set them individually
# below (instead of using the above variables).

@lists = (
  { list_name    => 'list1', },
  { list_name    => 'list2', email_to => 'someoneelse@example.com' },
);

foreach $list ( @lists ) {
  my %list1 = %$list;
  my $list_name = $list1{'list_name'};
  my $account_name = $list1{'account_name'} if defined $list1{'account_name'};
  my $email_to = $list1{'email_to'} if defined $list1{'email_to'};
  my $email_from = $list1{'email_from'} if defined $list1{'email_from'};
  my $account_path = $list1{'account_path'} if defined $list1{'account_path'};

  check_list ( $list_name, $account_name, $email_to, $email_from, $account_path );
}

#####
# This last section was added for my own peace of mind. All it does is append
# to a file the time that this script is run.
#
# If the log file doesn't exist, this section does nothing.
#####

if ( -e $logfile ) {
  open( TRACK, ">>$logfile" );
  $now = localtime;
  print TRACK "$now\n"; # . " $mailbox_path\n";
  close( TRACK );
}

###################################################
sub check_list {
  my ( $list_name, $account_name, $email_to, $email_from, $account_path ) = @_;

  print "Checking $list_name\n" if $debug;

  my $mailbox_path = "$account_path/$account_name.macnt/$list_name.folder/approval.mbox";
  my $list_trigger_file = "$account_path/$account_name.macnt/$list_name.folder/.approval_mail_sent";

  # sanity check

  die "$mailbox_path is missing!" if ! -e $mailbox_path;

  #####
  # check if the size of the approval mbox is > zero.
  #####

  if ( -s $mailbox_path ) {

    print "$mailbox_path has mail.\n" if $debug;

    my $send_email = 1;
    if ( -e $list_trigger_file ) {
      my $trigger_mod_date = ( stat $list_trigger_file )[9];
      my $mailbox_mod_date = ( stat $mailbox_path) [9];
      $send_email = 0 if ( $trigger_mod_date > $mailbox_mod_date );
    }
    if ( $send_email ) {
      create_email( $email_to, $email_from, $list_name );
      unlink $list_trigger_file if -e $list_trigger_file;
      open( TRIGGER, ">$list_trigger_file" );
      close( TRIGGER );
    }

  } else {

    unlink $list_trigger_file if -e $list_trigger_file;

    print "$mailbox_path is empty.\n" if $debug;

  }

}

sub create_email {
  my ( $email_to, $email_from, $list_name ) = @_;

  my $new_email = "/var/CommuniGate/Submitted/approval.sub";
  my $tmp_email = "/var/CommuniGate/Submitted/approval.tmp";
  my $max_secs_to_wait = 100;

  #####
  # We create the initial mail file with a .tmp extension instead of
  # a .sub extension to make sure CGPro doesn't try to process it
  # before we are done creating it.
  #####

  unlink $tmp_email if -e $tmp_email;

  open( MAIL, ">$tmp_email" ) or die "Couldn't write to $tmp_email: $!";

  print MAIL "X-Priority: 1 (Highest)\n";
  print MAIL "To: $email_to\n";

  #####
  # Enter the local sending email address here.
  # Notes: The @ symbol must be preceeded by a '\' .
  #
  #        The account 'automated' doesn't really exist on my system and the
  #        user must know this, else if they try to reply, their email will
  #        bounce with 'unknown account'.
  #####

  print MAIL "From: $email_from\n";
  print MAIL "Subject: Approval needed: $list_name\n\n";

  print MAIL "Message is awaiting your approval from: $list_name.\n";

  close( MAIL );

  #####
  # Change the newly created file extension from .tmp to .sub so that CGPro
  # will process the file.
  #####
  my $timer = 0;
  while ( -e $new_email ) {
    sleep 1; # wait for CGP to process previous email.
    if ( $timer > $max_secs_to_wait ) {
      die "Is CGP running? $new_email has not been processed after waiting $max_secs_to_wait seconds.";
    }
   
  }
  rename( $tmp_email , $new_email ) or die "Couldn't rename $tmp_email to $new_email.\n";

  print "Mail sent to $email_to.\n" if $debug;
}



Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to Listmaster