summaryrefslogtreecommitdiffstats
path: root/extensions/RequestNagger/bin/send-request-nags.pl
blob: 93265b9ee4e75683ecf6c731f8243cb09d72f83f (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/perl

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use strict;
use warnings;

use FindBin qw($RealBin);
use lib "$RealBin/../../..";

use Bugzilla;
BEGIN { Bugzilla->extensions() }

use Bugzilla::Attachment;
use Bugzilla::Bug;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Extension::RequestNagger::Constants;
use Bugzilla::Extension::RequestNagger::Bug;
use Bugzilla::Mailer;
use Bugzilla::User;
use Bugzilla::Util qw(format_time);
use Email::MIME;
use Sys::Hostname;

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);

my $DO_NOT_NAG = grep { $_ eq '-d' } @ARGV;

my $dbh = Bugzilla->dbh;
my $date = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
$date = format_time($date, '%a, %d %b %Y %T %z', 'UTC');

# delete expired defers
$dbh->do("DELETE FROM nag_defer WHERE defer_until <= CURRENT_DATE()");
Bugzilla->switch_to_shadow_db();

# send nags to requestees
send_nags(
    sql             => REQUESTEE_NAG_SQL,
    template        => 'requestee',
    recipient_field => 'requestee_id',
    date            => $date,
);

# send nags to watchers
send_nags(
    sql             => WATCHING_NAG_SQL,
    template        => 'watching',
    recipient_field => 'watcher_id',
    date            => $date,
);

sub send_nags {
    my (%args) = @_;
    my $rows = $dbh->selectall_arrayref($args{sql}, { Slice => {} });

    # iterate over rows, sending email when the current recipient changes
    my $requests = [];
    my $current_recipient;
    foreach my $request (@$rows) {
        # send previous user's requests
        if (!$current_recipient || $request->{$args{recipient_field}} != $current_recipient->id) {
            send_email(%args, recipient => $current_recipient, requests => $requests);
            $current_recipient = Bugzilla::User->new({ id => $request->{$args{recipient_field}}, cache => 1 });
            $requests = [];
        }

        # check group membership
        $request->{requestee} = Bugzilla::User->new({ id => $request->{requestee_id}, cache => 1 });
        my $group;
        foreach my $type (FLAG_TYPES) {
            next unless $type->{type} eq $request->{flag_type};
            $group = $type->{group};
            last;
        }
        next unless $request->{requestee}->in_group($group);

        # check bug visibility
        next unless $current_recipient->can_see_bug($request->{bug_id});

        # create objects
        $request->{bug} = Bugzilla::Bug->new({ id => $request->{bug_id}, cache => 1 });
        $request->{requester} = Bugzilla::User->new({ id => $request->{requester_id}, cache => 1 });
        $request->{flag} = Bugzilla::Flag->new({ id => $request->{flag_id}, cache => 1 });
        if ($request->{attach_id}) {
            $request->{attachment} = Bugzilla::Attachment->new({ id => $request->{attach_id}, cache => 1 });
            # check attachment visibility
            next if $request->{attachment}->isprivate && !$current_recipient->is_insider;
        }
        if (exists $request->{watcher_id}) {
            $request->{watcher} = Bugzilla::User->new({ id => $request->{watcher_id}, cache => 1 });
        }

        # add this request to the current user's list
        push(@$requests, $request);
    }
    send_email(%args, recipient => $current_recipient, requests => $requests);
}

sub send_email {
    my (%vars) = @_;
    my $vars = \%vars;
    return unless $vars->{recipient} && @{ $vars->{requests} };

    my $request_list = delete $vars->{requests};

    # if securemail is installed, we need to encrypt or censor emails which
    # contain non-public bugs
    my $default_user = Bugzilla::User->new();
    my $securemail = $vars->{recipient}->can('public_key');
    my $has_key = $securemail && $vars->{recipient}->public_key;
    # have to do this each time as objects are shared between requests
    my $has_private_bug = 0;
    foreach my $request (@{ $request_list }) {
        # rebless bug objects into our subclass
        bless($request->{bug}, 'Bugzilla::Extension::RequestNagger::Bug');
        # and tell that object to hide the summary if required
        if ($securemail && !$default_user->can_see_bug($request->{bug})) {
            $has_private_bug = 1;
            $request->{bug}->{secure_bug} = !$has_key;
        }
        else {
            $request->{bug}->{secure_bug} = 0;
        }
    }
    my $encrypt = $securemail && $has_private_bug && $has_key;

    # restructure the list to group by requestee then flag type
    my $requests = {};
    my %seen_types;
    foreach my $request (@{ $request_list }) {
        # by requestee
        my $requestee_login = $request->{requestee}->login;
        $requests->{$requestee_login} ||= {
            requestee => $request->{requestee},
            types     => {},
            typelist  => [],
        };

        # by flag type
        my $types = $requests->{$requestee_login}->{types};
        my $flag_type = $request->{flag_type};
        $types->{$flag_type} ||= [];

        push @{ $types->{$flag_type} }, $request;
        $seen_types{$requestee_login}{$flag_type} = 1;
    }
    foreach my $requestee_login (keys %seen_types) {
        my @flag_types;
        foreach my $flag_type (map { $_->{type} } FLAG_TYPES) {
            push @flag_types, $flag_type if $seen_types{$requestee_login}{$flag_type};
        }
        $requests->{$requestee_login}->{typelist} = \@flag_types;
    }
    $vars->{requests} = $requests;

    # generate email
    my $template = Bugzilla->template_inner($vars->{recipient}->setting('lang'));
    my $template_file = $vars->{template};

    my ($header, $text);
    $template->process("email/request_nagging-$template_file-header.txt.tmpl", $vars, \$header)
        || ThrowTemplateError($template->error());
    $header .= "\n";
    $template->process("email/request_nagging-$template_file.txt.tmpl", $vars, \$text)
        || ThrowTemplateError($template->error());

    my @parts = (
        Email::MIME->create(
            attributes => { content_type => "text/plain" },
            body => $text,
        )
    );
    if ($vars->{recipient}->setting('email_format') eq 'html') {
        my $html;
        $template->process("email/request_nagging-$template_file.html.tmpl", $vars, \$html)
            || ThrowTemplateError($template->error());
        push @parts, Email::MIME->create(
            attributes => { content_type => "text/html" },
            body => $html,
        );
    }

    my $email = Email::MIME->new($header);
    $email->header_set('X-Generated-By' => hostname());
    if (scalar(@parts) == 1) {
        $email->content_type_set($parts[0]->content_type);
    } else {
        $email->content_type_set('multipart/alternative');
    }
    $email->parts_set(\@parts);
    if ($encrypt) {
        $email->header_set('X-Bugzilla-Encrypt' => '1');
    }

    # send
    if ($DO_NOT_NAG) {
        print $email->as_string, "\n";
    } else {
        MessageToMTA($email);
    }
}