]> git.donarmstrong.com Git - debbugs.git/blob - cgi/common.pl
[project @ 2002-07-10 04:11:43 by doogie]
[debbugs.git] / cgi / common.pl
1 #!/usr/bin/perl -w
2
3 use DB_File;
4 use Fcntl qw/O_RDONLY/;
5
6 my $common_archive = 0;
7 my $common_repeatmerged = 1;
8 my %common_include = ();
9 my %common_exclude = ();
10 my $common_raw_sort = 0;
11 my $common_bug_reverse = 0;
12 my $common_pending_reverse = 0;
13 my $common_severity_reverse = 0;
14
15 my @common_pending_include = ();
16 my @common_pending_exclude = ();
17 my @common_severity_include = ();
18 my @common_severity_exclude = ();
19
20 my $debug = 0;
21
22 sub set_option {
23     my ($opt, $val) = @_;
24     if ($opt eq "archive") { $common_archive = $val; }
25     if ($opt eq "repeatmerged") { $common_repeatmerged = $val; }
26     if ($opt eq "exclude") { %common_exclude = %{$val}; }
27     if ($opt eq "include") { %common_include = %{$val}; }
28     if ($opt eq "raw") { $common_raw_sort = $val; }
29     if ($opt eq "bug-rev") { $common_bug_reverse = $val; }
30     if ($opt eq "pend-rev") { $common_pending_reverse = $val; }
31     if ($opt eq "sev-rev") { $common_severity_reverse = $val; }
32     if ($opt eq "pend-exc") {
33         my @vals;
34         @vals = ( $val ) if (ref($val) eq "" && $val );
35         @vals = ( $$val ) if (ref($val) eq "SCALAR" && $$val );
36         @vals = @{$val} if (ref($val) eq "ARRAY" );
37         @common_pending_exclude = @vals if (@vals);
38     }
39     if ($opt eq "pend-inc") {
40         my @vals;
41         @vals = ( $val, ) if (ref($val) eq "" && $val );
42         @vals = ( $$val, ) if (ref($val) eq "SCALAR" && $$val );
43         @vals = @{$val} if (ref($val) eq "ARRAY" );
44         @common_pending_include = @vals if (@vals);
45     }
46     if ($opt eq "sev-exc") {
47         my @vals;
48         @vals = ( $val ) if (ref($val) eq "" && $val );
49         @vals = ( $$val ) if (ref($val) eq "SCALAR" && $$val );
50         @vals = @{$val} if (ref($val) eq "ARRAY" );
51         @common_severity_exclude = @vals if (@vals);
52     }
53     if ($opt eq "sev-inc") {
54         my @vals;
55         @vals = ( $val ) if (ref($val) eq "" && $val );
56         @vals = ( $$val ) if (ref($val) eq "SCALAR" && $$val );
57         @vals = @{$val} if (ref($val) eq "ARRAY" );
58         @common_severity_include = @vals if (@vals);
59     }
60 }
61
62 sub readparse {
63     my ($in, $key, $val, %ret);
64     if (defined $ENV{"QUERY_STRING"} && $ENV{"QUERY_STRING"} ne "") {
65         $in=$ENV{QUERY_STRING};
66     } elsif(defined $ENV{"REQUEST_METHOD"}
67         && $ENV{"REQUEST_METHOD"} eq "POST")
68     {
69         read(STDIN,$in,$ENV{CONTENT_LENGTH});
70     } else {
71         return;
72     }
73     foreach (split(/&/,$in)) {
74         s/\+/ /g;
75         ($key, $val) = split(/=/,$_,2);
76         $key=~s/%(..)/pack("c",hex($1))/ge;
77         $val=~s/%(..)/pack("c",hex($1))/ge;
78         if ( exists $ret{$key} ) {
79             if ( !exists $ret{"&$key"} ) {
80                 $ret{"&$key"} = [ $ret{$key} ];
81             }
82             push @{$ret{"&$key"}},$val;
83         }
84         $ret{$key}=$val;
85     }
86 $debug = 1 if (defined $ret{"debug"} && $ret{"debug"} eq "aj");
87     return %ret;
88 }
89
90 sub quit {
91     my $msg = shift;
92     print "Content-Type: text/html\n\n";
93     print "<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n";
94     print "An error occurred. Dammit.\n";
95     print "Error was: $msg.\n";
96     print "</BODY></HTML>\n";
97     exit 0;
98 }
99
100 #sub abort {
101 #    my $msg = shift;
102 #    my $Archive = $common_archive ? "archive" : "";
103 #    print header . start_html("Sorry");
104 #    print "Sorry bug #$msg doesn't seem to be in the $Archive database.\n";
105 #    print end_html;
106 #    exit 0;
107 #}
108
109 sub htmlindexentry {
110     my $ref = shift;
111     my %status = %{getbugstatus($ref)};
112     return htmlindexentrystatus(%status) if (%status);
113     return "";
114 }
115
116 sub htmlindexentrystatus {
117     my $s = shift;
118     my %status = %{$s};
119
120     my $result = "";
121
122     if  ($status{severity} eq 'normal') {
123         $showseverity = '';
124     } elsif (grep($status{severity} eq $_, @debbugs::gStrongSeverities)) {
125         $showseverity = "<strong>Severity: $status{severity}</strong>;\n";
126     } else {
127         $showseverity = "Severity: <em>$status{severity}</em>;\n";
128     }
129
130     $result .= "Package: <a href=\"" . pkgurl($status{"package"}) . "\">"
131                . "<strong>" . htmlsanit($status{"package"}) . "</strong></a>;\n"
132                if (length($status{"package"}));
133     $result .= $showseverity;
134     $result .= "Reported by: <a href=\"" . submitterurl($status{originator})
135                . "\">" . htmlsanit($status{originator}) . "</a>";
136     $result .= ";\nTags: <strong>" 
137                  . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
138                  . "</strong>"
139                        if (length($status{tags}));
140
141     my @merged= split(/ /,$status{mergedwith});
142     my $mseparator= ";\nmerged with ";
143     for my $m (@merged) {
144         $result .= $mseparator."<A href=\"" . bugurl($m) . "\">#$m</A>";
145         $mseparator= ", ";
146     }
147
148     if (length($status{done})) {
149         $result .= ";\n<strong>Done:</strong> " . htmlsanit($status{done});
150     } elsif (length($status{forwarded})) {
151         $result .= ";\n<strong>Forwarded</strong> to "
152                    . htmlsanit($status{forwarded});
153     } else {
154         my $daysold = int((time - $status{date}) / 86400);   # seconds to days
155         if ($daysold >= 7) {
156             my $font = "";
157             my $efont = "";
158             $font = "em" if ($daysold > 30);
159             $font = "strong" if ($daysold > 60);
160             $efont = "</$font>" if ($font);
161             $font = "<$font>" if ($font);
162
163             my $yearsold = int($daysold / 364);
164             $daysold = $daysold - $yearsold * 364;
165
166             $result .= ";\n $font";
167             $result .= "1 year and " if ($yearsold == 1);
168             $result .= "$yearsold years and " if ($yearsold > 1);
169             $result .= "1 day old" if ($daysold == 1);
170             $result .= "$daysold days old" if ($daysold != 1);
171             $result .= "$efont";
172         }
173     }
174
175     $result .= ".";
176
177     return $result;
178 }
179
180 sub submitterurl {
181     my $ref = shift || "";
182     my $params = "submitter=" . emailfromrfc822($ref);
183     $params .= "&archive=yes" if ($common_archive);
184     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
185     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
186 }
187
188 sub mainturl {
189     my $ref = shift || "";
190     my $params = "maint=" . emailfromrfc822($ref);
191     $params .= "&archive=yes" if ($common_archive);
192     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
193     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
194 }
195
196 sub pkgurl {
197     my $ref = shift;
198     my $params = "pkg=$ref";
199     $params .= "&archive=yes" if ($common_archive);
200     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
201     
202     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
203 }
204
205 sub srcurl {
206     my $ref = shift;
207     my $params = "src=$ref";
208     $params .= "&archive=yes" if ($common_archive);
209     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
210     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
211 }
212
213 sub urlsanit {
214     my $url = shift;
215     $url =~ s/%/%25/g;
216     $url =~ s/\+/%2b/g;
217     my %saniarray = ('<','lt', '>','gt', '"','quot');
218     my $out;
219     while ($url =~ m/[<>"]/) {
220         $out .= $`. '&'. $saniarray{$&}. ';';
221         $url = $';
222     }
223     $out .= $url;
224     return $out;
225 }
226
227 sub htmlsanit {
228     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
229     my $in = shift || "";
230     my $out;
231     while ($in =~ m/[<>&"]/) {
232         $out .= $`. '&'. $saniarray{$&}. ';';
233         $in = $';
234     }
235     $out .= $in;
236     return $out;
237 }
238
239 sub bugurl {
240     my $ref = shift;
241     my $params = "bug=$ref";
242     foreach my $val (@_) {
243         $params .= "\&msg=$1" if ($val =~ /^msg=([0-9]+)/);
244         $params .= "\&archive=yes" if (!$common_archive && $val =~ /^archive.*$/);
245     }
246     $params .= "&archive=yes" if ($common_archive);
247     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
248
249     return urlsanit($debbugs::gCGIDomain . "bugreport.cgi" . "?" . "$params");
250 }
251
252 sub packageurl {
253     my $ref = shift;
254     return urlsanit($debbugs::gCGIDomain . "package.cgi" . "?" . "package=$ref");
255 }
256
257 sub allbugs {
258     my @bugs = ();
259
260     opendir(D, "$debbugs::gSpoolDir/db") or &quit("opendir db: $!");
261     @bugs = sort {$a<=>$b} grep s/\.status$//,
262                  (grep m/^[0-9]+\.status$/,
263                  (readdir(D)));
264     closedir(D);
265
266     return @bugs;
267 }
268
269 sub htmlizebugs {
270     $b = $_[0];
271     my @bugs = @$b;
272     my @rawsort;
273
274     my %section = ();
275
276     my %displayshowpending = ("pending", "outstanding",
277                               "pending-fixed", "pending upload",
278                               "fixed", "fixed in NMU",
279                               "done", "resolved",
280                               "forwarded", "forwarded to upstream software authors");
281
282     if (@bugs == 0) {
283         return "<HR><H2>No reports found!</H2></HR>\n";
284     }
285
286     if ( $common_bug_reverse ) {
287         @bugs = sort {$b<=>$a} @bugs;
288     } else {
289         @bugs = sort {$a<=>$b} @bugs;
290     }
291     foreach my $bug (@bugs) {
292         my %status = %{getbugstatus($bug)};
293         next unless %status;
294         my @merged = sort {$a<=>$b} ($bug, split(/ /, $status{mergedwith}));
295         next unless ($common_repeatmerged || $bug == $merged[0]);
296         if (%common_include) {
297             my $okay = 0;
298             foreach my $t (split /\s+/, $status{tags}) {
299                 $okay = 1, last if (defined $common_include{$t});
300             }
301             if (defined $common_include{subj}) {
302                 if (index($status{subject}, $common_include{subj}) > -1) {
303                     $okay = 1;
304                 }
305             }
306             next unless ($okay);
307         }
308         if (%common_exclude) {
309             my $okay = 1;
310             foreach my $t (split /\s+/, $status{tags}) {
311                 $okay = 0, last if (defined $common_exclude{$t});
312             }
313             if (defined $common_exclude{subj}) {
314                 if (index($status{subject}, $common_exclude{subj}) > -1) {
315                     $okay = 0;
316                 }
317             }
318             next unless ($okay);
319         }
320             
321         my $html = sprintf "<li><a href=\"%s\">#%d: %s</a>\n<br>",
322             bugurl($bug), $bug, htmlsanit($status{subject});
323         $html .= htmlindexentrystatus(\%status) . "\n";
324         $section{$status{pending} . "_" . $status{severity}} .= $html;
325         push @rawsort, $html if $common_raw_sort;
326     }
327
328     my $result = "";
329     my $anydone = 0;
330     if ($common_raw_sort) {
331         $result .= "<UL>\n" . join("", @rawsort ) . "</UL>\n";
332     } else {
333         my @pendingList = qw(pending forwarded pending-fixed fixed done);
334         @pendingList = @common_pending_include if @common_pending_include;
335         @pendingList = reverse @pendingList if $common_pending_reverse;
336 #print STDERR join(",",@pendingList)."\n";
337 #print STDERR join(",",@common_pending_include).":$#common_pending_include\n";
338     foreach my $pending (@pendingList) {
339         next if grep( /^$pending$/, @common_pending_exclude);
340         my @severityList = @debbugs::gSeverityList;
341         @severityList = @common_severity_include if @common_severity_include;
342         @severityList = reverse @severityList if $common_severity_reverse;
343 #print STDERR join(",",@severityList)."\n";
344
345 #        foreach my $severity(@debbugs::gSeverityList) {
346         foreach my $severity(@severityList) {
347             next if grep( /^$severity$/, @common_severity_exclude);
348             $severity = $debbugs::gDefaultSeverity if ($severity eq '');
349             next unless defined $section{${pending} . "_" . ${severity}};
350             $result .= "<HR><H2>$debbugs::gSeverityDisplay{$severity} - $displayshowpending{$pending}</H2>\n";
351             #$result .= "(A list of <a href=\"http://${debbugs::gWebDomain}/db/si/$pending$severity\">all such bugs</a> is available).\n";
352             $result .= "(A list of all such bugs used to be available).\n";
353             $result .= "<UL>\n";
354             $result .= $section{$pending . "_" . $severity}; 
355             $result .= "</UL>\n";
356             $anydone = 1 if ($pending eq "done");
357          }
358     }
359
360     }
361     $result .= $debbugs::gHTMLExpireNote if ($anydone);
362     return $result;
363 }
364
365 sub countbugs {
366     my $bugfunc = shift;
367     if ($common_archive) {
368         open I, "<$debbugs::gSpoolDir/index.archive" or &quit("bugindex: $!");
369     } else {
370         open I, "<$debbugs::gSpoolDir/index.db" or &quit("bugindex: $!");
371     }
372
373     my %count = ();
374     while(<I>) 
375     {
376         if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
377             my $x = $bugfunc->(pkg => $1, bug => $2, status => $4, 
378                                submitter => $5, severity => $6, tags => $7);
379             $count{$x}++;
380         }
381     }
382     close I;
383     return %count;
384 }
385
386 sub getbugs {
387     my $bugfunc = shift;
388     my $opt = shift;
389
390     my @result = ();
391
392     if (!$common_archive && defined $opt && 
393         -e "$debbugs::gSpoolDir/by-$opt.idx") 
394     {
395         my %lookup;
396 print STDERR "optimized\n" if ($debug);
397         tie %lookup, DB_File => "$debbugs::gSpoolDir/by-$opt.idx", O_RDONLY
398             or die "$0: can't open $debbugs::gSpoolDir/by-$opt.idx ($!)\n";
399         while ($key = shift) {
400             my $bugs = $lookup{$key};
401             if (defined $bugs) {
402                 push @result, (unpack 'N*', $bugs);
403             }
404         }
405         untie %lookup;
406 print STDERR "done optimized\n" if ($debug);
407     } else {
408         if ( $common_archive ) {
409             open I, "<$debbugs::gSpoolDir/index.archive" 
410                 or &quit("bugindex: $!");
411         } else {
412             open I, "<$debbugs::gSpoolDir/index.db" 
413                 or &quit("bugindex: $!");
414         }
415         while(<I>) {
416             if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
417                 if ($bugfunc->(pkg => $1, bug => $2, status => $4,
418                             submitter => $5, severity => $6, tags => $7)) 
419                 {
420                     push (@result, $2);
421                 }
422             }
423         }
424         close I;
425     }
426     @result = sort {$a <=> $b} @result;
427     return \@result;
428 }
429
430 sub emailfromrfc822 {
431     my $email = shift;
432     $email =~ s/\s*\(.*\)\s*//;
433     $email = $1 if ($email =~ m/<(.*)>/);
434     return $email;
435 }
436
437 sub maintencoded {
438     my $input = shift;
439     my $encoded = '';
440
441     while ($input =~ m/\W/) {
442         $encoded.=$`.sprintf("-%02x_",unpack("C",$&));
443         $input= $';
444     }
445
446     $encoded.= $input;
447     $encoded =~ s/-2e_/\./g;
448     $encoded =~ s/^([^,]+)-20_-3c_(.*)-40_(.*)-3e_/$1,$2,$3,/;
449     $encoded =~ s/^(.*)-40_(.*)-20_-28_([^,]+)-29_$/,$1,$2,$3/;
450     $encoded =~ s/-20_/_/g;
451     $encoded =~ s/-([^_]+)_-/-$1/g;
452     return $encoded;
453 }
454
455 my $_maintainer;
456 sub getmaintainers {
457     return $_maintainer if $_maintainer;
458     my %maintainer;
459
460     open(MM,"$gMaintainerFile") or &quit("open $gMaintainerFile: $!");
461     while(<MM>) {
462         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
463         ($a,$b)=($1,$2);
464         $a =~ y/A-Z/a-z/;
465         $maintainer{$a}= $b;
466     }
467     close(MM);
468     open(MM,"$gMaintainerFileOverride") or &quit("open $gMaintainerFileOverride: $!");
469     while(<MM>) {
470         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
471         ($a,$b)=($1,$2);
472         $a =~ y/A-Z/a-z/;
473         $maintainer{$a}= $b;
474     }
475     close(MM);
476     $_maintainer = \%maintainer;
477     return $_maintainer;
478 }
479
480 my $_pkgsrc;
481 sub getpkgsrc {
482     return $_pkgsrc if $_pkgsrc;
483     my %pkgsrc;
484
485     open(MM,"$gPackageSource") or &quit("open $gPackageSource: $!");
486     while(<MM>) {
487         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
488         ($a,$b)=($1,$2);
489         $a =~ y/A-Z/a-z/;
490         $pkgsrc{$a}= $b;
491     }
492     close(MM);
493     $_pkgsrc = \%pkgsrc;
494     return $_pkgsrc;
495 }
496
497 sub getbugdir {
498     my ( $bugnum, $ext ) = @_;
499     my $archdir = sprintf "%02d", $bugnum % 100;
500     foreach ( ( "$gSpoolDir/db-h/$archdir", "$gSpoolDir/db", "$gSpoolDir/archive/$archdir" ) ) {
501         return $_ if ( -r "$_/$bugnum.$ext" );
502     }
503     return undef;
504 }
505     
506 sub getbugstatus {
507     my $bugnum = shift;
508
509     my %status;
510
511     my $dir = getbugdir( $bugnum, "status" );
512     return {} if ( !$dir );
513     open S, "< $dir/$bugnum.status";
514     my @lines = qw(originator date subject msgid package tags done
515                         forwarded mergedwith severity);
516     while(<S>) {
517         chomp;
518         $status{shift @lines} = $_;
519     }
520     close(S);
521     $status{shift @lines} = '' while(@lines);
522
523     $status{"package"} =~ s/\s*$//;
524     $status{"package"} = 'unknown' if ($status{"package"} eq '');
525     $status{"severity"} = 'normal' if ($status{"severity"} eq '');
526
527     $status{"pending"} = 'pending';
528     $status{"pending"} = 'forwarded'        if (length($status{"forwarded"}));
529     $status{"pending"} = 'fixed'            if ($status{"tags"} =~ /\bfixed\b/);
530     $status{"pending"} = 'pending-fixed'    if ($status{"tags"} =~ /\bpending\b/);
531     $status{"pending"} = 'done'             if (length($status{"done"}));
532
533     return \%status;
534 }
535
536 sub getsrcpkgs {
537     my $src = shift;
538     return () if !$src;
539     my %pkgsrc = %{getpkgsrc()};
540     my @pkgs;
541     foreach ( keys %pkgsrc ) {
542         push @pkgs, $_ if $pkgsrc{$_} eq $src;
543     }
544     return @pkgs;
545 }
546    
547 sub buglog {
548     my $bugnum = shift;
549
550     my $dir = getbugdir( $bugnum, "log" );
551     return "" if ( !$dir );
552     return "$dir/$bugnum.log";
553 }
554
555 1;