On Mon, Feb 23, 2004 at 02:10:38PM -0600, Michael Parker wrote:
> On Thu, Feb 19, 2004 at 10:59:35PM -0000, [EMAIL PROTECTED] wrote:
> > 
> > Added: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Constants.pm
> > ==============================================================================
> > --- (empty file)
> > +++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Constants.pm Thu Feb 
> > 19 14:59:35 2004
> > @@ -0,0 +1,113 @@
> > +# Constants used in many parts of the SpamAssassin codebase.
> 
> Would anyone mind if I turned this into a more complete Constants
> file?  Something that would make use of use constant, export certain
> variables and use export tags?  We could then refer to things as true
> constants rather than variables in the Mail::SpamAssassin package.
> 

More precise, something like the attached.  Ignore the Bayes stuff.

Michael
Index: lib/Mail/SpamAssassin/Constants.pm
===================================================================
--- lib/Mail/SpamAssassin/Constants.pm  (revision 6837)
+++ lib/Mail/SpamAssassin/Constants.pm  (working copy)
@@ -1,10 +1,5 @@
 # Constants used in many parts of the SpamAssassin codebase.
 #
-# Note that these are added to the "Mail::SpamAssassin" package,
-# for brevity.  Also note that they are scalars, not constants in
-# the "use constant" sense; perl does not permit cross-package use
-# of "use constant" constants.  This is arguably a perl bug.
-#
 # TODO! we need to reimplement parts of the RESERVED regexp!
 
 # <@LICENSE>
@@ -23,12 +18,31 @@
 # limitations under the License.
 # </@LICENSE>
 
-package Mail::SpamAssassin;
+package Mail::SpamAssassin::Constants;
 
 use vars qw (
-  $IPV4_ADDRESS $IP_ADDRESS $IP_IN_RESERVED_RANGE $LOCALHOST
-);
+             @BAYES_VARS @IP_VARS
+             );
 
+use base qw( Exporter );
+
[EMAIL PROTECTED] = qw ( IP_IN_RESERVED_RANGE LOCALHOST IPV4_ADDRESS 
IP_ADDRESS);
[EMAIL PROTECTED] = qw( DUMP_MAGIC DUMP_TOKEN DUMP_BACKUP );
+
[EMAIL PROTECTED] = ( @BAYES_VARS, @IP_VARS );
+%EXPORT_TAGS = (
+                bayes => [ @BAYES_VARS ],
+                ip => [ @IP_VARS ],
+                all => [ @BAYES_VARS, @IP_VARS ],
+                );
+
+# BAYES_VARS
+use constant DUMP_MAGIC  => 1;
+use constant DUMP_TOKEN  => 2;
+use constant DUMP_SEEN   => 4;
+use constant DUMP_BACKUP => 8;
+
+# IP_VARS
 # ---------------------------------------------------------------------------
 # Initialize a regexp for reserved IPs, i.e. ones that could be
 # used inside a company and be the first or second relay hit by
@@ -53,7 +67,7 @@
 # **REIMPLEMENT**: This needs to be extended to re-include the ranges
 # from the RFCs and documents above.
 #
