BugMail.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Netscape Communications
  16. # Corporation. Portions created by Netscape are
  17. # Copyright (C) 1998 Netscape Communications Corporation. All
  18. # Rights Reserved.
  19. #
  20. # Contributor(s): Terry Weissman <terry@mozilla.org>,
  21. # Bryce Nesbitt <bryce-mozilla@nextbus.com>
  22. # 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. # Bradley Baetz <bbaetz@student.usyd.edu.au>
  27. # J. Paul Reed <preed@sigkill.com>
  28. # Gervase Markham <gerv@gerv.net>
  29. # Byron Jones <bugzilla@glob.com.au>
  30. use strict;
  31. package Bugzilla::BugMail;
  32. use Bugzilla::Error;
  33. use Bugzilla::User;
  34. use Bugzilla::Constants;
  35. use Bugzilla::Util;
  36. use Bugzilla::Bug;
  37. use Bugzilla::Classification;
  38. use Bugzilla::Product;
  39. use Bugzilla::Component;
  40. use Bugzilla::Status;
  41. use Bugzilla::Mailer;
  42. use Date::Parse;
  43. use Date::Format;
  44. use constant FORMAT_TRIPLE => "%19s|%-28s|%-28s";
  45. use constant FORMAT_3_SIZE => [19,28,28];
  46. use constant FORMAT_DOUBLE => "%19s %-55s";
  47. use constant FORMAT_2_SIZE => [19,55];
  48. use constant BIT_DIRECT => 1;
  49. use constant BIT_WATCHING => 2;
  50. # We need these strings for the X-Bugzilla-Reasons header
  51. # Note: this hash uses "," rather than "=>" to avoid auto-quoting of the LHS.
  52. use constant REL_NAMES => {
  53. REL_ASSIGNEE , "AssignedTo",
  54. REL_REPORTER , "Reporter",
  55. REL_QA , "QAcontact",
  56. REL_CC , "CC",
  57. REL_VOTER , "Voter",
  58. REL_GLOBAL_WATCHER, "GlobalWatcher"
  59. };
  60. # We use this instead of format because format doesn't deal well with
  61. # multi-byte languages.
  62. sub multiline_sprintf {
  63. my ($format, $args, $sizes) = @_;
  64. my @parts;
  65. my @my_sizes = @$sizes; # Copy this so we don't modify the input array.
  66. foreach my $string (@$args) {
  67. my $size = shift @my_sizes;
  68. my @pieces = split("\n", wrap_hard($string, $size));
  69. push(@parts, \@pieces);
  70. }
  71. my $formatted;
  72. while (1) {
  73. # Get the first item of each part.
  74. my @line = map { shift @$_ } @parts;
  75. # If they're all undef, we're done.
  76. last if !grep { defined $_ } @line;
  77. # Make any single undef item into ''
  78. @line = map { defined $_ ? $_ : '' } @line;
  79. # And append a formatted line
  80. $formatted .= sprintf($format, @line);
  81. # Remove trailing spaces, or they become lots of =20's in
  82. # quoted-printable emails.
  83. $formatted =~ s/\s+$//;
  84. $formatted .= "\n";
  85. }
  86. return $formatted;
  87. }
  88. sub three_columns {
  89. return multiline_sprintf(FORMAT_TRIPLE, \@_, FORMAT_3_SIZE);
  90. }
  91. # This is a bit of a hack, basically keeping the old system()
  92. # cmd line interface. Should clean this up at some point.
  93. #
  94. # args: bug_id, and an optional hash ref which may have keys for:
  95. # changer, owner, qa, reporter, cc
  96. # Optional hash contains values of people which will be forced to those
  97. # roles when the email is sent.
  98. # All the names are email addresses, not userids
  99. # values are scalars, except for cc, which is a list
  100. # This hash usually comes from the "mailrecipients" var in a template call.
  101. sub Send {
  102. my ($id, $forced) = (@_);
  103. my @headerlist;
  104. my %defmailhead;
  105. my %fielddescription;
  106. my $msg = "";
  107. my $dbh = Bugzilla->dbh;
  108. # XXX - These variables below are useless. We could use field object
  109. # methods directly. But we first have to implement a cache in
  110. # Bugzilla->get_fields to avoid querying the DB all the time.
  111. foreach my $field (Bugzilla->get_fields({obsolete => 0})) {
  112. push(@headerlist, $field->name);
  113. $defmailhead{$field->name} = $field->in_new_bugmail;
  114. $fielddescription{$field->name} = $field->description;
  115. }
  116. my %values = %{$dbh->selectrow_hashref(
  117. 'SELECT ' . join(',', editable_bug_fields()) . ', reporter,
  118. lastdiffed AS start_time, LOCALTIMESTAMP(0) AS end_time
  119. FROM bugs WHERE bug_id = ?',
  120. undef, $id)};
  121. my $product = new Bugzilla::Product($values{product_id});
  122. $values{product} = $product->name;
  123. if (Bugzilla->params->{'useclassification'}) {
  124. $values{classification} = Bugzilla::Classification->new($product->classification_id)->name;
  125. }
  126. my $component = new Bugzilla::Component($values{component_id});
  127. $values{component} = $component->name;
  128. my ($start, $end) = ($values{start_time}, $values{end_time});
  129. # User IDs of people in various roles. More than one person can 'have' a
  130. # role, if the person in that role has changed, or people are watching.
  131. my $reporter = $values{'reporter'};
  132. my @assignees = ($values{'assigned_to'});
  133. my @qa_contacts = ($values{'qa_contact'});
  134. my $cc_users = $dbh->selectall_arrayref(
  135. "SELECT cc.who, profiles.login_name
  136. FROM cc
  137. INNER JOIN profiles
  138. ON cc.who = profiles.userid
  139. WHERE bug_id = ?",
  140. undef, $id);
  141. my (@ccs, @cc_login_names);
  142. foreach my $cc_user (@$cc_users) {
  143. my ($user_id, $user_login) = @$cc_user;
  144. push (@ccs, $user_id);
  145. push (@cc_login_names, $user_login);
  146. }
  147. # Include the people passed in as being in particular roles.
  148. # This can include people who used to hold those roles.
  149. # At this point, we don't care if there are duplicates in these arrays.
  150. my $changer = $forced->{'changer'};
  151. if ($forced->{'owner'}) {
  152. push (@assignees, login_to_id($forced->{'owner'}, THROW_ERROR));
  153. }
  154. if ($forced->{'qacontact'}) {
  155. push (@qa_contacts, login_to_id($forced->{'qacontact'}, THROW_ERROR));
  156. }
  157. if ($forced->{'cc'}) {
  158. foreach my $cc (@{$forced->{'cc'}}) {
  159. push(@ccs, login_to_id($cc, THROW_ERROR));
  160. }
  161. }
  162. # Convert to names, for later display
  163. $values{'changer'} = $changer;
  164. # If no changer is specified, then it has no name.
  165. if ($changer) {
  166. $values{'changername'} = Bugzilla::User->new({name => $changer})->name;
  167. }
  168. $values{'assigned_to'} = user_id_to_login($values{'assigned_to'});
  169. $values{'reporter'} = user_id_to_login($values{'reporter'});
  170. if ($values{'qa_contact'}) {
  171. $values{'qa_contact'} = user_id_to_login($values{'qa_contact'});
  172. }
  173. $values{'cc'} = join(', ', @cc_login_names);
  174. $values{'estimated_time'} = format_time_decimal($values{'estimated_time'});
  175. if ($values{'deadline'}) {
  176. $values{'deadline'} = time2str("%Y-%m-%d", str2time($values{'deadline'}));
  177. }
  178. my $dependslist = $dbh->selectcol_arrayref(
  179. 'SELECT dependson FROM dependencies
  180. WHERE blocked = ? ORDER BY dependson',
  181. undef, ($id));
  182. $values{'dependson'} = join(",", @$dependslist);
  183. my $blockedlist = $dbh->selectcol_arrayref(
  184. 'SELECT blocked FROM dependencies
  185. WHERE dependson = ? ORDER BY blocked',
  186. undef, ($id));
  187. $values{'blocked'} = join(",", @$blockedlist);
  188. my @args = ($id);
  189. # If lastdiffed is NULL, then we don't limit the search on time.
  190. my $when_restriction = '';
  191. if ($start) {
  192. $when_restriction = ' AND bug_when > ? AND bug_when <= ?';
  193. push @args, ($start, $end);
  194. }
  195. my $diffs = $dbh->selectall_arrayref(
  196. "SELECT profiles.login_name, profiles.realname, fielddefs.description,
  197. bugs_activity.bug_when, bugs_activity.removed,
  198. bugs_activity.added, bugs_activity.attach_id, fielddefs.name
  199. FROM bugs_activity
  200. INNER JOIN fielddefs
  201. ON fielddefs.id = bugs_activity.fieldid
  202. INNER JOIN profiles
  203. ON profiles.userid = bugs_activity.who
  204. WHERE bugs_activity.bug_id = ?
  205. $when_restriction
  206. ORDER BY bugs_activity.bug_when", undef, @args);
  207. my @new_depbugs;
  208. my $difftext = "";
  209. my $diffheader = "";
  210. my @diffparts;
  211. my $lastwho = "";
  212. my $fullwho;
  213. my @changedfields;
  214. foreach my $ref (@$diffs) {
  215. my ($who, $whoname, $what, $when, $old, $new, $attachid, $fieldname) = (@$ref);
  216. my $diffpart = {};
  217. if ($who ne $lastwho) {
  218. $lastwho = $who;
  219. $fullwho = $whoname ? "$whoname <$who>" : $who;
  220. $diffheader = "\n$fullwho changed:\n\n";
  221. $diffheader .= three_columns("What ", "Removed", "Added");
  222. $diffheader .= ('-' x 76) . "\n";
  223. }
  224. $what =~ s/^(Attachment )?/Attachment #$attachid / if $attachid;
  225. if( $fieldname eq 'estimated_time' ||
  226. $fieldname eq 'remaining_time' ) {
  227. $old = format_time_decimal($old);
  228. $new = format_time_decimal($new);
  229. }
  230. if ($fieldname eq 'dependson') {
  231. push(@new_depbugs, grep {$_ =~ /^\d+$/} split(/[\s,]+/, $new));
  232. }
  233. if ($attachid) {
  234. ($diffpart->{'isprivate'}) = $dbh->selectrow_array(
  235. 'SELECT isprivate FROM attachments WHERE attach_id = ?',
  236. undef, ($attachid));
  237. }
  238. $difftext = three_columns($what, $old, $new);
  239. $diffpart->{'header'} = $diffheader;
  240. $diffpart->{'fieldname'} = $fieldname;
  241. $diffpart->{'text'} = $difftext;
  242. push(@diffparts, $diffpart);
  243. push(@changedfields, $what);
  244. }
  245. $values{'changed_fields'} = join(' ', @changedfields);
  246. my @depbugs;
  247. my $deptext = "";
  248. # Do not include data about dependent bugs when they have just been added.
  249. # Completely skip checking for dependent bugs on bug creation as all
  250. # dependencies bugs will just have been added.
  251. if ($start) {
  252. my $dep_restriction = "";
  253. if (scalar @new_depbugs) {
  254. $dep_restriction = "AND bugs_activity.bug_id NOT IN (" .
  255. join(", ", @new_depbugs) . ")";
  256. }
  257. my $dependency_diffs = $dbh->selectall_arrayref(
  258. "SELECT bugs_activity.bug_id, bugs.short_desc, fielddefs.name,
  259. bugs_activity.removed, bugs_activity.added
  260. FROM bugs_activity
  261. INNER JOIN bugs
  262. ON bugs.bug_id = bugs_activity.bug_id
  263. INNER JOIN dependencies
  264. ON bugs_activity.bug_id = dependencies.dependson
  265. INNER JOIN fielddefs
  266. ON fielddefs.id = bugs_activity.fieldid
  267. WHERE dependencies.blocked = ?
  268. AND (fielddefs.name = 'bug_status'
  269. OR fielddefs.name = 'resolution')
  270. $when_restriction
  271. $dep_restriction
  272. ORDER BY bugs_activity.bug_when, bugs.bug_id", undef, @args);
  273. my $thisdiff = "";
  274. my $lastbug = "";
  275. my $interestingchange = 0;
  276. foreach my $dependency_diff (@$dependency_diffs) {
  277. my ($depbug, $summary, $what, $old, $new) = @$dependency_diff;
  278. if ($depbug ne $lastbug) {
  279. if ($interestingchange) {
  280. $deptext .= $thisdiff;
  281. }
  282. $lastbug = $depbug;
  283. my $urlbase = Bugzilla->params->{"urlbase"};
  284. $thisdiff =
  285. "\nBug $id depends on bug $depbug, which changed state.\n\n" .
  286. "Bug $depbug Summary: $summary\n" .
  287. "${urlbase}show_bug.cgi?id=$depbug\n\n";
  288. $thisdiff .= three_columns("What ", "Old Value", "New Value");
  289. $thisdiff .= ('-' x 76) . "\n";
  290. $interestingchange = 0;
  291. }
  292. $thisdiff .= three_columns($fielddescription{$what}, $old, $new);
  293. if ($what eq 'bug_status'
  294. && is_open_state($old) ne is_open_state($new))
  295. {
  296. $interestingchange = 1;
  297. }
  298. push(@depbugs, $depbug);
  299. }
  300. if ($interestingchange) {
  301. $deptext .= $thisdiff;
  302. }
  303. $deptext = trim($deptext);
  304. if ($deptext) {
  305. my $diffpart = {};
  306. $diffpart->{'text'} = "\n" . trim("\n\n" . $deptext);
  307. push(@diffparts, $diffpart);
  308. }
  309. }
  310. my ($raw_comments, $anyprivate, $count) = get_comments_by_bug($id, $start, $end);
  311. ###########################################################################
  312. # Start of email filtering code
  313. ###########################################################################
  314. # A user_id => roles hash to keep track of people.
  315. my %recipients;
  316. my %watching;
  317. # Now we work out all the people involved with this bug, and note all of
  318. # the relationships in a hash. The keys are userids, the values are an
  319. # array of role constants.
  320. # Voters
  321. my $voters = $dbh->selectcol_arrayref(
  322. "SELECT who FROM votes WHERE bug_id = ?", undef, ($id));
  323. $recipients{$_}->{+REL_VOTER} = BIT_DIRECT foreach (@$voters);
  324. # CCs
  325. $recipients{$_}->{+REL_CC} = BIT_DIRECT foreach (@ccs);
  326. # Reporter (there's only ever one)
  327. $recipients{$reporter}->{+REL_REPORTER} = BIT_DIRECT;
  328. # QA Contact
  329. if (Bugzilla->params->{'useqacontact'}) {
  330. foreach (@qa_contacts) {
  331. # QA Contact can be blank; ignore it if so.
  332. $recipients{$_}->{+REL_QA} = BIT_DIRECT if $_;
  333. }
  334. }
  335. # Assignee
  336. $recipients{$_}->{+REL_ASSIGNEE} = BIT_DIRECT foreach (@assignees);
  337. # The last relevant set of people are those who are being removed from
  338. # their roles in this change. We get their names out of the diffs.
  339. foreach my $ref (@$diffs) {
  340. my ($who, $whoname, $what, $when, $old, $new) = (@$ref);
  341. if ($old) {
  342. # You can't stop being the reporter, and mail isn't sent if you
  343. # remove your vote.
  344. # Ignore people whose user account has been deleted or renamed.
  345. if ($what eq "CC") {
  346. foreach my $cc_user (split(/[\s,]+/, $old)) {
  347. my $uid = login_to_id($cc_user);
  348. $recipients{$uid}->{+REL_CC} = BIT_DIRECT if $uid;
  349. }
  350. }
  351. elsif ($what eq "QAContact") {
  352. my $uid = login_to_id($old);
  353. $recipients{$uid}->{+REL_QA} = BIT_DIRECT if $uid;
  354. }
  355. elsif ($what eq "AssignedTo") {
  356. my $uid = login_to_id($old);
  357. $recipients{$uid}->{+REL_ASSIGNEE} = BIT_DIRECT if $uid;
  358. }
  359. }
  360. }
  361. if (Bugzilla->params->{"supportwatchers"}) {
  362. # Find all those user-watching anyone on the current list, who is not
  363. # on it already themselves.
  364. my $involved = join(",", keys %recipients);
  365. my $userwatchers =
  366. $dbh->selectall_arrayref("SELECT watcher, watched FROM watch
  367. WHERE watched IN ($involved)");
  368. # Mark these people as having the role of the person they are watching
  369. foreach my $watch (@$userwatchers) {
  370. while (my ($role, $bits) = each %{$recipients{$watch->[1]}}) {
  371. $recipients{$watch->[0]}->{$role} |= BIT_WATCHING
  372. if $bits & BIT_DIRECT;
  373. }
  374. push (@{$watching{$watch->[0]}}, $watch->[1]);
  375. }
  376. }
  377. # Global watcher
  378. my @watchers = split(/[,\s]+/, Bugzilla->params->{'globalwatchers'});
  379. foreach (@watchers) {
  380. my $watcher_id = login_to_id($_);
  381. next unless $watcher_id;
  382. $recipients{$watcher_id}->{+REL_GLOBAL_WATCHER} = BIT_DIRECT;
  383. }
  384. # We now have a complete set of all the users, and their relationships to
  385. # the bug in question. However, we are not necessarily going to mail them
  386. # all - there are preferences, permissions checks and all sorts to do yet.
  387. my @sent;
  388. my @excluded;
  389. # Some comments are language specific. We cache them here.
  390. my %comments;
  391. foreach my $user_id (keys %recipients) {
  392. my %rels_which_want;
  393. my $sent_mail = 0;
  394. my $user = new Bugzilla::User($user_id);
  395. # Deleted users must be excluded.
  396. next unless $user;
  397. # What's the language chosen by this user for email?
  398. my $lang = $user->settings->{'lang'}->{'value'};
  399. if ($user->can_see_bug($id)) {
  400. # It's time to format language specific comments.
  401. unless (exists $comments{$lang}) {
  402. Bugzilla->template_inner($lang);
  403. $comments{$lang} = prepare_comments($raw_comments, $count);
  404. Bugzilla->template_inner("");
  405. }
  406. # Go through each role the user has and see if they want mail in
  407. # that role.
  408. foreach my $relationship (keys %{$recipients{$user_id}}) {
  409. if ($user->wants_bug_mail($id,
  410. $relationship,
  411. $diffs,
  412. $comments{$lang},
  413. $deptext,
  414. $changer,
  415. !$start))
  416. {
  417. $rels_which_want{$relationship} =
  418. $recipients{$user_id}->{$relationship};
  419. }
  420. }
  421. }
  422. if (scalar(%rels_which_want)) {
  423. # So the user exists, can see the bug, and wants mail in at least
  424. # one role. But do we want to send it to them?
  425. # If we are using insiders, and the comment is private, only send
  426. # to insiders
  427. my $insider_ok = 1;
  428. $insider_ok = 0 if (Bugzilla->params->{"insidergroup"} &&
  429. ($anyprivate != 0) &&
  430. (!$user->groups->{Bugzilla->params->{"insidergroup"}}));
  431. # We shouldn't send mail if this is a dependency mail (i.e. there
  432. # is something in @depbugs), and any of the depending bugs are not
  433. # visible to the user. This is to avoid leaking the summaries of
  434. # confidential bugs.
  435. my $dep_ok = 1;
  436. foreach my $dep_id (@depbugs) {
  437. if (!$user->can_see_bug($dep_id)) {
  438. $dep_ok = 0;
  439. last;
  440. }
  441. }
  442. # Make sure the user isn't in the nomail list, and the insider and
  443. # dep checks passed.
  444. if ($user->email_enabled &&
  445. $insider_ok &&
  446. $dep_ok)
  447. {
  448. # OK, OK, if we must. Email the user.
  449. $sent_mail = sendMail($user,
  450. \@headerlist,
  451. \%rels_which_want,
  452. \%values,
  453. \%defmailhead,
  454. \%fielddescription,
  455. \@diffparts,
  456. $comments{$lang},
  457. $anyprivate,
  458. ! $start,
  459. $id,
  460. exists $watching{$user_id} ?
  461. $watching{$user_id} : undef);
  462. }
  463. }
  464. if ($sent_mail) {
  465. push(@sent, $user->login);
  466. }
  467. else {
  468. push(@excluded, $user->login);
  469. }
  470. }
  471. $dbh->do('UPDATE bugs SET lastdiffed = ? WHERE bug_id = ?',
  472. undef, ($end, $id));
  473. return {'sent' => \@sent, 'excluded' => \@excluded};
  474. }
  475. sub sendMail {
  476. my ($user, $hlRef, $relRef, $valueRef, $dmhRef, $fdRef,
  477. $diffRef, $newcomments, $anyprivate, $isnew,
  478. $id, $watchingRef) = @_;
  479. my %values = %$valueRef;
  480. my @headerlist = @$hlRef;
  481. my %mailhead = %$dmhRef;
  482. my %fielddescription = %$fdRef;
  483. my @diffparts = @$diffRef;
  484. # Build difftext (the actions) by verifying the user should see them
  485. my $difftext = "";
  486. my $diffheader = "";
  487. my $add_diff;
  488. foreach my $diff (@diffparts) {
  489. $add_diff = 0;
  490. if (exists($diff->{'fieldname'}) &&
  491. ($diff->{'fieldname'} eq 'estimated_time' ||
  492. $diff->{'fieldname'} eq 'remaining_time' ||
  493. $diff->{'fieldname'} eq 'work_time' ||
  494. $diff->{'fieldname'} eq 'deadline')){
  495. if ($user->groups->{Bugzilla->params->{"timetrackinggroup"}}) {
  496. $add_diff = 1;
  497. }
  498. } elsif (($diff->{'isprivate'})
  499. && Bugzilla->params->{'insidergroup'}
  500. && !($user->groups->{Bugzilla->params->{'insidergroup'}})
  501. ) {
  502. $add_diff = 0;
  503. #if WEBKIT_CHANGES
  504. # If the only thing we are modifying is the in-rietveld flag, don't
  505. # include this diff. If multiple flags are being modified,
  506. # the diff text will have a comma seperating it.
  507. # This will prevent mail from being sent.
  508. } elsif ($diff->{'text'} =~ /in-rietveld/ && !($diff->{'text'} =~ /,/)) {
  509. $add_diff = 0;
  510. #endif // WEBKIT_CHANGES
  511. } else {
  512. $add_diff = 1;
  513. }
  514. if ($add_diff) {
  515. if (exists($diff->{'header'}) &&
  516. ($diffheader ne $diff->{'header'})) {
  517. $diffheader = $diff->{'header'};
  518. $difftext .= $diffheader;
  519. }
  520. $difftext .= $diff->{'text'};
  521. }
  522. }
  523. if ($difftext eq "" && $newcomments eq "" && !$isnew) {
  524. # Whoops, no differences!
  525. return 0;
  526. }
  527. # If an attachment was created, then add an URL. (Note: the 'g'lobal
  528. # replace should work with comments with multiple attachments.)
  529. if ( $newcomments =~ /Created an attachment \(/ ) {
  530. my $showattachurlbase =
  531. Bugzilla->params->{'urlbase'} . "attachment.cgi?id=";
  532. $newcomments =~ s/(Created an attachment \(id=([0-9]+)\))/$1\n --> \(${showattachurlbase}$2&action=review\)/g;
  533. }
  534. my $diffs = $difftext . "\n\n" . $newcomments;
  535. if ($isnew) {
  536. my $head = "";
  537. foreach my $f (@headerlist) {
  538. next unless $mailhead{$f};
  539. my $value = $values{$f};
  540. # If there isn't anything to show, don't include this header.
  541. next unless $value;
  542. # Only send estimated_time if it is enabled and the user is in the group.
  543. if (($f ne 'estimated_time' && $f ne 'deadline')
  544. || $user->groups->{Bugzilla->params->{'timetrackinggroup'}})
  545. {
  546. my $desc = $fielddescription{$f};
  547. $head .= multiline_sprintf(FORMAT_DOUBLE, ["$desc:", $value],
  548. FORMAT_2_SIZE);
  549. }
  550. }
  551. $diffs = $head . ($difftext ? "\n\n" : "") . $diffs;
  552. }
  553. my (@reasons, @reasons_watch);
  554. while (my ($relationship, $bits) = each %{$relRef}) {
  555. push(@reasons, $relationship) if ($bits & BIT_DIRECT);
  556. push(@reasons_watch, $relationship) if ($bits & BIT_WATCHING);
  557. }
  558. my @headerrel = map { REL_NAMES->{$_} } @reasons;
  559. my @watchingrel = map { REL_NAMES->{$_} } @reasons_watch;
  560. push(@headerrel, 'None') unless @headerrel;
  561. push(@watchingrel, 'None') unless @watchingrel;
  562. push @watchingrel, map { user_id_to_login($_) } @$watchingRef;
  563. my $threadingmarker = build_thread_marker($id, $user->id, $isnew);
  564. my $vars = {
  565. isnew => $isnew,
  566. to => $user->email,
  567. bugid => $id,
  568. alias => Bugzilla->params->{'usebugaliases'} ? $values{'alias'} : "",
  569. classification => $values{'classification'},
  570. product => $values{'product'},
  571. comp => $values{'component'},
  572. keywords => $values{'keywords'},
  573. severity => $values{'bug_severity'},
  574. status => $values{'bug_status'},
  575. priority => $values{'priority'},
  576. assignedto => $values{'assigned_to'},
  577. assignedtoname => Bugzilla::User->new({name => $values{'assigned_to'}})->name,
  578. targetmilestone => $values{'target_milestone'},
  579. changedfields => $values{'changed_fields'},
  580. summary => $values{'short_desc'},
  581. reasons => \@reasons,
  582. reasons_watch => \@reasons_watch,
  583. reasonsheader => join(" ", @headerrel),
  584. reasonswatchheader => join(" ", @watchingrel),
  585. changer => $values{'changer'},
  586. changername => $values{'changername'},
  587. reporter => $values{'reporter'},
  588. reportername => Bugzilla::User->new({name => $values{'reporter'}})->name,
  589. diffs => $diffs,
  590. threadingmarker => $threadingmarker
  591. };
  592. my $msg;
  593. my $template = Bugzilla->template_inner($user->settings->{'lang'}->{'value'});
  594. $template->process("email/newchangedmail.txt.tmpl", $vars, \$msg)
  595. || ThrowTemplateError($template->error());
  596. Bugzilla->template_inner("");
  597. MessageToMTA($msg);
  598. return 1;
  599. }
  600. # Get bug comments for the given period.
  601. sub get_comments_by_bug {
  602. my ($id, $start, $end) = @_;
  603. my $dbh = Bugzilla->dbh;
  604. my $result = "";
  605. my $count = 0;
  606. my $anyprivate = 0;
  607. # $start will be undef for new bugs, and defined for pre-existing bugs.
  608. if ($start) {
  609. # If $start is not NULL, obtain the count-index
  610. # of this comment for the leading "Comment #xxx" line.
  611. $count = $dbh->selectrow_array('SELECT COUNT(*) FROM longdescs
  612. WHERE bug_id = ? AND bug_when <= ?',
  613. undef, ($id, $start));
  614. }
  615. my $raw = 1; # Do not format comments which are not of type CMT_NORMAL.
  616. my $comments = Bugzilla::Bug::GetComments($id, "oldest_to_newest", $start, $end, $raw);
  617. if (Bugzilla->params->{'insidergroup'}) {
  618. $anyprivate = 1 if scalar(grep {$_->{'isprivate'} > 0} @$comments);
  619. }
  620. return ($comments, $anyprivate, $count);
  621. }
  622. # Prepare comments for the given language.
  623. sub prepare_comments {
  624. my ($raw_comments, $count) = @_;
  625. my $result = "";
  626. foreach my $comment (@$raw_comments) {
  627. if ($count) {
  628. $result .= "\n\n--- Comment #$count from " . $comment->{'author'}->identity .
  629. " " . format_time($comment->{'time'}) . " ---\n";
  630. }
  631. # Format language specific comments. We don't update $comment->{'body'}
  632. # directly, otherwise it would grow everytime you call format_comment()
  633. # with a different language as some text may be appended to the existing one.
  634. my $body = Bugzilla::Bug::format_comment($comment);
  635. $result .= ($comment->{'already_wrapped'} ? $body : wrap_comment($body));
  636. $count++;
  637. }
  638. return $result;
  639. }
  640. 1;