sendbugmail.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env perl -w
  2. #
  3. # sendbugmail.pl
  4. #
  5. # Nick Barnes, Ravenbrook Limited, 2004-04-01.
  6. #
  7. # $Id$
  8. #
  9. # Bugzilla email script for Bugzilla 2.17.4 and later. Invoke this to send
  10. # bugmail for a bug which has been changed directly in the database.
  11. # This uses Bugzilla's own BugMail facility, and will email the
  12. # users associated with the bug. Replaces the old "processmail"
  13. # script.
  14. #
  15. # Usage: perl -T contrib/sendbugmail.pl bug_id user_email
  16. use lib qw(. lib);
  17. use Bugzilla;
  18. use Bugzilla::Util;
  19. use Bugzilla::BugMail;
  20. use Bugzilla::User;
  21. my $dbh = Bugzilla->dbh;
  22. sub usage {
  23. print STDERR "Usage: $0 bug_id user_email\n";
  24. exit;
  25. }
  26. if (($#ARGV < 1) || ($#ARGV > 2)) {
  27. usage();
  28. }
  29. # Get the arguments.
  30. my $bugnum = $ARGV[0];
  31. my $changer = $ARGV[1];
  32. # Validate the bug number.
  33. if (!($bugnum =~ /^(\d+)$/)) {
  34. print STDERR "Bug number \"$bugnum\" not numeric.\n";
  35. usage();
  36. }
  37. detaint_natural($bugnum);
  38. my ($id) = $dbh->selectrow_array("SELECT bug_id FROM bugs WHERE bug_id = ?",
  39. undef, $bugnum);
  40. if (!$id) {
  41. print STDERR "Bug number $bugnum does not exist.\n";
  42. usage();
  43. }
  44. # Validate the changer address.
  45. my $match = Bugzilla->params->{'emailregexp'};
  46. if ($changer !~ /$match/) {
  47. print STDERR "Changer \"$changer\" doesn't match email regular expression.\n";
  48. usage();
  49. }
  50. if(!login_to_id($changer)) {
  51. print STDERR "\"$changer\" is not a login ID.\n";
  52. usage();
  53. }
  54. # Send the email.
  55. my $outputref = Bugzilla::BugMail::Send($bugnum, {'changer' => $changer });
  56. # Report the results.
  57. my $sent = scalar(@{$outputref->{sent}});
  58. my $excluded = scalar(@{$outputref->{excluded}});
  59. if ($sent) {
  60. print "email sent to $sent recipients:\n";
  61. } else {
  62. print "No email sent.\n";
  63. }
  64. foreach my $sent (@{$outputref->{sent}}) {
  65. print " $sent\n";
  66. }
  67. if ($excluded) {
  68. print "$excluded recipients excluded:\n";
  69. } else {
  70. print "No recipients excluded.\n";
  71. }
  72. foreach my $excluded (@{$outputref->{excluded}}) {
  73. print " $excluded\n";
  74. }
  75. # This document is copyright (C) 2004 Perforce Software, Inc. All rights
  76. # reserved.
  77. #
  78. # Redistribution and use of this document in any form, with or without
  79. # modification, is permitted provided that redistributions of this
  80. # document retain the above copyright notice, this condition and the
  81. # following disclaimer.
  82. #
  83. # THIS DOCUMENT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  84. # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  85. # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  86. # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  87. # HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  88. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  89. # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  90. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  91. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  92. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  93. # DOCUMENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.