processmail 7.6 KB
Newer Older
1 2
#!/usr/bonsaitools/bin/perl -w
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.0 (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): Terry Weissman <terry@mozilla.org>


23
# To recreate the shadow database,  run "processmail regenerate" .
terry%netscape.com's avatar
terry%netscape.com committed
24

25 26
use diagnostics;
use strict;
terry%netscape.com's avatar
terry%netscape.com committed
27

28
require "globals.pl";
terry%netscape.com's avatar
terry%netscape.com committed
29

30
$| = 1;
terry%netscape.com's avatar
terry%netscape.com committed
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
umask(0);

$::lockcount = 0;

sub Lock {
    if ($::lockcount <= 0) {
        $::lockcount = 0;
        if (!open(LOCKFID, ">>data/maillock")) {
            mkdir "data", 0777;
            chmod 0777, "data";
            open(LOCKFID, ">>data/maillock") || die "Can't open lockfile.";
        }
        my $val = flock(LOCKFID,2);
        if (!$val) { # '2' is magic 'exclusive lock' const.
            print "Lock failed: $val\n";
        }
        chmod 0666, "data/maillock";
terry%netscape.com's avatar
terry%netscape.com committed
49
    }
50
    $::lockcount++;
terry%netscape.com's avatar
terry%netscape.com committed
51 52
}

53 54 55 56 57 58 59
sub Unlock {
    $::lockcount--;
    if ($::lockcount <= 0) {
        flock(LOCKFID,8);       # '8' is magic 'unlock' const.
        close LOCKFID;
    }
}
terry%netscape.com's avatar
terry%netscape.com committed
60

61 62 63 64 65 66 67 68 69
sub FileSize {
    my ($filename) = (@_);
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks)
        = stat($filename);
    if (defined $size) {
        return $size;
    }
    return -1;
terry%netscape.com's avatar
terry%netscape.com committed
70 71 72
}


73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

sub Different {
    my ($file1, $file2) = (@_);
    my $size1 = FileSize($file1);
    my $size2 = FileSize($file2);
    if ($size1 != $size2) {
        return 1;
    }
    open(FID1, "<$file1") || die "Can't open $file1";
    open(FID2, "<$file2") || die "Can't open $file2";
    my $d1; 
    my $d2; 
    if (read(FID1, $d1, $size1) ne $size1) {
        die "Can't read $size1 bytes from $file1";
    }
    if (read(FID2, $d2, $size2) ne $size2) {
        die "Can't read $size2 bytes from $file2";
    }
    close FID1;
    close FID2;
    return ($d1 ne $d2);
}


sub DescCC {
    my ($cclist) = (@_);
    if (scalar(@$cclist) <= 0) {
        return "";
    }
102
    return "Cc: " . join(", ", @$cclist) . "\n";
103 104 105 106 107 108
}


sub GetBugText {
    my ($id) = (@_);
    undef %::bug;
terry%netscape.com's avatar
terry%netscape.com committed
109
    
110 111 112
    my @collist = ("bug_id", "product", "version", "rep_platform", "op_sys",
                   "bug_status", "resolution", "priority", "bug_severity",
                   "area", "assigned_to", "reporter", "bug_file_loc",
113 114
                   "short_desc", "component", "qa_contact", "target_milestone",
                   "status_whiteboard");
115 116 117 118 119 120 121 122 123

    my $query = "select " . join(", ", @collist) .
        " from bugs where bug_id = $id";

    SendSQL($query);

    my @row;
    if (!(@row = FetchSQLData())) {
        return "";
terry%netscape.com's avatar
terry%netscape.com committed
124
    }
125 126 127 128 129
    foreach my $field (@collist) {
        $::bug{$field} = shift @row;
        if (!defined $::bug{$field}) {
            $::bug{$field} = "";
        }
terry%netscape.com's avatar
terry%netscape.com committed
130 131
    }

132 133
    $::bug{'assigned_to'} = DBID_to_name($::bug{'assigned_to'});
    $::bug{'reporter'} = DBID_to_name($::bug{'reporter'});
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    my $qa_contact = "";
    my $target_milestone = "";
    my $status_whiteboard = "";
    if (Param('useqacontact') && $::bug{'qa_contact'} > 0) {
        $::bug{'qa_contact'} = DBID_to_name($::bug{'qa_contact'});
        $qa_contact = "QAContact: $::bug{'qa_contact'}\n";
    } else {
        $::bug{'qa_contact'} = "";
    }
    if (Param('usetargetmilestone') && $::bug{'target_milestone'} ne "") {
        $target_milestone = "TargetMilestone: $::bug{'target_milestone'}\n";
    }
    if (Param('usestatuswhiteboard') && $::bug{'status_whiteboard'} ne "") {
        $status_whiteboard = "StatusWhiteboard: $::bug{'status_whiteboard'}\n";
    }
terry%netscape.com's avatar
terry%netscape.com committed
149

150
    $::bug{'long_desc'} = GetLongDescription($id);
terry%netscape.com's avatar
terry%netscape.com committed
151

152 153 154
    my @cclist;
    @cclist = split(/,/, ShowCcList($id));
    $::bug{'cclist'} = \@cclist;
terry%netscape.com's avatar
terry%netscape.com committed
155 156 157


    return "Bug\#: $id
158 159 160 161 162 163 164 165 166 167 168 169
Product: $::bug{'product'}
Version: $::bug{'version'}
Platform: $::bug{'rep_platform'}
OS/Version: $::bug{'op_sys'}
Status: $::bug{'bug_status'}   
Resolution: $::bug{'resolution'}
Severity: $::bug{'bug_severity'}
Priority: $::bug{'priority'}
Component: $::bug{'component'}
Area: $::bug{'area'}
AssignedTo: $::bug{'assigned_to'}                            
ReportedBy: $::bug{'reporter'}               
170
$qa_contact$target_milestone${status_whiteboard}URL: $::bug{'bug_file_loc'}
171 172 173 174
" . DescCC($::bug{'cclist'}) . "Summary: $::bug{'short_desc'}

$::bug{'long_desc'}
";
terry%netscape.com's avatar
terry%netscape.com committed
175 176 177 178 179

}



