BugMail.pm 26.8 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

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

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

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

46 47
use Date::Parse;
use Date::Format;
48

49 50 51 52 53
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];

54 55 56
use constant BIT_DIRECT    => 1;
use constant BIT_WATCHING  => 2;

57 58
# We need these strings for the X-Bugzilla-Reasons header
# Note: this hash uses "," rather than "=>" to avoid auto-quoting of the LHS.
59
use constant REL_NAMES => {
60 61 62 63 64 65
    REL_ASSIGNEE      , "AssignedTo", 
    REL_REPORTER      , "Reporter",
    REL_QA            , "QAcontact",
    REL_CC            , "CC",
    REL_VOTER         , "Voter",
    REL_GLOBAL_WATCHER, "GlobalWatcher"
66 67
};

68 69 70 71 72 73
# 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.
74
    foreach my $string (@$args) {
75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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
89 90 91 92 93
        $formatted .= sprintf($format, @line);
        # Remove trailing spaces, or they become lots of =20's in 
        # quoted-printable emails.
        $formatted =~ s/\s+$//;
        $formatted .= "\n";
94 95
    }
    return $formatted;
96
}
97 98 99

sub three_columns {
    return multiline_sprintf(FORMAT_TRIPLE, \@_, FORMAT_3_SIZE);
100 101
}

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
# This hash usually comes from the "mailrecipients" var in a template call.
112
sub Send {
113
    my ($id, $forced) = (@_);
114 115 116 117 118 119 120

    my @headerlist;
    my %defmailhead;
    my %fielddescription;

    my $msg = "";

121
    my $dbh = Bugzilla->dbh;
122

123 124 125 126 127 128 129
    # XXX - These variables below are useless. We could use field object
    # methods directly. But we first have to implement a cache in
    # Bugzilla->get_fields to avoid querying the DB all the time.
    foreach my $field (Bugzilla->get_fields({obsolete => 0})) {
        push(@headerlist, $field->name);
        $defmailhead{$field->name} = $field->in_new_bugmail;
        $fielddescription{$field->name} = $field->description;
130
    }
131 132

    my %values = %{$dbh->selectrow_hashref(
133
        'SELECT ' . join(',', editable_bug_fields()) . ', reporter,
134
                lastdiffed AS start_time, LOCALTIMESTAMP(0) AS end_time
135 136
           FROM bugs WHERE bug_id = ?',
        undef, $id)};
137 138 139

    my $product = new Bugzilla::Product($values{product_id});
    $values{product} = $product->name;
140
    $values{classification} = Bugzilla::Classification->new($product->classification_id)->name;
141 142
    my $component = new Bugzilla::Component($values{component_id});
    $values{component} = $component->name;
143

144
    my ($start, $end) = ($values{start_time}, $values{end_time});
145

146 147 148 149 150
    # User IDs 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 $reporter = $values{'reporter'};
    my @assignees = ($values{'assigned_to'});
    my @qa_contacts = ($values{'qa_contact'});
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    my $cc_users = $dbh->selectall_arrayref(
           "SELECT cc.who, profiles.login_name
              FROM cc
        INNER JOIN profiles
                ON cc.who = profiles.userid
             WHERE bug_id = ?",
           undef, $id);

    my (@ccs, @cc_login_names);
    foreach my $cc_user (@$cc_users) {
        my ($user_id, $user_login) = @$cc_user;
        push (@ccs, $user_id);
        push (@cc_login_names, $user_login);
    }
166 167 168 169 170 171

    # 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'}) {
172
        push (@assignees, login_to_id($forced->{'owner'}, THROW_ERROR));
173
    }
174
    
175
    if ($forced->{'qacontact'}) {
176
        push (@qa_contacts, login_to_id($forced->{'qacontact'}, THROW_ERROR));
177
    }
178 179 180
    
    if ($forced->{'cc'}) {
        foreach my $cc (@{$forced->{'cc'}}) {
181
            push(@ccs, login_to_id($cc, THROW_ERROR));
182 183 184 185
        }
    }
    
    # Convert to names, for later display
186
    $values{'changer'} = $changer;
