blob: 223d91f6c6d85810a9fc268ea56dad771e00a0f3 [file] [log] [blame]
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +00001#!/usr/bin/perl -T
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
timothy@apple.comf42518d2008-02-06 20:19:16 +00005#
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +00006# This Source Code Form is "Incompatible With Secondary Licenses", as
7# defined by the Mozilla Public License, v. 2.0.
8
9use 5.10.1;
10use strict;
11use warnings;
timothy@apple.comf42518d2008-02-06 20:19:16 +000012
ddkilzer@apple.com097da082009-07-03 02:14:25 +000013use lib qw(. lib);
timothy@apple.comf42518d2008-02-06 20:19:16 +000014
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000015use Bugzilla;
16use Bugzilla::Util;
timothy@apple.comf42518d2008-02-06 20:19:16 +000017use Bugzilla::BugMail;
18use Bugzilla::User;
19
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000020my $dbh = Bugzilla->dbh;
21
timothy@apple.comf42518d2008-02-06 20:19:16 +000022sub usage {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000023 say STDERR "Usage: $0 bug_id user_email";
timothy@apple.comf42518d2008-02-06 20:19:16 +000024 exit;
25}
26
27if (($#ARGV < 1) || ($#ARGV > 2)) {
28 usage();
29}
30
31# Get the arguments.
32my $bugnum = $ARGV[0];
33my $changer = $ARGV[1];
34
35# Validate the bug number.
36if (!($bugnum =~ /^(\d+)$/)) {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000037 say STDERR "Bug number \"$bugnum\" not numeric.";
timothy@apple.comf42518d2008-02-06 20:19:16 +000038 usage();
39}
40
41detaint_natural($bugnum);
42
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000043my ($id) = $dbh->selectrow_array("SELECT bug_id FROM bugs WHERE bug_id = ?",
44 undef, $bugnum);
timothy@apple.comf42518d2008-02-06 20:19:16 +000045
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000046if (!$id) {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000047 say STDERR "Bug number $bugnum does not exist.";
timothy@apple.comf42518d2008-02-06 20:19:16 +000048 usage();
49}
50
51# Validate the changer address.
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000052my $match = Bugzilla->params->{'emailregexp'};
timothy@apple.comf42518d2008-02-06 20:19:16 +000053if ($changer !~ /$match/) {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000054 say STDERR "Changer \"$changer\" doesn't match email regular expression.";
timothy@apple.comf42518d2008-02-06 20:19:16 +000055 usage();
56}
ddkilzer@apple.com57772842014-10-16 16:00:58 +000057my $changer_user = new Bugzilla::User({ name => $changer });
58unless ($changer_user) {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000059 say STDERR "\"$changer\" is not a valid user.";
timothy@apple.comf42518d2008-02-06 20:19:16 +000060 usage();
61}
62
63# Send the email.
ddkilzer@apple.com57772842014-10-16 16:00:58 +000064my $outputref = Bugzilla::BugMail::Send($bugnum, {'changer' => $changer_user });
timothy@apple.comf42518d2008-02-06 20:19:16 +000065
66# Report the results.
67my $sent = scalar(@{$outputref->{sent}});
timothy@apple.comf42518d2008-02-06 20:19:16 +000068
69if ($sent) {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000070 say "email sent to $sent recipients:";
timothy@apple.comf42518d2008-02-06 20:19:16 +000071} else {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000072 say "No email sent.";
timothy@apple.comf42518d2008-02-06 20:19:16 +000073}
74
75foreach my $sent (@{$outputref->{sent}}) {
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000076 say " $sent";
timothy@apple.comf42518d2008-02-06 20:19:16 +000077}
78
79# This document is copyright (C) 2004 Perforce Software, Inc. All rights
80# reserved.
81#
82# Redistribution and use of this document in any form, with or without
83# modification, is permitted provided that redistributions of this
84# document retain the above copyright notice, this condition and the
85# following disclaimer.
86#
87# THIS DOCUMENT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
88# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
89# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
90# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
91# HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
92# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
93# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
94# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
95# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
96# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
97# DOCUMENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.