4 # read sendmail module config
5 $p = -l $0 ? readlink($0) : $0;
6 $p =~ /^(.*)\/[^\/]+$/;
7 if (open(CONF, "$1/config")) {
15 if (!$config{'sendmail_path'}) {
16 # Make some guesses about sendmail
17 if (-x "/usr/sbin/sendmail") {
18 %config = ( 'sendmail_path' => '/usr/sbin/sendmail' );
20 elsif (-x "/usr/lib/sendmail") {
21 %config = ( 'sendmail_path' => '/usr/lib/sendmail' );
24 die "Failed to find sendmail or config file";
27 # read headers and body
32 if (/^(\S+):\s+(.*)/) {
39 # Single line with a . confuses SMTP
42 elsif ($_ eq ".\r\n") {
50 # read the filter file
51 if (open(FILTER, $ARGV[0])) {
54 if (/^(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/) {
55 push(@filter, [ $1, $2, $3, $4 ]);
57 elsif (/^(\S+)\s+(\S+)$/) {
58 push(@filter, [ $1, $2 ]);
64 print STDERR "Filter file $ARGV[0] does not exist!\n";
68 # run the filter to find the first matching rule
69 open(LOG, ">>$ARGV[0].log");
70 foreach $f (@filter) {
71 local $field = $f->[2] eq 'body' ? $body : $header{$f->[2]};
74 $st = ($field !~ /$f->[3]/i);
76 elsif ($f->[0] == 1) {
77 $st = ($field =~ /$f->[3]/i);
79 elsif ($f->[0] == 2) {
84 if ($f->[1] =~ /^\//) {
86 open(MAIL, ">>$f->[1]") || die "Failed to open $f->[1] ; $!";
90 # Forward to another address
91 open(MAIL, "|$config{'sendmail_path'} ".
97 $now = localtime(time());
98 print LOG "[$now] [$header{'from'}] [",join(" ",@$f),"]\n";