187 188 189 190
    # If no changer is specified, then it has no name.
    if ($changer) {
        $values{'changername'} = Bugzilla::User->new({name => $changer})->name;
    }
191 192
    $values{'assigned_to'} = user_id_to_login($values{'assigned_to'});
    $values{'reporter'} = user_id_to_login($values{'reporter'});
193
    if ($values{'qa_contact'}) {
194
        $values{'qa_contact'} = user_id_to_login($values{'qa_contact'});
195
    }
196
    $values{'cc'} = join(', ', @cc_login_names);
197
    $values{'estimated_time'} = format_time_decimal($values{'estimated_time'});
198

199 200 201
    if ($values{'deadline'}) {
        $values{'deadline'} = time2str("%Y-%m-%d", str2time($values{'deadline'}));
    }
202

203 204 205 206
    my $dependslist = $dbh->selectcol_arrayref(
        'SELECT dependson FROM dependencies
         WHERE blocked = ? ORDER BY dependson',
        undef, ($id));
207

208 209 210 211 212 213
    $values{'dependson'} = join(",", @$dependslist);

    my $blockedlist = $dbh->selectcol_arrayref(
        'SELECT blocked FROM dependencies
         WHERE dependson = ? ORDER BY blocked',
        undef, ($id));
214

215 216 217
    $values{'blocked'} = join(",", @$blockedlist);

    my @args = ($id);
218

219
    # If lastdiffed is NULL, then we don't limit the search on time.
220 221 222 223
    my $when_restriction = '';
    if ($start) {
        $when_restriction = ' AND bug_when > ? AND bug_when <= ?';
        push @args, ($start, $end);
224
    }
225 226
    
    my $diffs = $dbh->selectall_arrayref(
227
           "SELECT profiles.login_name, profiles.realname, fielddefs.description,
228 229 230 231
                   bugs_activity.bug_when, bugs_activity.removed, 
                   bugs_activity.added, bugs_activity.attach_id, fielddefs.name
              FROM bugs_activity
        INNER JOIN fielddefs
232
                ON fielddefs.id = bugs_activity.fieldid
233 234 235 236 237
        INNER JOIN profiles
                ON profiles.userid = bugs_activity.who
             WHERE bugs_activity.bug_id = ?
                   $when_restriction
          ORDER BY bugs_activity.bug_when", undef, @args);
238

239
    my @new_depbugs;
240
    my $difftext = "";
241 242
    my $diffheader = "";
    my @diffparts;
243
    my $lastwho = "";
244
    my $fullwho;
245
    my @changedfields;
246
    foreach my $ref (@$diffs) {
247
        my ($who, $whoname, $what, $when, $old, $new, $attachid, $fieldname) = (@$ref);
248
        my $diffpart = {};
249 250
        if ($who ne $lastwho) {
            $lastwho = $who;
251
            $fullwho = $whoname ? "$whoname <$who>" : $who;
252
            $diffheader = "\n$fullwho changed:\n\n";
253
            $diffheader .= three_columns("What    ", "Removed", "Added");
254
            $diffheader .= ('-' x 76) . "\n";
255
        }
256
        $what =~ s/^(Attachment )?/Attachment #$attachid / if $attachid;
257 258
        if( $fieldname eq 'estimated_time' ||
            $fieldname eq 'remaining_time' ) {
259 260
            $old = format_time_decimal($old);
            $new = format_time_decimal($new);
261
        }
262 263 264
        if ($fieldname eq 'dependson') {
            push(@new_depbugs, grep {$_ =~ /^\d+$/} split(/[\s,]+/, $new));
        }
265
        if ($attachid) {
266 267 268
            ($diffpart->{'isprivate'}) = $dbh->selectrow_array(
                'SELECT isprivate FROM attachments WHERE attach_id = ?',
                undef, ($attachid));
269
        }
270
        $difftext = three_columns($what, $old, $new);
271 272 273 274
        $diffpart->{'header'} = $diffheader;
        $diffpart->{'fieldname'} = $fieldname;
        $diffpart->{'text'} = $difftext;
        push(@diffparts, $diffpart);
275
        push(@changedfields, $what);
276
    }
277
    $values{'changed_fields'} = join(' ', @changedfields);
278

279
    my @depbugs;
280
    my $deptext = "";
281 282 283 284 285 286 287 288 289
    # 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) . ")";
        }
