]> git.donarmstrong.com Git - debbugs.git/blob - cgi/common.pl
[project @ 2001-11-17 06:49:28 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: " . htmlsanit($status{originator});
135     $result .= ";\nTags: <strong>" 
136                  . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
137                  . "</strong>"
138                        if (length($status{tags}));
139
140     my @merged= split(/ /,$status{mergedwith});
141     my $mseparator= ";\nmerged with ";
142     for my $m (@merged) {
143         $result .= $mseparator."<A href=\"" . bugurl($m) . "\">#$m</A>";
144         $mseparator= ", ";
145     }
146
147     if (length($status{done})) {
148         $result .= ";\n<strong>Done:</strong> " . htmlsanit($status{done});
149     } elsif (length($status{forwarded})) {
150         $result .= ";\n<strong>Forwarded</strong> to "
151                    . htmlsanit($status{forwarded});
152     } else {
153         my $daysold = int((time - $status{date}) / 86400);   # seconds to days
154         if ($daysold >= 7) {
155             my $font = "";
156             my $efont = "";
157             $font = "em" if ($daysold > 30);
158             $font = "strong" if ($daysold > 60);
159             $efont = "</$font>" if ($font);
160             $font = "<$font>" if ($font);
161
162             my $yearsold = int($daysold / 364);
163             $daysold = $daysold - $yearsold * 364;
164
165             $result .= ";\n $font";
166             $result .= "1 year and " if ($yearsold == 1);
167             $result .= "$yearsold years and " if ($yearsold > 1);
168             $result .= "1 day old" if ($daysold == 1);
169             $result .= "$daysold days old" if ($daysold != 1);
170             $result .= "$efont";
171         }
172     }
173
174     $result .= ".";
175
176     return $result;
177 }
178
179 sub submitterurl {
180     my $ref = shift || "";
181     my $params = "submitter=" . emailfromrfc822($ref);
182     $params .= "&archive=yes" if ($common_archive);
183     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
184     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
185 }
186
187 sub mainturl {
188     my $ref = shift || "";
189     my $params = "maint=" . emailfromrfc822($ref);
190     $params .= "&archive=yes" if ($common_archive);
191     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
192     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
193 }
194
195 sub pkgurl {
196     my $ref = shift;
197     my $params = "pkg=$ref";
198     $params .= "&archive=yes" if ($common_archive);
199     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
200     
201     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
202 }
203
204 sub srcurl {
205     my $ref = shift;
206     my $params = "src=$ref";
207     $params .= "&archive=yes" if ($common_archive);
208     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
209     return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
210 }
211
212 sub urlsanit {
213     my $url = shift;
214     $url =~ s/%/%25/g;
215     $url =~ s/\+/%2b/g;
216     my %saniarray = ('<','lt', '>','gt', '"','quot');
217     my $out;
218     while ($url =~ m/[<>"]/) {
219         $out .= $`. '&'. $saniarray{$&}. ';';
220         $url = $';
221     }
222     $out .= $url;
223     return $out;
224 }
225
226 sub htmlsanit {
227     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
228     my $in = shift || "";
229     my $out;
230     while ($in =~ m/[<>&"]/) {
231         $out .= $`. '&'. $saniarray{$&}. ';';
232         $in = $';
233     }
234     $out .= $in;
235     return $out;
236 }
237
238 sub bugurl {
239     my $ref = shift;
240     my $params = "bug=$ref";
241     foreach my $val (@_) {
242         $params .= "\&msg=$1" if ($val =~ /^msg=([0-9]+)/);
243         $params .= "\&archive=yes" if (!$common_archive && $val =~ /^archive.*$/);
244     }
245     $params .= "&archive=yes" if ($common_archive);
246     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
247
248     return urlsanit($debbugs::gCGIDomain . "bugreport.cgi" . "?" . "$params");
249 }
250
251 sub packageurl {
252     my $ref = shift;
253     return urlsanit($debbugs::gCGIDomain . "package.cgi" . "?" . "package=$ref");
254 }
255
256 sub allbugs {
257     my @bugs = ();
258
259     opendir(D, "$debbugs::gSpoolDir/db") or &quit("opendir db: $!");
260     @bugs = sort {$a<=>$b} grep s/\.status$//,
261                  (grep m/^[0-9]+\.status$/,
262                  (readdir(D)));
263     closedir(D);
264
265     return @bugs;
266 }
267
268 sub htmlizebugs {
269     $b = $_[0];
270     my @bugs = @$b;
271     my @rawsort;
272
273     my %section = ();
274
275     my %displayshowpending = ("pending", "outstanding",
276                               "pending-fixed", "pending upload",
277                               "fixed", "fixed in NMU",
278                               "done", "resolved",
279                               "forwarded", "forwarded to upstream software authors");
280
281     if (@bugs == 0) {
282         return "<HR><H2>No reports found!</H2></HR>\n";
283     }
284
285     if ( $common_bug_reverse ) {
286         @bugs = sort {$b<=>$a} @bugs;
287     } else {
288         @bugs = sort {$a<=>$b} @bugs;
289     }
290     foreach my $bug (@bugs) {
291         my %status = %{getbugstatus($bug)};
292         next unless %status;
293         my @merged = sort {$a<=>$b} ($bug, split(/ /, $status{mergedwith}));
294         next unless ($common_repeatmerged || $bug == $merged[0]);
295         if (%common_include) {
296             my $okay = 0;
297             foreach my $t (split /\s+/, $status{tags}) {
298                 $okay = 1, last if (defined $common_include{$t});
299             }
300             if (defined $common_include{subj}) {
301                 if (index($status{subject}, $common_include{subj}) > -1) {
302                     $okay = 1;
303                 }
304             }
305             next unless ($okay);
306         }
307         if (%common_exclude) {
308             my $okay = 1;
309             foreach my $t (split /\s+/, $status{tags}) {
310                 $okay = 0, last if (defined $common_exclude{$t});
311             }
312             if (defined $common_exclude{subj}) {
313                 if (index($status{subject}, $common_exclude{subj}) > -1) {
314                     $okay = 0;
315                 }
316             }
317             next unless ($okay);
318         }
319             
320         my $html = sprintf "<li><a href=\"%s\">#%d: %s</a>\n<br>",
321             bugurl($bug), $bug, htmlsanit($status{subject});
322         $html .= htmlindexentrystatus(\%status) . "\n";
323         $section{$status{pending} . "_" . $status{severity}} .= $html;
324         push @rawsort, $html if $common_raw_sort;
325     }
326
327     my $result = "";
328     my $anydone = 0;
329     if ($common_raw_sort) {
330         $result .= "<UL>\n" . join("", @rawsort ) . "</UL>\n";
331     } else {
332         my @pendingList = qw(pending forwarded pending-fixed fixed done);
333         @pendingList = @common_pending_include if @common_pending_include;
334         @pendingList = reverse @pendingList if $common_pending_reverse;
335 #print STDERR join(",",@pendingList)."\n";
336 #print STDERR join(",",@common_pending_include).":$#common_pending_include\n";
337     foreach my $pending (@pendingList) {
338         next if grep( /^$pending$/, @common_pending_exclude);
339         my @severityList = @debbugs::gSeverityList;
340         @severityList = @common_severity_include if @common_severity_include;
341         @severityList = reverse @severityList if $common_severity_reverse;
342 #print STDERR join(",",@severityList)."\n";
343
344 #        foreach my $severity(@debbugs::gSeverityList) {
345         foreach my $severity(@severityList) {
346             next if grep( /^$severity$/, @common_severity_exclude);
347             $severity = $debbugs::gDefaultSeverity if ($severity eq '');
348             next unless defined $section{${pending} . "_" . ${severity}};
349             $result .= "<HR><H2>$debbugs::gSeverityDisplay{$severity} - $displayshowpending{$pending}</H2>\n";
350             #$result .= "(A list of <a href=\"http://${debbugs::gWebDomain}/db/si/$pending$severity\">all such bugs</a> is available).\n";
351             $result .= "(A list of all such bugs used to be available).\n";
352             $result .= "<UL>\n";
353             $result .= $section{$pending . "_" . $severity}; 
354             $result .= "</UL>\n";
355             $anydone = 1 if ($pending eq "done");
356          }
357     }
358
359     }
360     $result .= $debbugs::gHTMLExpireNote if ($anydone);
361     return $result;
362 }
363
364 sub countbugs {
365     my $bugfunc = shift;
366     if ($common_archive) {
367         open I, "<$debbugs::gSpoolDir/index.archive" or &quit("bugindex: $!");
368     } else {
369         open I, "<$debbugs::gSpoolDir/index.db" or &quit("bugindex: $!");
370     }
371
372     my %count = ();
373     while(<I>) 
374     {
375         if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
376             my $x = $bugfunc->(pkg => $1, bug => $2, status => $4, 
377                                submitter => $5, severity => $6, tags => $7);
378             $count{$x}++;
379         }
380     }
381     close I;
382     return %count;
383 }
384
385 sub getbugs {
386     my $bugfunc = shift;
387     my $opt = shift;
388
389     my @result = ();
390
391     if (!$common_archive && defined $opt && 
392         -e "$debbugs::gSpoolDir/by-$opt.idx") 
393     {
394         my %lookup;
395 print STDERR "optimized\n" if ($debug);
396         tie %lookup, DB_File => "$debbugs::gSpoolDir/by-$opt.idx", O_RDONLY
397             or die "$0: can't open $debbugs::gSpoolDir/by-$opt.idx ($!)\n";
398         while ($key = shift) {
399             my $bugs = $lookup{$key};
400             if (defined $bugs) {
401                 push @result, (unpack 'N*', $bugs);
402             }
403         }
404         untie %lookup;
405 print STDERR "done optimized\n" if ($debug);
406     } else {
407         if ( $common_archive ) {
408             open I, "<$debbugs::gSpoolDir/index.archive" 
409                 or &quit("bugindex: $!");
410         } else {
411             open I, "<$debbugs::gSpoolDir/index.db" 
412                 or &quit("bugindex: $!");
413         }
414         while(<I>) {
415             if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
416                 if ($bugfunc->(pkg => $1, bug => $2, status => $4,
417                             submitter => $5, severity => $6, tags => $7)) 
418                 {
419                     push (@result, $2);
420                 }
421             }
422         }
423         close I;
424     }
425     @result = sort {$a <=> $b} @result;
426     return \@result;
427 }
428
429 sub emailfromrfc822 {
430     my $email = shift;
431     $email =~ s/\s*\(.*\)\s*//;
432     $email = $1 if ($email =~ m/<(.*)>/);
433     return $email;
434 }
435
436 sub maintencoded {
437     my $input = shift;
438     my $encoded = '';
439
440     while ($input =~ m/\W/) {
441         $encoded.=$`.sprintf("-%02x_",unpack("C",$&));
442         $input= $';
443     }
444
445     $encoded.= $input;
446     $encoded =~ s/-2e_/\./g;
447     $encoded =~ s/^([^,]+)-20_-3c_(.*)-40_(.*)-3e_/$1,$2,$3,/;
448     $encoded =~ s/^(.*)-40_(.*)-20_-28_([^,]+)-29_$/,$1,$2,$3/;
449     $encoded =~ s/-20_/_/g;
450     $encoded =~ s/-([^_]+)_-/-$1/g;
451     return $encoded;
452 }
453
454 my $_maintainer;
455 sub getmaintainers {
456     return $_maintainer if $_maintainer;
457     my %maintainer;
458
459     open(MM,"$gMaintainerFile") or &quit("open $gMaintainerFile: $!");
460     while(<MM>) {
461         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
462         ($a,$b)=($1,$2);
463         $a =~ y/A-Z/a-z/;
464         $maintainer{$a}= $b;
465     }
466     close(MM);
467     open(MM,"$gMaintainerFileOverride") or &quit("open $gMaintainerFileOverride: $!");
468     while(<MM>) {
469         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
470         ($a,$b)=($1,$2);
471         $a =~ y/A-Z/a-z/;
472         $maintainer{$a}= $b;
473     }
474     close(MM);
475     $_maintainer = \%maintainer;
476     return $_maintainer;
477 }
478
479 my $_pkgsrc;
480 sub getpkgsrc {
481     return $_pkgsrc if $_pkgsrc;
482     my %pkgsrc;
483
484     open(MM,"$gPackageSource") or &quit("open $gPackageSource: $!");
485     while(<MM>) {
486         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
487         ($a,$b)=($1,$2);
488         $a =~ y/A-Z/a-z/;
489         $pkgsrc{$a}= $b;
490     }
491     close(MM);
492     $_pkgsrc = \%pkgsrc;
493     return $_pkgsrc;
494 }
495
496 sub getbugdir {
497     my ( $bugnum, $ext ) = @_;
498     my $archdir = sprintf "%02d", $bugnum % 100;
499     foreach ( ( "$gSpoolDir/db-h/$archdir", "$gSpoolDir/db", "$gSpoolDir/archive/$archdir" ) ) {
500         return $_ if ( -r "$_/$bugnum.$ext" );
501     }
502     return undef;
503 }
504     
505 sub getbugstatus {
506     my $bugnum = shift;
507
508     my %status;
509
510     my $dir = getbugdir( $bugnum, "status" );
511     return {} if ( !$dir );
512     open S, "< $dir/$bugnum.status";
513     my @lines = qw(originator date subject msgid package tags done
514                         forwarded mergedwith severity);
515     while(<S>) {
516         chomp;
517         $status{shift @lines} = $_;
518     }
519     close(S);
520     $status{shift @lines} = '' while(@lines);
521
522     $status{"package"} =~ s/\s*$//;
523     $status{"package"} = 'unknown' if ($status{"package"} eq '');
524     $status{"severity"} = 'normal' if ($status{"severity"} eq '');
525
526     $status{"pending"} = 'pending';
527     $status{"pending"} = 'forwarded'        if (length($status{"forwarded"}));
528     $status{"pending"} = 'fixed'            if ($status{"tags"} =~ /\bfixed\b/);
529     $status{"pending"} = 'pending-fixed'    if ($status{"tags"} =~ /\bpending\b/);
530     $status{"pending"} = 'done'             if (length($status{"done"}));
531
532     return \%status;
533 }
534
535 sub getsrcpkgs {
536     my $src = shift;
537
538     my %pkgsrc = %{getpkgsrc()};
539     my @pkgs;
540     foreach ( keys %pkgsrc ) {
541         push @pkgs, $_ if $pkgsrc{$_} eq $src;
542     }
543     return @pkgs;
544 }
545    
546 sub buglog {
547     my $bugnum = shift;
548
549     my $dir = getbugdir( $bugnum, "log" );
550     return "" if ( !$dir );
551     return "$dir/$bugnum.log";
552 }
553
554 1;