Mail::...Plugin(3pm)
okay so bestie, welcome to the <pink>Mail::...Plugin(3pm)</pink> manual page โ it's giving User Contributed Perl Documentation energy, and the actual package is Mail::SpamAssassin::Plugin. This is the base class for SpamAssassin plugins, and it's lowkey the blueprint for making your own plugin. No cap. ๐
โจ NAME โจ
Mail::SpamAssassin::Plugin โ SpamAssassin plugin base class. That's the whole vibe, fr fr.
๐ธ SYNOPSIS ๐ธ
SpamAssassin configuration:
loadplugin MyPlugin /path/to/myplugin.pm
Perl code:
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;
๐ฆ DESCRIPTION ๐ฆ
This is the base class for SpamAssassin plugins; all plugins must be objects that implement this class. Periodt.
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. So like, you're not reinventing the wheel, you're just customizing the vibe. ๐
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. It's giving I got this, bestie, you can chill. ๐ซถ
If you're looking to write a simple eval rule, skip straight to register_eval_rule(), below. That's the shortcut, iykyk.
๐ INTERFACE ๐
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. So like, it's future-proofed, slay.
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";
}
๐ METHODS ๐
The following methods can be overridden by subclasses to handle events. These are the main character hooks, bestie. โจ
$plugin = MyPluginClass->new ($mailsaobject)
Constructor. Plugins that need to register themselves will need to define their own; the default super-class constructor will work fine for plugins that just override a method.
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; instead this should be stored on the PerMsgStatus object, see check_start() below. It is also likewise recommended that configuration settings be stored on the Conf object; see parse_config().
$plugin->parse_config ( { options ... } )
Parse a configuration line that hasn't already been handled. options is a reference to a hash containing these options:
lineโ The line of configuration text to parse. This has leading and trailing whitespace, and comments, removed.keyโ The configuration key; ie. the first "word" on the line.valueโ The configuration value; everything after the first "word" and any whitespace after that.confโ TheMail::SpamAssassin::Confobject on which the configuration data should be stored.user_configโ A boolean: 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}, or as conf in the $options hash reference above. By storing it on conf, this allows per-user and system-wide configuration precedence to be dealt with correctly.
$plugin->finish_parsing_start ( { options ... } )
Signals that the system-wide configuration has been completely read, but internal data structures are not yet created. It is possible to use this hook to dynamically change the configuration already read in or add new config options.
options is a reference to a hash containing these options:
confโ TheMail::SpamAssassin::Confobject on which the configuration data should be stored.
Note: there are no guarantees that the internal data structures of SpamAssassin will not change from release to release. In particular to this plugin hook, if you modify the rules data structures in a third-party plugin, all bets are off until such time that an API is present for modifying that configuration data.
$plugin->finish_parsing_end ( { options ... } )
Signals that the system-wide configuration parsing has just finished, and SpamAssassin is nearly ready to check messages.
options is a reference to a hash containing these options:
confโ TheMail::SpamAssassin::Confobject on which the configuration data should be stored.
Note: there are no guarantees that the internal data structures of SpamAssassin will not change from release to release. In particular to this plugin hook, if you modify the rules data structures in a third-party plugin, all bets are off until such time that an API is present for modifying that configuration data.
$plugin->user_conf_parsing_start ( { options ... } )
Signals that the per-user configuration has been completely read, but not converted to internal data structures. It is possible to use this hook to dynamically change the configuration already read in or add new config options.
If allow_user_rules is enabled in the configuration, it is possible that additional rules have been added since the finish_parsing_start plugin hook invocation was called.
confโ TheMail::SpamAssassin::Confobject on which the configuration data should be stored.
Note: there are no guarantees that the internal data structures of SpamAssassin will not change from release to release. In particular to this plugin hook, if you modify the rules data structures in a third-party plugin, all bets are off until such time that an API is present for modifying that configuration data.
$plugin->user_conf_parsing_end ( { options ... } )
Signals that the per-user configuration parsing has just finished, and SpamAssassin is nearly ready to check messages. If allow_user_rules is enabled in the configuration, it is possible that additional rules have been added since the finish_parsing_end plugin hook invocation was called.
options is a reference to a hash containing these options:
confโ TheMail::SpamAssassin::Confobject on which the configuration data should be stored.
Note: there are no guarantees that the internal data structures of SpamAssassin will not change from release to release. In particular to this plugin hook, if you modify the rules data structures in a third-party plugin, all bets are off until such time that an API is present for modifying that configuration data.
$plugin->signal_user_changed ( { options ... } )
Signals that the current user has changed for a new one.
usernameโ The new user's username.user_dirโ The new user's home directory. (equivalent to~.)userstate_dirโ The new user's storage directory. (equivalent to~/.spamassassin.)
$plugin->services_authorized_for_username ( { options ... } )
Validates that a given username is authorized to use certain services.
In order to authorize a user, the plugin should first check that it can handle any of the services passed into the method and then set the value for each allowed service to true (or any non-negative value).
The current supported services are: bayessql
usernameโ A usernameservicesโ Reference to a hash containing the services you want to check.
{
'bayessql' => 0
}
confโ TheMail::SpamAssassin::Confobject on which the configuration data should be stored.
$plugin->compile_now_start ( { options ... } )
This is called at the beginning of Mail::SpamAssassin::compile_now() so plugins can do any necessary initialization for multi-process SpamAssassin (such as spamd or mass-check -j).
use_user_prefsโ The value of$use_user_prefsoption incompile_now().keep_userstateโ The value of$keep_userstateoption incompile_now().
$plugin->compile_now_finish ( { options ... } )
This is called at the end of Mail::SpamAssassin::compile_now() so plugins can do any necessary initialization for multi-process SpamAssassin (such as spamd or mass-check -j).
use_user_prefsโ The value of$use_user_prefsoption incompile_now().keep_userstateโ The value of$keep_userstateoption incompile_now().
$plugin->check_start ( { options ... } )
Signals that a message check operation is starting.
permsgstatusโ TheMail::SpamAssassin::PerMsgStatuscontext 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.
$plugin->check_main ( { options ... } )
Signals that a message should be checked. Note that implementations of this hook should return 1.
permsgstatusโ TheMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐ซ $plugin->check_tick ( { options ... } ) ๐ซ
okay so this one gets called periodically while a message check is happening. if you set a callback for it, it's lowkey the perfect spot to run through an event loop and deal with network events that got triggered in a parse_metadata method, like, for example. โจ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan. that's your girl, she's holding all the context.
๐ $plugin->check_dnsbl ( { options ... } ) ๐
this fires when DNSBL or other network lookups are being launched, and it implies the current running priority is -100. bestie, this is the place to kick off your own asynchronously-started network lookups. go off. ๐
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐พ $plugin->check_post_dnsbl ( { options ... } ) ๐พ
called after the DNSBL results have been harvested. so if you started some async network lookups of your own? this is where you harvest them. it's giving "reaping what you sowed" energy ๐ซถ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐งน $plugin->check_cleanup ( { options ... } ) ๐งน
this one runs just before message check is finishing and before possible auto-learning. and here's the tea: it is guaranteed to always be called, unlike check_tick and check_post_dnsbl. iconic behaviour, honestly. use it for cleaning up left callbacks or forked children etc. โ it's your last chance to make rules hit. ๐ค
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐ $plugin->check_post_learn ( { options ... } ) ๐
called after auto-learning may (or may not) have taken place. note the "may or may not" โ no promises. if you wanna do extra learning, whether or not auto-learning actually happened, this is the spot. ๐
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐ $plugin->check_end ( { options ... } ) ๐
signals that a message check operation has just finished, and the results are about to be handed back to the caller. end of an era fr fr.
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan. the current score, names of rules that hit, etc. can all be retrieved using the public APIs on this object, so go grab 'em. ๐
๐งผ $plugin->finish_tests ( { options ... } ) ๐งผ
called via Mail::SpamAssassin::finish. it should clear up any tests that a plugin has added to the namespace, okay? super important.
so sometimes plugins find it useful to compile perl functions from the ruleset, like, on the fly. but bestie โ it is important to remove these once the Mail::SpamAssassin object is deleted, and this API is what lets you do that. and no passing the buck: each plugin is responsible for its own generated perl functions. ๐
confโ theMail::SpamAssassin::Confobject on which the configuration data should be stored.
also see the register_generated_rule_method helper API, below. ๐
๐ $plugin->extract_metadata ( { options ... } ) ๐
signals that a message is being mined for metadata. some plugins may wanna add their own metadata too, and this is where you do that. ๐
msgโ theMail::SpamAssassin::Messageobject for this message.permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐๏ธ $plugin->parsed_metadata ( { options ... } ) ๐๏ธ
signals that a message's metadata has been parsed, and can now be accessed by the plugin. door's open, come on in. ๐ฆ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐ฌ $plugin->start_rules ( { options ... } ) ๐ฌ
called before testing a set of rules of a given type and priority. like a little pre-show. ๐ฟ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.ruletypeโ the type of the rules about to be performed.priorityโ the priority level of the rules about to be performed.
๐ฅ $plugin->hit_rule ( { options ... } ) ๐ฅ
called when a rule fires. yes, we love a rule that ate. ๐ฅ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.ruletypeโ the type of the rule that fired.rulenameโ the name of the rule that fired.scoreโ the rule's score in the active scoreset.
โ
$plugin->ran_rule ( { options ... } ) โ
called after a rule has been tested, whether or not it fired. and heads up: when the rule fires, the hit_rule callback is always called before this one. that order is non-negotiable. ๐
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.ruletypeโ the type of the rule that was tested.rulenameโ the name of the rule that was tested.
๐ค $plugin->autolearn_discriminator ( { options ... } ) ๐ค
this one controls whether a just-scanned message should be learned as either spam or ham. your method should return exactly one of these:
1โ learn the message as spam0โ learn as hamundefโ don't learn from the message at all ๐permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐ง $plugin->autolearn ( { options ... } ) ๐ง
signals that a message is about to be auto-learned as either ham or spam. big moment for the message honestly. โญ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.isspamโ1if the message is spam,0if ham.
๐ชฆ $plugin->per_msg_finish ( { options ... } ) ๐ชฆ
signals that a Mail::SpamAssassin::PerMsgStatus object is being destroyed, and any per-scan context that your plugin is holding on that object should get destroyed too. cleanup era. ๐
normally, any member variables on the PerMsgStatus object will be cleaned up automatically โ but if your plugin has made a circular reference on that object, this is the place to break them so that garbage collection can actually operate correctly. don't leave GC in the group chat with no closure. ๐ญ
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
โก $plugin->have_shortcircuited ( { options ... } ) โก
has the current scan operation 'short-circuited'? in other words โ can further scanning just be skipped, since the message is already definitively classified as either spam or ham?
plugins should return 0 to say that scanning should continue, or 1 to say that short-circuiting has taken effect.
permsgstatusโ theMail::SpamAssassin::PerMsgStatuscontext object for this scan.
๐ $plugin->bayes_learn ( { options ... } ) ๐
called at the end of a bayes learn operation. this phase is the best place to map the raw (original) token value to the SHA1 hashed value. iykyk. ๐
toksrefโ reference to hash returned by call to tokenize. the hash takes the format of:
{
'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 backward compatibility 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. choose your fighter. ๐ซ
isspamโ boolean value stating what flavor of message the tokens represent. if true then message was specified as spam, false is nonspam. note: when function is scan then isspam value is not valid. ๐msgidโ generated message id of the message just learned.msgatimeโ received date of the current message, or current time if received date could not be determined. in addition, if the receive date is more than 24 hrs into the future it will be reset to current datetime. future-dated mail, be gone. ๐ฐ๏ธ
๐๏ธ $plugin->bayes_forget ( { options ... } ) ๐๏ธ
called at the end of a bayes forget operation. memory holed. โจ
toksrefโ reference to hash returned by call to tokenize. seebayes_learndocumentation for additional information on the format.isspamโ boolean value stating what flavor of message the tokens represent. if true then message was specified as spam, false is nonspam. note: when function is scan then isspam value is not valid.msgidโ generated message id of the message just forgotten.
๐ $plugin->bayes_scan ( { options ... } ) ๐
called at the end of a bayes scan operation.
NOTE: will not be called in case of error or if the message is otherwise skipped. ๐ญ
toksrefโ reference to hash returned by call to tokenize. seebayes_learndocumentation for additional information on the format.probsrefโ reference to hash of calculated probabilities for tokens found in the database.
{
'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'
}
}
scoreโ score calculated for this particular message.msgatimeโ calculated atime of the message just learned. note it may have been adjusted if it was determined to be too far into the future.significant_tokensโ array ref of the tokens found to be significant in determining the score for this message. the actual main characters of the tokenizer. ๐
๐ฎ $plugin->plugin_report ( { options ... } ) ๐ฎ
called if the message is to be reported as spam. if the reporting system is available, the variable $options->{report}->report_available} should be set to 1; if the reporting system successfully reported the message, the variable $options->{report}->report_return} should be set to 1. ๐ซถ
reportโ reference to the Reporter object ($options->{report}in the paragraph above.)textโ reference to a markup removed copy of the message in scalar string format.msgโ reference to the original message object.
โฉ๏ธ $plugin->plugin_revoke ( { options ... } ) โฉ๏ธ
called if the message is to be reported as ham โ i.e. it revokes a spam report. the "jk bestie" of the reporting world. ๐
if the reporting system is available, the variable $options->{revoke}->revoke_available} should be set to 1; if the reporting system successfully revoked the message, the variable $options->{revoke}->revoke_return} should be set to 1.
revokeโ a reference to the Reporter object ($options->{revoke}in the paragraph above). ๐textโ a reference to a markup removed copy of the message, in scalar string format. โจmsgโ a reference to the original message object. ๐ซถ
$plugin->welcomelist_address( { options ... } ) ๐
Previously whitelist_address which will work interchangeably until 4.1.
Called when a request is made to add an address to a persistent address list.
addressโ Address you wish to add. ๐ฌcli_pโ Indicate if the call is being made from a command line interface. ๐ป
$plugin->blocklist_address( { options ... } ) ๐ซ
Previously blacklist_address which will work interchangeably until 4.1.
Called when a request is made to add an address to a persistent address list.
addressโ Address you wish to add. ๐ฌcli_pโ Indicate if the call is being made from a command line interface. ๐ป
$plugin->remove_address( { options ... } ) ๐๏ธ
Called when a request is made to remove an address to a persistent address list.
addressโ Address you wish to remove. โ๏ธcli_pโ Indicate if the call is being made from a command line interface. ๐ป
$plugin->spamd_child_init () ๐ถ
Called in each new child process when it starts up under spamd. ๐ง
$plugin->log_scan_result ( { options ... } ) ๐
Called when spamd has completed scanning a message. Currently, only spamd calls this API.
resultโ Theresult: ...line for this scan. Format is as described athttps://wiki.apache.org/spamassassin/SpamdSyslogFormat. ๐
$plugin->spamd_child_post_connection_close () ๐
Called when child returns from handling a connection.
If there was an accept failure, the child will die and this code will not be called. ๐
$plugin->finish () ๐งน
Called when the Mail::SpamAssassin object is destroyed. ๐ฅ
$plugin->learner_new () ๐ง
Used to support human-trained probabilistic classifiers like the BAYES_* ruleset. Called when a new Mail::SpamAssassin::Bayes object has been created; typically when a new user's scan is about to start. โจ
$plugin->learn_message () ๐
Train the classifier with a training message.
isspamโ 1 if the message is spam, 0 if it's non-spam. โ๏ธmsgโ The message'sMail::SpamAssassin::Messageobject. ๐จidโ An optional message-identification string, used internally to tag the message. If it isundef, one will be generated. It should be unique to that message. ๐ท๏ธ
$plugin->forget_message () ๐งฝ
Tell the classifier to 'forget' its training about a specific message.
msgโ The message'sMail::SpamAssassin::Messageobject. ๐จidโ An optional message-identification string, used internally to tag the message. If it isundef, one will be generated. It should be unique to that message. ๐ท๏ธ
$plugin->learner_sync () ๐
Tell the classifier to 'sync' any pending changes against the current user's training database. This is called by sa-learn --sync.
If you do not need to implement these for your classifier, create an implementation that just contains return 1. ๐
$plugin->learner_expire_old_training () โณ
Tell the classifier to perform infrequent, time-consuming cleanup of the current user's training database. This is called by sa-learn --force-expire.
If you do not need to implement these for your classifier, create an implementation that just contains return 1. ๐
$plugin->learner_is_scan_available () ๐ฏ
Should return 1 if it is possible to use the current user's training data for a message-scan operation, or 0 otherwise. ๐
$plugin->learner_dump_database () ๐ค
Dump information about the current user's training data to stdout. This is called by sa-learn --dump.
magicโ Set to 1 ifmagicname-value metadata should be dumped. ๐ชtoksโ Set to 1 if the database of tokens should be dumped. ๐ชregexโ Eitherundefto dump all tokens, or a value which specifies a regular expression subset of the tokens to dump. ๐
$plugin->learner_close () ๐
Close any open databases.
quietโ Set to 1 if warning messages should be suppressed. ๐คซ
โจ HELPER APIS โจ
These methods provide an API for plugins to register themselves to receive specific events, or control the callback chain behaviour. ๐ซถ
$plugin->register_eval_rule ($nameofevalsub, $ruletype) ๐งช
Plugins that implement an eval test will need to call this, so that SpamAssassin calls into the object when that eval test is encountered. See the REGISTERING EVAL RULES section for full details.
Since 4.0, optional $ruletype can be specified to enforce that eval function cannot be called with wrong ruletype from configuration, for example user using header FOO eval:foobar() instead of body FOO eval:foobar(). Mismatch will result in lint failure. $ruletype can be one of:
$Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS (allows both body and rawbody)
$Mail::SpamAssassin::Conf::TYPE_RAWBODY_EVALS
$Mail::SpamAssassin::Conf::TYPE_FULL_EVALS
$plugin->register_generated_rule_method ($nameofsub) ๐ ๏ธ
In certain circumstances, plugins may find it useful to compile perl functions from the ruleset, on the fly. It is important to remove these once the Mail::SpamAssassin object is deleted, however, and this API allows this.
Once the method $nameofsub has been generated, call this API with the name of the method (including full package scope). This indicates that it's a temporary piece of generated code, built from the SpamAssassin ruleset, and when Mail::SpamAssassin::finish() is called, the method will be destroyed.
This API was added in SpamAssassin 3.2.0. โจ
$plugin->register_method_priority($methodname, $priority) ๐ข
Indicate that the method named $methodname on the current object has a callback priority of $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 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. ๐ข
$plugin->inhibit_further_callbacks() โ
Tells the plugin handler to inhibit calling into other plugins in the plugin chain for the current callback. Frequently used when parsing configuration settings using parse_config(). ๐
๐ LOGGING ๐
Mail::SpamAssassin::Plugin::dbg($message) ๐
Output a debugging message $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 callMail::SpamAssassin::Plugin::dbg($msg). ๐
Mail::SpamAssassin::Plugin::info($message) โน๏ธ
Output an informational message $message, if the SpamAssassin object is running with informational messages turned on.
NOTE: This function is not available in the package namespace of general plugins and can't be called via$self->info(). If a plugin wishes to output debug information, it should callMail::SpamAssassin::Plugin::info($msg). ๐
In general, it is better for plugins to use the Mail::SpamAssassin::Logger module to import dbg and info directly, like so:
use Mail::SpamAssassin::Logger;
dbg("some message");
info("some other message");
๐ REGISTERING EVAL RULES ๐
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', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS)
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:
- The plugin object itself
- The
Mail::SpamAssassin::PerMsgStatusobject calling the rule - standard arguments for the rule type in use
- any and all arguments as specified in the configuration file
In other words, the eval test method should look something like this:
sub check_for_foo {
my ($self, $permsgstatus, ...arguments...) = @_;
...code returning 0 (miss), 1 (hit), or undef (async function)
}
The eval rule should return 1 for a hit, or 0 if the rule is not hit. Special case of return undef must be used when result is not yet ready and it will be later declared with PerMsgStatus functions got_hit() or rule_ready() - see their documentation for more info. Make sure not to return undef by mistake. โ ๏ธ
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}.
State for a single message being scanned should be stored on the $permsgstatus object, not on the $self object, since $self persists between scan operations. See the 'lifecycle note' on the check_start() method above. ๐ง
๐ STANDARD ARGUMENTS FOR RULE TYPES ๐
Plugins will be called with the same arguments as a standard EvalTest. Different rule types receive different information by default:
- header tests: no extra arguments
- body tests: fully rendered message as array reference
- rawbody tests: fully decoded message as array reference
- full tests: pristine message as scalar reference
The configuration file arguments will be passed in after the standard arguments. ๐
โจ BACKWARD COMPATIBILITY โจ
ok so here's the tea ๐ซ โ writing plugins means you might need to know whether a specific helper method is even supported on Mail::SpamAssassin::Plugin at the moment you're running. bestie, you don't just assume. you check. like this:
if ($self->can("name_of_method")) {
eval {
$self->name_of_method(); # etc.
}
} else {
# take fallback action
}
the $self->can("name_of_method") is giving "do you even go here" and if the answer is yes, you go for it inside an eval (safety first ๐
), and if the answer is no, you do the fallback thing instead. no crashing, no drama, no cap.
and this same trick? literally works everywhere โ same deal for the public APIs on objects of other types, like Mail::SpamAssassin::PerMsgStatus. same vibe check, different object. iykyk ๐ฆ
๐ SEE ALSO ๐
Mail::SpamAssassin(3)Mail::SpamAssassin::PerMsgStatus(3)https://wiki.apache.org/spamassassin/PluginWritingTipshttps://bz.apache.org/SpamAssassin/show_bug.cgi?id=2163
and that's the whole page, bestie!! ๐ you made it to the end, iconic behavior honestly ๐ this one's from perl v5.40.1, dated 2026-07-23, and it's the Mail::...Plugin(3pm) page in the Mail::...Plugin era of the docs โจ go write something slay, ily, bye ๐
๐ฆ where this page comes from
- ubuntu stonking: spamassassin 4.0.2+svn1935691-1build1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms