--- nfo/perl/libs/Mail/Audit/Dispatch.pm 2003/01/22 17:58:21 1.4 +++ nfo/perl/libs/Mail/Audit/Dispatch.pm 2003/01/30 23:20:21 1.5 @@ -5,10 +5,13 @@ # with the Perl-module "Mail::Audit" # available from CPAN. # -# $Id: Dispatch.pm,v 1.4 2003/01/22 17:58:21 root Exp $ +# $Id: Dispatch.pm,v 1.5 2003/01/30 23:20:21 root Exp $ # # ============================================================ # $Log: Dispatch.pm,v $ +# Revision 1.5 2003/01/30 23:20:21 root +# + fixed and enhanced +# # Revision 1.4 2003/01/22 17:58:21 root # + fixed and enhanced many things # - refactored code to Data::Code @@ -39,6 +42,7 @@ use base qw( DesignPattern::Object + DesignPattern::Bridge ); # DesignPattern::Object::Logger @@ -49,7 +53,7 @@ use Data::Code::Symbol qw( export_symbols ); use Data::Storage::Handler::File qw( a2f ); -use org::netfrag::shortcuts qw( now get_chomped ); +use org::netfrag::shortcuts qw( now get_chomped run_cmd ); sub _init { @@ -63,7 +67,7 @@ sub _init_options { my $self = shift; - foreach (qw( user base rules LOG VERBOSE TRACE )) { + foreach (qw( user base rules LOG VERBOSE TRACE mode newsgroup )) { $self->{options}->{$_} = $self->{$_}; } } @@ -89,7 +93,8 @@ sub _init_settings_paths { my $self = shift; $self->{settings}->{MAILDIR} = "$self->{settings}->{HOME}/Mail"; - $self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc.pm"; + #$self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc.pm"; + $self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc"; #$self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.procmail.lockfile"; $self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.dispatchmail.lockfile"; $self->{settings}->{LOGFILE} = "$self->{settings}->{MAILDIR}/.dispatchmail.log"; @@ -113,23 +118,24 @@ # main # - - - - - - - - - - - - - - - - - - - - - sub traceEntry { - my $self = shift; - $self->appendLog('-' x 40 . ' TRACE ' . '-' x 10); - $self->appendLog("From: " . get_chomped($self->{incoming}->from)); - $self->appendLog("To: " . get_chomped($self->{incoming}->to)); - $self->appendLog("Subject: " . get_chomped($self->{incoming}->subject)); - $self->appendLog('-' x 40 . ' TRACE ' . '-' x 10); - } - - -sub _run { - +sub run { my $self = shift; + $self->{options}->{mode} ||= 'recieve'; + my $call = '_' . $self->{options}->{mode}; + # "jump" into processing of new incoming mail and get a "handler" to this mail $self->{incoming} = Mail::Audit->new(); + my $result = $self->$call(@_); + return $result; + +} + +sub _recieve { + + my $self = shift; + # 0.a. pre flight tracing my $now = now(); $self->report("$now - $0 running for user '$self->{settings}->{USER}'."); @@ -141,7 +147,7 @@ # check if $self->{settings}->{HOME} exists if (! -e $self->{settings}->{MAILDIR}) { - my $msg = "Delivery failed, base directory $self->{settings}->{MAILDIR} does not exist."; + my $msg = "ERROR: Delivery failed, base directory '$self->{settings}->{MAILDIR}' does not exist."; $self->report($msg); $self->forward_delivery(); } @@ -149,7 +155,7 @@ # 1. include rules or fallback # check if $self->{settings}->{RULESFILE} exists if (-f $self->{settings}->{RULESFILE}) { - $self->report("Loading rules from \"$self->{settings}->{RULESFILE}\"."); + $self->report("RULES: Loading from \"$self->{settings}->{RULESFILE}\"."); require $self->{settings}->{RULESFILE}; } else { #die("$self->{settings}->{RULESFILE} doesn't exist"); @@ -158,11 +164,11 @@ } # 2. export required stuff to rules namespace - my @symbols = qw( jaccept report compareTarget ); + my @symbols = qw( jaccept report compareTarget accept copy ignore ); export_symbols(\@symbols, 'rules'); # 3. run dispatcher - $self->report("Running \"rules::dispatch\"."); + $self->report("RULES: Running Perl sub \"rules::dispatch\"."); rules::dispatch($self); # 4. dispatcher didn't do anything @@ -179,15 +185,29 @@ } -sub run { + +sub _mail2news { my $self = shift; - return $self->_run(@_); + $self->report("MAIL2NEWS: $self->{options}->{newsgroup}"); + my $plugin = 'Newsgate'; + $self->load($plugin); + $self->$plugin($self->{options}->{newsgroup}); } + # - - - - - - - - - - - - - - - - - - - - # tracing & reporting # - - - - - - - - - - - - - - - - - - - - + sub traceEntry { + my $self = shift; + $self->appendLog('-' x 40 . ' TRACE ' . '-' x 10); + $self->appendLog("From: " . get_chomped($self->{incoming}->from)); + $self->appendLog("To: " . get_chomped($self->{incoming}->to)); + $self->appendLog("Subject: " . get_chomped($self->{incoming}->subject)); + $self->appendLog('-' x 40 . ' TRACE ' . '-' x 10); + } + sub appendLog { my $self = shift; my $msg = shift; @@ -220,6 +240,20 @@ return $ok; } + sub accept_spool { + my $self = shift; + my $path = "/var/spool/mail/$self->{settings}->{USER}"; + $self->report("defaulting to spool delivery ($path)"); + $self->{incoming}->accept($path); + } + + sub forward_delivery { + my $self = shift; + $self->report("Forwarding delivery to next handler in queue (probably /var/spool/mail)."); + return $self->{incoming}->accept; + } + + sub jaccept { my $self = shift; my $deliver_to = shift; @@ -228,26 +262,50 @@ # check deliver_to path if (! -e $deliver_to) { - $self->report("deliver_to path \"$deliver_to\" doesn't exist"); - $self->forward_delivery(); - return; + my $good = 0; + $self->report("ERROR: TARGET Path/File doesn't exist."); + if ($self->{settings}->{AUTOCREATE_FOLDERS}) { + my $cmd = "touch $deliver_to"; + $self->report("TARGET: AUTOCREATE_FOLDERS is enabled: touching TARGET."); + #if (mkdir $deliver_to) { + run_cmd($cmd); + # re-test TARGET - for existance now + if (-e $deliver_to) { + $good = 1; + } else { + $self->report("ERROR: TARGET creation failed (command was: '$cmd')."); + } + } + if (!$good) { + $self->forward_delivery(); + return; + } } - $self->{incoming}->accept($deliver_to); + return $self->{incoming}->accept($deliver_to); } - sub accept_spool { + + sub accept { my $self = shift; - my $path = "/var/spool/mail/$self->{settings}->{USER}"; - $self->report("defaulting to spool delivery ($path)"); - $self->{incoming}->accept($path); + return $self->jaccept(@_); } - sub forward_delivery { + sub copy { my $self = shift; - $self->report("Forwarding delivery to next handler in queue (probably /var/spool/mail)."); - $self->{incoming}->accept; + my $plugin = shift; + my $deliver_to = shift; + + $self->report("COPY: $plugin: $deliver_to"); + + $self->load($plugin); + return $self->$plugin($deliver_to); } + sub ignore { + my $self = shift; + $self->report("IGNORE"); + return $self->{incoming}->ignore; + } 1;