]> git.uio.no Git - usit-rt.git/blame - local/lib/RT/Interface/Email/Filter/SpamHeader.pm
Changed SpamHeader to accept SpamNoFilterRegexp
[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) {
a128e57f
MKG
52 my $plain_regexp = "";
53 if (defined $RT::SpamNoFilterRegexp) {$plain_regexp = $RT::SpamNoFilterRegexp}
54 my @ToObjs = Mail::Address->parse( $head->get('To') );
84fb5b46
MKG
55 my @CcObjs = Mail::Address->parse( $head->get('Cc') );
56 my @BccObjs = Mail::Address->parse( $head->get('Envelope-To') );
a128e57f 57 my $regexp = ("^($plain_regexp".(join "|",map quotemeta,@RT::SpamNoFilter).")\$");
84fb5b46
MKG
58 for (@ToObjs, @CcObjs, @BccObjs) {
59 if ($_->address =~ m/$regexp/i) {
60 $RT::Logger->info("SpamHeader: Accepting message for nonfiltered list - ".$_->address);
61 return ($args{'CurrentUser'}, $args{'AuthLevel'});
62 }
63 }
64 }
65
4286a352 66 my $spamtag = $head->get($RT::SpamHeader) || '';
84fb5b46
MKG
67 if (defined $RT::SpamHighMatch && $spamtag =~ m/$RT::SpamHighMatch/) {
68 if ($RT::SpamHighQueue) {
69 # Move to spam queue
70 $args{'Queue'}->Load( $RT::SpamHighQueue );
71 } else {
72 # tell Gateway() to drop the mail
73 $RT::Logger->info("SpamHeader: Dropping spam message!");
74 return ($args{'CurrentUser'}, -1);
75 }
76 }
77
78 if ($spamtag =~ m/$RT::SpamLowMatch/) {
79 if ($RT::SpamLowQueue) {
80 # Move to spam queue
81 $args{'Queue'}->Load( $RT::SpamLowQueue );
82 } else {
83 # tell Gateway() to drop the mail
84 $RT::Logger->info("SpamHeader: Dropping spam message!");
85 return ($args{'CurrentUser'}, -1);
86 }
87 }
88
89 $RT::Logger->debug("SpamHeader: Accepting non-spam message.");
90 return ($args{'CurrentUser'}, $args{'AuthLevel'});
91}
92
93=head1 NAME
94
95RT::Interface::Email::Filter::SpamHeader - Filter spam based on header tags
96
97=head1 SYNOPSIS
98
99 Set($SpamHeader, "X-UiO-Spam-Score"); # Required
100 Set($SpamLowMatch, "ss+"); # Required
101 Set($SpamLowQueue, "spam"); # Optional
102 Set($SpamHighMatch, "ssssss+"); # Optional
103 Set($SpamHighQueue, undef); # Optional
104 @RT::MailPlugins = ("Filter::SpamHeader", ...); # Required
105
106=head1 DESCRIPTION
107
108This plugin checks to see if an incoming mail is spam by looking at
109the header field given in $SpamHeader, matching it using the regex in
110$SpamLowMatch and $SpamHighMatch. If the regex matches, it is
111considered spam and dropped on the floor or moved to a spam queue if
112the corresponding queue variable is set. Otherwise, it is handled as
113normal.
114
115=cut
116
1171;