You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by fe...@apache.org on 2004/11/06 05:45:46 UTC
svn commit: rev 56728 - in spamassassin/trunk/lib/Mail/SpamAssassin: . Conf Plugin
Author: felicity
Date: Fri Nov 5 20:45:45 2004
New Revision: 56728
Modified:
spamassassin/trunk/lib/Mail/SpamAssassin/Conf.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Conf/Parser.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Hashcash.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm
Log:
bug 3869: instead of using parse_config, put the configuration directly in the Conf array. to do so, changed around some internal functions to be a little more modular, etc.
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Conf.pm
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Conf.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Conf.pm Fri Nov 5 20:45:45 2004
@@ -75,7 +75,7 @@
use bytes;
use vars qw{
- @ISA $VERSION $DEFAULT_COMMANDS
+ @ISA $VERSION
$CONF_TYPE_STRING $CONF_TYPE_BOOL
$CONF_TYPE_NUMERIC $CONF_TYPE_HASH_KEY_VALUE
$CONF_TYPE_ADDRLIST $CONF_TYPE_TEMPLATE
@@ -130,7 +130,7 @@
###########################################################################
sub set_default_commands {
- return if (defined $DEFAULT_COMMANDS);
+ my($self) = @_;
# see "perldoc Mail::SpamAssassin::Conf::Parser" for details on this fmt.
# push each config item like this, to avoid a POD bug; it can't just accept
@@ -3170,7 +3170,7 @@
=cut
- $DEFAULT_COMMANDS = \@cmds;
+ return \@cmds;
}
###########################################################################
@@ -3179,14 +3179,12 @@
my $class = shift;
$class = ref($class) || $class;
my $self = {
- main => shift
+ main => shift,
+ registered_commands => [],
}; bless ($self, $class);
$self->{parser} = Mail::SpamAssassin::Conf::Parser->new($self);
-
- set_default_commands();
- $self->{registered_commands} = $DEFAULT_COMMANDS;
- $self->{parser}->set_defaults_from_command_list();
+ $self->{parser}->register_commands($self->set_default_commands());
$self->{errors} = 0;
$self->{plugins_loaded} = { };
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Conf/Parser.pm
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Conf/Parser.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Conf/Parser.pm Fri Nov 5 20:45:45 2004
@@ -111,9 +111,6 @@
=back
-Note that the registered commands array can be extended by plugins, by adding
-the new config settings to the C<$conf-<gt>{registered_commands}> array ref.
-
=head1 METHODS
=over 4
@@ -146,16 +143,29 @@
'conf' => $conf
};
+ $self->{command_luts} = { };
+ $self->{command_luts}->{frequent} = { };
+ $self->{command_luts}->{remaining} = { };
+
bless ($self, $class);
$self;
}
###########################################################################
+sub register_commands {
+ my($self, $arrref) = @_;
+ my $conf = $self->{conf};
+
+ $self->set_defaults_from_command_list($arrref);
+ $self->build_command_luts($arrref);
+ push(@{$conf->{registered_commands}}, @{$arrref});
+}
+
sub set_defaults_from_command_list {
- my ($self) = @_;
+ my ($self, $arrref) = @_;
my $conf = $self->{conf};
- foreach my $cmd (@{$conf->{registered_commands}}) {
+ foreach my $cmd (@{$arrref}) {
# note! exists, not defined -- we want to be able to set
# "undef" default values.
if (exists($cmd->{default})) {
@@ -165,19 +175,12 @@
}
sub build_command_luts {
- my ($self) = @_;
-
- return if $self->{already_built_config_lookup};
- $self->{already_built_config_lookup} = 1;
+ my ($self, $arrref) = @_;
- $self->{command_luts} = { };
- $self->{command_luts}->{frequent} = { };
- $self->{command_luts}->{remaining} = { };
my $conf = $self->{conf};
my $set;
- foreach my $cmd (@{$conf->{registered_commands}})
- {
+ foreach my $cmd (@{$arrref}) {
# first off, decide what set this is in.
if ($cmd->{is_frequent}) { $set = 'frequent'; }
else { $set = 'remaining'; }
@@ -217,8 +220,7 @@
$lang =~ s/[@.+,].*$//; # Strip codeset, modifier/audience, etc.
} # (eg. .utf8 or @euro)
- # build and get fast-access handles on the command lookup tables
- $self->build_command_luts();
+ # get fast-access handles on the command lookup tables
my $lut_frequent = $self->{command_luts}->{frequent};
my $lut_remaining = $self->{command_luts}->{remaining};
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Hashcash.pm
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Hashcash.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Hashcash.pm Fri Nov 5 20:45:45 2004
@@ -24,53 +24,6 @@
# limitations under the License.
# </...@LICENSE>
-package Mail::SpamAssassin::Plugin::Hashcash;
-
-use Mail::SpamAssassin::Plugin;
-use Digest::SHA1 qw(sha1);
-use Fcntl;
-use File::Path;
-use File::Basename;
-use strict;
-use warnings;
-use bytes;
-
-use vars qw(@ISA);
-@ISA = qw(Mail::SpamAssassin::Plugin);
-
-use constant HAS_DB_FILE => eval { require DB_File; };
-
-# constructor: register the eval rule
-sub new {
- my $class = shift;
- my $mailsaobject = shift;
-
- # some boilerplate...
- $class = ref($class) || $class;
- my $self = $class->SUPER::new($mailsaobject);
- bless ($self, $class);
-
- my $conf = $mailsaobject->{conf};
- $conf->{use_hashcash} = 1;
- $conf->{hashcash_accept} = { };
- $conf->{hashcash_doublespend_path} = '__userstate__/hashcash_seen';
- $conf->{hashcash_doublespend_file_mode} = "0700";
-
- $self->register_eval_rule ("check_hashcash_value");
- $self->register_eval_rule ("check_hashcash_double_spend");
-
- return $self;
-}
-
-###########################################################################
-
-sub parse_config {
- my ($self, $opts) = @_;
- my $conf = $opts->{conf};
- my $key = $opts->{key};
- my $value = $opts->{value};
- my $line = $opts->{line};
-
=over 4
=item use_hashcash { 1 | 0 } (default: 1)
@@ -79,14 +32,6 @@
=cut
- if ( $key eq 'use_hashcash' ) {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_numeric_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks();
- return 1;
- }
-
=item hashcash_accept add@ress.com ...
Used to specify addresses that we accept HashCash tokens for. You should set
@@ -105,12 +50,6 @@
=cut
- if ( $key eq 'hashcash_accept' ) {
- $conf->add_to_addrlist ('hashcash_accept', split (/\s+/, $value));
- $self->inhibit_further_callbacks();
- return 1;
- }
-
=item hashcash_doublespend_path /path/to/file (default: ~/.spamassassin/hashcash_seen)
Path for HashCash double-spend database. HashCash tokens are only usable once,
@@ -123,14 +62,6 @@
=cut
- if ( $key eq 'hashcash_doublespend_path' ) {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_string_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks();
- return 1;
- }
-
=item hashcash_doublespend_file_mode (default: 0700)
The file mode bits used for the HashCash double-spend database file.
@@ -141,48 +72,72 @@
=cut
- if ( $key eq 'hashcash_doublespend_file_mode' ) {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_numeric_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks();
- return 1;
- }
+package Mail::SpamAssassin::Plugin::Hashcash;
+
+use Mail::SpamAssassin::Plugin;
+use Digest::SHA1 qw(sha1);
+use Fcntl;
+use File::Path;
+use File::Basename;
+use strict;
+use warnings;
+use bytes;
+
+use vars qw(@ISA);
+@ISA = qw(Mail::SpamAssassin::Plugin);
+
+use constant HAS_DB_FILE => eval { require DB_File; };
+
+# constructor: register the eval rule
+sub new {
+ my $class = shift;
+ my $mailsaobject = shift;
+
+ # some boilerplate...
+ $class = ref($class) || $class;
+ my $self = $class->SUPER::new($mailsaobject);
+ bless ($self, $class);
+
+ $self->register_eval_rule ("check_hashcash_value");
+ $self->register_eval_rule ("check_hashcash_double_spend");
+
+ $self->set_config($mailsaobject->{conf});
- return 0;
+ return $self;
}
-sub handle_parser_error {
- my($self, $opts, $ret_value) = @_;
+###########################################################################
+
+sub set_config {
+ my($self, $conf) = @_;
+ my @cmds = ();
+
+ push(@cmds, {
+ setting => 'use_hashcash',
+ default => 1,
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+ });
+
+ push(@cmds, {
+ setting => 'hashcash_doublespend_path',
+ default => '__userstate__/hashcash_seen',
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
+ });
+
+ push(@cmds, {
+ setting => 'hashcash_doublespend_file_mode',
+ default => "0700",
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+ });
+
+ push(@cmds, {
+ setting => 'hashcash_accept',
+ default => {},
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_ADDRLIST,
+ });
- my $conf = $opts->{conf};
- my $key = $opts->{key};
- my $value = $opts->{value};
- my $line = $opts->{line};
-
- my $msg = '';
-
- if ($ret_value && $ret_value eq $Mail::SpamAssassin::Conf::INVALID_VALUE) {
- $msg = "config: SpamAssassin failed to parse line, ".
- "\"$value\" is not valid for \"$key\", ".
- "skipping: $line";
- }
- elsif ($ret_value && $ret_value eq $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE) {
- $msg = "config: SpamAssassin failed to parse line, ".
- "no value provided for \"$key\", ".
- "skipping: $line";
- }
-
- return unless $msg;
-
- if ($conf->{lint_rules}) {
- warn $msg."\n";
- } else {
- dbg($msg);
- }
- $conf->{errors}++;
- return;
-}
+ $conf->{parser}->register_commands(\@cmds);
+}
###########################################################################
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm Fri Nov 5 20:45:45 2004
@@ -44,16 +44,15 @@
my $self = $class->SUPER::new($mailsaobject);
bless ($self, $class);
- $mailsaobject->{conf}->{use_razor2} = 0;
- $mailsaobject->{conf}->{razor_timeout} = 10;
-
+ # figure out if razor is even available or not ...
+ $self->{razor2_available} = 0;
if ($mailsaobject->{local_tests_only}) {
dbg("razor2: local tests only, skipping razor2");
}
else {
if (eval { require Razor2::Client::Agent; }) {
dbg("razor2: razor2 is available");
- $mailsaobject->{conf}->{use_razor2} = 1;
+ $self->{razor2_available} = 1;
}
else {
dbg("razor2: razor2 is not available");
@@ -63,25 +62,27 @@
$self->register_eval_rule ("check_razor2");
$self->register_eval_rule ("check_razor2_range");
+ $self->set_config($mailsaobject->{conf});
+
return $self;
}
-sub parse_config {
- my ($self, $opts) = @_;
+sub set_config {
+ my($self, $conf) = @_;
+ my @cmds = ();
- my $conf = $opts->{conf};
- my $key = $opts->{key};
- my $value = $opts->{value};
- my $line = $opts->{line};
-
- # Backward compatibility ... use_razor2 is implicit if the plugin is loaded
- if ($key eq 'use_razor2') {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_numeric_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks();
- return 1;
- }
+=item use_razor2 (0|1) (default: 1)
+
+How many seconds you wait for razor to complete before you go on without
+the results
+
+=cut
+
+ push(@cmds, {
+ setting => 'use_razor2',
+ default => 1,
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+ });
=item razor_timeout n (default: 10)
@@ -90,13 +91,11 @@
=cut
- if ($key eq 'razor_timeout') {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_numeric_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks();
- return 1;
- }
+ push(@cmds, {
+ setting => 'razor_timeout',
+ default => 10,
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+ });
=item razor_config filename
@@ -105,50 +104,14 @@
=cut
- if ($key eq 'razor_config') {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_string_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks();
- return 1;
- }
+ push(@cmds, {
+ setting => 'razor_config',
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
+ });
- return 0;
+ $conf->{parser}->register_commands(\@cmds);
}
-sub handle_parser_error {
- my($self, $opts, $ret_value) = @_;
-
- my $conf = $opts->{conf};
- my $key = $opts->{key};
- my $value = $opts->{value};
- my $line = $opts->{line};
-
- my $msg = '';
-
- if ($ret_value && $ret_value eq $Mail::SpamAssassin::Conf::INVALID_VALUE) {
- $msg = "config: SpamAssassin failed to parse line, ".
- "\"$value\" is not valid for \"$key\", ".
- "skipping: $line";
- }
- elsif ($ret_value && $ret_value eq $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE) {
- $msg = "config: SpamAssassin failed to parse line, ".
- "no value provided for \"$key\", ".
- "skipping: $line";
- }
-
- return unless $msg;
-
- if ($conf->{lint_rules}) {
- warn $msg."\n";
- } else {
- dbg($msg);
- }
- $conf->{errors}++;
- return;
-}
-
-
sub razor2_lookup {
my ($self, $permsgstatus, $fulltext) = @_;
my $timeout=$self->{main}->{conf}->{razor_timeout};
@@ -158,8 +121,8 @@
return $self->{razor2_result} if ( defined $self->{razor2_result} );
$self->{razor2_result} = 0;
- # this test covers all aspects of availability
- if (!$self->{main}->{conf}->{use_razor2}) { return 0; }
+ return unless $self->{main}->{conf}->{use_razor2};
+ return unless $self->{razor2_available};
# razor also debugs to stdout. argh. fix it to stderr...
if ($Mail::SpamAssassin::DEBUG) {
@@ -327,6 +290,7 @@
my ($self, $permsgstatus) = @_;
return unless $self->{main}->{conf}->{use_razor2};
+ return unless $self->{razor2_available};
return $self->{razor2_result} if (defined $self->{razor2_result});
my $full = $permsgstatus->{msg}->get_pristine();
@@ -341,6 +305,7 @@
# If Razor2 isn't available, or the general test is disabled, don't
# continue.
return 0 unless $self->{main}->{conf}->{use_razor2};
+ return unless $self->{razor2_available};
return 0 unless $self->{main}->{conf}->{scores}->{'RAZOR2_CHECK'};
# If Razor2 hasn't been checked yet, go ahead and run it.
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm Fri Nov 5 20:45:45 2004
@@ -129,11 +129,8 @@
}
$self->register_eval_rule ("check_uridnsbl");
+ $self->set_config($samain->{conf});
- # set default config settings
- $samain->{conf}->{uridnsbl_timeout} = 3;
- $samain->{conf}->{uridnsbl_max_domains} = 20;
- $samain->{conf}->{uridnsbl_skip_domains} = {};
return $self;
}
@@ -214,118 +211,88 @@
return 1;
}
-sub parse_config {
- my ($self, $opts) = @_;
-
- my $conf = $opts->{conf};
- my $key = $opts->{key};
- my $value = $opts->{value};
- my $line = $opts->{line};
-
- if ($key eq 'uridnsbl') {
- if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)$/) {
- my $rulename = $1;
- my $zone = $2;
- my $type = $3;
-
- $opts->{conf}->{uridnsbls}->{$rulename} = {
- zone => $zone, type => $type,
- is_rhsbl => 0
- };
- $self->inhibit_further_callbacks(); return 1;
+sub set_config {
+ my($self, $conf) = @_;
+ my @cmds = ();
+
+ push(@cmds, {
+ setting => 'uridnsbl_timeout',
+ default => 3,
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+ });
+
+ push(@cmds, {
+ setting => 'uridnsbl_max_domains',
+ default => 20,
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+ });
+
+ push (@cmds, {
+ setting => 'uridnsbl',
+ code => sub {
+ my ($self, $key, $value, $line) = @_;
+ if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)$/) {
+ my $rulename = $1;
+ my $zone = $2;
+ my $type = $3;
+ $self->{uridnsbls}->{$rulename} = {
+ zone => $zone, type => $type,
+ is_rhsbl => 0
+ };
+ }
}
- }
+ });
- if ($key eq 'urirhsbl') {
- if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)$/) {
- my $rulename = $1;
- my $zone = $2;
- my $type = $3;
-
- $opts->{conf}->{uridnsbls}->{$rulename} = {
- zone => $zone, type => $type,
- is_rhsbl => 1
- };
- $self->inhibit_further_callbacks(); return 1;
+ push (@cmds, {
+ setting => 'urirhsbl',
+ code => sub {
+ my ($self, $key, $value, $line) = @_;
+ if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)$/) {
+ my $rulename = $1;
+ my $zone = $2;
+ my $type = $3;
+ $self->{uridnsbls}->{$rulename} = {
+ zone => $zone, type => $type,
+ is_rhsbl => 1
+ };
+ }
}
- }
+ });
- if ($key eq 'urirhssub') {
- if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/) {
- my $rulename = $1;
- my $zone = $2;
- my $type = $3;
- my $subrule = $4;
-
- $opts->{conf}->{uridnsbls}->{$rulename} = {
- zone => $zone, type => $type,
- is_rhsbl => 1, is_subrule => 1
- };
-
- $opts->{conf}->{uridnsbl_subs}->{$zone} ||= { };
- $opts->{conf}->{uridnsbl_subs}->{$zone}->{$subrule} = {
- rulename => $rulename
- };
-
- $self->inhibit_further_callbacks(); return 1;
+ push (@cmds, {
+ setting => 'urirhssub',
+ code => sub {
+ my ($self, $key, $value, $line) = @_;
+ if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/) {
+ my $rulename = $1;
+ my $zone = $2;
+ my $type = $3;
+ my $subrule = $4;
+ $self->{uridnsbls}->{$rulename} = {
+ zone => $zone, type => $type,
+ is_rhsbl => 1, is_subrule => 1
+ };
+ $self->{uridnsbl_subs}->{$zone} ||= { };
+ $self->{uridnsbl_subs}->{$zone}->{$subrule} = {
+ rulename => $rulename
+ };
+ }
}
- }
-
- if ($key eq 'uridnsbl_timeout') {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_numeric_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks(); return 1;
- }
+ });
- if ($key eq 'uridnsbl_max_domains') {
- $self->handle_parser_error($opts,
- Mail::SpamAssassin::Conf::Parser::set_numeric_value($conf, $key, $value, $line)
- );
- $self->inhibit_further_callbacks(); return 1;
- }
-
- if ($key eq 'uridnsbl_skip_domain') {
- foreach my $domain (split(/\s+/, $value)) {
- $opts->{conf}->{uridnsbl_skip_domains}->{lc $domain} = 1;
+ push (@cmds, {
+ setting => 'uridnsbl_skip_domain',
+ default => {},
+ code => sub {
+ my ($self, $key, $value, $line) = @_;
+ foreach my $domain (split(/\s+/, $value)) {
+ $self->{uridnsbl_skip_domains}->{lc $domain} = 1;
+ }
}
- $self->inhibit_further_callbacks(); return 1;
- }
+ });
- return 0;
+ $conf->{parser}->register_commands(\@cmds);
}
-
-sub handle_parser_error {
- my($self, $opts, $ret_value) = @_;
-
- my $conf = $opts->{conf};
- my $key = $opts->{key};
- my $value = $opts->{value};
- my $line = $opts->{line};
-
- my $msg = '';
-
- if ($ret_value && $ret_value eq $Mail::SpamAssassin::Conf::INVALID_VALUE) {
- $msg = "config: SpamAssassin failed to parse line, ".
- "\"$value\" is not valid for \"$key\", ".
- "skipping: $line";
- }
- elsif ($ret_value && $ret_value eq $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE) {
- $msg = "config: SpamAssassin failed to parse line, ".
- "no value provided for \"$key\", ".
- "skipping: $line";
- }
-
- return unless $msg;
-
- if ($conf->{lint_rules}) {
- warn $msg."\n";
- } else {
- dbg($msg);
- }
- $conf->{errors}++;
- return;
-}
sub check_tick {
my ($self, $opts) = @_;