1=head1 testplugin.pm
2
3To try this out, write these lines to /etc/mail/spamassassin/plugintest.cf:
4
5  loadplugin     myTestPlugin
6  header         MY_TEST_PLUGIN eval:check_test_plugin()
7
8=cut
9
10package myTestPlugin;
11
12use Mail::SpamAssassin::Plugin;
13use Mail::SpamAssassin::Logger;
14use strict;
15use bytes;
16
17our @ISA = qw(Mail::SpamAssassin::Plugin);
18
19# constructor: register the eval rule
20sub new {
21  my $class = shift;
22  my $mailsaobject = shift;
23
24  # some boilerplate...
25  $class = ref($class) || $class;
26  my $self = $class->SUPER::new($mailsaobject);
27  bless ($self, $class);
28
29  # the important bit!
30  $self->register_eval_rule ("check_test_plugin");
31  $self->register_eval_rule ("check_return_2");
32  $self->register_eval_rule ("sleep_based_on_header");
33
34  print "registered myTestPlugin: $self\n";
35  return $self;
36}
37
38# and the eval rule itself
39sub check_test_plugin {
40  my ($self, $permsgstatus) = @_;
41  print "myTestPlugin eval test called: $self\n";
42
43  print "test: plugins loaded: ".
44        join(" ", sort $self->{main}->get_loaded_plugins_list()).
45        "\n";
46
47  my $file = $ENV{'SPAMD_PLUGIN_COUNTER_FILE'};
48  if ($file) {
49    open (IN, "<$file") or warn;
50    my $count = <IN>; $count += 0;
51    close IN;
52
53    dbg("test: called myTestPlugin, round $count");
54
55    open (OUT, ">$file") or warn;
56    print OUT ++$count;
57    close OUT or warn;
58  }
59
60  return 1;
61}
62
63sub sleep_based_on_header {
64  my ($self, $permsgstatus) = @_;
65  my $secs = $permsgstatus->{msg}->get_header("Sleep-Time");
66  chop $secs;
67
68  if ($secs) {
69    warn "sleeping for $secs seconds...";
70    sleep ($secs+0);
71  }
72
73  return 1;
74}
75
76sub check_return_2 {
77  return 2;
78}
79
80sub extract_metadata {
81  my ($self, $opts) = @_;
82  my $msg = $opts->{msg};
83  print "myTestPlugin extract_metadata: $self\n";
84  $msg->put_metadata("Plugin-Meta-Test", "bar");
85  return 1;
86}
87
88sub per_msg_finish {
89  my ($self, $permsgstatus) = @_;
90  print "myTestPlugin finishing: $self\n";
91  return 1;
92}
93
941;
95