]> git.donarmstrong.com Git - debbugs.git/blob - cgi/common.pl
12371f118ff7d01eca0aa08ad647eb031de1e08f
[debbugs.git] / cgi / common.pl
1 #!/usr/bin/perl -w
2
3 my $common_archive = 0;
4 my $common_repeatmerged = 1;
5 my %common_include = ();
6 my %common_exclude = ();
7
8 sub set_option {
9     my ($opt, $val) = @_;
10     if ($opt eq "archive") { $common_archive = $val; }
11     if ($opt eq "repeatmerged") { $common_repeatmerged = $val; }
12     if ($opt eq "exclude") { %common_exclude = %{$val}; }
13     if ($opt eq "include") { %common_include = %{$val}; }
14 }
15
16 sub quit {
17     my $msg = shift;
18     print "Content-Type: text/html\n\n";
19     print "<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n";
20     print "An error occurred. Dammit.\n";
21     print "Error was: $msg.\n";
22     print "</BODY></HTML>\n";
23     exit 0;
24 }
25
26 #sub abort {
27 #    my $msg = shift;
28 #    my $Archive = $common_archive ? "archive" : "";
29 #    print header . start_html("Sorry");
30 #    print "Sorry bug #$msg doesn't seem to be in the $Archive database.\n";
31 #    print end_html;
32 #    exit 0;
33 #}
34
35 sub htmlindexentry {
36     my $ref = shift;
37     my %status = getbugstatus($ref);
38     return htmlindexentrystatus(%status) if (%status);
39     return "";
40 }
41
42 sub htmlindexentrystatus {
43     my $s = shift;
44     my %status = %{$s};
45
46     my $result = "";
47
48     if  ($status{severity} eq 'normal') {
49         $showseverity = '';
50     } elsif (grep($status{severity} eq $_, @debbugs::gStrongSeverities)) {
51         $showseverity = "<strong>Severity: $status{severity}</strong>;\n";
52     } else {
53         $showseverity = "Severity: <em>$status{severity}</em>;\n";
54     }
55
56     $result .= "Package: <a href=\"" . pkgurl($status{"package"}) . "\">"
57                . "<strong>" . htmlsanit($status{"package"}) . "</strong></a>;\n"
58                if (length($status{"package"}));
59     $result .= $showseverity;
60     $result .= "Reported by: " . htmlsanit($status{originator});
61     $result .= ";\nTags: <strong>" 
62                  . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
63                  . "</strong>"
64                        if (length($status{tags}));
65
66     my @merged= split(/ /,$status{mergedwith});
67     my $mseparator= ";\nmerged with ";
68     for my $m (@merged) {
69         $result .= $mseparator."<A href=\"" . bugurl($m) . "\">#$m</A>";
70         $mseparator= ", ";
71     }
72
73     if (length($status{done})) {
74         $result .= ";\n<strong>Done:</strong> " . htmlsanit($status{done});
75     } elsif (length($status{forwarded})) {
76         $result .= ";\n<strong>Forwarded</strong> to "
77                    . htmlsanit($status{forwarded});
78     } else {
79         my $daysold = int((time - $status{date}) / 86400);   # seconds to days
80         if ($daysold >= 7) {
81             my $font = "";
82             my $efont = "";
83             $font = "em" if ($daysold > 30);
84             $font = "strong" if ($daysold > 60);
85             $efont = "</$font>" if ($font);
86             $font = "<$font>" if ($font);
87
88             my $yearsold = int($daysold / 364);
89             $daysold = $daysold - $yearsold * 364;
90
91             $result .= ";\n $font";
92             $result .= "1 year and " if ($yearsold == 1);
93             $result .= "$yearsold years and " if ($yearsold > 1);
94             $result .= "1 day old" if ($daysold == 1);
95             $result .= "$daysold days old" if ($daysold != 1);
96             $result .= "$efont";
97         }
98     }
99
100     $result .= ".";
101
102     return $result;
103 }
104
105 sub mainturl {
106     my $ref = shift;
107     my $params = "maintenc=" . maintencoded($ref);
108     $params .= "&archive=yes" if ($common_archive);
109     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
110     return $debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params;
111 }
112
113 sub pkgurl {
114     my $ref = shift;
115     my $params = "pkg=$ref";
116     $params .= "&archive=yes" if ($common_archive);
117     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
118     
119     return $debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params";
120 }
121
122 sub htmlsanit {
123     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
124     my $in = shift;
125     my $out;
126     while ($in =~ m/[<>&"]/) {
127         $out .= $`. '&'. $saniarray{$&}. ';';
128         $in = $';
129     }
130     $out .= $in;
131     return $out;
132 }
133
134 sub bugurl {
135     my $ref = shift;
136     my $params = "bug=$ref";
137     foreach my $val (@_) {
138         $params .= "\&msg=$1" if ($val =~ /^msg=([0-9]+)/);
139         $params .= "\&archive=yes" if (!$common_archive && $val =~ /^archive.*$/);
140     }
141     $params .= "&archive=yes" if ($common_archive);
142     $params .= "&repeatmerged=yes" if ($common_repeatmerged);
143
144     return $debbugs::gCGIDomain . "bugreport.cgi" . "?" . "$params";
145 }
146
147 sub packageurl {
148     my $ref = shift;
149     return $debbugs::gCGIDomain . "package.cgi" . "?" . "package=$ref";
150 }
151
152 sub allbugs {
153     my @bugs = ();
154
155     opendir(D, "$debbugs::gSpoolDir/db") or &quit("opendir db: $!");
156     @bugs = sort {$a<=>$b} grep s/\.status$//,
157                  (grep m/^[0-9]+\.status$/,
158                  (readdir(D)));
159     closedir(D);
160
161     return @bugs;
162 }
163
164 sub htmlizebugs {
165     my @bugs = @_;
166
167     my %section = ();
168
169     my %displayshowpending = ("pending", "outstanding",
170                               "done", "resolved",
171                               "forwarded", "forwarded to upstream software authors");
172
173     if (@bugs == 0) {
174         return "<HR><H2>No reports found!</H2></HR>\n";
175     }
176
177     foreach my $bug (sort {$a<=>$b} @bugs) {
178         my %status = getbugstatus($bug);
179         next unless %status;
180         my @merged = sort {$a<=>$b} ($bug, split(/ /, $status{mergedwith}));
181         next unless ($common_repeatmerged || $bug == $merged[0]);
182         if (%common_include) {
183             my $okay = 0;
184             foreach my $t (split /\s+/, $status{tags}) {
185                 $okay = 1, last if (defined $common_include{$t});
186             }
187             next unless ($okay);
188         }
189         if (%common_exclude) {
190             my $okay = 1;
191             foreach my $t (split /\s+/, $status{tags}) {
192                 $okay = 0, last if (defined $comon_exclude{$t});
193             }
194             next unless ($okay);
195         }
196             
197         $section{$status{pending} . "_" . $status{severity}} .=
198             sprintf "<li><a href=\"%s\">#%d: %s</a>\n<br>",
199                 bugurl($bug), $bug, htmlsanit($status{subject});
200         $section{$status{pending} . "_" . $status{severity}} .=
201             htmlindexentrystatus(\%status) . "\n";
202     }
203
204     my $result = "";
205     my $anydone = 0;
206     foreach my $pending (qw(pending forwarded done)) {
207         foreach my $severity(@debbugs::gSeverityList) {
208             $severity = $debbugs::gDefaultSeverity if ($severity eq '');
209             next unless defined $section{${pending} . "_" . ${severity}};
210             $result .= "<HR><H2>$debbugs::gSeverityDisplay{$severity} - $displayshowpending{$pending}</H2>\n";
211             $result .= "(A list of <a href=\"http://www.debian.org/Bugs/db/si/$pending$severity\">all such bugs</a> is available).\n";
212             $result .= "<UL>\n";
213             $result .= $section{$pending . "_" . $severity}; 
214             $result .= "</UL>\n";
215             $anydone = 1 if ($pending eq "done");
216          }
217     }
218
219     $result .= $debbugs::gHTMLExpireNote if ($anydone);
220     return $result;
221 }
222
223 sub submitterbugs {
224     my $submitter = shift;
225     my $chk = sub {
226         my %d = @_;
227         ($subemail = $d{"submitter"}) =~ s/\s*\(.*\)\s*//;
228         if ($subemail =~ m/<(.*)>/) { $subemail = $1 }
229         return $subemail eq $submitter;
230     };
231     return getbugs($chk);
232 }
233
234 sub severitybugs {
235     my $status = shift;
236     my $severity = shift;
237     my $chk = sub {
238         my %d = @_; 
239         return ($d{"severity"} eq $severity) && ($d{"status"} eq $status); 
240     };
241     return getbugs($chk);
242 }
243
244 sub maintbugs {
245     my $maint = shift;
246     my %maintainers = getmaintainers();
247     my $chk = sub {
248         my %d = @_;
249         ($maintemail = $maintainers{$d{"pkg"}} || "") =~ s/\s*\(.*\)\s*//;
250         if ($maintemail =~ m/<(.*)>/) { $maintemail = $1 }
251         return $maintemail eq $maint;
252     };
253     return getbugs($chk);
254 }
255
256 sub maintencbugs {
257     my $maint = shift;
258     my %maintainers = getmaintainers();
259     return getbugs(sub {my %d=@_; return maintencoded($maintainers{$d{"pkg"}} || "") eq $maint});
260 }
261
262 sub pkgbugs {
263     my $inpkg = shift;
264     return getbugs( sub { my %d = @_; return $inpkg eq $d{"pkg"} });
265 }
266
267 sub getbugs {
268     my $bugfunc = shift;
269
270     if ( $common_archive ) {
271         open I, "<$debbugs::gSpoolDir/index.archive" or &quit("bugindex: $!");
272     } else {
273         open I, "<$debbugs::gSpoolDir/index.db" or &quit("bugindex: $!");
274     }
275     
276     my @result = ();
277     while(<I>) 
278     {
279         if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
280             if ($bugfunc->(pkg => $1, bug => $2, status => $4, submitter => $5,
281                            severity => $6, tags => $7))
282             {
283                 push (@result, $2);
284                 #last if (@result > 400);
285             }
286         }
287     }
288     close I;
289     return sort {$a <=> $b} @result;
290 }
291
292 sub pkgbugsindex {
293     my %descstr = ();
294     if ( $common_archive ) {
295         open I, "<$debbugs::gSpoolDir/index.archive" or &quit("bugindex: $!");
296     } else {
297         open I, "<$debbugs::gSpoolDir/index.db" or &quit("bugindex: $!");
298     }
299     while(<I>) { 
300         $descstr{ $1 } = 1 if (m/^(\S+)/);
301     }
302     close(I);
303     return %descstr;
304 }
305
306 sub maintencoded {
307     my $input = shift;
308     my $encoded = '';
309
310     while ($input =~ m/\W/) {
311         $encoded.=$`.sprintf("-%02x_",unpack("C",$&));
312         $input= $';
313     }
314
315     $encoded.= $input;
316     $encoded =~ s/-2e_/\./g;
317     $encoded =~ s/^([^,]+)-20_-3c_(.*)-40_(.*)-3e_/$1,$2,$3,/;
318     $encoded =~ s/^(.*)-40_(.*)-20_-28_([^,]+)-29_$/,$1,$2,$3/;
319     $encoded =~ s/-20_/_/g;
320     $encoded =~ s/-([^_]+)_-/-$1/g;
321     return $encoded;
322 }
323
324 sub getmaintainers {
325     my %maintainer;
326
327     open(MM,"$gMaintainerFile") or &quit("open $gMaintainerFile: $!");
328     while(<MM>) {
329         next unless m/^(\S+)\s+(\S.*\S)\s*$/;
330         ($a,$b)=($1,$2);
331         $a =~ y/A-Z/a-z/;
332         $maintainer{$a}= $b;
333     }
334     close(MM);
335
336     return %maintainer;
337 }
338
339 sub getbugstatus {
340     my $bugnum = shift;
341
342     my %status;
343
344     unless (open(S,"$gSpoolDir/db/$bugnum.status")) {
345         my $archdir = sprintf "%02d", $bugnum % 100;
346         open(S,"$gSpoolDir/archive/$archdir/$bugnum.status" ) or return ();
347     }
348     my @lines = qw(originator date subject msgid package tags done
349                         forwarded mergedwith severity);
350     while(<S>) {
351         chomp;
352         $status{shift @lines} = $_;
353     }
354     close(S);
355     $status{shift @lines} = '' while(@lines);
356
357     $status{"package"} =~ s/\s*$//;
358     $status{"package"} = 'unknown' if ($status{"package"} eq '');
359     $status{"severity"} = 'normal' if ($status{"severity"} eq '');
360
361     $status{"pending"} = 'pending';
362     $status{"pending"} = 'forwarded' if (length($status{"forwarded"}));
363     $status{"pending"} = 'done'      if (length($status{"done"}));
364
365     return %status;
366 }
367
368 sub buglog {
369     my $bugnum = shift;
370     my $res;
371
372     $res = "$gSpoolDir/db/$bugnum.log"; 
373     return $res if ( -e $res );
374
375     my $archdir = sprintf "%02d", $bugnum % 100;
376     $res = "$gSpoolDir/archive/$archdir/$bugnum.log";
377     return $res if ( -e $res );
378
379     return "";
380 }
381
382 1