Mail::SpamAssassin::Plugin - SpamAssassin plugin base class
loadplugin MyPlugin /path/to/myplugin.pm
package MyPlugin;
use Mail::SpamAssassin::Plugin; our @ISA = qw(Mail::SpamAssassin::Plugin);
sub new { my ($class, $mailsa) = @_; # the usual perlobj boilerplate to create a subclass object $class = ref($class) || $class; my $self = $class->SUPER::new($mailsa); bless ($self, $class); # then register an eval rule, if desired... $self->register_eval_rule ("check_for_foo");
# and return the new plugin object return $self; }
...methods...
1;
This is the base class for SpamAssassin plugins; all plugins must be objects that implement this class.
This class provides no-op stub methods for all the callbacks that a plugin can receive. It is expected that your plugin will override one or more of these stubs to perform its actions.
SpamAssassin implements a plugin chain; each callback event is passed to each
of the registered plugin objects in turn. Any plugin can call
$self->inhibit_further_callbacks()
to block delivery of that event to
later plugins in the chain. This is useful if the plugin has handled the
event, and there will be no need for later plugins to handle it as well.
If you're looking to write a simple eval rule, skip straight to
register_eval_rule()
, below.
In all the plugin APIs below, options
refers to a reference to a hash
containing name-value pairs. This is used to ensure future-compatibility, in
that we can add new options in future without affecting objects built to an
earlier version of the API.
For example, here would be how to print out the line
item in a
parse_config()
method:
sub parse_config { my ($self, $opts) = @_; print "MyPlugin: parse_config got ".$opts->{line}."\n"; }
The following methods can be overridden by subclasses to handle events that SpamAssassin will call back to:
Note that subclasses must provide the $mailsaobject
to the
superclass constructor, like so:
my $self = $class->SUPER::new($mailsaobject);
Lifecycle note: plugins that will need to store per-scan state should not store
that on the Plugin object; see check_start()
below. It is also likewise
recommended that configuration settings be stored on the Conf object; see
parse_config()
.
options
is a reference to a hash containing these options:
Mail::SpamAssassin::Conf
object on which the configuration
data should be stored.
1
if reading a user's configuration, 0
if reading the
system-wide configuration files.
If the configuration line was a setting that is handled by this plugin, the
method implementation should call $self->inhibit_further_callbacks()
.
If the setting is not handled by this plugin, the method should return 0
so
that a later plugin may handle it, or so that SpamAssassin can output a warning
message to the user if no plugin understands it.
Lifecycle note: it is suggested that configuration be stored on the
Mail::SpamAssassin::Conf
object in use, instead of the plugin object itself.
That can be found as $plugin->{main}->{conf}
. This allows per-user and
system-wide configuration to be dealt with correctly, with per-user overriding
system-wide.
~
.)
~/.spamassassin
.)
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
Lifecycle note: it is recommended that rules that need to track test state on a per-scan basis should store that state on this object, not on the plugin object itself, since the plugin object will be shared between all active scanners.
The message being scanned is accessible through the
$permsgstatus->get_message()
API; there are a number of other public
APIs on that object, too. See Mail::SpamAssassin::PerMsgStatus
perldoc.
Mail::SpamAssassin::Message
object for this message.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
parse_metadata
method, for example.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
The current score, names of rules that hit, etc. can be retrieved
using the public APIs on this object.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
1
if the message is spam, 0
if ham.
Mail::SpamAssassin::PerMsgStatus
object is being
destroyed, and any per-scan context held on that object by this
plugin should be destroyed as well.
Mail::SpamAssassin::PerMsgStatus
context object for this scan.
This phase is the best place to map the raw (original) token value to the SHA1 hashed value.
{
'SHA1 Hash Value' => 'raw (original) value'
}
NOTE: This data structure has changed since it was originally introduced in version 3.0.0. The values are no longer perl anonymous hashes, they are a single string containing the raw token value. You can test for backwards compatability by checking to see if the value for a key is a reference to a perl HASH, for instance:
if (ref($toksref->{$sometokenkey}) eq 'HASH') {...
If it is, then you are using the old interface, otherwise you are using the current interface.
{
'SHA1 Hash Value' => {
'prob' => 'calculated probability',
'spam_count' => 'Total number of spam msgs w/ token',
'ham_count' => 'Total number of ham msgs w/ token',
'atime' => 'Atime value for token in database'
}
}
Mail::SpamAssassin
object is destroyed.
These methods provide an API for plugins to register themselves to receive specific events, or control the callback chain behaviour.
inhibit_further_callbacks()
parse_config()
.
$message
, if the SpamAssassin object is running
with debugging turned on.
NOTE: This function is not available in the package namespace
of general plugins and can't be called via $self->dbg(). If a
plugin wishes to output debug information, it should call
Mail::SpamAssassin::Plugin::dbg($msg)
.
Plugins that implement an eval test must register the methods that can be called from rules in the configuration files, in the plugin class' constructor.
For example,
$plugin->register_eval_rule ('check_for_foo')
will cause $plugin->check_for_foo()
to be called for this
SpamAssassin rule:
header FOO_RULE eval:check_for_foo()
Note that eval rules are passed the following arguments:
Mail::SpamAssassin::PerMsgStatus
object calling the ruleIn other words, the eval test method should look something like this:
sub check_for_foo { my ($self, $permsgstatus, ...arguments...) = @_; ...code returning 0 or 1 }
Note that the headers can be accessed using the get()
method on the
Mail::SpamAssassin::PerMsgStatus
object, and the body by
get_decoded_stripped_body_text_array()
and other similar methods.
Similarly, the Mail::SpamAssassin::Conf
object holding the current
configuration may be accessed through $permsgstatus->{main}->{conf}
.
The eval rule should return 1
for a hit, or 0
if the rule
is not hit.
State for a single message being scanned should be stored on the $checker
object, not on the $self
object, since $self
persists between scan
operations. See the 'lifecycle note' on the check_start()
method above.
Plugins will be called with the same arguments as a standard EvalTest. Different rule types receive different information by default:
The configuration file arguments will be passed in after the standard arguments.
Mail::SpamAssassin
Mail::SpamAssassin::PerMsgStatus