]> git.donarmstrong.com Git - debbugs.git/blob - cgi/common.pl
* Strip out the rest of the functions from cgi/common.pl that have
[debbugs.git] / cgi / common.pl
1 #!/usr/bin/perl -w
2
3 use DB_File;
4 use Fcntl qw/O_RDONLY/;
5 use Mail::Address;
6 use MLDBM qw(DB_File Storable);
7 use POSIX qw/ceil/;
8
9 use URI::Escape;
10
11 use Debbugs::Config qw(:globals :text);
12 $config_path = '/etc/debbugs';
13 $lib_path = '/usr/lib/debbugs';
14 require "$lib_path/errorlib";
15
16 use Debbugs::Packages qw(:versions :mapping);
17 use Debbugs::Versions;
18 use Debbugs::MIME qw(decode_rfc1522);
19 use Debbugs::Common qw(:util);
20 use Debbugs::Status qw(:read :versions);
21 use Debbugs::CGI qw(:all);
22
23 $MLDBM::RemoveTaint = 1;
24
25 my %common_bugusertags;
26 my $common_mindays = 0;
27 my $common_maxdays = -1;
28 my $common_archive = 0;
29 my $common_repeatmerged = 1;
30 my %common_include = ();
31 my %common_exclude = ();
32 my $common_raw_sort = 0;
33 my $common_bug_reverse = 0;
34
35 my $common_leet_urls = 0;
36
37 my %common_reverse = (
38     'pending' => 0,
39     'severity' => 0,
40 );
41 my %common = (
42     'show_list_header' => 1,
43     'show_list_footer' => 1,
44 );
45
46 sub exact_field_match {
47     my ($field, $values, $status) = @_; 
48     my @values = @$values;
49     my @ret = grep {$_ eq $status->{$field} } @values;
50     $#ret != -1;
51 }
52 sub contains_field_match {
53     my ($field, $values, $status) = @_; 
54     foreach my $data (@$values) {
55         return 1 if (index($status->{$field}, $data) > -1);
56     }
57     return 0;        
58 }
59
60 sub detect_user_agent {
61     my $userAgent = $ENV{HTTP_USER_AGENT};
62     return { 'name' => 'unknown' } unless defined $userAgent;
63     return { 'name' => 'links' } if ( $userAgent =~ m,^ELinks,);
64     return { 'name' => 'lynx' } if ( $userAgent =~ m,^Lynx,);
65     return { 'name' => 'wget' } if ( $userAgent =~ m,^Wget,);
66     return { 'name' => 'gecko' } if ( $userAgent =~ m,^Mozilla.* Gecko/,);
67     return { 'name' => 'ie' } if ( $userAgent =~ m,^.*MSIE.*,);
68     return { 'name' => 'unknown' };
69 }
70
71 my %field_match = (
72     'subject' => \&contains_field_match,
73     'tags' => sub {
74         my ($field, $values, $status) = @_; 
75         my %values = map {$_=>1} @$values;
76         foreach my $t (split /\s+/, $status->{$field}) {
77             return 1 if (defined $values{$t});
78         }
79         return 0;
80     },
81     'severity' => \&exact_field_match,
82     'pending' => \&exact_field_match,
83     'originator' => \%contains_field_match,
84     'forwarded' => \%contains_field_match,
85     'owner' => \%contains_field_match,
86 );
87 my @common_grouping = ( 'severity', 'pending' );
88 my %common_grouping_order = (
89     'pending' => [ qw( pending forwarded pending-fixed fixed done absent ) ],
90     'severity' => \@gSeverityList,
91 );
92 my %common_grouping_display = (
93     'pending' => 'Status',
94     'severity' => 'Severity',
95 );
96 my %common_headers = (
97     'pending' => {
98         "pending"       => "outstanding",
99         "pending-fixed" => "pending upload",
100         "fixed"         => "fixed in NMU",
101         "done"          => "resolved",
102         "forwarded"     => "forwarded to upstream software authors",
103         "absent"        => "not applicable to this version",
104     },
105     'severity' => \%gSeverityDisplay,
106 );
107
108 my $common_version;
109 my $common_dist;
110 my $common_arch;
111
112 my $debug = 0;
113 my $use_bug_idx = 0;
114 my %bugidx;
115
116 sub array_option($) {
117     my ($val) = @_;
118     my @vals;
119     @vals = ( $val ) if (ref($val) eq "" && $val );
120     @vals = ( $$val ) if (ref($val) eq "SCALAR" && $$val );
121     @vals = @{$val} if (ref($val) eq "ARRAY" );
122     return @vals;
123 }
124
125 sub filter_include_exclude($\%) {
126     my ($val, $filter_map) = @_;
127     my @vals = array_option($val);
128     my @data = map {
129         if (/^([^:]*):(.*)$/) { if ($1 eq 'subj') { ['subject', $2]; } else { [$1, $2] } } else { ['tags', $_] }
130     } split /[\s,]+/, join ',', @vals;
131     foreach my $data (@data) {
132         &quitcgi("Invalid filter key: '$data->[0]'") if (!exists($field_match{$data->[0]}));
133         push @{$filter_map->{$data->[0]}}, $data->[1];
134     }
135 }
136
137 sub filter_option($$\%) {
138     my ($key, $val, $filter_map) = @_;
139     my @vals = array_option($val);
140     foreach $val (@vals) {
141         push @{$filter_map->{$key}}, $val;
142     }
143 }
144
145 sub set_option {
146     my ($opt, $val) = @_;
147     if ($opt eq "use-bug-idx") {
148         $use_bug_idx = $val;
149         if ( $val ) {
150             $common_headers{pending}{open} = $common_headers{pending}{pending};
151             my $bugidx = tie %bugidx, MLDBM => "$gSpoolDir/realtime/bug.idx", O_RDONLY
152                 or quitcgi( "$0: can't open $gSpoolDir/realtime/bug.idx ($!)\n" );
153             $bugidx->RemoveTaint(1);
154         } else {
155             untie %bugidx;
156         }
157     }
158     if ($opt =~ m/^show_list_(foot|head)er$/) { $common{$opt} = $val; }
159     if ($opt eq "archive") { $common_archive = $val; }
160     if ($opt eq "repeatmerged") { $common_repeatmerged = $val; }
161     if ($opt eq "exclude") {
162         filter_include_exclude($val, %common_exclude);
163     }
164     if ($opt eq "include") {
165         filter_include_exclude($val, %common_include);
166     }
167     if ($opt eq "raw") { $common_raw_sort = $val; }
168     if ($opt eq "bug-rev") { $common_bug_reverse = $val; }
169     if ($opt eq "pend-rev") { $common_reverse{pending} = $val; }
170     if ($opt eq "sev-rev") { $common_reverse{severity} = $val; }
171     if ($opt eq "pend-exc") {
172         filter_option('pending', $val, %common_exclude);
173     }
174     if ($opt eq "pend-inc") {
175         filter_option('pending', $val, %common_include);
176     }
177     if ($opt eq "sev-exc") {
178         filter_option('severity', $val, %common_exclude);
179     }
180     if ($opt eq "sev-inc") {
181         filter_option('severity', $val, %common_include);
182     }
183     if ($opt eq "version") { $common_version = $val; }
184     if ($opt eq "dist") { $common_dist = $val; }
185     if ($opt eq "arch") { $common_arch = $val; }
186     if ($opt eq "maxdays") { $common_maxdays = $val; }
187     if ($opt eq "mindays") { $common_mindays = $val; }
188     if ($opt eq "bugusertags") { %common_bugusertags = %{$val}; }
189 }
190
191 sub readparse {
192     my ($key, $val, %ret);
193     my $in = "";
194     if ($#ARGV >= 0) {
195         $in .= ";" . join("&", map { s/&/%26/g; s/;/%3b/g; $_ } @ARGV);
196     }
197     if (defined $ENV{"QUERY_STRING"} && $ENV{"QUERY_STRING"} ne "") {
198         $in .= ";" . $ENV{QUERY_STRING};
199     }
200     if (defined $ENV{"REQUEST_METHOD"} && $ENV{"REQUEST_METHOD"} eq "POST"
201           && defined $ENV{"CONTENT_TYPE"}
202           && $ENV{"CONTENT_TYPE"} eq "application/x-www-form-urlencoded")
203     {
204         my $inx;
205         read(STDIN,$inx,$ENV{CONTENT_LENGTH});
206         $in .= ";" . $inx;
207     }
208     return unless ($in ne "");
209
210     if (defined $ENV{"HTTP_COOKIE"}) {
211         my $x = $ENV{"HTTP_COOKIE"};
212         $x =~ s/;\s+/;/g;
213         $in = "$x;$in";
214     }
215     $in =~ s/&/;/g;
216     $in =~ s/;;+/;/g; $in =~ s/^;//; $in =~ s/;$//;
217     foreach (split(/[&;]/,$in)) {
218         s/\+/ /g;
219         ($key, $val) = split(/=/,$_,2);
220         $key=~s/%(..)/pack("c",hex($1))/ge;
221         $val=~s/%(..)/pack("c",hex($1))/ge;
222         if ( exists $ret{$key} ) {
223             if ( !exists $ret{"&$key"} ) {
224                 $ret{"&$key"} = [ $ret{$key} ];
225             }
226             push @{$ret{"&$key"}},$val;
227         }
228         $ret{$key}=$val;
229     }
230
231 $debug = 1 if (defined $ret{"debug"} && $ret{"debug"} eq "aj");
232
233     $common_leet_urls = 1
234        if (defined $ret{"leeturls"} && $ret{"leeturls"} eq "yes");
235
236     return %ret;
237 }
238
239 #sub abort {
240 #    my $msg = shift;
241 #    my $Archive = $common_archive ? "archive" : "";
242 #    print header . start_html("Sorry");
243 #    print "Sorry bug #$msg doesn't seem to be in the $Archive database.\n";
244 #    print end_html;
245 #    exit 0;
246 #}
247
248 # Split a package string from the status file into a list of package names.
249 sub splitpackages {
250     my $pkgs = shift;
251     return unless defined $pkgs;
252     return map lc, split /[ \t?,()]+/, $pkgs;
253 }
254
255 # Generate a comma-separated list of HTML links to each package given in
256 # $pkgs. $pkgs may be empty, in which case an empty string is returned, or
257 # it may be a comma-separated list of package names.
258 sub htmlpackagelinks {
259      return htmlize_packagelinks(@_);
260 }
261
262 # Generate a comma-separated list of HTML links to each address given in
263 # $addresses, which should be a comma-separated list of RFC822 addresses.
264 # $urlfunc should be a reference to a function like mainturl or submitterurl
265 # which returns the URL for each individual address.
266 sub htmladdresslinks {
267      htmlize_addresslinks(@_);
268 }
269
270 # Generate a comma-separated list of HTML links to each maintainer given in
271 # $maints, which should be a comma-separated list of RFC822 addresses.
272 sub htmlmaintlinks {
273     my ($prefixfunc, $maints) = @_;
274     return htmladdresslinks($prefixfunc, \&mainturl, $maints);
275 }
276
277 sub htmlindexentry {
278     my $ref = shift;
279     my %status = %{getbugstatus($ref)};
280     return htmlindexentrystatus(%status) if (%status);
281     return "";
282 }
283
284 sub htmlindexentrystatus {
285     my $s = shift;
286     my %status = %{$s};
287
288     my $result = "";
289
290     if  ($status{severity} eq 'normal') {
291         $showseverity = '';
292     } elsif (isstrongseverity($status{severity})) {
293         $showseverity = "<strong>Severity: $status{severity}</strong>;\n";
294     } else {
295         $showseverity = "Severity: <em>$status{severity}</em>;\n";
296     }
297
298     $result .= htmlpackagelinks($status{"package"}, 1);
299
300     my $showversions = '';
301     if (@{$status{found_versions}}) {
302         my @found = @{$status{found_versions}};
303         local $_;
304         s{/}{ } foreach @found;
305         $showversions .= join ', ', map htmlsanit($_), @found;
306     }
307     if (@{$status{fixed_versions}}) {
308         $showversions .= '; ' if length $showversions;
309         $showversions .= '<strong>fixed</strong>: ';
310         my @fixed = @{$status{fixed_versions}};
311         local $_;
312         s{/}{ } foreach @fixed;
313         $showversions .= join ', ', map htmlsanit($_), @fixed;
314     }
315     $result .= " ($showversions)" if length $showversions;
316     $result .= ";\n";
317
318     $result .= $showseverity;
319     $result .= htmladdresslinks("Reported by: ", \&submitterurl,
320                                 $status{originator});
321     $result .= ";\nOwned by: " . htmlsanit($status{owner})
322                if length $status{owner};
323     $result .= ";\nTags: <strong>" 
324                  . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
325                  . "</strong>"
326                        if (length($status{tags}));
327     my @merged= split(/ /,$status{mergedwith});
328     my $mseparator= ";\nmerged with ";
329     for my $m (@merged) {
330         $result .= $mseparator."<A href=\"" . bugurl($m) . "\">#$m</A>";
331         $mseparator= ", ";
332     }
333
334     if (length($status{done})) {
335         $result .= ";\n<strong>Done:</strong> " . htmlsanit($status{done});
336         $days = ceil($gRemoveAge - -M buglog($status{id}));
337         if ($days >= 0) {
338             $result .= ";\n<strong>Will be archived:</strong>" . ( $days == 0 ? " today" : $days == 1 ? " in $days day" : " in $days days" );
339         } else {
340             $result .= ";\n<strong>Archived</strong>";
341         }
342     }
343
344     unless (length($status{done})) {
345         if (length($status{forwarded})) {
346             $result .= ";\n<strong>Forwarded</strong> to "
347                        . maybelink($status{forwarded});
348         }
349         my $daysold = int((time - $status{date}) / 86400);   # seconds to days
350         if ($daysold >= 7) {
351             my $font = "";
352             my $efont = "";
353             $font = "em" if ($daysold > 30);
354             $font = "strong" if ($daysold > 60);
355             $efont = "</$font>" if ($font);
356             $font = "<$font>" if ($font);
357
358             my $yearsold = int($daysold / 365);
359             $daysold -= $yearsold * 365;
360
361             $result .= ";\n $font";
362             my @age;
363             push @age, "1 year" if ($yearsold == 1);
364             push @age, "$yearsold years" if ($yearsold > 1);
365             push @age, "1 day" if ($daysold == 1);
366             push @age, "$daysold days" if ($daysold > 1);
367             $result .= join(" and ", @age);
368             $result .= " old$efont";
369         }
370     }
371
372     $result .= ".";
373
374     return $result;
375 }
376
377 sub urlargs {
378     my $args = '';
379     $args .= ";archive=yes" if $common_archive;
380     $args .= ";repeatmerged=no" unless $common_repeatmerged;
381     $args .= ";mindays=${common_mindays}" unless $common_mindays == 0;
382     $args .= ";maxdays=${common_maxdays}" unless $common_maxdays == -1;
383     $args .= ";version=$common_version" if defined $common_version;
384     $args .= ";dist=$common_dist" if defined $common_dist;
385     $args .= ";arch=$common_arch" if defined $common_arch;
386     return $args;
387 }
388
389 sub submitterurl { pkg_url(submitter => emailfromrfc822($_[0] || "")); }
390 sub mainturl { pkg_url(maint => emailfromrfc822($_[0] || "")); }
391 sub pkgurl { pkg_url(pkg => $_[0] || ""); }
392 sub srcurl { pkg_url(src => $_[0] || ""); }
393 sub tagurl { pkg_url(tag => $_[0] || ""); }
394
395 sub pkg_etc_url {
396     my $ref = shift;
397     my $code = shift;
398     if ($common_leet_urls) {
399         $code = "package" if ($code eq "pkg");
400         $code = "source" if ($code eq "src");
401         return urlsanit("/x/$code/$ref");
402     } else {
403         my $addurlargs = shift || 1;
404         my $params = "$code=$ref";
405         $params .= urlargs() if $addurlargs;
406         return urlsanit("pkgreport.cgi" . "?" . $params);
407     }
408 }
409
410 sub urlsanit {
411     my $url = shift;
412     $url =~ s/%/%25/g;
413     $url =~ s/#/%23/g;
414     $url =~ s/\+/%2b/g;
415     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
416     $url =~ s/([<>&"])/\&$saniarray{$1};/g;
417     return $url;
418 }
419
420 sub htmlsanit {
421     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
422     my $in = shift || "";
423     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
424     return $in;
425 }
426
427 sub bugurl {
428     my $ref = shift;
429     my $params = "bug=$ref";
430     my $filename = '';
431
432     if ($common_leet_urls) {
433         my $msg = "";
434         my $mbox = "";
435         my $att = "";
436         foreach my $val (@_) {
437             $mbox = "/mbox" if ($val eq "mbox");
438             $msg = "/$1" if ($val =~ /^msg=([0-9]+)/);
439             $att = "/$1" if ($val =~ /^att=([0-9]+)/);
440             $filename = "/$1" if ($val =~ /^filename=(.*)$/);
441         }
442         my $ext = "";
443         if ($mbox ne "") {
444             $ext = $mbox;
445         } elsif ($att ne "") {
446             $ext = "$att$filename";
447         }
448         return urlsanit("/x/$ref$msg$ext");
449     } else {
450         foreach my $val (@_) {
451             $params .= ";mbox=yes" if ($val eq "mbox");
452             $params .= ";msg=$1" if ($val =~ /^msg=([0-9]+)/);
453             $params .= ";att=$1" if ($val =~ /^att=([0-9]+)/);
454             $filename = $1 if ($val =~ /^filename=(.*)$/);
455             $params .= ";archive=yes" if (!$common_archive && $val =~ /^archive.*$/);
456         }
457         $params .= ";archive=yes" if ($common_archive);
458         $params .= ";repeatmerged=no" unless ($common_repeatmerged);
459
460         my $pathinfo = '';
461         $pathinfo = '/'.uri_escape($filename) if $filename ne '';
462
463         return urlsanit("bugreport.cgi" . $pathinfo . "?" . $params);
464     }
465 }
466
467 sub dlurl { bugurl(@_); }
468 sub mboxurl { return bugurl($ref, "mbox"); }
469
470 sub allbugs {
471     return @{getbugs(sub { 1 })};
472 }
473
474 sub bugmatches(\%\%) {
475     my ($hash, $status) = @_;
476     foreach my $key( keys( %$hash ) ) {
477         my $value = $hash->{$key};
478         my $sub = $field_match{$key};
479         return 1 if ($sub->($key, $value, $status));
480     }
481     return 0;
482 }
483 sub bugfilter($%) {
484     my ($bug, %status) = @_;
485     our (%seenmerged);
486     if (%common_include) {
487         return 1 if (!bugmatches(%common_include, %status));
488     }
489     if (%common_exclude) {
490         return 1 if (bugmatches(%common_exclude, %status));
491     }
492     my @merged = sort {$a<=>$b} $bug, split(/ /, $status{mergedwith});
493     my $daysold = int((time - $status{date}) / 86400);   # seconds to days
494     return 1 unless ($common_mindays <= $daysold);
495     return 1 unless ($common_maxdays == -1 || $daysold <= $common_maxdays);
496     return 1 unless ($common_repeatmerged || !$seenmerged{$merged[0]});
497     $seenmerged{$merged[0]} = 1;
498     return 0;
499 }
500
501 sub htmlizebugs {
502     $b = $_[0];
503     my @bugs = @$b;
504     my $anydone = 0;
505
506     my @status = ();
507     my %count;
508     my $header = '';
509     my $footer = '';
510
511     if (@bugs == 0) {
512         return "<HR><H2>No reports found!</H2></HR>\n";
513     }
514
515     if ( $common_bug_reverse ) {
516         @bugs = sort {$b<=>$a} @bugs;
517     } else {
518         @bugs = sort {$a<=>$b} @bugs;
519     }
520     my %seenmerged;
521     foreach my $bug (@bugs) {
522         my %status = %{getbugstatus($bug)};
523         next unless %status;
524         next if bugfilter($bug, %status);
525
526         my $html = sprintf "<li><a href=\"%s\">#%d: %s</a>\n<br>",
527             bugurl($bug), $bug, htmlsanit($status{subject});
528         $html .= htmlindexentrystatus(\%status) . "\n";
529         my $key = join( '_', map( {$status{$_}} @common_grouping ) );
530         $section{$key} .= $html;
531         $count{"_$key"}++;
532         foreach my $grouping ( @common_grouping ) {
533             $count{"${grouping}_$status{$grouping}"}++;
534         }
535         $anydone = 1 if $status{pending} eq 'done';
536         push @status, [ $bug, \%status, $html ];
537     }
538
539     my $result = "";
540     if ($common_raw_sort) {
541         $result .= "<UL>\n" . join("", map( { $_->[ 2 ] } @status ) ) . "</UL>\n";
542     } else {
543         my (@order, @headers);
544         for( my $i = 0; $i < @common_grouping; $i++ ) {
545             my $grouping_name = $common_grouping[ $i ];
546             my @items = @{ $common_grouping_order{ $grouping_name } };
547             @items = reverse( @items ) if ( $common_reverse{ $grouping_name } );
548             my @neworder = ();
549             my @newheaders = ();
550             if ( @order ) {
551                 foreach my $grouping ( @items ) {
552                     push @neworder, map( { "${_}_$grouping" } @order );
553                     push @newheaders, map( { "$_ - $common_headers{$grouping_name}{$grouping}" } @headers );
554                 }
555                 @order = @neworder;
556                 @headers = @newheaders;
557             } else {
558                 push @order, @items;
559                 push @headers, map( { $common_headers{$common_grouping[$i]}{$_} } @items );
560             }
561         }
562         $header .= "<ul>\n";
563         for ( my $i = 0; $i < @order; $i++ ) {
564             my $order = $order[ $i ];
565             next unless defined $section{$order};
566             my $count = $count{"_$order"};
567             my $bugs = $count == 1 ? "bug" : "bugs";
568             $header .= "<li><a href=\"#$order\">$headers[$i]</a> ($count $bugs)</li>\n";
569         }
570         $header .= "</ul>\n";
571         for ( my $i = 0; $i < @order; $i++ ) {
572             my $order = $order[ $i ];
573             next unless defined $section{$order};
574             if ($common{show_list_header}) {
575                 my $count = $count{"_$order"};
576                 my $bugs = $count == 1 ? "bug" : "bugs";
577                 $result .= "<HR><H2><a name=\"$order\"></a>$headers[$i] ($count $bugs)</H2>\n";
578             } else {
579                 $result .= "<HR><H2>$headers[$i]</H2>\n";
580             }
581             $result .= "<UL>\n";
582             $result .= $section{$order};
583             $result .= "</UL>\n";
584         }    
585         $footer .= "<ul>\n";
586         foreach my $grouping ( @common_grouping ) {
587             my $local_result = '';
588             foreach my $key ( @{$common_grouping_order{ $grouping }} ) {
589                 my $count = $count{"${grouping}_$key"};
590                 next if !$count;
591                 $local_result .= "<li>$count $common_headers{$grouping}{$key}</li>\n";
592             }
593             if ( $local_result ) {
594                 $footer .= "<li>$common_grouping_display{$grouping}<ul>\n$local_result</ul></li>\n";
595             }
596         }
597         $footer .= "</ul>\n";
598     }
599
600     $result = $header . $result if ( $common{show_list_header} );
601     $result .= $gHTMLExpireNote if $gRemoveAge and $anydone;
602     $result .= "<hr>" . $footer if ( $common{show_list_footer} );
603     return $result;
604 }
605
606 sub countbugs {
607     my $bugfunc = shift;
608     if ($common_archive) {
609         open I, "<$gSpoolDir/index.archive"
610             or &quitcgi("$gSpoolDir/index.archive: $!");
611     } else {
612         open I, "<$gSpoolDir/index.db"
613             or &quitcgi("$gSpoolDir/index.db: $!");
614     }
615
616     my %count = ();
617     while(<I>) 
618     {
619         if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
620             my @x = $bugfunc->(pkg => $1, bug => $2, status => $4, 
621                                submitter => $5, severity => $6, tags => $7);
622             local $_;
623             $count{$_}++ foreach @x;
624         }
625     }
626     close I;
627     return %count;
628 }
629
630 sub getbugs {
631     my $bugfunc = shift;
632     my $opt = shift;
633
634     my @result = ();
635
636     my $fastidx;
637     if (!defined $opt) {
638         # leave $fastidx undefined;
639     } elsif (!$common_archive) {
640         $fastidx = "$gSpoolDir/by-$opt.idx";
641     } else {
642         $fastidx = "$gSpoolDir/by-$opt-arc.idx";
643     }
644
645     if (defined $fastidx && -e $fastidx) {
646         my %lookup;
647 print STDERR "optimized\n" if ($debug);
648         tie %lookup, MLDBM => $fastidx, O_RDONLY
649             or die "$0: can't open $fastidx ($!)\n";
650         while ($key = shift) {
651             my $bugs = $lookup{$key};
652             if (defined $bugs) {
653                  push @result, keys %{$bugs};
654             }
655         }
656         untie %lookup;
657 print STDERR "done optimized\n" if ($debug);
658     } else {
659         if ( $common_archive ) {
660             open I, "<$gSpoolDir/index.archive" 
661                 or &quitcgi("$gSpoolDir/index.archive: $!");
662         } else {
663             open I, "<$gSpoolDir/index.db" 
664                 or &quitcgi("$gSpoolDir/index.db: $!");
665         }
666         while(<I>) {
667             if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
668                 if ($bugfunc->(pkg => $1, bug => $2, status => $4,
669                             submitter => $5, severity => $6, tags => $7)) 
670                 {
671                     push (@result, $2);
672                 }
673             }
674         }
675         close I;
676     }
677     @result = sort {$a <=> $b} @result;
678     return \@result;
679 }
680
681 sub emailfromrfc822 {
682     my $email = shift;
683     $email =~ s/\s*\(.*\)\s*//;
684     $email = $1 if ($email =~ m/<(.*)>/);
685     return $email;
686 }
687
688 sub maintencoded {
689     my $input = shift;
690     my $encoded = '';
691
692     while ($input =~ m/\W/) {
693         $encoded.=$`.sprintf("-%02x_",unpack("C",$&));
694         $input= $';
695     }
696
697     $encoded.= $input;
698     $encoded =~ s/-2e_/\./g;
699     $encoded =~ s/^([^,]+)-20_-3c_(.*)-40_(.*)-3e_/$1,$2,$3,/;
700     $encoded =~ s/^(.*)-40_(.*)-20_-28_([^,]+)-29_$/,$1,$2,$3/;
701     $encoded =~ s/-20_/_/g;
702     $encoded =~ s/-([^_]+)_-/-$1/g;
703     return $encoded;
704 }
705
706
707 sub getbugstatus {
708     my ($bug) = @_;
709     return get_bug_status(bug => $bug,
710                           $use_bug_idx?(bug_index => \%bugidx):(),
711                           usertags => \%common_bugusertags,
712                           (defined $common_dist)?(dist => $common_dist):(),
713                           (defined $common_version)?(version => $common_version):(),
714                           (defined $common_arch)?(arch => $common_arch):(),
715                          );
716 }
717
718 sub getversiondesc {
719     my $pkg = shift;
720
721     if (defined $common_version) {
722         return "version $common_version";
723     } elsif (defined $common_dist) {
724         my @distvers = getversions($pkg, $common_dist, $common_arch);
725         @distvers = sort @distvers;
726         local $" = ', ';
727         if (@distvers > 1) {
728             return "versions @distvers";
729         } elsif (@distvers == 1) {
730             return "version @distvers";
731         }
732     }
733
734     return undef;
735 }
736
737 1;