-$IP_IN_RESERVED_RANGE = qr{^(?:
+use constant IP_IN_RESERVED_RANGE => qr{^(?:
   192\.168|                        # 192.168/16:       Private Use
   10|                              # 10/8:             Private Use
   172\.(?:1[6-9]|2[0-9]|3[01])|    # 172.16-172.31/16: Private Use
@@ -78,35 +92,35 @@
 # ---------------------------------------------------------------------------
 # match the various ways of saying "localhost".
 # 
-$LOCALHOST = qr{(?:
-                  localhost(?:\.localdomain|)|
-                  127\.0\.0\.1|
-                  ::ffff:127\.0\.0\.1
-                )}oxi;
+use constant LOCALHOST => qr{(?:
+                              localhost(?:\.localdomain|)|
+                              127\.0\.0\.1|
+                              ::ffff:127\.0\.0\.1
+                              )}oxi;
 
 # ---------------------------------------------------------------------------
 # an IP address, in IPv4 format only.
 #
-$IPV4_ADDRESS = qr/\b(?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)
-                  \b/ox;
+use constant IPV4_ADDRESS => qr/\b(?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
+                                  (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
+                                  (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
+                                  (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)
+                             \b/ox;
 
 # ---------------------------------------------------------------------------
 # an IP address, in IPv4, IPv4-mapped-in-IPv6, or IPv6 format.  NOTE: cannot
 # just refer to $IPV4_ADDRESS, due to perl bug reported in nesting qr//s. :(
 #
-$IP_ADDRESS = qr/\b (?:IPv6:|) (?: (?:0*:0*:ffff:(?:0*:|)|) # 
IPv4-mapped-in-IPv6
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
-                    (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)
-                  | # an IPv6 address, seems to always be at least 6 words
-                    [a-f0-9]{0,4} \:[a-f0-9]{0,4}
-                    \:[a-f0-9]{0,4} \:[a-f0-9]{0,4}
-                    \:[a-f0-9]{0,4} \:[a-f0-9]{0,4} (?:\:[a-f0-9]{0,4})*
-                  )\b/oxi;
+use constant IP_ADDRESS => qr/\b (?:IPv6:|) (?: (?:0*:0*:ffff:(?:0*:|)|) # 
IPv4-mapped-in-IPv6
+                                 (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
+                                 (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
+                                 (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
+                                 (?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)
+                                 | # an IPv6 address, seems to always be at 
least 6 words
+                                 [a-f0-9]{0,4} \:[a-f0-9]{0,4}
+                                 \:[a-f0-9]{0,4} \:[a-f0-9]{0,4}
+                                 \:[a-f0-9]{0,4} \:[a-f0-9]{0,4} 
(?:\:[a-f0-9]{0,4})*
+                             )\b/oxi;
 
 # ---------------------------------------------------------------------------
 
Index: lib/Mail/SpamAssassin/EvalTests.pm
===================================================================
--- lib/Mail/SpamAssassin/EvalTests.pm  (revision 6837)
+++ lib/Mail/SpamAssassin/EvalTests.pm  (working copy)
@@ -29,6 +29,7 @@
 use Mail::SpamAssassin::PerMsgStatus;
 use Mail::SpamAssassin::SHA1 qw(sha1);
 use Mail::SpamAssassin::TextCat;
+use Mail::SpamAssassin::Constants qw(:ip);
 
 use Fcntl;
 use File::Path;
@@ -414,7 +415,7 @@
        # allow private IP addrs here, could be a legit screwup
        if ($hclassb && $fclassb && 
                $hclassb ne $fclassb &&
-               !($hlo =~ /${Mail::SpamAssassin::IP_IN_RESERVED_RANGE}/o))
+               !($hlo =~ /IP_IN_RESERVED_RANGE/o))
        {
          dbg ("forged-HELO: massive mismatch on IP-addr HELO: '$hlo' != 
'$fip'");
          $self->{mismatch_ip_helo}++;
@@ -472,7 +473,7 @@
         /from mail pickup service by hotmail\.com with Microsoft SMTPSVC;/);
 
   my $ip = $self->get ('X-Originating-Ip');
-  if ($ip =~ /${Mail::SpamAssassin::IP_ADDRESS}/) { $ip = 1; } else { $ip = 0; 
}
+  if ($ip =~ /IP_ADDRESS/) { $ip = 1; } else { $ip = 0; }
 
   # Hotmail formats its received headers like this:
   # Received: from hotmail.com (f135.law8.hotmail.com [216.33.241.135])
@@ -555,7 +556,7 @@
   $rcvd =~ s/\s+/ /gs;         # just spaces, simplify the regexp
 
   my $ip = $self->get ('X-Sender-Ip');
-  if ($ip =~ /${Mail::SpamAssassin::IP_ADDRESS}/) { $ip = 1; } else { $ip = 0; 
}
+  if ($ip =~ /IP_ADDRESS/) { $ip = 1; } else { $ip = 0; }
 
   # Eudoramail formats its received headers like this:
   # Received: from Unknown/Local ([?.?.?.?]) by shared1-mail.whowhere.com;
@@ -625,7 +626,7 @@
   if ($rcvd =~ /by web\S+\.mail\.yahoo\.com via HTTP/) { return 0; }
   if ($rcvd =~ /by smtp\S+\.yahoo\.com with SMTP/) { return 0; }
   if ($rcvd =~
-      /from \[${Mail::SpamAssassin::IP_ADDRESS}\] by 
\S+\.(?:groups|grp\.scd)\.yahoo\.com with NNFMP/) {
+      /from \[IP_ADDRESS\] by \S+\.(?:groups|grp\.scd)\.yahoo\.com with 
NNFMP/) {
     return 0;
   }
 
@@ -661,12 +662,12 @@
   my $rcvd = $self->get('Received');
 
   if (!$xorig) {  # New style Juno has no X-Originating-IP header, and other 
changes
-    if($rcvd !~ 
/from.*\b(?:juno|untd)\.com.*[\[\(]${Mail::SpamAssassin::IP_ADDRESS}[\]\)].*by/
+    if($rcvd !~ /from.*\b(?:juno|untd)\.com.*[\[\(]IP_ADDRESS[\]\)].*by/
         && $rcvd !~ / cookie\.(?:juno|untd)\.com /) { return 1; }
     if($xmailer !~ /Juno /) { return 1; }
   } else {
-    if($rcvd !~ 
/from.*\bmail\.com.*\[${Mail::SpamAssassin::IP_ADDRESS}\].*by/) { return 1; }
-    if($xorig !~ /${Mail::SpamAssassin::IP_ADDRESS}/) { return 1; }
+    if($rcvd !~ /from.*\bmail\.com.*\[IP_ADDRESS\].*by/) { return 1; }
+    if($xorig !~ /IP_ADDRESS/) { return 1; }
     if($xmailer !~ /\bmail\.com/) { return 1; }
   }
 
@@ -1319,7 +1320,7 @@
   for my $header ('X-Originating-IP', 'X-Apparently-From') {
     my $str = $self->get($header);
     next unless defined $str;
-    push (@originating, ($str =~ m/(${Mail::SpamAssassin::IP_ADDRESS})/g));
+    push (@originating, ($str =~ m/(IP_ADDRESS)/g));
   }
 
   # Let's go ahead and trim away all Reserved ips (KLC)
@@ -1459,7 +1460,7 @@
   foreach my $ip (@origips) {
     next unless $ip;
     next if (exists ($seen{$ip})); $seen{$ip} = 1;
-    next if ($ip =~ /${Mail::SpamAssassin::IP_IN_RESERVED_RANGE}/o);
+    next if ($ip =~ /IP_IN_RESERVED_RANGE/o);
     push(@ips, $ip);
   }
   return @ips;
@@ -1773,7 +1774,7 @@
   #     Fri, 30 Nov 2001 08:57:47 +1000
   if ($rcvd =~ /
        \nfrom\b.{0,20}\s(\S+\.${CCTLDS_WITH_LOTS_OF_OPEN_RELAYS})\s\(.{0,200}
-       
\nfrom\b.{0,20}\s([-_A-Za-z0-9.]+)\s.{0,30}\[(${Mail::SpamAssassin::IPV4_ADDRESS})\]
+       \nfrom\b.{0,20}\s([-_A-Za-z0-9.]+)\s.{0,30}\[(IPV4_ADDRESS)\]
   /osix) { $relay = $1; $relayer = $2; $relayerip = $3; goto gotone; }
 
   return 0;
Index: lib/Mail/SpamAssassin/Received.pm
===================================================================
--- lib/Mail/SpamAssassin/Received.pm   (revision 6837)
+++ lib/Mail/SpamAssassin/Received.pm   (working copy)
@@ -47,6 +47,7 @@
 
 use Mail::SpamAssassin::Dns;
 use Mail::SpamAssassin::PerMsgStatus;
+use Mail::SpamAssassin::Constants qw(:ip);
 
 use vars qw{
 };
@@ -74,7 +75,7 @@
 
   # urgh, droppings. TODO: move into loop below?
   $hdrs =~ s/\n
-         Received:\ from\ \S*hotmail\.com\ 
\(\[${Mail::SpamAssassin::IP_ADDRESS}\]\)\ 
+         Received:\ from\ \S*hotmail\.com\ \(\[IP_ADDRESS\]\)\ 
              by\ \S+\.hotmail.com with\ Microsoft\ SMTPSVC\(5\.0\.\S+\);
              \ \S+,\ \S+\ \S+\ \d{4}\ \d{2}:\d{2}:\d{2}\ \S+\n
              /\n/gx;
@@ -221,9 +222,9 @@
        my $found_non_rsvd = 0;
        my $found_rsvd = 0;
        foreach my $ip (@ips) {
-         next if ($ip =~ /^${Mail::SpamAssassin::LOCALHOST}$/o);
+         next if ($ip =~ /^LOCALHOST$/o);
 
-         if ($ip !~ /${Mail::SpamAssassin::IP_IN_RESERVED_RANGE}/o) {
+         if ($ip !~ /IP_IN_RESERVED_RANGE/o) {
            dbg ("received-header: 'by' ".$relay->{by}." has public IP $ip");
            $found_non_rsvd = 1;
          } else {
@@ -386,7 +387,7 @@
   my $ident = '';
   my $envfrom = '';
   my $mta_looked_up_dns = 0;
-  my $IP_ADDRESS = $Mail::SpamAssassin::IP_ADDRESS;
+  my $IP_ADDRESS = IP_ADDRESS;
 
   # Received: (qmail 27981 invoked by uid 225); 14 Mar 2003 07:24:34 -0000
   # Received: (qmail 84907 invoked from network); 13 Feb 2003 20:59:28 -0000
@@ -917,11 +918,11 @@
   # with SMTP id h2R2iivG093740; Wed, 26 Mar 2003 20:44:44 -0600 
   # (CST) (envelope-from [EMAIL PROTECTED])
   # Received: from localhost (localhost [127.0.0.1]) (uid 500) by mail with 
local; Tue, 07 Jan 2003 11:40:47 -0600
-  if (/^from ${Mail::SpamAssassin::LOCALHOST} 
\((?:\S+\@|)${Mail::SpamAssassin::LOCALHOST}[\) ]/) { return; }
+  if (/^from LOCALHOST \((?:\S+\@|)LOCALHOST[\) ]/) { return; }
 
   # Received: from olgisoft.com (127.0.0.1) by 127.0.0.1 (EzMTS MTSSmtp
   # 1.55d5) ; Thu, 20 Mar 03 10:06:43 +0100 for <[EMAIL PROTECTED]>
-  if (/^from \S+ \((?:\S+\@|)${Mail::SpamAssassin::LOCALHOST}\) /) { return; }
+  if (/^from \S+ \((?:\S+\@|)LOCALHOST\) /) { return; }
 
   # Received: from casper.ghostscript.com ([EMAIL PROTECTED] [127.0.0.1]) 
h148aux8016336verify=FAIL); Tue, 4 Feb 2003 00:36:56 -0800
   # TODO: could use IPv6 localhost
@@ -1067,7 +1068,7 @@
   dbg ("received-header: parsed as $asstr");
   $relay->{as_string} = $asstr;
 
-  my $isrsvd = ($ip =~ /${Mail::SpamAssassin::IP_IN_RESERVED_RANGE}/o);
+  my $isrsvd = ($ip =~ /IP_IN_RESERVED_RANGE/o);
   $relay->{ip_is_reserved} = $isrsvd;
 
   # add it to an internal array so Eval tests can use it

Reply via email to