sendbugmail.pl 2.77 KB
Newer Older
1
#!/usr/bin/perl -T
2 3 4
# 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/.
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
8

9 10
use 5.10.1;
use strict;
11 12
use warnings;

13
use lib qw(. lib);
14

15
use Bugzilla;
16
use Bugzilla::Util;
17
use Bugzilla::BugMail;
18
use Bugzilla::User;
19

20 21
my $dbh = Bugzilla->dbh;

22
sub usage {
23
    say STDERR "Usage: $0 bug_id user_email";
24 25 26 27 28 29 30 31 32 33 34 35 36
    exit;
}

if (($#ARGV < 1) || ($#ARGV > 2)) {
    usage();
}

# Get the arguments.
my $bugnum = $ARGV[0];
my $changer = $ARGV[1];

# Validate the bug number.
if (!($bugnum =~ /^(\d+)$/)) {
37
  say STDERR "Bug number \"$bugnum\" not numeric.";
38 39 40
  usage();
}

41 42
detaint_natural($bugnum);

43 44
my ($id) = $dbh->selectrow_array("SELECT bug_id FROM bugs WHERE bug_id = ?", 
                                 undef, $bugnum);
45

46
if (!$id) {
47
  say STDERR "Bug number $bugnum does not exist.";
48 49 50 51
  usage();
}

# Validate the changer address.
52
my $match = Bugzilla->params->{'emailregexp'};
53
if ($changer !~ /$match/) {
54
    say STDERR "Changer \"$changer\" doesn't match email regular expression.";
55 56
    usage();
}
57 58
my $changer_user = new Bugzilla::User({ name => $changer });
unless ($changer_user) {
59
    say STDERR "\"$changer\" is not a valid user.";
60 61 62 63
    usage();
}

# Send the email.
64
my $outputref = Bugzilla::BugMail::Send($bugnum, {'changer' => $changer_user });
65 66 67 68 69

# Report the results.
my $sent = scalar(@{$outputref->{sent}});

if ($sent) {
70
    say "email sent to $sent recipients:";
71
} else {
72
    say "No email sent.";
73 74 75
}

foreach my $sent (@{$outputref->{sent}}) {
76
  say "  $sent";
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

# This document is copyright (C) 2004 Perforce Software, Inc.  All rights
# reserved.
# 
# Redistribution and use of this document in any form, with or without
# modification, is permitted provided that redistributions of this
# document retain the above copyright notice, this condition and the
# following disclaimer.
# 
# THIS DOCUMENT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# DOCUMENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.