summaryrefslogtreecommitdiffstats
path: root/extensions/RequestNagger/bin/send-request.nags.pl
blob: c62d91f0395d90d33e21c912de8ede90e8f14844 (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
#!/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::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} };

    # restructure the list to group by requestee then flag type
    my $request_list = delete $vars->{requests};
    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);

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