--- nfo/perl/scripts/dispatchmail/bin/dispatchmail 2003/01/22 05:38:44 1.2 +++ nfo/perl/scripts/dispatchmail/bin/dispatchmail 2003/01/22 07:16:51 1.3 @@ -7,10 +7,14 @@ # with the Perl-module "Mail::Audit" # available from CPAN. # -# $Id: dispatchmail,v 1.2 2003/01/22 05:38:44 collector Exp $ +# $Id: dispatchmail,v 1.3 2003/01/22 07:16:51 root Exp $ # # ============================================================ # $Log: dispatchmail,v $ +# Revision 1.3 2003/01/22 07:16:51 root +# + sub traceEntry +# + further refactorings +# # Revision 1.2 2003/01/22 05:38:44 collector # + prepared refactoring to 'dispatchmail' # @@ -24,6 +28,11 @@ use Getopt::Long; +use lib qw( /data/libs/nfo/perl/libs ); +use org::netfrag::shortcuts qw( now ); + + + # - - - - - - - - - - - - - - - - - - - - # options # - - - - - - - - - - - - - - - - - - - - @@ -36,6 +45,9 @@ $opt_base ||= $ENV{HOME}; $opt_base ||= $ENV{PWD}; +my $LOG = 1; # writes reports to logfile +my $VERBOSE = 1; # writes reports to STDOUT +my $TRACE = 1; # writes contents of messages to logfile # - - - - - - - - - - - - - - - - - - - - # @@ -46,17 +58,14 @@ # mail should be delivered to # # - - - - - - - - - - - - - - - - - - - - -#my $HOME = "/home/joko/virtual/joko_mail"; my $USER = $ENV{USER}; my $HOME = $opt_base; my $MAILDIR = "$HOME/Mail"; -my $LOGFILE = "$MAILDIR/recievemail.log"; -my $RULESFILE = "$HOME/.recievemailrc.pm"; -my $LOCKFILE = "$HOME/.procmail.lockfile"; +my $RULESFILE = "$HOME/.dispatchmailrc.pm"; +#my $LOCKFILE = "$HOME/.procmail.lockfile"; +my $LOCKFILE = "$HOME/.dispatchmail.lockfile"; +my $LOGFILE = "$MAILDIR/.dispatchmail.log"; my $DEFAULT = "$MAILDIR/Inbox"; -my $DEBUG = 0; -my $TRACE = 1; -my $VERBOSE = 1; # override settings # override $RULESFILE if given as option on the command line @@ -64,6 +73,9 @@ # change logfile $LOGFILE = "recievemail-emerg.log" if (! -e $MAILDIR); + $USER ||= ''; + + # - - - - - - - - - - - - - - - - - - - - # main @@ -72,12 +84,19 @@ # "jump" into processing of new incoming mail and get a "handler" to this mail my $incoming = Mail::Audit->new; + sub traceEntry { + s2f('-' x 40 . ' TRACE ' . '-' x 10); + s2f("From: " . gchomp($incoming->from)); + s2f("To: " . gchomp($incoming->to)); + s2f("Subject: " . gchomp($incoming->subject)); + s2f('-' x 40 . ' TRACE ' . '-' x 10); + } + + # 0.a. pre flight tracing - if ($TRACE) { - s2f("-" x 80); - s2f("Mail from " . gchomp($incoming->from) . " to " . gchomp($incoming->to)); - s2f("Subject: " . gchomp($incoming->subject)); - } + my $now = now(); + report("$0 running at $now for user '$USER'."); + traceEntry() if $TRACE; # 0.b. pre flight checks @@ -87,25 +106,25 @@ if (! -e $MAILDIR) { my $msg = "delivery failed, base directory $MAILDIR does not exist"; report($msg); - accept_spool(); + forward_delivery(); } # 1. include rules or fallback # check if $RULESFILE exists if (-f $RULESFILE) { - report("loading rules from \"$RULESFILE\""); + report("Loading rules from \"$RULESFILE\"."); require $RULESFILE; } else { #die("$RULESFILE doesn't exist"); - report("configured rulesfile \"$RULESFILE\" doesn't exist"); - accept_spool(); + report("Configured rulesfile \"$RULESFILE\" doesn't exist."); + forward_delivery(); } # 2. export required stuff to rules namespace export_symbols(); # 3. run dispatcher - report("running \"rules::dispatch\""); + report("Running \"rules::dispatch\"."); rules::dispatch(); # 4. dispatcher didn't do anything @@ -139,7 +158,7 @@ # TODO: tracing, debugging print $msg, "\n" if $VERBOSE; - if ($TRACE) { + if ($LOG) { s2f($msg); } @@ -161,12 +180,12 @@ sub jaccept { my $deliver_to = shift; - report("delivering to: $deliver_to"); + report("ACCEPT: $deliver_to"); # check deliver_to path if (! -e $deliver_to) { report("deliver_to path \"$deliver_to\" doesn't exist"); - accept_spool(); + forward_delivery(); return; } @@ -179,6 +198,11 @@ $incoming->accept($path); } + sub forward_delivery { + report("Forwarding delivery to next handler in queue (probably /var/spool/mail)."); + $incoming->accept; + } + # - - - - - - - - - - - - - - - - - - - - # helper functions