--- nfo/perl/scripts/dispatchmail/recieveMail 2002/10/21 12:03:53 1.1 +++ nfo/perl/scripts/dispatchmail/recieveMail 2002/10/21 12:22:55 1.3 @@ -1,28 +1,53 @@ #!/usr/bin/perl # ========================================================================== -# a simple mail filter done in perl with CPAN-module "Mail::Audit" -# more to come ... -# TODO: -# - more sophisticated filtering -# - configuration-comfort (use arrays and hashes, no "matching-code-worm") -# - Html-Gui -# 2001-12-05, amo@netfrag.org +# +# recieveMail v0.03 +# a simple mail filter done in perl with CPAN-module "Mail::Audit" +# +# +# 2002-10-14, joko@netfrag.org +# + $LOGFILE is used now (recievemail.log) +# + tracing (uses $LOGFILE) +# + checks delivery path for existance, +# changes $LOGFILE if needed +# +# 2002-07-17, joko@netfrag.org +# + added filtering by target (destination-routing) +# (looks in "to", "cc" and "bcc") +# +# 2001-12-05, joko@netfrag.org +# + initial internal release +# TODO: +# - more sophisticated filtering +# - configuration-comfort (use arrays and hashes, no "matching-code-worm") +# - Html-Gui to define rules +# - rule base located in LDAP (local delivery routing) +# # ========================================================================== - -# variables have to be declared! use strict; +# don't use warnings; # ---------------------------------------------------------- # declare and initialize some variables # these are mostly directories for routing our mail to -my $HOME = "/home/amo/virtual/home/amo_mail/"; +my $HOME = "/home/joko/virtual/joko_mail"; my $MAILDIR = "$HOME/Mail"; -my $LOGFILE = "$MAILDIR/procmail.log"; +my $LOGFILE = "$MAILDIR/recievemail.log"; my $LOCKFILE = "$HOME/.procmail.lockfile"; -my $DEFAULT = "$MAILDIR/UNSORTED/Current/Inbox"; - +my $DEFAULT = "$MAILDIR/SORTED/misc/Inbox"; +my $DEBUG = 0; +my $TRACE = 1; + +if (! -e $HOME) { + $LOGFILE = "log/recievemail-emerg.log"; + my $msg = "delivery failed, base directory $HOME does not exist"; + open(FH, '>>' . $LOGFILE); + print FH $msg, "\n"; + close(FH); + die($msg); +} # ---------------------------------------------------------- # main @@ -34,15 +59,93 @@ # "jump" into processing of new incoming mail and get a "handler" to this mail my $incoming = Mail::Audit->new; + my $from = $incoming->from; + my $to = $incoming->to; + my $subject = $incoming->subject; + + chomp($from); + chomp($to); + chomp($subject); + + # - - - - - - - - - - - - - - - - - - - - + # tracing mail # - - - - - - - - - - - - - - - - - - - - - # process mail + sub s2f { + my $str = shift; + open(FH, '>>' . $LOGFILE); + print FH $str, "\n"; + close(FH); + } + if ($TRACE) { + s2f("Mail from $from to $to"); + s2f("Subject: $subject"); + } + # - - - - - - - - - - - - - - - - - - - - - if ($incoming->to =~ /ilo\.de/) { - $incoming->accept("$MAILDIR/SORTED/ilo.de/Current/Inbox"); + # processing mail + # - - - - - - - - - - - - - - - - - - - - + + 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/); + return $ok; + } + + sub jaccept { + my $deliver_to = shift; + # TODO: tracing, debugging + if ($TRACE) { + s2f("deliver to: $deliver_to"); + } + $incoming->accept($deliver_to); + } + + # ----- + # source-routing + #if ($incoming->from =~ /root\@smtp\.f7x\.net/i) { + # $incoming->accept("$MAILDIR/SORTED/netfrag.org/Current/status-ns1.f7x.net"); + #} + if ($incoming->from =~ /(root|admin)\@cashew\.netfrag\.org/i) { + jaccept("$MAILDIR/SORTED/netfrag.org/Status/cashew.netfrag.org"); } - if ($incoming->to =~ /web\.de/) { - $incoming->accept("$MAILDIR/SORTED/web.de/Current/Inbox"); + if ($incoming->from =~ /(root|admin)\@quepasa\.netfrag\.org/i) { + jaccept("$MAILDIR/SORTED/netfrag.org/Status/quepasa.netfrag.org"); } + if ($incoming->from =~ /(root|service|netsaint)\@h1\.service\.netfrag\.org/i) { + jaccept("$MAILDIR/SORTED/netfrag.org/Status/h1.service.netfrag.org"); + } + + + # ----- + # source && destination - routing + if ($incoming->from =~ /andreas\.motl\@ilo\.de/ && compareTarget('joko\@netfrag\.org')) { + jaccept("$MAILDIR/SORTED/netfrag.org/Info"); + } + + + # ----- + # destination-routing + my $bool_ilo = ($incoming->to =~ m/ilo\.de/i); + my $bool_ilo_news1 = ($incoming->to =~ m/kritletter\@kbx\.de/i); + my $bool_from_kolumnen_de = ($incoming->to =~ m/kolumnen\.de/i); + my $bool_from_strixner = ($incoming->to =~ m/strixner\@web\.de/i); + if ($bool_ilo || $bool_ilo_news1 || $bool_from_kolumnen_de || $bool_from_strixner) { + jaccept("$MAILDIR/SORTED/ilo.de/Inbox"); + } + + if ($incoming->to =~ /web\.de/i) { + jaccept("$MAILDIR/SORTED/web.de/Current/Inbox"); + } + if ($incoming->to =~ /wor\.net/i) { + jaccept("$MAILDIR/SORTED/wor.net/Current/Inbox"); + } + if ($incoming->to =~ /netfrag\.org/i || $incoming->to =~ /archivists-talk\@yahoogroups\.com/) { + jaccept("$MAILDIR/SORTED/netfrag.org/Inbox"); + } + # - - - - - - - - - - - - - - - - - - - - # the default-handler: simply accept all mails and route them to "/var/spool/mail" @@ -52,4 +155,5 @@ # $incoming->reject; # catch all mails and route them to a "DEFAULT"-inbox -$incoming->accept($DEFAULT); +jaccept($DEFAULT); +