RT#76877 Add an outgoing email notification blacklist
[freeside.git] / rt / lib / RT / Action / SendEmail_Local.pm
1 use strict;
2 use warnings;
3 no warnings qw(redefine);
4
5 package RT::Action::SendEmail;
6
7 =head1 DESCRIPTION
8
9 Overlay for RT::Action::SendEmail to implement a global email notifications
10 blacklist.  All components that send email using the SendEmail action will
11 be affected by this blacklist.
12
13 The web interface uses these filters to decide which email addresses to
14 display as sendable.  This gives us the added bonus of transparency.  If
15 an e-mail address is blacklisted, it will never appear in the recipient
16 list on a ticket correspondance.
17
18 =head1 USAGE
19
20 To enable the blacklist, add a configuration option to RT_SiteConfig.pm
21
22     Set(@NotifyBlacklist,(qw(reddit.com slashdot.org frank)));
23
24 If an email address regex matches any item in the list, no email is sent
25
26 =head1 DEV NOTE
27
28 This overlay implementation will need to be maintained if RT updates
29 the SendEmail action to filter addresses differently.  The benefit of
30 using rt overlays is our library changes easily persist between rt versions,
31 and don't need to be reimplemented with each release of rt.  The downside
32 of overlays if the underlying rt core functionality changes, our overlay
33 may break rt until it is removed or updated.
34
35 For information on RT library overlays,
36 see L<https://rt-wiki.bestpractical.com/wiki/CustomizingWithOverlays>
37
38 =cut
39
40 sub RecipientFilter {
41     my $self = shift;
42
43     unless (ref $self->{RecipientFilter}) {
44       my @blacklist;
45       eval { @blacklist = @RT::NotifyBlacklist };
46       if (@blacklist) {
47         push @{$self->{RecipientFilter}}, {
48           All => 1,
49           Callback => sub {
50             my $email = shift;
51             for my $block (@blacklist) {
52               return "$email is blacklisted by NotifyBlacklist, skipping"
53                 if $email =~ /$block/i;
54             }
55             return 0;
56           }
57         };
58       }
59     }
60     push @{ $self->{RecipientFilter}}, {@_};
61 }
62
63 1;