BugMail.pm 20.6 KB
Newer Older
1
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
2
#
3 4 5 6 7 8 9 10 11 12
# 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.
#
terry%netscape.com's avatar
terry%netscape.com committed
13
# The Original Code is the Bugzilla Bug Tracking System.
14
#
terry%netscape.com's avatar
terry%netscape.com committed
15
# The Initial Developer of the Original Code is Netscape Communications
16 17 18 19
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
20
# Contributor(s): Terry Weissman <terry@mozilla.org>,
21 22
#                 Bryce Nesbitt <bryce-mozilla@nextbus.com>
#                 Dan Mosedale <dmose@mozilla.org>
23
#                 Alan Raetz <al_raetz@yahoo.com>
24
#                 Jacob Steenhagen <jake@actex.net>
25
#                 Matthew Tuck <matty@chariot.net.au>
26 27
#                 Bradley Baetz <bbaetz@student.usyd.edu.au>
#                 J. Paul Reed <preed@sigkill.com>
28
#                 Gervase Markham <gerv@gerv.net>
29
#                 Byron Jones <bugzilla@glob.com.au>
30
#                 Reed Loden <reed@reedloden.com>
31

32
use strict;
terry%netscape.com's avatar
terry%netscape.com committed
33

34
package Bugzilla::BugMail;
terry%netscape.com's avatar
terry%netscape.com committed
35

36
use Bugzilla::Error;
37
use Bugzilla::User;
38
use Bugzilla::Constants;
39
use Bugzilla::Util;
40
use Bugzilla::Bug;
41
use Bugzilla::Classification;
42
use Bugzilla::Product;
43
use Bugzilla::Component;
44
use Bugzilla::Status;
45
use Bugzilla::Mailer;
46
use Bugzilla::Hook;
47

48 49
use Date::Parse;
use Date::Format;
50

51 52 53 54 55
use constant FORMAT_TRIPLE => "%19s|%-28s|%-28s";
use constant FORMAT_3_SIZE => [19,28,28];
use constant FORMAT_DOUBLE => "%19s %-55s";
use constant FORMAT_2_SIZE => [19,55];

56 57 58
use constant BIT_DIRECT    => 1;
use constant BIT_WATCHING  => 2;

59 60 61 62 63 64
# We use this instead of format because format doesn't deal well with
# multi-byte languages.
sub multiline_sprintf {
    my ($format, $args, $sizes) = @_;
    my @parts;
    my @my_sizes = @$sizes; # Copy this so we don't modify the input array.
65
    foreach my $string (@$args) {
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        my $size = shift @my_sizes;
        my @pieces = split("\n", wrap_hard($string, $size));
        push(@parts, \@pieces);
    }

    my $formatted;
    while (1) {
        # Get the first item of each part.
        my @line = map { shift @$_ } @parts;
        # If they're all undef, we're done.
        last if !grep { defined $_ } @line;
        # Make any single undef item into ''
        @line = map { defined $_ ? $_ : '' } @line;
        # And append a formatted line
80 81 82 83 84
        $formatted .= sprintf($format, @line);
        # Remove trailing spaces, or they become lots of =20's in 
        # quoted-printable emails.
        $formatted =~ s/\s+$//;
        $formatted .= "\n";
85 86
    }
    return $formatted;
87
}
88 89 90

sub three_columns {
    return multiline_sprintf(FORMAT_TRIPLE, \@_, FORMAT_3_SIZE);
91 92
}

93 94 95 96 97 98 99 100 101
sub relationships {
    my $ref = RELATIONSHIPS;
    # Clone it so that we don't modify the constant;
    my %relationships = %$ref;
    Bugzilla::Hook::process('bugmail_relationships', 
                            { relationships => \%relationships });
    return %relationships;
}

