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