--- nfo/perl/libs/Mail/Audit/Dispatch.pm 2003/01/22 07:45:20 1.1 +++ nfo/perl/libs/Mail/Audit/Dispatch.pm 2003/01/22 07:54:24 1.2 @@ -5,10 +5,13 @@ # with the Perl-module "Mail::Audit" # available from CPAN. # -# $Id: Dispatch.pm,v 1.1 2003/01/22 07:45:20 root Exp $ +# $Id: Dispatch.pm,v 1.2 2003/01/22 07:54:24 joko Exp $ # # ============================================================ # $Log: Dispatch.pm,v $ +# Revision 1.2 2003/01/22 07:54:24 joko +# + replaced global variables with class-variables +# # Revision 1.1 2003/01/22 07:45:20 root # + initial check-in - refactored from 'dispatchmail' # @@ -38,23 +41,30 @@ my $LOG = 1; # writes reports to logfile -my $VERBOSE = 1; # writes reports to STDOUT -my $TRACE = 1; # writes contents of messages to logfile +my $self->{options}->{VERBOSE} = 1; # writes reports to STDOUT +my $self->{options}->{TRACE} = 1; # writes contents of messages to logfile sub _init { my $self = shift; $self->_init_options(); + $self->_override_options(); $self->_init_settings(); $self->_override_settings(); + $self->_run(); } sub _init_options { my $self = shift; - $self->{options}->{base} = $self->{base}; + foreach (qw( base rules LOG VERBOSE TRACE )) { + $self->{options}->{$_} = $self->{$_}; + } +} + +sub _override_options { + my $self = shift; $self->{options}->{base} ||= $ENV{HOME}; $self->{options}->{base} ||= $ENV{PWD}; - $self->{options}->{rules} = $self->{rules}; } # - - - - - - - - - - - - - - - - - - - - @@ -66,7 +76,7 @@ # mail should be delivered to # # - - - - - - - - - - - - - - - - - - - - -sub _init_paths { +sub _init_settings { my $self = shift; $self->{settings}->{USER} = $ENV{USER}; $self->{settings}->{HOME} = $self->{options}->{base}; @@ -74,13 +84,13 @@ $self->{settings}->{RULESFILE} = "$HOME/.dispatchmailrc.pm"; #$self->{settings}->{LOCKFILE} = "$HOME/.procmail.lockfile"; $self->{settings}->{LOCKFILE} = "$HOME/.dispatchmail.lockfile"; - $self->{settings}->{LOGFILE} = "$MAILDIR/.dispatchmail.log"; - $self->{settings}->{DEFAULT} = "$MAILDIR/Inbox"; + $self->{settings}->{LOGFILE} = "$self->{settings}->{MAILDIR}/.dispatchmail.log"; + $self->{settings}->{DEFAULT} = "$self->{settings}->{MAILDIR}/Inbox"; } sub _override_settings { my $self = shift; - # override $RULESFILE if given as option on the command line + # override $self->{settings}->{RULESFILE} if given as option on the command line $self->{settings}->{RULESFILE} = $self->{options}->{rules} if $self->{options}->{rules}; # change logfile @@ -96,14 +106,20 @@ # main # - - - - - - - - - - - - - - - - - - - - +sub _run { + my $self = shift; + # "jump" into processing of new incoming mail and get a "handler" to this mail - my $incoming = Mail::Audit->new; + $self->{incoming} = Mail::Audit->new(); + +} sub traceEntry { + my $self = shift; s2f('-' x 40 . ' TRACE ' . '-' x 10); - s2f("From: " . gchomp($incoming->from)); - s2f("To: " . gchomp($incoming->to)); - s2f("Subject: " . gchomp($incoming->subject)); + s2f("From: " . gchomp($self->{incoming}->from)); + s2f("To: " . gchomp($self->{incoming}->to)); + s2f("Subject: " . gchomp($self->{incoming}->subject)); s2f('-' x 40 . ' TRACE ' . '-' x 10); } @@ -111,27 +127,27 @@ # 0.a. pre flight tracing my $now = now(); report("$0 running at $now for user '$USER'."); - traceEntry() if $TRACE; + traceEntry() if $self->{options}->{TRACE}; # 0.b. pre flight checks # TODO: check if $HOME is empty # check if $HOME exists - if (! -e $MAILDIR) { - my $msg = "delivery failed, base directory $MAILDIR does not exist"; + if (! -e $self->{settings}->{MAILDIR}) { + my $msg = "delivery failed, base directory $self->{settings}->{MAILDIR} does not exist"; report($msg); forward_delivery(); } # 1. include rules or fallback - # check if $RULESFILE exists - if (-f $RULESFILE) { - report("Loading rules from \"$RULESFILE\"."); - require $RULESFILE; + # check if $self->{settings}->{RULESFILE} exists + if (-f $self->{settings}->{RULESFILE}) { + report("Loading rules from \"$self->{settings}->{RULESFILE}\"."); + require $self->{settings}->{RULESFILE}; } else { - #die("$RULESFILE doesn't exist"); - report("Configured rulesfile \"$RULESFILE\" doesn't exist."); + #die("$self->{settings}->{RULESFILE} doesn't exist"); + report("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist."); forward_delivery(); } @@ -146,13 +162,13 @@ report("dispatcher could not apply any filter, using default delivery"); # the default-handler: simply accept all mails and route them to "/var/spool/mail" - # $incoming->accept(); + # $self->{incoming}->accept(); # if you want to reject all mails coming through to here, do a ... - # $incoming->reject; + # $self->{incoming}->reject; # catch all mails and route them to a "DEFAULT"-inbox - jaccept($DEFAULT); + jaccept($self->{settings}->{DEFAULT}); @@ -163,7 +179,7 @@ # - - - - - - - - - - - - - - - - - - - - sub s2f { my $str = shift; - open(FH, '>>' . $LOGFILE); + open(FH, '>>' . $self->{settings}->{LOGFILE}); print FH $str, "\n"; close(FH); } @@ -172,7 +188,7 @@ my $msg = shift; # TODO: tracing, debugging - print $msg, "\n" if $VERBOSE; + print $msg, "\n" if $self->{options}->{VERBOSE}; if ($LOG) { s2f($msg); } @@ -186,9 +202,9 @@ sub compareTarget { my $pattern = shift; my $ok = 0; - $ok = 1 if ($incoming->to =~ m/$pattern/); - $ok = 1 if ($incoming->cc =~ m/$pattern/); - $ok = 1 if ($incoming->bcc =~ m/$pattern/); + $ok = 1 if ($self->{incoming}->to =~ m/$pattern/); + $ok = 1 if ($self->{incoming}->cc =~ m/$pattern/); + $ok = 1 if ($self->{incoming}->bcc =~ m/$pattern/); return $ok; } @@ -204,18 +220,18 @@ return; } - $incoming->accept($deliver_to); + $self->{incoming}->accept($deliver_to); } sub accept_spool { my $path = "/var/spool/mail/$USER"; report("defaulting to spool delivery ($path)"); - $incoming->accept($path); + $self->{incoming}->accept($path); } sub forward_delivery { report("Forwarding delivery to next handler in queue (probably /var/spool/mail)."); - $incoming->accept; + $self->{incoming}->accept; } @@ -245,8 +261,8 @@ *{"rules::report"} = get_coderef('main', 'report'); *{"rules::compareTarget"} = get_coderef('main', 'compareTarget'); } - $rules::MAILDIR = $MAILDIR; - $rules::incoming = $incoming; + $rules::MAILDIR = $self->{settings}->{MAILDIR}; + $rules::incoming = $self->{incoming}; } sub gchomp { @@ -254,3 +270,5 @@ chomp($str); return $str; } + +1;