290

291
        my $dependency_diffs = $dbh->selectall_arrayref(
292 293 294 295 296 297 298 299
           "SELECT bugs_activity.bug_id, bugs.short_desc, fielddefs.name, 
                   bugs_activity.removed, bugs_activity.added
              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
300
                ON fielddefs.id = bugs_activity.fieldid
301 302 303 304
             WHERE dependencies.blocked = ?
               AND (fielddefs.name = 'bug_status'
                    OR fielddefs.name = 'resolution')
                   $when_restriction
305
                   $dep_restriction
306 307
          ORDER BY bugs_activity.bug_when, bugs.bug_id", undef, @args);

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        my $thisdiff = "";
        my $lastbug = "";
        my $interestingchange = 0;
        foreach my $dependency_diff (@$dependency_diffs) {
            my ($depbug, $summary, $what, $old, $new) = @$dependency_diff;

            if ($depbug ne $lastbug) {
                if ($interestingchange) {
                    $deptext .= $thisdiff;
                }
                $lastbug = $depbug;
                my $urlbase = Bugzilla->params->{"urlbase"};
                $thisdiff =
                  "\nBug $id depends on bug $depbug, which changed state.\n\n" .
                  "Bug $depbug Summary: $summary\n" .
                  "${urlbase}show_bug.cgi?id=$depbug\n\n";
324
                $thisdiff .= three_columns("What    ", "Old Value", "New Value");
325 326
                $thisdiff .= ('-' x 76) . "\n";
                $interestingchange = 0;
327
            }
328
            $thisdiff .= three_columns($fielddescription{$what}, $old, $new);
329
            if ($what eq 'bug_status'
330
                && is_open_state($old) ne is_open_state($new))
331 332 333 334
            {
                $interestingchange = 1;
            }
            push(@depbugs, $depbug);
335 336
        }

337 338 339 340
        if ($interestingchange) {
            $deptext .= $thisdiff;
        }
        $deptext = trim($deptext);
341

342 343 344 345 346
        if ($deptext) {
            my $diffpart = {};
            $diffpart->{'text'} = "\n" . trim("\n\n" . $deptext);
            push(@diffparts, $diffpart);
        }
347 348
    }

349
    my ($raw_comments, $anyprivate, $count) = get_comments_by_bug($id, $start, $end);
350

351
    ###########################################################################
352
    # Start of email filtering code
353
    ###########################################################################
354
    
355 356
    # A user_id => roles hash to keep track of people.
    my %recipients;
357
    my %watching;
358 359 360 361 362 363
    
    # 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.
    
    # Voters
364 365
    my $voters = $dbh->selectcol_arrayref(
        "SELECT who FROM votes WHERE bug_id = ?", undef, ($id));
366
        
367
    $recipients{$_}->{+REL_VOTER} = BIT_DIRECT foreach (@$voters);
368

369
    # CCs
370
    $recipients{$_}->{+REL_CC} = BIT_DIRECT foreach (@ccs);
371 372
    
    # Reporter (there's only ever one)
373
    $recipients{$reporter}->{+REL_REPORTER} = BIT_DIRECT;
374 375
    
    # QA Contact
376
    if (Bugzilla->params->{'useqacontact'}) {
377 378
        foreach (@qa_contacts) {
            # QA Contact can be blank; ignore it if so.
379
            $recipients{$_}->{+REL_QA} = BIT_DIRECT if $_;
380
        }
381
    }
382

383
    # Assignee
384
    $recipients{$_}->{+REL_ASSIGNEE} = BIT_DIRECT foreach (@assignees);
385

386 387
    # 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.
