| 1 | #!/usr/bin/perl | 
| 2 |  | 
| 3 | # ========================================================================== | 
| 4 | # | 
| 5 | # recieveMail v0.04 | 
| 6 | #   a simple mail filter done in perl with CPAN-module "Mail::Audit" | 
| 7 | # | 
| 8 | # | 
| 9 | # 2002-11-09, joko@netfrag.org | 
| 10 | #   + recieveMail now can run globally (/etc/mail/smrsh!) | 
| 11 | #     + $HOME is taken from $ENV{HOME} or $ENV{PWD} | 
| 12 | #     + rules are taken from $HOME/Mail/.rules.pm | 
| 13 | #     + fallback mechanism(s) | 
| 14 | #   + bugfixes | 
| 15 | # | 
| 16 | # 2002-10-14, joko@netfrag.org | 
| 17 | #   + $LOGFILE is used now (recievemail.log) | 
| 18 | #   + tracing (uses $LOGFILE) | 
| 19 | #   + checks delivery path for existance, | 
| 20 | #     changes $LOGFILE if needed | 
| 21 | # | 
| 22 | # 2002-07-17, joko@netfrag.org | 
| 23 | #   + added filtering by target (destination-routing) | 
| 24 | #     (looks in "to", "cc" and "bcc") | 
| 25 | # | 
| 26 | # 2001-12-05, joko@netfrag.org | 
| 27 | #   + initial internal release | 
| 28 | # | 
| 29 | # | 
| 30 | # TODO: | 
| 31 | #   - more sophisticated filtering | 
| 32 | #   - configuration-comfort (use perl-arrays and -hashes for rule-declaration) | 
| 33 | #   - Html-Gui to add/edit/remove rules | 
| 34 | #   - rule base located in LDAP (local delivery routing) | 
| 35 | #   - completely hide away regex-stuff and provide simpler wildcarding | 
| 36 | #     - hide needed quoting of dots (.) and ats (@) in addresses | 
| 37 | #     - provide: beginsWith(string), endsWith(string), beginsAt(string, pos|regex) | 
| 38 | #       - this could become a CPAN-module sometimes (?): | 
| 39 | #       - "String"-Object to be inherited from gives these methods to you | 
| 40 | #       - examples: | 
| 41 | #         - routeTo("mbox:/path/to/mbox") if $to->beginsWith("hello"); | 
| 42 | #         - routeTo("fax:+4930123456")    if $subject->contains("gatefax"); | 
| 43 | #   - metadata: | 
| 44 | #     - add some info about the context we are running in: | 
| 45 | #       - console | 
| 46 | #       - sendmail/normal | 
| 47 | #       - sendmail/smrsh | 
| 48 | #     - add some info about the user we are doing this for: | 
| 49 | #       - username | 
| 50 | #       - home-directory | 
| 51 | # | 
| 52 | # WISHLIST: | 
| 53 | #   - format basic log-output similar to procmail.log | 
| 54 | #   - introduce some "extended logging" including the chosen routing path | 
| 55 | # | 
| 56 | # ========================================================================== | 
| 57 |  | 
| 58 | use strict; | 
| 59 | # don't use warnings; | 
| 60 |  | 
| 61 | use Mail::Audit; | 
| 62 | use Data::Dumper; | 
| 63 | use Getopt::Long; | 
| 64 |  | 
| 65 |  | 
| 66 | # - - - - - - - - - - - - - - - - - - - - | 
| 67 | #               options | 
| 68 | # - - - - - - - - - - - - - - - - - - - - | 
| 69 | my $opt_base      = ''; | 
| 70 | my $opt_rulesfile = ''; | 
| 71 | GetOptions( | 
| 72 | 'base=s'  => \$opt_base, | 
| 73 | 'rules=s' => \$opt_rulesfile, | 
| 74 | ); | 
| 75 | $opt_base ||= $ENV{HOME}; | 
| 76 | $opt_base ||= $ENV{PWD}; | 
| 77 |  | 
| 78 |  | 
| 79 | # - - - - - - - - - - - - - - - - - - - - | 
| 80 | # | 
| 81 | #                targets | 
| 82 | # | 
| 83 | #  declare and initialize some variables | 
| 84 | #      these are mostly base paths | 
| 85 | #      mail should be delivered to | 
| 86 | # | 
| 87 | # - - - - - - - - - - - - - - - - - - - - | 
| 88 | #my $HOME = "/home/joko/virtual/joko_mail"; | 
| 89 | my $USER      = $ENV{USER}; | 
| 90 | my $HOME      = $opt_base; | 
| 91 | my $MAILDIR   = "$HOME/Mail"; | 
| 92 | my $LOGFILE   = "$MAILDIR/recievemail.log"; | 
| 93 | my $RULESFILE = "$MAILDIR/.rules.pm"; | 
| 94 | my $LOCKFILE  = "$HOME/.procmail.lockfile"; | 
| 95 | my $DEFAULT   = "$MAILDIR/Inbox"; | 
| 96 | my $DEBUG   = 0; | 
| 97 | my $TRACE   = 1; | 
| 98 | my $VERBOSE = 1; | 
| 99 |  | 
| 100 | # override settings | 
| 101 | # override $RULESFILE if given as option on the command line | 
| 102 | $RULESFILE = $opt_rulesfile if ($opt_rulesfile); | 
| 103 | # change logfile | 
| 104 | $LOGFILE = "recievemail-emerg.log" if (! -e $MAILDIR); | 
| 105 |  | 
| 106 |  | 
| 107 | # - - - - - - - - - - - - - - - - - - - - | 
| 108 | #                 main | 
| 109 | # - - - - - - - - - - - - - - - - - - - - | 
| 110 |  | 
| 111 | # "jump" into processing of new incoming mail and get a "handler" to this mail | 
| 112 | my $incoming = Mail::Audit->new; | 
| 113 |  | 
| 114 | # 0.a. pre flight tracing | 
| 115 | if ($TRACE) { | 
| 116 | s2f("Mail from " . gchomp($incoming->from) . " to " . gchomp($incoming->to)); | 
| 117 | s2f("Subject: " . gchomp($incoming->subject)); | 
| 118 | } | 
| 119 |  | 
| 120 | # 0.b. pre flight checks | 
| 121 |  | 
| 122 | # TODO: check if $HOME is empty | 
| 123 |  | 
| 124 | # check if $HOME exists | 
| 125 | if (! -e $MAILDIR) { | 
| 126 | my $msg = "delivery failed, base directory $MAILDIR does not exist"; | 
| 127 | report($msg); | 
| 128 | accept_spool(); | 
| 129 | } | 
| 130 |  | 
| 131 | # 1. include rules or fallback | 
| 132 | # check if $RULESFILE exists | 
| 133 | if (-f $RULESFILE) { | 
| 134 | report("loading rules from \"$RULESFILE\""); | 
| 135 | require $RULESFILE; | 
| 136 | } else { | 
| 137 | #die("$RULESFILE doesn't exist"); | 
| 138 | report("configured rulesfile \"$RULESFILE\" doesn't exist"); | 
| 139 | accept_spool(); | 
| 140 | } | 
| 141 |  | 
| 142 | # 2. export required stuff to rules namespace | 
| 143 | export_symbols(); | 
| 144 |  | 
| 145 | # 3. run dispatcher | 
| 146 | report("running \"rules::dispatch\""); | 
| 147 | rules::dispatch(); | 
| 148 |  | 
| 149 | # 4. dispatcher didn't do anything | 
| 150 | report("dispatcher could not apply any filter, using default delivery"); | 
| 151 |  | 
| 152 | # the default-handler: simply accept all mails and route them to "/var/spool/mail" | 
| 153 | # $incoming->accept(); | 
| 154 |  | 
| 155 | # if you want to reject all mails coming through to here, do a ... | 
| 156 | # $incoming->reject; | 
| 157 |  | 
| 158 | # catch all mails and route them to a "DEFAULT"-inbox | 
| 159 | jaccept($DEFAULT); | 
| 160 |  | 
| 161 |  | 
| 162 |  | 
| 163 |  | 
| 164 |  | 
| 165 | # - - - - - - - - - - - - - - - - - - - - | 
| 166 | #          tracing & reporting | 
| 167 | # - - - - - - - - - - - - - - - - - - - - | 
| 168 | sub s2f { | 
| 169 | my $str = shift; | 
| 170 | open(FH, '>>' . $LOGFILE); | 
| 171 | print FH $str, "\n"; | 
| 172 | close(FH); | 
| 173 | } | 
| 174 |  | 
| 175 | sub report { | 
| 176 | my $msg = shift; | 
| 177 | # TODO: tracing, debugging | 
| 178 |  | 
| 179 | print $msg, "\n" if $VERBOSE; | 
| 180 | if ($TRACE) { | 
| 181 | s2f($msg); | 
| 182 | } | 
| 183 |  | 
| 184 | } | 
| 185 |  | 
| 186 | # - - - - - - - - - - - - - - - - - - - - | 
| 187 | #             processing mail | 
| 188 | # - - - - - - - - - - - - - - - - - - - - | 
| 189 |  | 
| 190 | sub compareTarget { | 
| 191 | my $pattern = shift; | 
| 192 | my $ok = 0; | 
| 193 | $ok = 1 if ($incoming->to =~ m/$pattern/); | 
| 194 | $ok = 1 if ($incoming->cc =~ m/$pattern/); | 
| 195 | $ok = 1 if ($incoming->bcc =~ m/$pattern/); | 
| 196 | return $ok; | 
| 197 | } | 
| 198 |  | 
| 199 | sub jaccept { | 
| 200 | my $deliver_to = shift; | 
| 201 |  | 
| 202 | report("delivering to: $deliver_to"); | 
| 203 |  | 
| 204 | # check deliver_to path | 
| 205 | if (! -e $deliver_to) { | 
| 206 | report("deliver_to path \"$deliver_to\" doesn't exist"); | 
| 207 | accept_spool(); | 
| 208 | return; | 
| 209 | } | 
| 210 |  | 
| 211 | $incoming->accept($deliver_to); | 
| 212 | } | 
| 213 |  | 
| 214 | sub accept_spool { | 
| 215 | my $path = "/var/spool/mail/$USER"; | 
| 216 | report("defaulting to spool delivery ($path)"); | 
| 217 | $incoming->accept($path); | 
| 218 | } | 
| 219 |  | 
| 220 |  | 
| 221 | # - - - - - - - - - - - - - - - - - - - - | 
| 222 | #             helper functions | 
| 223 | # - - - - - - - - - - - - - - - - - - - - | 
| 224 |  | 
| 225 | sub get_coderef { | 
| 226 | my $codepack = shift; | 
| 227 | my $method = shift; | 
| 228 | $codepack || return '[error]'; | 
| 229 | $method ||= ''; | 
| 230 | $method && ($codepack .= '::'); | 
| 231 | return eval '\&' . $codepack . $method . ';'; | 
| 232 | } | 
| 233 |  | 
| 234 | sub export_symbols { | 
| 235 | #  my $callpack = 'rules'; | 
| 236 | #  my @EXPORT = qw( incoming subject MAILDIR jaccept ); | 
| 237 | #  foreach my $sym (@EXPORT) { | 
| 238 | no strict 'refs'; | 
| 239 | #    *{"${callpack}::$sym"} = get_coderef('main', $sym); | 
| 240 | #  } | 
| 241 | { | 
| 242 | no strict 'refs'; | 
| 243 | *{"rules::jaccept"}       = get_coderef('main', 'jaccept'); | 
| 244 | *{"rules::report"}        = get_coderef('main', 'report'); | 
| 245 | *{"rules::compareTarget"} = get_coderef('main', 'compareTarget'); | 
| 246 | } | 
| 247 | $rules::MAILDIR  = $MAILDIR; | 
| 248 | $rules::incoming = $incoming; | 
| 249 | } | 
| 250 |  | 
| 251 | sub gchomp { | 
| 252 | my $str = shift; | 
| 253 | chomp($str); | 
| 254 | return $str; | 
| 255 | } |