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