388
    foreach my $ref (@$diffs) {
389
        my ($who, $whoname, $what, $when, $old, $new) = (@$ref);
390 391 392
        if ($old) {
            # You can't stop being the reporter, and mail isn't sent if you
            # remove your vote.
393
            # Ignore people whose user account has been deleted or renamed.
394
            if ($what eq "CC") {
395
                foreach my $cc_user (split(/[\s,]+/, $old)) {
396
                    my $uid = login_to_id($cc_user);
397
                    $recipients{$uid}->{+REL_CC} = BIT_DIRECT if $uid;
398
                }
399 400
            }
            elsif ($what eq "QAContact") {
401
                my $uid = login_to_id($old);
402
                $recipients{$uid}->{+REL_QA} = BIT_DIRECT if $uid;
403 404
            }
            elsif ($what eq "AssignedTo") {
405
                my $uid = login_to_id($old);
406
                $recipients{$uid}->{+REL_ASSIGNEE} = BIT_DIRECT if $uid;
407
            }
408
        }
409 410
    }
    
411
    if (Bugzilla->params->{"supportwatchers"}) {
412 413 414 415 416 417
        # 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 
418
                                      WHERE watched IN ($involved)");
419 420 421

        # Mark these people as having the role of the person they are watching
        foreach my $watch (@$userwatchers) {
422 423 424 425 426
            while (my ($role, $bits) = each %{$recipients{$watch->[1]}}) {
                $recipients{$watch->[0]}->{$role} |= BIT_WATCHING
                    if $bits & BIT_DIRECT;
            }
            push (@{$watching{$watch->[0]}}, $watch->[1]);
427 428
        }
    }
429 430 431 432 433 434 435 436 437

    # 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;
    }

438 439 440 441 442
    # 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;
443

444 445 446
    # Some comments are language specific. We cache them here.
    my %comments;

447
    foreach my $user_id (keys %recipients) {
448
        my %rels_which_want;
449
        my $sent_mail = 0;
450

451
        my $user = new Bugzilla::User($user_id);
452 453 454
        # Deleted users must be excluded.
        next unless $user;

455 456 457 458 459 460 461 462 463 464 465
        # What's the language chosen by this user for email?
        my $lang = $user->settings->{'lang'}->{'value'};

        if ($user->can_see_bug($id)) {
            # It's time to format language specific comments.
            unless (exists $comments{$lang}) {
                Bugzilla->template_inner($lang);
                $comments{$lang} = prepare_comments($raw_comments, $count);
                Bugzilla->template_inner("");
            }

466 467
            # Go through each role the user has and see if they want mail in
            # that role.
468
            foreach my $relationship (keys %{$recipients{$user_id}}) {
469 470
                if ($user->wants_bug_mail($id,
                                          $relationship, 
471
                                          $diffs, 
472
                                          $comments{$lang},
473
                                          $deptext,
474 475
                                          $changer,
                                          !$start))
476
                {
477 478
                    $rels_which_want{$relationship} = 
                        $recipients{$user_id}->{$relationship};
479 480
                }
            }
481
        }
482
        
483
        if (scalar(%rels_which_want)) {
484 485 486 487 488 489
            # 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?

            # If we are using insiders, and the comment is private, only send 
            # to insiders
            my $insider_ok = 1;
490
            $insider_ok = 0 if (Bugzilla->params->{"insidergroup"} && 
491
                                ($anyprivate != 0) && 
492
                                (!$user->groups->{Bugzilla->params->{"insidergroup"}}));
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

            # 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.
508
            if ($user->email_enabled &&
509 510 511 512 513 514
                $insider_ok &&
                $dep_ok)
            {
                # OK, OK, if we must. Email the user.
                $sent_mail = sendMail($user, 
                                      \@headerlist,
515
                                      \%rels_which_want, 
516 517 518 519
                                      \%values,
                                      \%defmailhead, 
                                      \%fielddescription, 
                                      \@diffparts,
520
                                      $comments{$lang},
521
                                      $anyprivate, 
522
                                      ! $start, 
523 524 525
                                      $id,
                                      exists $watching{$user_id} ?
                                             $watching{$user_id} : undef);
526
            }
527
        }
528 529 530 531 532 533 534 535
       
        if ($sent_mail) {
            push(@sent, $user->login); 
        } 
        else {
            push(@excluded, $user->login); 
        } 
    }
536
    
537 538
    $dbh->do('UPDATE bugs SET lastdiffed = ? WHERE bug_id = ?',
             undef, ($end, $id));
539

540
    return {'sent' => \@sent, 'excluded' => \@excluded};
