move.pl 5.43 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# 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.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Dawn Endico    <endico@mozilla.org>
#                 Terry Weissman <terry@mozilla.org>
23
#                 Dave Miller    <justdave@bugzilla.org>
24 25

use strict;
26 27 28

use lib qw(.);

29
require "CGI.pl";
30

31
use vars qw($template $userid %COOKIE);
32 33

use Bug;
34
use Bugzilla;
35
use Bugzilla::Config qw(:DEFAULT $datadir);
36
use Bugzilla::BugMail;
37

38 39
$::lockcount = 0;

40 41 42 43 44 45
unless ( Param("move-enabled") ) {
  print "\n<P>Sorry. Bug moving is not enabled here. ";
  print "If you need to move a bug, contact " . Param("maintainer");
  exit;
}

46
ConnectToDatabase();
47
confirm_login();
48

49 50
my $cgi = Bugzilla->cgi;

51 52 53
sub Log {
    my ($str) = (@_);
    Lock();
54
    open(FID, ">>$datadir/maillog") || die "Can't write to $datadir/maillog";
55 56 57 58 59 60 61 62
    print FID time2str("%D %H:%M", time()) . ": $str\n";
    close FID;
    Unlock();
}

sub Lock {
    if ($::lockcount <= 0) {
        $::lockcount = 0;
63
        open(LOCKFID, ">>$datadir/maillock") || die "Can't open $datadir/maillock: $!";
64 65
        my $val = flock(LOCKFID,2);
        if (!$val) { # '2' is magic 'exclusive lock' const.
66
            print $cgi->header();
67 68
            print "Lock failed: $val\n";
        }
69
        chmod 0666, "$datadir/maillock";
70 71 72 73 74 75 76 77 78 79 80 81 82
    }
    $::lockcount++;
}

sub Unlock {
    $::lockcount--;
    if ($::lockcount <= 0) {
        flock(LOCKFID,8);       # '8' is magic 'unlock' const.
        close LOCKFID;
    }
}

if ( !defined $::FORM{'buglist'} ) {
83
  print $cgi->header();
84 85
  PutHeader("Move Bugs");
  print "Move bugs either from the bug display page or perform a ";
86
  print "<A HREF=\"query.cgi\">query</A> and change several bugs at once.\n";
87 88 89
  print "If you don't see the move button, then you either aren't ";
  print "logged in or aren't permitted to.";
  PutFooter();
90 91 92 93
  exit;
}

my $exporter = $::COOKIE{"Bugzilla_login"};
94
my $movers = Param("movers");
95
$movers =~ s/\s?,\s?/|/g;
96 97
$movers =~ s/@/\@/g;
unless ($exporter =~ /($movers)/) {
98
  print $cgi->header();
99 100 101 102 103 104
  PutHeader("Move Bugs");
  print "<P>You do not have permission to move bugs<P>\n";
  PutFooter();
  exit;
}

105 106
my @bugs;

107
print "<P>\n";
108 109
foreach my $id (split(/:/, $::FORM{'buglist'})) {
  my $bug = new Bug($id, $::userid);
110
  push @bugs, $bug;
111
  if (!$bug->error) {
112 113 114 115 116
    my $exporterid = DBNameToIdAndCheck($exporter);

    my $fieldid = GetFieldID("bug_status");
    my $cur_status= $bug->bug_status;
    SendSQL("INSERT INTO bugs_activity " .
117
            "(bug_id,who,bug_when,fieldid,removed,added) VALUES " .
118
            "($id,$exporterid,now(),$fieldid,'$cur_status','RESOLVED')");
119
    $fieldid = GetFieldID("resolution");
120 121
    my $cur_res= $bug->resolution;
    SendSQL("INSERT INTO bugs_activity " .
122
            "(bug_id,who,bug_when,fieldid,removed,added) VALUES " .
123 124 125 126 127
            "($id,$exporterid,now(),$fieldid,'$cur_res','MOVED')");

    SendSQL("UPDATE bugs SET bug_status =\"RESOLVED\" where bug_id=\"$id\"");
    SendSQL("UPDATE bugs SET resolution =\"MOVED\" where bug_id=\"$id\"");

128 129 130 131 132
    my $comment = "";
    if (defined $::FORM{'comment'} && $::FORM{'comment'} !~ /^\s*$/) {
        $comment .= $::FORM{'comment'} . "\n\n";
    }
    $comment .= "Bug moved to " . Param("move-to-url") . ".\n\n";
133
    $comment .= "If the move succeeded, $exporter will receive a mail\n";
134 135 136
    $comment .= "containing the number of the new bug in the other database.\n";
    $comment .= "If all went well,  please mark this bug verified, and paste\n";
    $comment .= "in a link to the new bug. Otherwise, reopen this bug.\n";
137
    SendSQL("INSERT INTO longdescs (bug_id, who, bug_when, thetext) VALUES " .
138 139 140
        "($id,  $exporterid, now(), " . SqlQuote($comment) . ")");

    print "<P>Bug $id moved to " . Param("move-to-url") . ".<BR>\n";
141
    Bugzilla::BugMail::Send($id, { 'changer' => $exporter });
142
  }
143
}
144
print "<P>\n";
145

146 147
my $buglist = $::FORM{'buglist'};
$buglist =~ s/:/,/g;
148 149
my $host = Param("urlbase");
$host =~ s#http://([^/]+)/.*#$1#;
150 151
my $to = Param("move-to-address");
$to =~ s/@/\@/;
152
my $msg = "To: $to\n";
153 154 155
my $from = Param("moved-from-address");
$from =~ s/@/\@/;
$msg .= "From: Bugzilla <" . $from . ">\n";
156
$msg .= "Subject: Moving bug(s) $buglist\n\n";
157

158 159 160 161 162 163 164 165 166 167
my @fieldlist = (Bug::fields(), 'group', 'long_desc', 'attachment');
my %displayfields;
foreach (@fieldlist) {
    $displayfields{$_} = 1;
}

$template->process("bug/show.xml.tmpl", { user => { login => $exporter },
                                          bugs => \@bugs,
                                          displayfields => \%displayfields,
                                        }, \$msg)
168 169 170
  || ThrowTemplateError($template->error());

$msg .= "\n";
171 172

open(SENDMAIL,
173
  "|/usr/lib/sendmail -ODeliveryMode=background -t -i") ||
174 175 176 177 178 179
    die "Can't open sendmail";
print SENDMAIL $msg;
close SENDMAIL;

my $logstr = "XML: bugs $buglist sent to $to";
Log($logstr);