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