541 542
}

543
sub sendMail {
544 545
    my ($user, $hlRef, $relRef, $valueRef, $dmhRef, $fdRef,
        $diffRef, $newcomments, $anyprivate, $isnew,
546
        $id, $watchingRef) = @_;
547

548 549
    my %values = %$valueRef;
    my @headerlist = @$hlRef;
550
    my %mailhead = %$dmhRef;
551
    my %fielddescription = %$fdRef;
552
    my @diffparts = @$diffRef;    
553 554 555 556 557
    my $head = "";
    
    foreach my $f (@headerlist) {
      if ($mailhead{$f}) {
        my $value = $values{$f};
558 559
        # If there isn't anything to show, don't include this header
        if (! $value) {
560 561
          next;
        }
562
        # Only send estimated_time if it is enabled and the user is in the group
563
        if (($f ne 'estimated_time' && $f ne 'deadline') ||
564
             $user->groups->{Bugzilla->params->{'timetrackinggroup'}}) {
565

566
            my $desc = $fielddescription{$f};
567 568
            $head .= multiline_sprintf(FORMAT_DOUBLE, ["$desc:", $value], 
                                       FORMAT_2_SIZE);
569
        }
570 571
      }
    }
572 573 574 575 576

    # Build difftext (the actions) by verifying the user should see them
    my $difftext = "";
    my $diffheader = "";
    my $add_diff;
577

578 579 580
    foreach my $diff (@diffparts) {
        $add_diff = 0;
        
581
        if (exists($diff->{'fieldname'}) && 
582 583
            ($diff->{'fieldname'} eq 'estimated_time' ||
             $diff->{'fieldname'} eq 'remaining_time' ||
584 585
             $diff->{'fieldname'} eq 'work_time' ||
             $diff->{'fieldname'} eq 'deadline')){
586
            if ($user->groups->{Bugzilla->params->{"timetrackinggroup"}}) {
587 588
                $add_diff = 1;
            }
589
        } elsif (($diff->{'isprivate'}) 
590 591
                 && Bugzilla->params->{'insidergroup'}
                 && !($user->groups->{Bugzilla->params->{'insidergroup'}})
592 593
                ) {
            $add_diff = 0;
594 595 596
        } else {
            $add_diff = 1;
        }
597

598
        if ($add_diff) {
599 600
            if (exists($diff->{'header'}) && 
             ($diffheader ne $diff->{'header'})) {
601 602 603 604 605 606
                $diffheader = $diff->{'header'};
                $difftext .= $diffheader;
            }
            $difftext .= $diff->{'text'};
        }
    }
607
 
608
    if ($difftext eq "" && $newcomments eq "" && !$isnew) {
609
      # Whoops, no differences!
610
      return 0;
611 612
    }
    
613 614 615 616 617 618
    # If an attachment was created, then add an URL. (Note: the 'g'lobal
    # replace should work with comments with multiple attachments.)

    if ( $newcomments =~ /Created an attachment \(/ ) {

        my $showattachurlbase =
619
            Bugzilla->params->{'urlbase'} . "attachment.cgi?id=";
620

621
        $newcomments =~ s/(Created an attachment \(id=([0-9]+)\))/$1\n --> \(${showattachurlbase}$2\)/g;
622 623
    }

624
    my $diffs = $difftext . "\n\n" . $newcomments;
625
    if ($isnew) {
626
        $diffs = $head . ($difftext ? "\n\n" : "") . $diffs;
627
    }
628

629 630 631 632
    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);
633
    }
634 635 636 637 638

    my @headerrel   = map { REL_NAMES->{$_} } @reasons;
    my @watchingrel = map { REL_NAMES->{$_} } @reasons_watch;
    push(@headerrel,   'None') unless @headerrel;
    push(@watchingrel, 'None') unless @watchingrel;
639
    push @watchingrel, map { user_id_to_login($_) } @$watchingRef;
640

641
    my $sitespec = '@' . Bugzilla->params->{'urlbase'};
642 643 644 645 646
    $sitespec =~ s/:\/\//\./; # Make the protocol look like part of the domain
    $sitespec =~ s/^([^:\/]+):(\d+)/$1/; # Remove a port number, to relocate
    if ($2) {
        $sitespec = "-$2$sitespec"; # Put the port number back in, before the '@'
    }