180 181 182 183 184
sub fixaddresses {
    my ($list) = (@_);
    my @result;
    my %seen;
    foreach my $i (@$list) {
185
        if ($i ne "" && !defined $::nomail{$i} && !defined $seen{$i}) {
186 187
            push @result, $i;
            $seen{$i} = 1;
terry%netscape.com's avatar
terry%netscape.com committed
188 189
        }
    }
190
    return join(", ",  @result);
terry%netscape.com's avatar
terry%netscape.com committed
191 192 193
}


194 195 196 197 198 199 200
sub Log {
    my ($str) = (@_);
    Lock();
    open(FID, ">>data/maillog") || die "Can't write to data/maillog";
    print FID time2str("%D %H:%M", time()) . ": $str\n";
    close FID;
    Unlock();
terry%netscape.com's avatar
terry%netscape.com committed
201 202 203
}
    

204
ConnectToDatabase();
terry%netscape.com's avatar
terry%netscape.com committed
205 206


207
Lock();
terry%netscape.com's avatar
terry%netscape.com committed
208 209 210 211 212

# foreach i [split [read_file -nonewline "okmail"] "\n"] {
#     set okmail($i) 1
# }

213 214 215 216 217


if (open(FID, "<data/nomail")) {
    while (<FID>) {
        $::nomail{trim($_)} = 1;
terry%netscape.com's avatar
terry%netscape.com committed
218
    }
219
    close FID;
terry%netscape.com's avatar
terry%netscape.com committed
220 221 222
}


223
my $regenerate = 0;
terry%netscape.com's avatar
terry%netscape.com committed
224

225
if ($ARGV[0] eq "regenerate") {
226
    $regenerate = 1;
227
    shift @ARGV;
228 229 230 231
    SendSQL("select bug_id from bugs order by bug_id");
    my @row;
    while (@row = FetchSQLData()) {
        push @ARGV, $row[0];
terry%netscape.com's avatar
terry%netscape.com committed
232
    }
233
    print "$#ARGV bugs to be regenerated.\n";
terry%netscape.com's avatar
terry%netscape.com committed
234 235
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
foreach my $i (@ARGV) {
    my $old = "shadow/$i";
    my $new = "shadow/$i.tmp.$$";
    my $diffs = "shadow/$i.diffs.$$";
    my $verb = "Changed";
    if (!stat($old)) {
        mkdir "shadow", 0777;
        chmod 0777, "shadow";
        open(OLD, ">$old") || die "Couldn't create null $old";
        close OLD;
        $verb = "New";
    }
    my $text = GetBugText($i);
    if ($text eq "") {
        die "Couldn't find bug $i.";
    }
    open(FID, ">$new") || die "Couldn't create $new";
    print FID $text;
    close FID;
    if (Different($old, $new)) {
256
        system("diff -c -b $old $new > $diffs");
257 258
        my $tolist = fixaddresses([$::bug{'assigned_to'}, $::bug{'reporter'},
                                   $::bug{'qa_contact'}]);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        my $cclist = fixaddresses($::bug{'cclist'});
        my $logstr = "Bug $i changed";
        if ($tolist ne "" || $cclist ne "") {
            my %substs;

            $substs{"to"} = $tolist;
            $substs{"cc"} = $cclist;
            $substs{"bugid"} = $i;
            $substs{"diffs"} = "";
            open(DIFFS, "<$diffs") || die "Can't open $diffs";
            while (<DIFFS>) {
                $substs{"diffs"} .= $_;
            }
            close DIFFS;
            $substs{"neworchanged"} = $verb;
            $substs{"summary"} = $::bug{'short_desc'};
            my $msg = PerformSubsts(Param("changedmail"), \%substs);

            if (!$regenerate) {
                open(SENDMAIL, "|/usr/lib/sendmail -t") ||
                    die "Can't open sendmail";
                print SENDMAIL $msg;
                close SENDMAIL;
                $logstr = "$logstr; mail sent to $tolist $cclist";
terry%netscape.com's avatar
terry%netscape.com committed
283 284
            }
        }
285 286
        unlink($diffs);
        Log($logstr);
terry%netscape.com's avatar
terry%netscape.com committed
287
    }
288 289 290 291
    rename($new, $old) || die "Can't rename $new to $old";
    chmod 0666, $old;
    if ($regenerate) {
        print "$i ";
terry%netscape.com's avatar
terry%netscape.com committed
292 293 294
    }
}

295
exit;