Tuesday, June 29, 2010

Silver Theatre Ottawa

Spamassassin plugin - part 2 How to write

As promised, we are the second and final round of this series.
inform you immediately that simple rules can be written directly into the configuration file. Within the plugin should be made more complex rules or based on data that changes frequently.

First you need to inherit from the base class Plugin
  package Mail:: SpamAssassin:: Plugin:: myPlugin; 
use Mail:: SpamAssassin:: Plugin;
use DBI;
our @ISA = qw(Mail::SpamAssassin::Plugin);

sub new {
my ($class, $mailsa) = @_;

# the usual perlobj boilerplate to create a subclass object
$class = ref($class) At this point we need to put the new rule that we want to introduce into the tool.
In the code snippet above we also important DBI libraries that allow us to query the database with a comfortable Object Model.

Here's an example of implementing a rule-based DB.


sub check_myplugin_header


{my ($ self, $ pms) = @ _;

# Extract informations from desidered
mail header $ head = $ pms-> get ('CustomHeader');

$ result = 0;
study;

chomp ($ head);
$ head = ~ s /;//;

$ dbh = DBI-> connect ("DBI: mysql: spamassassindb", "user", "pass ");
        
if(!defined $dbh)
{
die "Connessione al database non riuscita: $DBI::errstrn";
}

$query = "SELECT count(accepted_headers) FROM headers WHERE accepted_headers = $head";

# Insert query into database instructions
$sth = $dbh->prepare($query);

# Database query execution
$sth->execute;

$count = $sth->fetchrow();


if($count eq 0)
{
# Report error
$result = 1;
}

# Disconnect from database
$dbh->disconnect();

return $result;
}



Questo frammento di codice è piuttosto semplice e fa una query al db, tramite DBI, dopo aver bonificato la stringa da passare.
Una cosa sicuramente da notare è il paramtro $pms che consente di accedere a tutti gli headers della mail.
Forniamo allora questo messaggio


From god@heaven.com Thu Jun 17 21:33:41 2010
Return-Path: <god@heaven.com>
X-Original-To: account@post-server
Delivered-To: account@post-server
Received: from post-server (host [127.0.0.1])
by post-server (Postfix) with SMTP id B66E9424F5
for \u0026lt;account@post-server>; Thu, 30 Jun 2010 21:33:20 +0200 (BST)
CustomHeader: blablabla
Subject: Try this
Simple mail to test my plugin.



to spamassassin and have two different behaviors:
  
If the string "blablabla" is contained in the database, the rule will not add to the points defined in the configuration file (2.0 from the previous post)

If the string " blablabla "is not included then the rule will have a hit that will add 2 points to total score. In the latter case, if this score does exceed the threshold (defined always in the various configuration files) then the message will identified as SPAM!



is a rather vast topic that takes time to be assimilated. This is just one input that wants to stimulate interest. I hope I was clear and are available for on-line help, as I do.

Have fun;)

0 comments:

Post a Comment