647
    my $threadingmarker;
648
    if ($isnew) {
649
        $threadingmarker = "Message-ID: <bug-$id-" . $user->id . "$sitespec>";
650 651
    }
    else {
652 653
        $threadingmarker = "In-Reply-To: <bug-$id-" . $user->id . "$sitespec>" .
                            "\nReferences: <bug-$id-" . $user->id . "$sitespec>";
654
    }
655
    
656 657

    my $vars = {
658
        isnew => $isnew,
659 660
        to => $user->email,
        bugid => $id,
661 662
        alias => Bugzilla->params->{'usebugaliases'} ? $values{'alias'} : "",
        classification => $values{'classification'},
663 664 665 666 667 668 669
        product => $values{'product'},
        comp => $values{'component'},
        keywords => $values{'keywords'},
        severity => $values{'bug_severity'},
        status => $values{'bug_status'},
        priority => $values{'priority'},
        assignedto => $values{'assigned_to'},
670
        assignedtoname => Bugzilla::User->new({name => $values{'assigned_to'}})->name,
671 672 673
        targetmilestone => $values{'target_milestone'},
        changedfields => $values{'changed_fields'},
        summary => $values{'short_desc'},
674 675
        reasons => \@reasons,
        reasons_watch => \@reasons_watch,
676 677 678 679
        reasonsheader => join(" ", @headerrel),
        reasonswatchheader => join(" ", @watchingrel),
        changer => $values{'changer'},
        changername => $values{'changername'},
680 681
        reporter => $values{'reporter'},
        reportername => Bugzilla::User->new({name => $values{'reporter'}})->name,
682 683 684 685 686
        diffs => $diffs,
        threadingmarker => $threadingmarker
    };

    my $msg;
687
    my $template = Bugzilla->template_inner($user->settings->{'lang'}->{'value'});
688 689
    $template->process("email/newchangedmail.txt.tmpl", $vars, \$msg)
      || ThrowTemplateError($template->error());
690
    Bugzilla->template_inner("");
691

692 693 694 695 696
    MessageToMTA($msg);

    return 1;
}

697
# Get bug comments for the given period.
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
sub get_comments_by_bug {
    my ($id, $start, $end) = @_;
    my $dbh = Bugzilla->dbh;

    my $result = "";
    my $count = 0;
    my $anyprivate = 0;

    # $start will be undef for new bugs, and defined for pre-existing bugs.
    if ($start) {
        # If $start is not NULL, obtain the count-index
        # of this comment for the leading "Comment #xxx" line.
        $count = $dbh->selectrow_array('SELECT COUNT(*) FROM longdescs
                                        WHERE bug_id = ? AND bug_when <= ?',
                                        undef, ($id, $start));
    }

715 716 717 718 719 720 721 722 723 724 725 726 727
    my $raw = 1; # Do not format comments which are not of type CMT_NORMAL.
    my $comments = Bugzilla::Bug::GetComments($id, "oldest_to_newest", $start, $end, $raw);

    if (Bugzilla->params->{'insidergroup'}) {
        $anyprivate = 1 if scalar(grep {$_->{'isprivate'} > 0} @$comments);
    }

    return ($comments, $anyprivate, $count);
}

# Prepare comments for the given language.
sub prepare_comments {
    my ($raw_comments, $count) = @_;
728

729 730
    my $result = "";
    foreach my $comment (@$raw_comments) {
731
        if ($count) {
732 733
            $result .= "\n\n--- Comment #$count from " . $comment->{'author'}->identity .
                       "  " . format_time($comment->{'time'}) . " ---\n";
734
        }
735 736 737 738 739
        # Format language specific comments. We don't update $comment->{'body'}
        # directly, otherwise it would grow everytime you call format_comment()
        # with a different language as some text may be appended to the existing one.
        my $body = Bugzilla::Bug::format_comment($comment);
        $result .= ($comment->{'already_wrapped'} ? $body : wrap_comment($body));
740 741
        $count++;
    }
742
    return $result;
743 744
}

745
1;