summaryrefslogtreecommitdiffstats
path: root/extensions/Profanivore/Extension.pm
blob: 266ab9ff06c24ad44ba7a37f65b1574ca1fe6197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Profanivore Bugzilla Extension.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2010 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Gervase Markham <gerv@gerv.net>

package Bugzilla::Extension::Profanivore;
use strict;
use base qw(Bugzilla::Extension);

use Regexp::Common 'RE_ALL';

our $VERSION = '0.01';

sub bug_format_comment {
    my ($self, $args) = @_;
    my $regexes = $args->{'regexes'};
    my $comment = $args->{'comment'};
  
    # Censor profanities if the comment author is not reasonably trusted.
    # However, allow people to see their own profanities, which might stop
    # them immediately noticing and trying to go around the filter. (I.e.
    # it tries to stop an arms race starting.)
    if ($comment &&
        !$comment->author->in_group('editbugs') &&
        $comment->author->id != Bugzilla->user->id) 
    {
        push (@$regexes, {
            match => RE_profanity(),
            replace => \&_replace_profanity
        });
    }
}

sub _replace_profanity {
    # We don't have access to the actual profanity.
    return "****";
}

sub mailer_before_send {
    my ($self, $args) = @_;
    my $email = $args->{'email'};
    
    my $author    = $email->header("X-Bugzilla-Who");
    my $recipient = $email->header("To");
    
    if ($author && $recipient) {
        my $email_suffix = Bugzilla->params->{'emailsuffix'};
        if ($email_suffix ne '') {
            $recipient =~ s/\Q$email_suffix\E$//;
            $author    =~ s/\Q$email_suffix\E$//;
        }
        
        $author    = new Bugzilla::User({ name => $author });
        $recipient = new Bugzilla::User({ name => $recipient });
    
        if ($author->id && 
            !$author->in_group('editbugs') &&
            $author->id ne $recipient->id) 
        {
            my $body = $email->body_str();

            my $offensive = RE_profanity();
            $body =~ s/$offensive/****/g;

            $email->body_str_set($body);
        }
    }
}

__PACKAGE__->NAME;