--- /root/unstable/mailscanner/mailscanner/bin/MailScanner/CustomConfig.pm	Sun Nov 24 12:16:54 2002
+++ CustomConfig.pm	Sun Jan 12 17:33:59 2003
@@ -99,5 +99,136 @@
 #  #return 1;
 #}
 
+#
+# This set of functions provides per-domain simple spam whitelists and
+# blacklists. Each of the 2 directories set below contains 1 file for
+# each domain, with the domain name being the filename. The file contains
+# a list of entries, 1 per line, each one either being a full address:
+#    user@domain.com
+# or an entire domain:
+#    domain.com
+# The addresses contained in the file for a domain make up the entire
+# spam whitelist or blacklist for that domain.
+#
+
+# Set these to be the location of your whitelist files and blacklist files
+my $WhitelistDir = '/etc/MailScanner/spam.bydomain/whitelist';
+my $BlacklistDir = '/etc/MailScanner/spam.bydomain/blacklist';
+
+
+use DirHandle;
+use FileHandle;
+
+my(%Whitelist, %Blacklist);
+
+#
+# Initialise by-domain spam whitelist and blacklist
+#
+sub InitByDomainSpamWhitelist {
+  MailScanner::Log::InfoLog("Starting up by-domain spam whitelist, " .
+                            "reading from %s", $WhitelistDir);
+  my $domains = CreateByDomainList($WhitelistDir, \%Whitelist);
+  MailScanner::Log::InfoLog("Read whitelist for %d domains", $domains);
+}
+
+sub InitByDomainSpamBlacklist {
+  MailScanner::Log::InfoLog("Starting up by-domain spam blacklist, " .
+                            "reading from %s", $BlacklistDir);
+  my $domains = CreateByDomainList($BlacklistDir, \%Blacklist);
+  MailScanner::Log::InfoLog("Read blacklist for %d domains", $domains);
+}
+
+
+#
+# Lookup a message in the by-domain whitelist and blacklist
+#
+sub ByDomainSpamWhitelist {
+  my($message) = @_;
+
+  return LookupByDomainList($message, \%Whitelist);
+}
+
+sub ByDomainSpamBlacklist {
+  my($message) = @_;
+
+  return LookupByDomainList($message, \%Blacklist);
+}
+
+
+#
+# Close down the by-domain whitelist and blacklist
+#
+sub EndByDomainSpamWhitelist {
+  MailScanner::Log::InfoLog("Closing down by-domain spam whitelist");
+}
+
+sub EndByDomainSpamBlacklist {
+  MailScanner::Log::InfoLog("Closing down by-domain spam blacklist");
+}
+
+
+#
+# Setup the per-domain spam white or black list.
+# Note this doesn't do anything much in the way of syntax-checking the
+# files, so they better be right! If there are duff lines in the files,
+# they just won't produce any matches, they can't actually cause any harm.
+#
+sub CreateByDomainList {
+  my($dirname, $BlackWhite) = @_;
+
+  my($dir, $filename, $fh, $domains);
+
+  $dir = new DirHandle;
+  $dir->open($dirname) or return 0;
+  $domains = 0; # Count the number of domains we have read
+  while ($filename = $dir->read()) {
+    next if $filename =~ /^\./;
+    next unless -f "$dir/$filename";
+
+    $fh = new FileHandle;
+    $fh->open("$dir/$filename") or next;
+    $filename = lc($filename); # Going to store the name in lower case
+    while(<$fh>) {
+      chomp;
+      s/#.*$//; # Strip comments
+      s/\s//g; # Strip whitespace
+      next if /^$/; # Strip blank lines
+      $BlackWhite->{$filename}{$_} = 1; # Store the whitelist entry
+    }
+    $fh->close();
+    $domains++;
+  }
+  $dir->close();
+
+  return $domains;
+}
+
+
+#
+# Based on the address it is going to, choose the right spam white/blacklist.
+# Return 1 if the "from" address is white/blacklisted, 0 if not.
+#
+sub LookupByDomainList {
+  my($message, $BlackWhite) = @_;
+
+  return 0 unless $message; # Sanity check the input
+
+  # Find the "from" address and the first "to" address
+  my($from, $fromdomain, @todomain, $todomain);
+  $from       = $message->{from};
+  $fromdomain = $message->{fromdomain};
+  @todomain   = @{$message->{todomain}};
+  $todomain   = $todomain[0];
+
+  # It is in the list if either the exact address is listed,
+  # or the domain is listed
+  return 1 if $BlackWhite->{$todomain}{$from};
+  return 1 if $BlackWhite->{$todomain}{$fromdomain};
+
+  # It is not in the list
+  return 0;
+}
+
+
 1;
 