102 103 104 105 106 107 108 109 110
# This is a bit of a hack, basically keeping the old system()
# cmd line interface. Should clean this up at some point.
#
# args: bug_id, and an optional hash ref which may have keys for:
# changer, owner, qa, reporter, cc
# Optional hash contains values of people which will be forced to those
# roles when the email is sent.
# All the names are email addresses, not userids
# values are scalars, except for cc, which is a list
111
sub Send {
112
    my ($id, $forced) = (@_);
113

114
    my $dbh = Bugzilla->dbh;
115
    my $bug = new Bugzilla::Bug($id);
116

117
    # Only used for headers in bugmail for new bugs
118
    my @fields = Bugzilla->get_fields({obsolete => 0, mailhead => 1});
119

120 121
    my $start = $bug->lastdiffed;
    my $end   = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
122

123 124 125 126 127
    # Bugzilla::User objects of people in various roles. More than one person
    # can 'have' a role, if the person in that role has changed, or people are
    # watching.
    my @assignees = ($bug->assigned_to);
    my @qa_contacts = ($bug->qa_contact);
128

129
    my @ccs = @{ $bug->cc_users };
130 131 132 133 134 135

    # Include the people passed in as being in particular roles.
    # This can include people who used to hold those roles.
    # At this point, we don't care if there are duplicates in these arrays.
    my $changer = $forced->{'changer'};
    if ($forced->{'owner'}) {
136
        push (@assignees, Bugzilla::User->check($forced->{'owner'}));
137
    }
138
    
139
    if ($forced->{'qacontact'}) {
140
        push (@qa_contacts, Bugzilla::User->check($forced->{'qacontact'}));
141
    }
142 143 144
    
    if ($forced->{'cc'}) {
        foreach my $cc (@{$forced->{'cc'}}) {
145
            push(@ccs, Bugzilla::User->check($cc));
146 147 148
        }
    }
    
149
    my @args = ($bug->id);
150

151
    # If lastdiffed is NULL, then we don't limit the search on time.
152 153 154 155
    my $when_restriction = '';
    if ($start) {
        $when_restriction = ' AND bug_when > ? AND bug_when <= ?';
        push @args, ($start, $end);
156
    }
157 158
    
    my $diffs = $dbh->selectall_arrayref(
159
           "SELECT profiles.login_name, profiles.realname, fielddefs.description,
160
                   bugs_activity.bug_when, bugs_activity.removed, 
161 162
                   bugs_activity.added, bugs_activity.attach_id, fielddefs.name,
                   bugs_activity.comment_id
163 164
              FROM bugs_activity
        INNER JOIN fielddefs
165
                ON fielddefs.id = bugs_activity.fieldid
166 167 168 169 170
        INNER JOIN profiles
                ON profiles.userid = bugs_activity.who
             WHERE bugs_activity.bug_id = ?
                   $when_restriction
          ORDER BY bugs_activity.bug_when", undef, @args);
171

172
    my @new_depbugs;
173
    my $difftext = "";
174 175
    my $diffheader = "";
    my @diffparts;
176
    my $lastwho = "";
177
    my $fullwho;
178
    my @changedfields;
179
    foreach my $ref (@$diffs) {
180
        my ($who, $whoname, $what, $when, $old, $new, $attachid, $fieldname, $comment_id) = (@$ref);
181
        my $diffpart = {};
182 183
        if ($who ne $lastwho) {
            $lastwho = $who;
184
            $fullwho = $whoname ? "$whoname <$who>" : $who;
185
            $diffheader = "\n$fullwho changed:\n\n";
186
            $diffheader .= three_columns("What    ", "Removed", "Added");
187
            $diffheader .= ('-' x 76) . "\n";
188
        }
189
        $what =~ s/^(Attachment )?/Attachment #$attachid / if $attachid;
190 191
        if( $fieldname eq 'estimated_time' ||
            $fieldname eq 'remaining_time' ) {
192 193
            $old = format_time_decimal($old);
            $new = format_time_decimal($new);
194
        }
195 196 197
        if ($fieldname eq 'dependson') {
            push(@new_depbugs, grep {$_ =~ /^\d+$/} split(/[\s,]+/, $new));
        }
198
        if ($attachid) {
199 200 201
            ($diffpart->{'isprivate'}) = $dbh->selectrow_array(
                'SELECT isprivate FROM attachments WHERE attach_id = ?',
                undef, ($attachid));
202
        }
203 204 205 206 207 208
        if ($fieldname eq 'longdescs.isprivate') {
            my $comment = Bugzilla::Comment->new($comment_id);
            my $comment_num = $comment->count;
            $what =~ s/^(Comment )?/Comment #$comment_num /;
            $diffpart->{'isprivate'} = $new;
        }
209
        $difftext = three_columns($what, $old, $new);
210 211 212 213
        $diffpart->{'header'} = $diffheader;
        $diffpart->{'fieldname'} = $fieldname;
        $diffpart->{'text'} = $difftext;
        push(@diffparts, $diffpart);
214
        push(@changedfields, $what);
215 216
    }

217
    my @depbugs;
218
    my $deptext = "";
219 220 221 222 223 224 225 226 227
    # Do not include data about dependent bugs when they have just been added.
    # Completely skip checking for dependent bugs on bug creation as all
    # dependencies bugs will just have been added.
    if ($start) {
        my $dep_restriction = "";
        if (scalar @new_depbugs) {
            $dep_restriction = "AND bugs_activity.bug_id NOT IN (" .
                               join(", ", @new_depbugs) . ")";
        }
228

229
        my $dependency_diffs = $dbh->selectall_arrayref(
230
           "SELECT bugs_activity.bug_id, bugs.short_desc, fielddefs.name, 
231 232
                   fielddefs.description, bugs_activity.removed,
                   bugs_activity.added
233 234 235 236 237 238
              FROM bugs_activity
        INNER JOIN bugs
                ON bugs.bug_id = bugs_activity.bug_id
        INNER JOIN dependencies
                ON bugs_activity.bug_id = dependencies.dependson
        INNER JOIN fielddefs
239
                ON fielddefs.id = bugs_activity.fieldid
240 241 242 243
             WHERE dependencies.blocked = ?
               AND (fielddefs.name = 'bug_status'
                    OR fielddefs.name = 'resolution')
                   $when_restriction
244
                   $dep_restriction
245 246
          ORDER BY bugs_activity.bug_when, bugs.bug_id", undef, @args);

247 248 249 250
        my $thisdiff = "";
        my $lastbug = "";
        my $interestingchange = 0;
        foreach my $dependency_diff (@$dependency_diffs) {
251
            my ($depbug, $summary, $fieldname, $what, $old, $new) = @$dependency_diff;
252 253 254 255 256 257 258 259 260

            if ($depbug ne $lastbug) {
                if ($interestingchange) {
                    $deptext .= $thisdiff;
                }
                $lastbug = $depbug;
                $thisdiff =
                  "\nBug $id depends on bug $depbug, which changed state.\n\n" .
                  "Bug $depbug Summary: $summary\n" .
261
                  correct_urlbase() . "show_bug.cgi?id=$depbug\n\n";
262
                $thisdiff .= three_columns("What    ", "Old Value", "New Value");
263 264
                $thisdiff .= ('-' x 76) . "\n";
                $interestingchange = 0;
265
            }
266 267
            $thisdiff .= three_columns($what, $old, $new);
            if ($fieldname eq 'bug_status'
268
                && is_open_state($old) ne is_open_state($new))
269 270 271 272
            {
                $interestingchange = 1;
            }
            push(@depbugs, $depbug);
273 274
        }

275 276 277 278
        if ($interestingchange) {
            $deptext .= $thisdiff;
        }
        $deptext = trim($deptext);
279

280 281
        if ($deptext) {
            my $diffpart = {};
282
            $diffpart->{'text'} = "\n" . trim($deptext);
283 284
            push(@diffparts, $diffpart);
        }
285 286
    }

287
    my $comments = $bug->comments({ after => $start, to => $end });
288

289
    ###########################################################################
290
    # Start of email filtering code
291
    ###########################################################################
292
    
293 294
    # A user_id => roles hash to keep track of people.
    my %recipients;
295
    my %watching;
296 297 298 299 300 301
    
    # Now we work out all the people involved with this bug, and note all of
    # the relationships in a hash. The keys are userids, the values are an
    # array of role constants.
    
    # CCs
302
    $recipients{$_->id}->{+REL_CC} = BIT_DIRECT foreach (@ccs);
303 304
    
    # Reporter (there's only ever one)
305
    $recipients{$bug->reporter->id}->{+REL_REPORTER} = BIT_DIRECT;
306 307
    
    # QA Contact
308
    if (Bugzilla->params->{'useqacontact'}) {
309 310
        foreach (@qa_contacts) {
            # QA Contact can be blank; ignore it if so.
311
            $recipients{$_->id}->{+REL_QA} = BIT_DIRECT if $_;
312
        }
313
    }
314

315
    # Assignee
316
    $recipients{$_->id}->{+REL_ASSIGNEE} = BIT_DIRECT foreach (@assignees);
317

318 319
    # The last relevant set of people are those who are being removed from 
    # their roles in this change. We get their names out of the diffs.
320
    foreach my $ref (@$diffs) {
321
        my ($who, $whoname, $what, $when, $old, $new) = (@$ref);
322
        if ($old) {
323 324
            # You can't stop being the reporter, so we don't check that
            # relationship here.
325
            # Ignore people whose user account has been deleted or renamed.
326
            if ($what eq "CC") {
327
                foreach my $cc_user (split(/[\s,]+/, $old)) {
328
                    my $uid = login_to_id($cc_user);
329
                    $recipients{$uid}->{+REL_CC} = BIT_DIRECT if $uid;
330
                }
331 332
            }
            elsif ($what eq "QAContact") {
333
                my $uid = login_to_id($old);
334
                $recipients{$uid}->{+REL_QA} = BIT_DIRECT if $uid;
335 336
            }
            elsif ($what eq "AssignedTo") {
337
                my $uid = login_to_id($old);
338
                $recipients{$uid}->{+REL_ASSIGNEE} = BIT_DIRECT if $uid;
339
            }
340
        }
341
    }
342 343

    Bugzilla::Hook::process('bugmail_recipients',
344
                            { bug => $bug, recipients => \%recipients });
345

346 347 348 349 350 351 352 353 354 355 356 357 358
    # Find all those user-watching anyone on the current list, who is not
    # on it already themselves.
    my $involved = join(",", keys %recipients);

    my $userwatchers =
        $dbh->selectall_arrayref("SELECT watcher, watched FROM watch
                                  WHERE watched IN ($involved)");

    # Mark these people as having the role of the person they are watching
    foreach my $watch (@$userwatchers) {
        while (my ($role, $bits) = each %{$recipients{$watch->[1]}}) {
            $recipients{$watch->[0]}->{$role} |= BIT_WATCHING
                if $bits & BIT_DIRECT;
359
        }
360
        push(@{$watching{$watch->[0]}}, $watch->[1]);
361
    }
362 363 364 365 366 367 368 369 370

    # Global watcher
    my @watchers = split(/[,\s]+/, Bugzilla->params->{'globalwatchers'});
    foreach (@watchers) {
        my $watcher_id = login_to_id($_);
        next unless $watcher_id;
        $recipients{$watcher_id}->{+REL_GLOBAL_WATCHER} = BIT_DIRECT;
    }

371 372 373 374 375
    # We now have a complete set of all the users, and their relationships to
    # the bug in question. However, we are not necessarily going to mail them
    # all - there are preferences, permissions checks and all sorts to do yet.
    my @sent;
    my @excluded;
376

377
    foreach my $user_id (keys %recipients) {
378
        my %rels_which_want;
379 380
        my $sent_mail = 0;
        my $user = new Bugzilla::User($user_id);
381 382 383
        # Deleted users must be excluded.
        next unless $user;

384
        if ($user->can_see_bug($id)) {
385 386
            # Go through each role the user has and see if they want mail in
            # that role.
387
            foreach my $relationship (keys %{$recipients{$user_id}}) {
388 389
                if ($user->wants_bug_mail($id,
                                          $relationship, 
390
                                          $diffs, 
391
                                          $comments,
392
                                          $deptext,
393 394
                                          $changer,
                                          !$start))
395
                {
396 397
                    $rels_which_want{$relationship} = 
                        $recipients{$user_id}->{$relationship};
398 399
                }
            }
400
        }
401
        
402
        if (scalar(%rels_which_want)) {
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
            # So the user exists, can see the bug, and wants mail in at least
            # one role. But do we want to send it to them?

            # We shouldn't send mail if this is a dependency mail (i.e. there 
            # is something in @depbugs), and any of the depending bugs are not 
            # visible to the user. This is to avoid leaking the summaries of 
            # confidential bugs.
            my $dep_ok = 1;
            foreach my $dep_id (@depbugs) {
                if (!$user->can_see_bug($dep_id)) {
                   $dep_ok = 0;
                   last;
                }
            }

            # Make sure the user isn't in the nomail list, and the insider and 
            # dep checks passed.
420
            if ($user->email_enabled && $dep_ok) {
421
                # OK, OK, if we must. Email the user.
422 423 424 425 426 427 428 429 430 431 432 433 434
                $sent_mail = sendMail(
                    { to       => $user, 
                      fields   => \@fields,
                      bug      => $bug,
                      comments => $comments,
                      is_new   => !$start,
                      changer  => $changer,
                      watchers => exists $watching{$user_id} ?
                                  $watching{$user_id} : undef,
                      diff_parts      => \@diffparts,
                      rels_which_want => \%rels_which_want,
                      changed_fields  => \@changedfields,
                    });
435
            }
436
        }
437 438 439 440 441 442 443 444
       
        if ($sent_mail) {
            push(@sent, $user->login); 
        } 
        else {
            push(@excluded, $user->login); 
        } 
    }
445
    
446 447
    $dbh->do('UPDATE bugs SET lastdiffed = ? WHERE bug_id = ?',
             undef, ($end, $id));
448
    $bug->{lastdiffed} = $end;
449

450
    return {'sent' => \@sent, 'excluded' => \@excluded};
451 452
}

453
sub sendMail {
454
    my $params = shift;
455
    
456 457 458 459 460 461 462 463 464 465 466
    my $user   = $params->{to};
    my @fields = @{ $params->{fields} };
    my $bug    = $params->{bug};
    my @send_comments = @{ $params->{comments} };
    my $isnew   = $params->{is_new};
    my $changer = $params->{changer};
    my $watchingRef = $params->{watchers};
    my @diffparts   = @{ $params->{diff_parts} };
    my $relRef      = $params->{rels_which_want};
    my @changed_fields = @{ $params->{changed_fields} };

467 468 469 470
    # Build difftext (the actions) by verifying the user should see them
    my $difftext = "";
    my $diffheader = "";
    my $add_diff;
471

472 473 474
    foreach my $diff (@diffparts) {
        $add_diff = 0;
        
475
        if (exists($diff->{'fieldname'}) && 
476 477
            ($diff->{'fieldname'} eq 'estimated_time' ||
             $diff->{'fieldname'} eq 'remaining_time' ||
478
             $diff->{'fieldname'} eq 'work_time' ||
479 480 481 482 483 484
             $diff->{'fieldname'} eq 'deadline'))
        {
            $add_diff = 1 if $user->is_timetracker;
        } elsif ($diff->{'isprivate'} 
                 && !$user->is_insider)
        {
485
            $add_diff = 0;
486 487 488
        } else {
            $add_diff = 1;
        }
489

490
        if ($add_diff) {
491 492
            if (exists($diff->{'header'}) && 
             ($diffheader ne $diff->{'header'})) {
493 494 495 496 497 498
                $diffheader = $diff->{'header'};
                $difftext .= $diffheader;
            }
            $difftext .= $diff->{'text'};
        }
    }
499

500
    if (!$user->is_insider) {
501
        @send_comments = grep { !$_->is_private } @send_comments;
502 503 504
    }

    if ($difftext eq "" && !scalar(@send_comments) && !$isnew) {
505
      # Whoops, no differences!
506
      return 0;
507
    }
508

509
    my $diffs = $difftext;
510 511
    # Remove extra newlines.
    $diffs =~ s/^\n+//s; $diffs =~ s/\n+$//s;
512
    if ($isnew) {
513
        my $head = "";
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        foreach my $field (@fields) {
            my $name = $field->name;
            my $value = $bug->$name;

            if (ref $value eq 'ARRAY') {
                $value = join(', ', @$value);
            }
            elsif (ref $value && $value->isa('Bugzilla::User')) {
                $value = $value->login;
            }
            elsif (ref $value && $value->isa('Bugzilla::Object')) {
                $value = $value->name;
            }
            elsif ($name eq 'estimated_time') {
                $value = format_time_decimal($value);
            }
            elsif ($name eq 'deadline') {
                $value = time2str("%Y-%m-%d", str2time($value));
            }

534 535 536
            # If there isn't anything to show, don't include this header.
            next unless $value;
            # Only send estimated_time if it is enabled and the user is in the group.
537 538
            if (($name ne 'estimated_time' && $name ne 'deadline') || $user->is_timetracker) {
                my $desc = $field->description;
539 540 541 542
                $head .= multiline_sprintf(FORMAT_DOUBLE, ["$desc:", $value],
                                           FORMAT_2_SIZE);
            }
        }
543
        $diffs = $head . ($difftext ? "\n\n" : "") . $diffs;
544
    }
545

546 547 548 549
    my (@reasons, @reasons_watch);
    while (my ($relationship, $bits) = each %{$relRef}) {
        push(@reasons, $relationship) if ($bits & BIT_DIRECT);
        push(@reasons_watch, $relationship) if ($bits & BIT_WATCHING);
550
    }
551

552 553 554
    my %relationships = relationships();
    my @headerrel   = map { $relationships{$_} } @reasons;
    my @watchingrel = map { $relationships{$_} } @reasons_watch;
555 556
    push(@headerrel,   'None') unless @headerrel;
    push(@watchingrel, 'None') unless @watchingrel;
557
    push @watchingrel, map { user_id_to_login($_) } @$watchingRef;
558

559
    my $vars = {
560
        isnew => $isnew,
561
        to_user => $user,
562
        bug => $bug,
563
        changedfields => \@changed_fields,
564 565
        reasons => \@reasons,
        reasons_watch => \@reasons_watch,
566 567
        reasonsheader => join(" ", @headerrel),
        reasonswatchheader => join(" ", @watchingrel),
568
        changer => $changer,
569
        diffs => $diffs,
570
        new_comments => \@send_comments,
571
        threadingmarker => build_thread_marker($bug->id, $user->id, $isnew),
572 573 574
    };

    my $msg;
575
    my $template = Bugzilla->template_inner($user->settings->{'lang'}->{'value'});
576 577
    $template->process("email/newchangedmail.txt.tmpl", $vars, \$msg)
      || ThrowTemplateError($template->error());
578

579 580 581 582 583
    MessageToMTA($msg);

    return 1;
}

584
1;