SpamHeader fixed to not warn about empty spamtag
[usit-rt.git] / local / lib / RT / Interface / Email / Filter / SpamHeader.pm
CommitLineData
84fb5b46
MKG
1# BEGIN LICENSE BLOCK
2#
3# Copyright (c) 2004 Petter Reinholdtsen <pere@hungry.com>
4#
5# This work is made available to you under the terms of Version 2 of
6# the GNU General Public License. A copy of that license should have
7# been provided with this software, but in any event can be snarfed
8# from www.gnu.org.
9#
10# This work is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# Unless otherwise specified, all modifications, corrections or
16# extensions to this work which alter its source code become the
17# property of Best Practical Solutions, LLC when submitted for
18# inclusion in the work.
19#
20#
21# END LICENSE BLOCK
22package RT::Interface::Email::Filter::SpamHeader;
23
24use RT::EmailParser;
25use Mail::Address;
26
27sub GetCurrentUser {
28 my %args = ( Message => undef,
29 CurrentUser => undef,
30 AuthLevel => undef,
31 Queue => undef,
32 @_ );
33
34 # Check configuration.
35 unless ($RT::SpamHeader) {
36 $RT::Logger->error("SpamHeader: Content of \$RT::SpamHeader is empty.");
37 return ($args{'CurrentUser'}, $args{'AuthLevel'});
38 }
39 unless ($RT::SpamLowMatch) {
40 $RT::Logger->error("SpamHeader: Content of \$RT::SpamLowMatch is empty.");
41 return ($args{'CurrentUser'}, $args{'AuthLevel'});
42 }
43 my $Message = $args{'Message'};
44 unless ($Message) {
45 $RT::Logger->error("SpamHeader: \$Message is empty.");
46 return ($args{'CurrentUser'}, $args{'AuthLevel'});
47 }
48
49 my $head = $Message->head;
50
51 if (defined @RT::SpamNoFilter) {
52 my @ToObjs = Mail::Address->parse( $head->get('To') );
53 my @CcObjs = Mail::Address->parse( $head->get('Cc') );
54 my @BccObjs = Mail::Address->parse( $head->get('Envelope-To') );
55 my $regexp = ("^(".(join "|",map quotemeta,@RT::SpamNoFilter).")\$");
56 for (@ToObjs, @CcObjs, @BccObjs) {
57 if ($_->address =~ m/$regexp/i) {
58 $RT::Logger->info("SpamHeader: Accepting message for nonfiltered list - ".$_->address);
59 return ($args{'CurrentUser'}, $args{'AuthLevel'});
60 }
61 }
62 }
63
4286a352 64 my $spamtag = $head->get($RT::SpamHeader) || '';
84fb5b46
MKG
65 if (defined $RT::SpamHighMatch && $spamtag =~ m/$RT::SpamHighMatch/) {
66 if ($RT::SpamHighQueue) {
67 # Move to spam queue
68 $args{'Queue'}->Load( $RT::SpamHighQueue );
69 } else {
70 # tell Gateway() to drop the mail
71 $RT::Logger->info("SpamHeader: Dropping spam message!");
72 return ($args{'CurrentUser'}, -1);
73 }
74 }
75
76 if ($spamtag =~ m/$RT::SpamLowMatch/) {
77 if ($RT::SpamLowQueue) {
78 # Move to spam queue
79 $args{'Queue'}->Load( $RT::SpamLowQueue );
80 } else {
81 # tell Gateway() to drop the mail
82 $RT::Logger->info("SpamHeader: Dropping spam message!");
83 return ($args{'CurrentUser'}, -1);
84 }
85 }
86
87 $RT::Logger->debug("SpamHeader: Accepting non-spam message.");
88 return ($args{'CurrentUser'}, $args{'AuthLevel'});
89}
90
91=head1 NAME
92
93RT::Interface::Email::Filter::SpamHeader - Filter spam based on header tags
94
95=head1 SYNOPSIS
96
97 Set($SpamHeader, "X-UiO-Spam-Score"); # Required
98 Set($SpamLowMatch, "ss+"); # Required
99 Set($SpamLowQueue, "spam"); # Optional
100 Set($SpamHighMatch, "ssssss+"); # Optional
101 Set($SpamHighQueue, undef); # Optional
102 @RT::MailPlugins = ("Filter::SpamHeader", ...); # Required
103
104=head1 DESCRIPTION
105
106This plugin checks to see if an incoming mail is spam by looking at
107the header field given in $SpamHeader, matching it using the regex in
108$SpamLowMatch and $SpamHighMatch. If the regex matches, it is
109considered spam and dropped on the floor or moved to a spam queue if
110the corresponding queue variable is set. Otherwise, it is handled as
111normal.
112
113=cut
114
1151;