Creating a custom tag via a plugin

2004-09-16 Thread Brian Keifer
Hi, list.

I'm using SpamAssassin 3.0 RC5 to tag messages according to per-user
preferences specified in an SQL database.  Users have the ability to
modify their settings in the database via a web-based application
(Sam, part of the Horde project).  One of the settings is
required_hits.  If the message gets tagged as spam, a maildrop script
delivers the message to the user's SPAM mailbox.  If it's not spam,
it gets delivered normally.

I'd like to add a second value, specified by the user and stored in
the same table as all of the other SA prefs.  This would be a second,
higher, score that I'd like to have inserted as a second header into
each message marked as spam.  Maildrop would then compare the value of
this new header to the number of hits the message got.  If the number
of hits is greater than this second threshold, the maildrop script
will delete the message without delivering it.  I'd envisioned the
second header appearing something like this:

X-Spam-Delete: 9.3

In this situation, any messages that got a score of 9.3 or greater
would never make it to the user's mailbox.

I've found the add_header option but naturally none of the _TAGS_
reference my custom value.  What I'd like to know is if there's a way
to create an additional tag via a SA 3.0 plugin.  Failing that, is
there a way to take an arbitrary value from SA's config table and
inject it into a header?

Thanks in advance,

-Brian


Re: Creating a custom tag via a plugin

2004-09-16 Thread Michael Parker
On Thu, Sep 16, 2004 at 12:01:21PM -0400, Brian Keifer wrote:
[snip]
 
 I'd like to add a second value, specified by the user and stored in
 the same table as all of the other SA prefs.  This would be a second,
 higher, score that I'd like to have inserted as a second header into
 each message marked as spam.  Maildrop would then compare the value of
 this new header to the number of hits the message got.  If the number
 of hits is greater than this second threshold, the maildrop script
 will delete the message without delivering it.  I'd envisioned the
 second header appearing something like this:
 
 X-Spam-Delete: 9.3
 
 In this situation, any messages that got a score of 9.3 or greater
 would never make it to the user's mailbox.
 
 I've found the add_header option but naturally none of the _TAGS_
 reference my custom value.  What I'd like to know is if there's a way
 to create an additional tag via a SA 3.0 plugin.  Failing that, is
 there a way to take an arbitrary value from SA's config table and
 inject it into a header?

add_header is the way to go.  Here is a short plugin that adds a new
config option (custom_delete_score) and a new TAG value that you can
use in the add_header command.

Here is the module, just drop this in somewhere, sorry for the lack of
documentation, hopefully it's self explainable:

package Mail::SpamAssassin::Plugin::CustomDeleteTag;

use Mail::SpamAssassin::Plugin;
use strict;
use bytes;

use vars qw(@ISA);
@ISA = qw(Mail::SpamAssassin::Plugin);

sub new {
  my $class = shift;
  my $mailsaobject = shift;

  # some boilerplate...
  $class = ref($class) || $class;
  my $self = $class-SUPER::new($mailsaobject);
  bless ($self, $class);

  return $self;
}

sub parse_config {
  my ($self, $opts) = @_;

  my $key = $opts-{key};

  if ($key eq 'custom_delete_score') {
if ($opts-{value} =~ /^\d+$/) {
  $opts-{conf}-{custom_delete_score} = $opts-{value};
  $self-inhibit_further_callbacks();
  return 1;
}
  }
  return 0;
}

sub parsed_metadata {
  my ($self, $opts) = @_;

  $opts-{permsgstatus}-{tag_data}-{CUSTOMDELETESCORE} = 
$opts-{permsgstatus}-{conf}-{custom_delete_score};
}

1;


Then I added this to my init.pre:

loadplugin Mail::SpamAssassin::Plugin::CustomDeleteTag

And this to my local.cf:

add_header all Delete _CUSTOMDELETESCORE_

In my user_prefs file I have:

custom_delete_score 56

You could just as easily put this in your SQL user prefs table.

Which when run against any message give this header:

X-Spam-Delete: 56

Hopefully that helps.

Michael



Re: Creating a custom tag via a plugin

2004-09-16 Thread Michael Parker
On Thu, Sep 16, 2004 at 09:35:34AM -0700, Justin Mason wrote:
 
 
 I'm pretty sure there's a bugzilla bug open about this... I can't find it
 though.   Could you open a bug at http://bugzilla.spamassassin.org/
 to request it?
 

Turns out it's easy enough to do, but it might be cool to add a hook
that gets called in _get_tag and passes in the %tags hash so you can
stick things in there.  Or something similar.

Michael


Re: Creating a custom tag via a plugin

2004-09-16 Thread Justin Mason
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


Michael Parker writes:
 On Thu, Sep 16, 2004 at 09:35:34AM -0700, Justin Mason wrote:
  
  I'm pretty sure there's a bugzilla bug open about this... I can't find it
  though.   Could you open a bug at http://bugzilla.spamassassin.org/
  to request it?
 
 Turns out it's easy enough to do, but it might be cool to add a hook
 that gets called in _get_tag and passes in the %tags hash so you can
 stick things in there.  Or something similar.

if I recall correctly, someone was suggesting a method callback;
in other words, the plugin would call a function something like

register_tag_callback(TAGNAME, $self, \my_method);

and the tag-expansion code would then know to call $self-my_method(...)
if that tag was encountered, to get its value ($self being the plugin obj
in that case).  this sounds perfect to me.

- --j.
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Exmh CVS

iD8DBQFBSc5BQTcbUG5Y7woRAvTXAJsFJMdrZGADrlwObevtxsh0YZdGywCePvlM
iTsrziyyxsxxQ/PwJwKE+aI=
=FFeQ
-END PGP SIGNATURE-