diff -Naur mimedefang-2.51/examples/suggested-minimum-filter-for-windows-clients mimedefang-2.51.nate/examples/suggested-minimum-filter-for-windows-clients --- mimedefang-2.51/examples/suggested-minimum-filter-for-windows-clients 2004-10-26 13:34:33.000000000 -0500 +++ mimedefang-2.51.nate/examples/suggested-minimum-filter-for-windows-clients 2005-03-08 20:48:08.000000000 -0600 @@ -76,6 +76,15 @@ if ($Features{"SpamAssassin"}) { spam_assassin_init()->compile_now(1) if defined(spam_assassin_init()); + if (!%cleanconfig) { $SASpamTester->copy_config(undef,\%cleanconfig); } + + # We have SpamAssassin, so set up an LDAP connection to be used for + # per-user LDAP prefs. + use Net::LDAP; + $ldap = Net::LDAP->new ("XXXXXX-ldap_server", onerror => "warn"); + $ldap->bind; + + # If you want to use auto-whitelisting: # if (defined($SASpamTester)) { # use Mail::SpamAssassin::DBBasedAddrList; @@ -296,7 +305,27 @@ # Only scan messages smaller than 100kB. Larger messages # are extremely unlikely to be spam, and SpamAssassin is # dreadfully slow on very large messages. - my($hits, $req, $names, $report) = spam_assassin_check(); + + # Initialize the variables. + my($hits, $req, $names, $report); + + # Initialize $username to an empty variable + $username = ""; + + # Call subroutine to figure out username from the first recipient + $username = get_username_ldap($Recipients[0]); + + # If we have a username, pass it on to spam_assassin_check, prefixed with 'ldap:' + if ($username) { + # Clear out user configuration for SA + $SASpamTester->copy_config(\%cleanconfig,undef); + # Check for spam. Note that the first variable is blank. + ($hits, $req, $names, $report) = spam_assassin_check("","ldap:$username"); + } else { + # Otherwise, call spam_assassin_check with no arguments. + ($hits, $req, $names, $report) = spam_assassin_check(); + } + my($score); if ($hits < 40) { $score = "*" x int($hits); @@ -346,6 +375,54 @@ # action_rebuild(); } +# Sample subroutine to find username in LDAP, based on e-mail address. +# You'll need to set the LDAP base at the very least. +# Note this uses the $ldap handle from above to connect to the server. + +sub get_username_ldap ($) { + my ($email) = @_; + my ($result) = ""; + my ($entry) = ""; + + my $base = "XXXXXX-dc=example,dc=com"; + + my($email) = lc($email); + $email =~ s/^$//; + + # Search for the actual e-mail address + my $result = $ldap->search( base => $base, + filter => "mail=$email", + scope => "sub", + attrs => [ 'uid' ] + ); + + # If we have results, return them, otherwise, continue. + if ($result->count == "1") { + my $entry = $result->shift_entry; + my @entries = $entry->get_value("uid"); + return ("$entries[0]"); + } else { + # Search for '@domain.com' + my ($junk,$email) = split(/\@/, $email); + my($email) = "@" . "$email"; + my $result = $ldap->search( base => $base, + filter => "mail=$email", + scope => "sub", + attrs => [ 'uid' ] + ); + + # If we have results, pass them back. Otherwise, return 0. + if ($result->count == "1") { + my $entry = $result->shift_entry; + my @entries = $entry->get_value("uid"); + return ("$entries[0]"); + } else { + return 0; + } + } +} + # DO NOT delete the next line, or Perl will complain. 1;