Mailing List Archive

svn commit: r487586 - in /spamassassin/trunk: MANIFEST lib/Mail/SpamAssassin/Plugin.pm lib/Mail/SpamAssassin/PluginHandler.pm t/data/testplugin2.pm t/plugin_priorities.t
Author: jm
Date: Fri Dec 15 08:03:23 2006
New Revision: 487586

URL: http://svn.apache.org/viewvc?view=rev&rev=487586
Log:
bug 5243: add Plugin::register_method_priority() API, allowing plugins to control the relative ordering of plugin callbacks relative to other plugins' implementations

Added:
spamassassin/trunk/t/data/testplugin2.pm
spamassassin/trunk/t/plugin_priorities.t (with props)
Modified:
spamassassin/trunk/MANIFEST
spamassassin/trunk/lib/Mail/SpamAssassin/Plugin.pm
spamassassin/trunk/lib/Mail/SpamAssassin/PluginHandler.pm

Modified: spamassassin/trunk/MANIFEST
URL: http://svn.apache.org/viewvc/spamassassin/trunk/MANIFEST?view=diff&rev=487586&r1=487585&r2=487586
==============================================================================
--- spamassassin/trunk/MANIFEST (original)
+++ spamassassin/trunk/MANIFEST Fri Dec 15 08:03:23 2006
@@ -467,3 +467,5 @@
lib/Mail/SpamAssassin/Plugin/VBounce.pm
rules/20_vbounce.cf
t/duplicates.t
+t/data/testplugin2.pm
+t/plugin_priorities.t

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin.pm?view=diff&rev=487586&r1=487585&r2=487586
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin.pm Fri Dec 15 08:03:23 2006
@@ -834,6 +834,30 @@
$nameofsub;
}

+=item $plugin->register_method_priority($methodname, $priority)
+
+Indicate that the method named C<$methodname> on the current object
+has a callback priority of C<$priority>.
+
+This is used by the plugin handler to determine the relative order of
+callbacks; plugins with lower-numbered priorities are called before plugins
+with higher-numbered priorities. Each method can have a different priority
+value. The default value is C<0>. The ordering of callbacks to methods with
+equal priority is undefined.
+
+Typically, you only need to worry about this if you need to ensure your
+plugin's method is called before another plugin's implementation of that
+method. It should be called from your plugin's constructor.
+
+This API was added in SpamAssassin 3.2.0.
+
+=cut
+
+sub register_method_priority {
+ my ($self, $methname, $pri) = @_;
+ $self->{method_priority}->{$methname} = $pri;
+}
+
=item $plugin->inhibit_further_callbacks()

Tells the plugin handler to inhibit calling into other plugins in the plugin

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/PluginHandler.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/PluginHandler.pm?view=diff&rev=487586&r1=487585&r2=487586
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/PluginHandler.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/PluginHandler.pm Fri Dec 15 08:03:23 2006
@@ -155,15 +155,26 @@
# have we set up the cache entry for this callback type?
if (!exists $self->{cbs}->{$subname}) {
# nope. run through all registered plugins and see which ones
- # implement this type of callback
- my @subs = ();
+ # implement this type of callback. sort by priority
+
+ my %subsbypri = ();
foreach my $plugin (@{$self->{plugins}}) {
my $methodref = $plugin->can ($subname);
if (defined $methodref) {
- push (@subs, [ $plugin, $methodref ]);
- dbg("plugin: ${plugin} implements '$subname'");
+ my $pri = $plugin->{method_priority}->{$subname} || 0;
+
+ $subsbypri{$pri} ||= [];
+ push (@{$subsbypri{$pri}}, [ $plugin, $methodref ]);
+
+ dbg("plugin: ${plugin} implements '$subname', priority $pri");
}
}
+
+ my @subs = ();
+ foreach my $pri (sort { $a <=> $b } keys %subsbypri) {
+ push @subs, @{$subsbypri{$pri}};
+ }
+
$self->{cbs}->{$subname} = \@subs;
}


Added: spamassassin/trunk/t/data/testplugin2.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/data/testplugin2.pm?view=auto&rev=487586
==============================================================================
--- spamassassin/trunk/t/data/testplugin2.pm (added)
+++ spamassassin/trunk/t/data/testplugin2.pm Fri Dec 15 08:03:23 2006
@@ -0,0 +1,50 @@
+=head1
+
+To try this out, write these lines to /etc/mail/spamassassin/plugintest.cf:
+
+ loadplugin myTestPlugin
+ header MY_TEST_PLUGIN eval:check_test_plugin()
+
+=cut
+
+package myTestPlugin2;
+
+use Mail::SpamAssassin::Plugin;
+use Mail::SpamAssassin::Logger;
+use strict;
+use bytes;
+
+our @ISA = qw(Mail::SpamAssassin::Plugin);
+
+# 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_method_priority('extract_metadata', 200);
+
+ print "registered myTestPlugin2: $self\n";
+ return $self;
+}
+
+sub extract_metadata {
+ my ($self, $opts) = @_;
+ my $msg = $opts->{msg};
+ print "myTestPlugin2 extract_metadata: $self\n";
+
+ # note: this has to run after myTestPlugin has run, via the magic
+ # of priorities, otherwise 'Plugin-Meta-Test2' will not contain
+ # 'bar2'.
+
+ if ($msg->get_metadata("Plugin-Meta-Test") =~ /bar/) {
+ $msg->put_metadata("Plugin-Meta-Test2", "bar2");
+ }
+ return 1;
+}
+
+1;

Added: spamassassin/trunk/t/plugin_priorities.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/plugin_priorities.t?view=auto&rev=487586
==============================================================================
--- spamassassin/trunk/t/plugin_priorities.t (added)
+++ spamassassin/trunk/t/plugin_priorities.t Fri Dec 15 08:03:23 2006
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+use lib '.'; use lib 't';
+use SATest; sa_t_init("plugin_priorities");
+use Test; BEGIN { plan tests => 2 };
+
+# ---------------------------------------------------------------------------
+
+%patterns = (
+
+q{ META2_FOUND } => '',
+
+);
+
+%anti_patterns = ();
+
+tstlocalrules ("
+ loadplugin myTestPlugin ../../data/testplugin.pm
+ loadplugin myTestPlugin2 ../../data/testplugin2.pm
+ header META2_FOUND Plugin-Meta-Test2 =~ /bar2/
+
+");
+
+ok (sarun ("-L -t < data/spam/gtube.eml", \&patterns_run_cb));
+ok_all_patterns();
+

Propchange: spamassassin/trunk/t/plugin_priorities.t
------------------------------------------------------------------------------
svn:executable = *