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