]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service
fix up the debug transcript addition
[debbugs.git] / scripts / service
1 #!/usr/bin/perl
2 # $Id: service.in,v 1.118 2005/10/19 01:22:14 don Exp $
3 #
4 # Usage: service <code>.nn
5 # Temps:  incoming/P<code>.nn
6
7 use warnings;
8 use strict;
9
10
11 use Debbugs::Config qw(:globals :config);
12
13 use File::Copy;
14 use MIME::Parser;
15
16 use Params::Validate qw(:types validate_with);
17
18 use Debbugs::Common qw(:util :quit :misc :lock);
19
20 use Debbugs::Status qw(:read :status :write :versions :hook);
21
22 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
23 use Debbugs::Mail qw(send_mail_message);
24 use Debbugs::User;
25 use Debbugs::Recipients qw(:all);
26 use HTML::Entities qw(encode_entities);
27 use Debbugs::Versions::Dpkg;
28
29 use Debbugs::Status qw(splitpackages);
30
31 use Debbugs::CGI qw(html_escape);
32 use Debbugs::Control qw(:all);
33 use Debbugs::Log qw(:misc);
34 use Debbugs::Text qw(:templates);
35
36 use Scalar::Util qw(looks_like_number);
37
38 use List::Util qw(first);
39
40 use Mail::RFC822::Address;
41
42 chdir($config{spool_dir}) or
43      die "Unable to chdir to spool_dir '$config{spool_dir}': $!";
44
45 my $debug = 0;
46 umask(002);
47
48 my ($nn,$control) = $ARGV[0] =~ m/^(([RC])\.\d+)$/;
49 if (not defined $control or not defined $nn) {
50      die "Bad argument to service.in";
51 }
52 if (!rename("incoming/G$nn","incoming/P$nn")) {
53     defined $! and $! =~ m/no such file or directory/i and exit 0;
54     die "Failed to rename incoming/G$nn to incoming/P$nn: $!";
55 }
56
57 my $log_fh = IO::File->new("incoming/P$nn",'r') or
58      die "Unable to open incoming/P$nn for reading: $!";
59 my @log=<$log_fh>;
60 my @msg=@log;
61 close($log_fh);
62
63 chomp @msg;
64
65 print "###\n",join("##\n",@msg),"\n###\n" if $debug;
66
67 # Bug numbers to send e-mail to, hash so that we don't send to the
68 # same bug twice.
69 my (%bug_affected);
70
71 my (@headerlines,@bodylines);
72
73 my $parse_output = Debbugs::MIME::parse(join('',@log));
74 @headerlines = @{$parse_output->{header}};
75 @bodylines = @{$parse_output->{body}};
76
77 my %header;
78 for (@headerlines) {
79     $_ = decode_rfc1522($_);
80     s/\n\s/ /g;
81     print ">$_<\n" if $debug;
82     if (s/^(\S+):\s*//) {
83         my $v = lc $1;
84         print ">$v=$_<\n" if $debug;
85         $header{$v} = $_;
86     } else {
87         print "!>$_<\n" if $debug;
88     }
89 }
90 $header{'message-id'} ||= '';
91 $header{subject} ||= '';
92
93 grep(s/\s+$//,@bodylines);
94
95 print "***\n",join("\n",@bodylines),"\n***\n" if $debug;
96
97 if (defined $header{'resent-from'} && !defined $header{'from'}) {
98     $header{'from'} = $header{'resent-from'};
99 }
100
101 defined($header{'from'}) || die "no From header";
102
103 delete $header{'reply-to'} 
104         if ( defined($header{'reply-to'}) && $header{'reply-to'} =~ m/^\s*$/ );
105
106 my $replyto;
107 if ( defined($header{'reply-to'}) && $header{'reply-to'} ne "" ) {
108     $replyto = $header{'reply-to'};
109 } else {
110     $replyto = $header{'from'};
111 }
112
113 # This is an error counter which should be incremented every time there is an error.
114 my $errors = 0;
115 my $controlrequestaddr= ($control ? 'control' : 'request').'@'.$config{email_domain};
116 my $transcript_scalar = '';
117 my $transcript = IO::Scalar->new(\$transcript_scalar) or
118      die "Unable to create new IO::Scalar";
119 print {$transcript} "Processing commands for $controlrequestaddr:\n\n";
120
121
122 my $dl = 0;
123 my %affected_packages;
124 my %recipients;
125 # this is the hashref which is passed to all control calls
126 my %limit = ();
127
128
129 my @common_control_options =
130     (transcript        => $transcript,
131      requester         => $header{from},
132      request_addr      => $controlrequestaddr,
133      request_msgid     => $header{'message-id'},
134      request_subject   => $header{subject},
135      request_nn        => $nn,
136      request_replyto   => $replyto,
137      message           => \@log,
138      affected_packages => \%affected_packages,
139      recipients        => \%recipients,
140      limit             => \%limit,
141     );
142
143 my $state= 'idle';
144 my $lowstate= 'idle';
145 my $mergelowstate= 'idle';
146 my $midix=0;
147
148 my $user = $replyto;
149 $user =~ s/,.*//;
150 $user =~ s/^.*<(.*)>.*$/$1/;
151 $user =~ s/[(].*[)]//;
152 $user =~ s/^\s*(\S+)\s+.*$/$1/;
153 $user = "" unless (Debbugs::User::is_valid_user($user));
154 my $indicated_user = 0;
155
156 my $quickabort = 0;
157
158
159 if (@gExcludeFromControl and grep {$replyto =~ m/\Q$_\E/} @gExcludeFromControl) {
160         print {$transcript} fill_template('mail/excluded_from_control');
161         $quickabort = 1;
162 }
163
164 my %limit_pkgs = ();
165 my %clonebugs = ();
166 my %bcc = ();
167
168
169 my @bcc;
170 sub addbcc {
171     push @bcc, $_[0] unless grep { $_ eq $_[0] } @bcc;
172 }
173
174 our $data;
175 our $message;
176 our $extramessage;
177 our $ref;
178
179 our $mismatch;
180 our $action;
181
182
183 my $ok = 0;
184 my $unknowns = 0;
185 my $procline=0;
186 for ($procline=0; $procline<=$#bodylines; $procline++) {
187     my $noriginator;
188     my $newsubmitter;
189     my $oldsubmitter;
190     my $newowner;
191     $state eq 'idle' || print "state: $state ?\n";
192     $lowstate eq 'idle' || print "lowstate: $lowstate ?\n";
193     $mergelowstate eq 'idle' || print "mergelowstate: $mergelowstate ?\n";
194     if ($quickabort) {
195          print {$transcript} "Stopping processing here.\n\n";
196          last;
197     }
198     $_= $bodylines[$procline]; s/\s+$//;
199     # Remove BOM markers from UTF-8 strings
200     # Fixes #488554
201     s/\xef\xbb\xbf//g;
202     next unless m/\S/;
203     print {$transcript} "> $_\n";
204     next if m/^\s*\#/;
205     $action= '';
206     if (m/^(?:stop|quit|--|thank(?:s|\s*you)?|kthxbye)\.*\s*$/i) {
207         print {$transcript} "Stopping processing here.\n\n";
208         last;
209     } elsif (m/^debug\s+(\d+)$/i && $1 >= 0 && $1 <= 1000) {
210         $dl= $1+0;
211         if ($dl > 0 and not grep /debug/,@common_control_options) {
212             push @common_control_options,(debug => $transcript);
213         }
214         print {$transcript} "Debug level $dl.\n\n";
215     } elsif (m/^(send|get)\s+\#?(\d{2,})$/i) {
216         $ref= $2+0;
217         &sendlynxdoc("bugreport.cgi?bug=$ref","logs for $gBug#$ref");
218     } elsif (m/^send-detail\s+\#?(\d{2,})$/i) {
219         $ref= $1+0;
220         &sendlynxdoc("bugreport.cgi?bug=$ref&boring=yes",
221                      "detailed logs for $gBug#$ref");
222     } elsif (m/^index(\s+full)?$/i) {
223         print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
224         $errors++;
225         $ok++; # well, it's not really ok, but it fixes #81224 :)
226     } elsif (m/^index-summary\s+by-package$/i) {
227         print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
228         $errors++;
229         $ok++; # well, it's not really ok, but it fixes #81224 :)
230     } elsif (m/^index-summary(\s+by-number)?$/i) {
231         print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
232         $errors++;
233         $ok++; # well, it's not really ok, but it fixes #81224 :)
234     } elsif (m/^index(\s+|-)pack(age)?s?$/i) {
235         &sendlynxdoc("pkgindex.cgi?indexon=pkg",'index of packages');
236     } elsif (m/^index(\s+|-)maints?$/i) {
237         &sendlynxdoc("pkgindex.cgi?indexon=maint",'index of maintainers');
238     } elsif (m/^index(\s+|-)maint\s+(\S+)$/i) {
239         my $maint = $2;
240         &sendlynxdoc("pkgreport.cgi?maint=" . urlsanit($maint),
241                      "$gBug list for maintainer \`$maint'");
242         $ok++;
243     } elsif (m/^index(\s+|-)pack(age)?s?\s+(\S.*\S)$/i) {
244         my $package = $+;
245         &sendlynxdoc("pkgreport.cgi?pkg=" . urlsanit($package),
246                      "$gBug list for package $package");
247         $ok++;
248     } elsif (m/^send-unmatched(\s+this|\s+-?0)?$/i) {
249         print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
250         $errors++;
251         $ok++; # well, it's not really ok, but it fixes #81224 :)
252     } elsif (m/^send-unmatched\s+(last|-1)$/i) {
253         print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
254         $errors++;
255         $ok++; # well, it's not really ok, but it fixes #81224 :)
256     } elsif (m/^send-unmatched\s+(old|-2)$/i) {
257         print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
258         $errors++;
259         $ok++; # well, it's not really ok, but it fixes #81224 :)
260     } elsif (m/^getinfo\s+([\w.-]+)$/i) {
261         # the following is basically a Debian-specific kludge, but who cares
262         my $req = $1;
263         if ($req =~ /^maintainers$/i && -f "$gConfigDir/Maintainers") {
264             &sendinfo("local", "$gConfigDir/Maintainers", "Maintainers file");
265         } elsif ($req =~ /^override\.(\w+)\.([\w.-]+)$/i) {
266             $req =~ s/.gz$//;
267             &sendinfo("ftp.d.o", "$req", "override file for $2 part of $1 distribution");
268         } elsif ($req =~ /^pseudo-packages\.(description|maintainers)$/i && -f "$gConfigDir/$req") {
269             &sendinfo("local", "$gConfigDir/$req", "$req file");
270         } else {
271             print {$transcript} "Info file $req does not exist.\n\n";
272         }
273     } elsif (m/^help/i) {
274         &sendhelp;
275         print {$transcript} "\n";
276         $ok++;
277     } elsif (m/^refcard/i) {
278         &sendtxthelp("bug-mailserver-refcard.txt","mail servers' reference card");
279     } elsif (m/^subscribe/i) {
280         print {$transcript} <<END;
281 There is no $gProject $gBug mailing list.  If you wish to review bug reports
282 please do so via http://$gWebDomain/ or ask this mail server
283 to send them to you.
284 soon: MAILINGLISTS_TEXT
285 END
286     } elsif (m/^unsubscribe/i) {
287         print {$transcript} <<END;
288 soon: UNSUBSCRIBE_TEXT
289 soon: MAILINGLISTS_TEXT
290 END
291     } elsif (m/^user\s+(\S+)\s*$/i) {
292         my $newuser = $1;
293         if (Debbugs::User::is_valid_user($newuser)) {
294             my $olduser = ($user ne "" ? " (was $user)" : "");
295             print {$transcript} "Setting user to $newuser$olduser.\n";
296             $user = $newuser;
297             $indicated_user = 1;
298         } else {
299             print {$transcript} "Selected user id ($newuser) invalid, sorry\n";
300             $errors++;
301             $user = "";
302             $indicated_user = 1;
303         }
304     } elsif (m/^usercategory\s+(\S+)(\s+\[hidden\])?\s*$/i) {
305         $ok++;
306         my $catname = $1;
307         my $hidden = (defined $2 and $2 ne "");
308
309         my $prefix = "";
310         my @cats;
311         my $bad = 0;
312         my $catsec = 0;
313         if ($user eq "") {
314             print {$transcript} "No valid user selected\n";
315             $errors++;
316             next;
317         }
318         if (not $indicated_user and defined $user) {
319              print {$transcript} "User is $user\n";
320              $indicated_user = 1;
321         }
322         my @ords = ();
323         while (++$procline <= $#bodylines) {
324             unless ($bodylines[$procline] =~ m/^\s*([*+])\s*(\S.*)$/) {
325                 $procline--;
326                 last;
327             }
328             print {$transcript} "> $bodylines[$procline]\n";
329             next if $bad;
330             my ($o, $txt) = ($1, $2);
331             if ($#cats == -1 && $o eq "+") {
332                 print {$transcript} "User defined category specification must start with a category name. Skipping.\n\n";
333                 $errors++;
334                 $bad = 1;
335                 next;
336             }
337             if ($o eq "+") {
338                 unless (ref($cats[-1]) eq "HASH") {
339                     $cats[-1] = { "nam" => $cats[-1], 
340                                   "pri" => [], "ttl" => [] };
341                 }
342                 $catsec++;
343                 my ($desc, $ord, $op);
344                 if ($txt =~ m/^(.*\S)\s*\[((\d+):\s*)?\]\s*$/) {
345                     $desc = $1; $ord = $3; $op = "";
346                 } elsif ($txt =~ m/^(.*\S)\s*\[((\d+):\s*)?(\S+)\]\s*$/) {
347                     $desc = $1; $ord = $3; $op = $4;
348                 } elsif ($txt =~ m/^([^[\s]+)\s*$/) {
349                     $desc = ""; $op = $1;
350                 } else {
351                     print {$transcript} "Unrecognised syntax for category section. Skipping.\n\n";
352                     $errors++;
353                     $bad = 1;
354                     next;
355                 }
356                 $ord = 999 unless defined $ord;
357
358                 if ($op) {
359                     push @{$cats[-1]->{"pri"}}, $prefix . $op;
360                     push @{$cats[-1]->{"ttl"}}, $desc;
361                     push @ords, "$ord $catsec";
362                 } else {
363                     $cats[-1]->{"def"} = $desc;
364                     push @ords, "$ord DEF";
365                     $catsec--;
366                 }
367                 @ords = sort {
368                     my ($a1, $a2, $b1, $b2) = split / /, "$a $b";
369                     ((looks_like_number($a1) and looks_like_number($a2))?$a1 <=> $b1:$a1 cmp $b1) ||
370                     ((looks_like_number($a2) and looks_like_number($b2))?$a2 <=> $b2:$a2 cmp $b2);
371                 } @ords;
372                 $cats[-1]->{"ord"} = [map { m/^.* (\S+)/; $1 eq "DEF" ? $catsec + 1 : $1 } @ords];
373             } elsif ($o eq "*") {
374                 $catsec = 0;
375                 my ($name);
376                 if ($txt =~ m/^(.*\S)(\s*\[(\S+)\])\s*$/) {
377                     $name = $1; $prefix = $3;
378                 } else {
379                     $name = $txt; $prefix = "";
380                 }
381                 push @cats, $name;
382             }
383         }
384         # XXX: got @cats, now do something with it
385         my $u = Debbugs::User::get_user($user);
386         if (@cats) {
387             print {$transcript} "Added usercategory $catname.\n\n";
388             $u->{"categories"}->{$catname} = [ @cats ];
389             if (not $hidden) {
390                  push @{$u->{visible_cats}},$catname;
391             }
392         } else {
393             print {$transcript} "Removed usercategory $catname.\n\n";
394             delete $u->{"categories"}->{$catname};
395             @{$u->{visible_cats}} = grep {$_ ne $catname} @{$u->{visible_cats}};
396         }
397         $u->write();
398     } elsif (m/^usertags?\s+\#?(-?\d+)\s+(([=+-])\s*)?(\S.*)?$/i) {
399         $ok++;
400         $ref = $1;
401         my $addsubcode = $3 || "+";
402         my $tags = $4;
403         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
404              $ref = $clonebugs{$ref};
405         }
406         if ($user eq "") {
407             print {$transcript} "No valid user selected\n";
408             $errors++;
409             $indicated_user = 1;
410         } elsif (&setbug) {
411             if (not $indicated_user and defined $user) {
412                  print {$transcript} "User is $user\n";
413                  $indicated_user = 1;
414             }
415             &nochangebug;
416             my %ut;
417             Debbugs::User::read_usertags(\%ut, $user);
418             my @oldtags = (); my @newtags = (); my @badtags = ();
419             my %chtags;
420             if (defined $tags and length $tags) {
421                  for my $t (split /[,\s]+/, $tags) {
422                       if ($t =~ m/^[a-zA-Z0-9.+\@-]+$/) {
423                            $chtags{$t} = 1;
424                       } else {
425                            push @badtags, $t;
426                       }
427                  }
428             }
429             if (@badtags) {
430                 print {$transcript} "Ignoring illegal tag/s: ".join(', ', @badtags).".\nPlease use only alphanumerics, at, dot, plus and dash.\n";
431                 $errors++;
432             }
433             for my $t (keys %chtags) {
434                 $ut{$t} = [] unless defined $ut{$t};
435             }
436             for my $t (keys %ut) {
437                 my %res = map { ($_, 1) } @{$ut{$t}};
438                 push @oldtags, $t if defined $res{$ref};
439                 my $addop = ($addsubcode eq "+" or $addsubcode eq "=");
440                 my $del = (defined $chtags{$t} ? $addsubcode eq "-" 
441                                                : $addsubcode eq "=");
442                 $res{$ref} = 1 if ($addop && defined $chtags{$t});
443                 delete $res{$ref} if ($del);
444                 push @newtags, $t if defined $res{$ref};
445                 $ut{$t} = [ sort { $a <=> $b } (keys %res) ];
446             }
447             if (@oldtags == 0) {
448                 print {$transcript} "There were no usertags set.\n";
449             } else {
450                 print {$transcript} "Usertags were: " . join(" ", @oldtags) . ".\n";
451             }
452             print {$transcript} "Usertags are now: " . join(" ", @newtags) . ".\n";
453             Debbugs::User::write_usertags(\%ut, $user);
454         }
455     } elsif (!$control) {
456         print {$transcript} <<END;
457 Unknown command or malformed arguments to command.
458 (Use control\@$gEmailDomain to manipulate reports.)
459
460 END
461         $errors++;
462         if (++$unknowns >= 3) {
463             print {$transcript} "Too many unknown commands, stopping here.\n\n";
464             last;
465         }
466 #### "developer only" ones start here
467     } elsif (m/^close\s+\#?(-?\d+)(?:\s+(\d.*))?$/i) {
468         $ok++;
469         $ref= $1;
470         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
471         $bug_affected{$ref}=1;
472         my $version= $2;
473         if (&setbug) {
474             print {$transcript} "'close' is deprecated; see http://$gWebDomain/Developer$gHTMLSuffix#closing.\n";
475             if (length($data->{done}) and not defined($version)) {
476                 print {$transcript} "$gBug is already closed, cannot re-close.\n\n";
477                 &nochangebug;
478             } else {
479                 $action= "$gBug " .
480                     (defined($version) ?
481                         "marked as fixed in version $version" :
482                         "closed") .
483                     ", send any further explanations to $data->{originator}";
484                 do {
485                    $affected_packages{$data->{package}} = 1;
486                    add_recipients(data => $data,
487                                   recipients => \%recipients,
488                                   actions_taken => {done => 1},
489                                   transcript   => $transcript,
490                                   ($dl > 0 ? (debug => $transcript):()),
491                                  );
492                     $data->{done}= $replyto;
493                     my @keywords= split ' ', $data->{keywords};
494                     my $extramessage = '';
495                     if (grep $_ eq 'pending', @keywords) {
496                         $extramessage= "Removed pending tag.\n";
497                         $data->{keywords}= join ' ', grep $_ ne 'pending',
498                                                 @keywords;
499                     }
500                     addfixedversions($data, $data->{package}, $version, 'binary');
501
502                     my $message= <<END;
503 From: $gMaintainerEmail ($gProject $gBug Tracking System)
504 To: $data->{originator}
505 Subject: $gBug#$ref acknowledged by developer
506          ($header{'subject'})
507 References: $header{'message-id'} $data->{msgid}
508 In-Reply-To: $data->{msgid}
509 Message-ID: <handler.$ref.$nn.notifdonectrl.$midix\@$gEmailDomain>
510 Reply-To: $ref\@$gEmailDomain
511 X-$gProject-PR-Message: they-closed-control $ref
512
513 This is an automatic notification regarding your $gBug report
514 #$ref: $data->{subject},
515 which was filed against the $data->{package} package.
516
517 It has been marked as closed by one of the developers, namely
518 $replyto.
519
520 You should be hearing from them with a substantive response shortly,
521 in case you haven't already. If not, please contact them directly.
522
523 $gMaintainer
524 (administrator, $gProject $gBugs database)
525
526 END
527                     &sendmailmessage($message,$data->{originator});
528                 } while (&getnextbug);
529             }
530         }
531     } elsif (m/^reassign\s+\#?(-?\d+)\s+ # bug and command
532                (?:(?:((?:src:|source:)?$config{package_name_re}) # new package
533                (?:\s+((?:$config{package_name_re}\/)?
534                        $config{package_version_re}))?)| # optional version
535                ((?:src:|source:)?$config{package_name_re} # multiple package form
536                (?:\s*\,\s*(?:src:|source:)?$config{package_name_re})+))
537                \s*$/xi) {
538         $ok++;
539         $ref= $1;
540         my @new_packages;
541         if (not defined $2) {
542             push @new_packages, split /\s*\,\s*/,$4;
543         }
544         else {
545             push @new_packages, $2;
546         }
547         @new_packages = map {y/A-Z/a-z/; s/^(?:src|source):/src:/; $_;} @new_packages;
548         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
549         $bug_affected{$ref}=1;
550         my $version= $3;
551         eval {
552             set_package(@common_control_options,
553                         bug          => $ref,
554                         package      => \@new_packages,
555                        );
556             # if there is a version passed, we make an internal call
557             # to set_found
558             if (defined($version) && length $version) {
559                 set_found(@common_control_options,
560                           bug   => $ref,
561                           found => $version,
562                          );
563             }
564         };
565         if ($@) {
566             $errors++;
567             print {$transcript} "Failed to clear fixed versions and reopen on $ref: ".cleanup_eval_fail($@,$debug)."\n";
568         }
569     } elsif (m/^reopen\s+\#?(-?\d+)(?:\s+([\=\!]|(?:\S.*\S)))?$/i) {
570         $ok++;
571         $ref= $1;
572         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
573         $bug_affected{$ref}=1;
574         my $new_submitter = $2;
575         if (defined $new_submitter) {
576             if ($new_submitter eq '=') {
577                 undef $new_submitter;
578             }
579             elsif ($new_submitter eq '!') {
580                 $new_submitter = $replyto;
581             }
582         }
583         eval {
584             reopen(@common_control_options,
585                    bug          => $ref,
586                    submitter    => $new_submitter,
587                   );
588         };
589         if ($@) {
590             $errors++;
591             print {$transcript} "Failed to reopen $ref: ".cleanup_eval_fail($@,$debug)."\n";
592         }
593     } elsif (m{^(?:(?i)found)\s+\#?(-?\d+)
594                (?:\s+((?:$config{package_name_re}\/)?
595                     $config{package_version_re}
596                 # allow for multiple packages
597                 (?:\s*,\s*(?:$config{package_name_re}\/)?
598                     $config{package_version_re})*)
599             )?$}x) {
600         $ok++;
601         $ref= $1;
602         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
603         my @versions;
604         if (defined $2) {
605             @versions = split /\s*,\s*/,$2;
606             eval {
607                 set_found(@common_control_options,
608                           bug          => $ref,
609                           found        => \@versions,
610                           add          => 1,
611                          );
612             };
613             if ($@) {
614                 $errors++;
615                 print {$transcript} "Failed to add found on $ref: ".cleanup_eval_fail($@,$debug)."\n";
616             }
617         }
618         else {
619             eval {
620                 set_fixed(@common_control_options,
621                           bug          => $ref,
622                           fixed        => [],
623                           reopen       => 1,
624                          );
625             };
626             if ($@) {
627                 $errors++;
628                 print {$transcript} "Failed to clear fixed versions and reopen on $ref: ".cleanup_eval_fail($@,$debug)."\n";
629             }
630         }
631     }
632     elsif (m{^(?:(?i)notfound)\s+\#?(-?\d+)
633              \s+((?:$config{package_name_re}\/)?
634                  $config{package_version_re}
635                 # allow for multiple packages
636                 (?:\s*,\s*(?:$config{package_name_re}\/)?
637                     $config{package_version_re})*
638             )$}x) {
639         $ok++;
640         $ref= $1;
641         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
642         my @versions;
643         @versions = split /\s*,\s*/,$2;
644         eval {
645             set_found(@common_control_options,
646                       bug          => $ref,
647                       found        => \@versions,
648                       remove       => 1,
649                      );
650         };
651         if ($@) {
652             $errors++;
653             print {$transcript} "Failed to remove found on $ref: ".cleanup_eval_fail($@,$debug)."\n";
654         }
655     }
656     elsif (m{^(?:(?i)fixed)\s+\#?(-?\d+)
657              \s+((?:$config{package_name_re}\/)?
658                     $config{package_version_re}
659                 # allow for multiple packages
660                 (?:\s*,\s*(?:$config{package_name_re}\/)?
661                     $config{package_version_re})*)
662             \s*$}x) {
663         $ok++;
664         $ref= $1;
665         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
666         my @versions;
667         @versions = split /\s*,\s*/,$2;
668         eval {
669             set_fixed(@common_control_options,
670                       bug          => $ref,
671                       fixed        => \@versions,
672                       add          => 1,
673                      );
674         };
675         if ($@) {
676             $errors++;
677             print {$transcript} "Failed to add fixed on $ref: ".cleanup_eval_fail($@,$debug)."\n";
678         }
679     }
680     elsif (m{^(?:(?i)notfixed)\s+\#?(-?\d+)
681              \s+((?:$config{package_name_re}\/)?
682                     $config{package_version_re}
683                 # allow for multiple packages
684                 (?:\s*,\s*(?:$config{package_name_re}\/)?
685                     $config{package_version_re})*)
686             \s*$}x) {
687         $ok++;
688         $ref= $1;
689         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
690         my @versions;
691         @versions = split /\s*,\s*/,$2;
692         eval {
693             set_fixed(@common_control_options,
694                       bug          => $ref,
695                       fixed        => \@versions,
696                       remove       => 1,
697                      );
698         };
699         if ($@) {
700             $errors++;
701             print {$transcript} "Failed to remove fixed on $ref: ".cleanup_eval_fail($@,$debug)."\n";
702         }
703     }
704     elsif (m/^submitter\s+\#?(-?\d+)\s+(\!|\S.*\S)$/i) {
705         $ok++;
706         $ref= $1;
707         $bug_affected{$ref}=1;
708         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
709         my $newsubmitter = $2 eq '!' ? $replyto : $2;
710         if (not Mail::RFC822::Address::valid($newsubmitter)) {
711              print {$transcript} "$newsubmitter is not a valid e-mail address; not changing submitter\n";
712              $errors++;
713         }
714         else {
715             eval {
716                 set_submitter(@common_control_options,
717                               bug       => $ref,
718                               submitter => $newsubmitter,
719                              );
720             };
721             if ($@) {
722                 $errors++;
723                 print {$transcript} "Failed to set submitter on $ref: ".cleanup_eval_fail($@,$debug)."\n";
724             }
725         }
726     } elsif (m/^forwarded\s+\#?(-?\d+)\s+(\S.*\S)$/i) {
727         $ok++;
728         $ref= $1;
729         my $forward_to= $2;
730         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
731         $bug_affected{$ref} = 1;
732         eval {
733             set_forwarded(@common_control_options,
734                           bug          => $ref,
735                           forwarded    => $forward_to,
736                           );
737         };
738         if ($@) {
739             $errors++;
740             print {$transcript} "Failed to set the forwarded-to-address of $ref: ".cleanup_eval_fail($@,$debug)."\n";
741         }
742     } elsif (m/^notforwarded\s+\#?(-?\d+)$/i) {
743         $ok++;
744         $ref= $1;
745         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
746         $bug_affected{$ref} = 1;
747         eval {
748             set_forwarded(@common_control_options,
749                           bug          => $ref,
750                           forwarded    => undef,
751                           );
752         };
753         if ($@) {
754             $errors++;
755             print {$transcript} "Failed to clear the forwarded-to-address of $ref: ".cleanup_eval_fail($@,$debug)."\n";
756         }
757     } elsif (m/^(?:severity|priority)\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i) {
758         $ok++;
759         $ref= $1;
760         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
761         $bug_affected{$ref}=1;
762         my $newseverity= $2;
763         if (exists $gObsoleteSeverities{$newseverity}) {
764             print {$transcript} "Severity level \`$newseverity' is obsolete. " .
765                  "Use $gObsoleteSeverities{$newseverity} instead.\n\n";
766                 $errors++;
767         } elsif (not defined first {$_ eq $newseverity}
768             (@gSeverityList, "$gDefaultSeverity")) {
769              print {$transcript} "Severity level \`$newseverity' is not known.\n".
770                   "Recognized are: $gShowSeverities.\n\n";
771             $errors++;
772         } else {
773             eval {
774                 set_severity(@common_control_options,
775                              bug => $ref,
776                              severity => $newseverity,
777                             );
778             };
779             if ($@) {
780                 $errors++;
781                 print {$transcript} "Failed to set severity of $config{bug} $ref to $newseverity: ".cleanup_eval_fail($@,$debug)."\n";
782             }
783         }
784     } elsif (m/^tags?\s+\#?(-?\d+)\s+(\S.*)$/i) {
785         $ok++;
786         $ref = $1;
787         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
788         $bug_affected{$ref}=1;
789         my $tags = $2;
790         my @tags = split /[\s,]+/, $tags;
791         # this is an array of hashrefs which contain two elements, the
792         # first of which is the array of tags, the second is the
793         # option to pass to set_tag (we use a hashref here to make it
794         # more obvious what is happening)
795         my @tag_operations;
796         my @badtags;
797         for my $tag (@tags) {
798             if ($tag =~ /^[=+-]$/) {
799                 if ($tag eq '=') {
800                     @tag_operations = {tags => [],
801                                        option => [],
802                                       };
803                 }
804                 elsif ($tag eq '-') {
805                     push @tag_operations,
806                         {tags => [],
807                          option => [remove => 1],
808                         };
809                 }
810                 elsif ($tag eq '+') {
811                     push @tag_operations,
812                         {tags => [],
813                          option => [add => 1],
814                         };
815                 }
816                 next;
817             }
818             if (not defined first {$_ eq $tag} @{$config{tags}}) {
819                 push @badtags, $tag;
820                 next;
821             }
822             if (not @tag_operations) {
823                 @tag_operations = {tags => [],
824                                    option => [add => 1],
825                                   };
826             }
827             push @{$tag_operations[-1]{tags}},$tag;
828         }
829         if (@badtags) {
830             print {$transcript} "Unknown tag/s: ".join(', ', @badtags).".\n".
831                  "Recognized are: ".join(' ', @gTags).".\n\n";
832             $errors++;
833         }
834         eval {
835             for my $operation (@tag_operations) {
836                 set_tag(@common_control_options,
837                         bug => $ref,
838                         tag => [@{$operation->{tags}}],
839                         warn_on_bad_tags => 0, # don't warn on bad tags,
840                         # 'cause we do that above
841                         @{$operation->{option}},
842                        );
843             }
844         };
845         if ($@) {
846             # we intentionally have two errors here if there is a bad
847             # tag and the above fails for some reason
848             $errors++;
849             print {$transcript} "Failed to alter tags of $config{bug} $ref: ".cleanup_eval_fail($@,$debug)."\n";
850         }
851     } elsif (m/^(un)?block\s+\#?(-?\d+)\s+(?:by|with)\s+(\S.*)?$/i) {
852         $ok++;
853         $ref= $2;
854         my $add_remove = defined $1 && $1 eq 'un';
855         my @blockers = split /[\s,]+/, $3;
856         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
857         $bug_affected{$ref} = 1;
858         eval {
859              set_blocks(@common_control_options,
860                         bug          => $ref,
861                         block        => \@blockers,
862                         $add_remove ? (remove => 1):(add => 1),
863                        );
864         };
865         if ($@) {
866             $errors++;
867             print {$transcript} "Failed to set blocking bugs of $ref: ".cleanup_eval_fail($@,$debug)."\n";
868         }
869     } elsif (m/^retitle\s+\#?(-?\d+)\s+(\S.*\S)\s*$/i) {
870         $ok++;
871         $ref= $1; my $newtitle= $2;
872         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
873         $bug_affected{$ref} = 1;
874         eval {
875              set_title(@common_control_options,
876                        bug          => $ref,
877                        title        => $newtitle,
878                       );
879         };
880         if ($@) {
881             $errors++;
882             print {$transcript} "Failed to set the title of $ref: ".cleanup_eval_fail($@,$debug)."\n";
883         }
884     } elsif (m/^unmerge\s+\#?(-?\d+)$/i) {
885         $ok++;
886         $ref= $1;
887         $bug_affected{$ref} = 1;
888         if (&setbug) {
889             if (!length($data->{mergedwith})) {
890                 print {$transcript} "$gBug is not marked as being merged with any others.\n\n";
891                 &nochangebug;
892             } else {
893                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
894                 $action= "Disconnected #$ref from all other report(s).";
895                 my @newmergelist= split(/ /,$data->{mergedwith});
896                 my $discref= $ref;
897                 @bug_affected{@newmergelist} = 1 x @newmergelist;
898                 do {
899                     $affected_packages{$data->{package}} = 1;
900                     add_recipients(data => $data,
901                                    recipients => \%recipients,
902                                    transcript   => $transcript,
903                                    ($dl > 0 ? (debug => $transcript):()),
904                                   );
905                     $data->{mergedwith}= ($ref == $discref) ? ''
906                         : join(' ',grep($_ ne $ref,@newmergelist));
907                 } while (&getnextbug);
908             }
909         }
910     } elsif (m/^merge\s+#?(-?\d+(\s+#?-?\d+)+)\s*$/i) {
911         $ok++;
912         my @tomerge= sort { $a <=> $b } split(/\s+#?/,$1);
913         my @newmergelist= ();
914         my %tags = ();
915         my %found = ();
916         my %fixed = ();
917         &getmerge;
918         while (defined($ref= shift(@tomerge))) {
919             print {$transcript} "D| checking merge $ref\n" if $dl;
920             $ref+= 0;
921             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
922                 $ref = $clonebugs{$ref};
923             }
924             next if grep($_ == $ref,@newmergelist);
925             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
926             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
927             &foundbug;
928             print {$transcript} "D| adding $ref ($data->{mergedwith})\n" if $dl;
929             $mismatch= '';
930             &checkmatch('package','m_package',$data->{package},@newmergelist);
931             &checkmatch('forwarded addr','m_forwarded',$data->{forwarded},@newmergelist);
932             $data->{severity} = '$gDefaultSeverity' if $data->{severity} eq '';
933             &checkmatch('severity','m_severity',$data->{severity},@newmergelist);
934             &checkmatch('blocks','m_blocks',$data->{blocks},@newmergelist);
935             &checkmatch('blocked-by','m_blockedby',$data->{blockedby},@newmergelist);
936             &checkmatch('done mark','m_done',length($data->{done}) ? 'done' : 'open',@newmergelist);
937             &checkmatch('owner','m_owner',$data->{owner},@newmergelist);
938             &checkmatch('summary','m_summary',$data->{summary},@newmergelist);
939             &checkmatch('affects','m_affects',$data->{affects},@newmergelist);
940             foreach my $t (split /\s+/, $data->{keywords}) { $tags{$t} = 1; }
941             foreach my $f (@{$data->{found_versions}}) { $found{$f} = 1; }
942             foreach my $f (@{$data->{fixed_versions}}) { $fixed{$f} = 1; }
943             if (length($mismatch)) {
944                 print {$transcript} "Mismatch - only $gBugs in same state can be merged:\n".
945                      $mismatch."\n";
946                 $errors++;
947                 &cancelbug; @newmergelist=(); last;
948             }
949             push(@newmergelist,$ref);
950             push(@tomerge,split(/ /,$data->{mergedwith}));
951             &cancelbug;
952         }
953         if (@newmergelist) {
954             @newmergelist= sort { $a <=> $b } @newmergelist;
955             $action= "Merged @newmergelist.";
956             delete @fixed{keys %found};
957             for $ref (@newmergelist) {
958                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
959                 $affected_packages{$data->{package}} = 1;
960                 add_recipients(data => $data,
961                                recipients => \%recipients,
962                                transcript   => $transcript,
963                                ($dl > 0 ? (debug => $transcript):()),
964                               );
965                 @bug_affected{@newmergelist} = 1 x @newmergelist;
966                 $data->{mergedwith}= join(' ',grep($_ != $ref,@newmergelist));
967                 $data->{keywords}= join(' ', keys %tags);
968                 $data->{found_versions}= [sort keys %found];
969                 $data->{fixed_versions}= [sort keys %fixed];
970                 &savebug;
971             }
972             print {$transcript} "$action\n\n";
973         }
974         &endmerge;
975     } elsif (m/^forcemerge\s+\#?(-?\d+(?:\s+\#?-?\d+)+)\s*$/i) {
976         $ok++;
977         my @temp = split /\s+\#?/,$1;
978         my $master_bug = shift @temp;
979         my $master_bug_data;
980         my @tomerge = sort { $a <=> $b } @temp;
981         unshift @tomerge,$master_bug;
982         print {$transcript} "D| force merging ".join(',',@tomerge)."\n" if $dl;
983         my @newmergelist= ();
984         my %tags = ();
985         my %found = ();
986         my %fixed = ();
987         # Here we try to do the right thing.
988         # First, if the bugs are in the same package, we merge all of the found, fixed, and tags.
989         # If not, we discard the found and fixed.
990         # Everything else we set to the values of the first bug.
991         &getmerge;
992         while (defined($ref= shift(@tomerge))) {
993             print {$transcript} "D| checking merge $ref\n" if $dl;
994             $ref+= 0;
995             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
996                 $ref = $clonebugs{$ref};
997             }
998             next if grep($_ == $ref,@newmergelist);
999             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
1000             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
1001             &foundbug;
1002             print {$transcript} "D| adding $ref ($data->{mergedwith})\n" if $dl;
1003             $master_bug_data = $data if not defined $master_bug_data;
1004             if ($data->{package} ne $master_bug_data->{package}) {
1005                  print {$transcript} "Mismatch - only $gBugs in the same package can be forcibly merged:\n".
1006                      "$gBug $ref is not in the same package as $master_bug\n";
1007                 $errors++;
1008                 &cancelbug; @newmergelist=(); last;
1009             }
1010             for my $t (split /\s+/,$data->{keywords}) {
1011                  $tags{$t} = 1;
1012             }
1013             @found{@{$data->{found_versions}}} = (1) x @{$data->{found_versions}};
1014             @fixed{@{$data->{fixed_versions}}} = (1) x @{$data->{fixed_versions}};
1015             push(@newmergelist,$ref);
1016             push(@tomerge,split(/ /,$data->{mergedwith}));
1017             &cancelbug;
1018         }
1019         if (@newmergelist) {
1020             @newmergelist= sort { $a <=> $b } @newmergelist;
1021             $action= "Forcibly Merged @newmergelist.";
1022             delete @fixed{keys %found};
1023             for $ref (@newmergelist) {
1024                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
1025                 $affected_packages{$data->{package}} = 1;
1026                 add_recipients(data => $data,
1027                                recipients => \%recipients,
1028                                transcript   => $transcript,
1029                                ($dl > 0 ? (debug => $transcript):()),
1030                               );
1031                 @bug_affected{@newmergelist} = 1 x @newmergelist;
1032                 $data->{mergedwith}= join(' ',grep($_ != $ref,@newmergelist));
1033                 $data->{keywords}= join(' ', keys %tags);
1034                 $data->{found_versions}= [sort keys %found];
1035                 $data->{fixed_versions}= [sort keys %fixed];
1036                 my @field_list = qw(forwarded package severity blocks blockedby owner done affects summary);
1037                 @{$data}{@field_list} = @{$master_bug_data}{@field_list};
1038                 &savebug;
1039             }
1040             print {$transcript} "$action\n\n";
1041         }
1042         &endmerge;
1043     } elsif (m/^clone\s+#?(\d+)\s+((-\d+\s+)*-\d+)\s*$/i) {
1044         $ok++;
1045
1046         my $origref = $1;
1047         my @newclonedids = split /\s+/, $2;
1048         my $newbugsneeded = scalar(@newclonedids);
1049
1050         $ref = $origref;
1051         $bug_affected{$ref} = 1;
1052         if (&setbug) {
1053             $affected_packages{$data->{package}} = 1;
1054             if (length($data->{mergedwith})) {
1055                 print {$transcript} "$gBug is marked as being merged with others. Use an existing clone.\n\n";
1056                 $errors++;
1057                 &nochangebug;
1058             } else {
1059                 &filelock("nextnumber.lock");
1060                 open(N,"nextnumber") || die "nextnumber: read: $!";
1061                 my $v=<N>; $v =~ s/\n$// || die "nextnumber bad format";
1062                 my $firstref= $v+0;  $v += $newbugsneeded;
1063                 open(NN,">nextnumber"); print NN "$v\n"; close(NN);
1064                 &unfilelock;
1065
1066                 my $lastref = $firstref + $newbugsneeded - 1;
1067
1068                 if ($newbugsneeded == 1) {
1069                     $action= "$gBug $origref cloned as bug $firstref.";
1070                 } else {
1071                     $action= "$gBug $origref cloned as bugs $firstref-$lastref.";
1072                 }
1073
1074                 my $blocks = $data->{blocks};
1075                 my $blockedby = $data->{blockedby};
1076                 
1077                 &getnextbug;
1078                 my $ohash = get_hashname($origref);
1079                 my $clone = $firstref;
1080                 @bug_affected{@newclonedids} = 1 x @newclonedids;
1081                 for my $newclonedid (@newclonedids) {
1082                     $clonebugs{$newclonedid} = $clone;
1083             
1084                     my $hash = get_hashname($clone);
1085                     copy("db-h/$ohash/$origref.log", "db-h/$hash/$clone.log");
1086                     copy("db-h/$ohash/$origref.status", "db-h/$hash/$clone.status");
1087                     copy("db-h/$ohash/$origref.summary", "db-h/$hash/$clone.summary");
1088                     copy("db-h/$ohash/$origref.report", "db-h/$hash/$clone.report");
1089                     &bughook('new', $clone, $data);
1090                 
1091                     # Update blocking info of bugs blocked by or blocking the
1092                     # cloned bug.
1093                     foreach $ref (split ' ', $blocks) {
1094                         &getbug;
1095                         $data->{blockedby} = manipset($data->{blockedby}, $clone, 1);
1096                         &savebug;
1097                     }
1098                     foreach $ref (split ' ', $blockedby) {
1099                         &getbug;
1100                         $data->{blocks} = manipset($data->{blocks}, $clone, 1);
1101                         &savebug;
1102                     }
1103
1104                     $clone++;
1105                 }
1106             }
1107         }
1108     } elsif (m/^package\:?\s+(\S.*\S)?\s*$/i) {
1109         $ok++;
1110         my @pkgs = split /\s+/, $1;
1111         if (scalar(@pkgs) > 0) {
1112                 %limit_pkgs = map { ($_, 1) } @pkgs;
1113                 $limit{package} = [@pkgs];
1114                 print {$transcript} "Ignoring bugs not assigned to: " .
1115                         join(" ", keys(%limit_pkgs)) . "\n\n";
1116         } else {
1117                 %limit_pkgs = ();
1118                 print {$transcript} "Not ignoring any bugs.\n\n";
1119         }
1120     } elsif (m/^limit\:?\s+(\S.*\S)\s*$/) {
1121         $ok++;
1122         my ($field,@options) = split /\s+/, $1;
1123         $field = lc($field);
1124         if ($field =~ /^(?:clear|unset|blank)$/) {
1125             %limit = ();
1126             print {$transcript} "Limit cleared.\n\n";
1127         }
1128         elsif (exists $Debbugs::Status::fields{$field} ) {
1129             # %limit can actually contain regexes, but because they're
1130             # not evaluated in Safe, DO NOT allow them through without
1131             # fixing this.
1132             $limit{$field} = [@options];
1133             print {$transcript} "Limiting to bugs with field '$field' containing at least one of ".join(', ',map {qq('$_')} @options)."\n";
1134             print {$transcript} "Limit currently set to ";
1135             for my $limit_field (keys %limit) {
1136                 print {$transcript} "  '$limit_field':".join(', ',map {qq('$_')} @options)."\n";
1137             }
1138             print {$transcript} "\n";
1139         }
1140         else {
1141             print {$transcript} "Limit key $field not understood. Stopping processing here.\n\n";
1142             $errors++;
1143             last;
1144         }
1145     } elsif (m/^affects?\s+\#?(-?\d+)(?:\s+((?:[=+-])?)\s*(\S.*)?)?\s*$/i) {
1146         $ok++;
1147         $ref = $1;
1148         my $add_remove = $2 || '';
1149         my $packages = $3 || '';
1150         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1151         $bug_affected{$ref} = 1;
1152         eval {
1153              affects(@common_control_options,
1154                      bug => $ref,
1155                      packages     => [splitpackages($3)],
1156                      ($add_remove eq '+'?(add => 1):()),
1157                      ($add_remove eq '-'?(remove => 1):()),
1158                     );
1159         };
1160         if ($@) {
1161             $errors++;
1162             print {$transcript} "Failed to mark $ref as affecting package(s): ".cleanup_eval_fail($@,$debug)."\n";
1163         }
1164
1165     } elsif (m/^summary\s+\#?(-?\d+)\s*(\d+|)\s*$/i) {
1166         $ok++;
1167         $ref = $1;
1168         my $summary_msg = length($2)?$2:undef;
1169         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1170         $bug_affected{$ref} = 1;
1171         eval {
1172             summary(@common_control_options,
1173                     bug          => $ref,
1174                     summary      => $summary_msg,
1175                    );
1176         };
1177         if ($@) {
1178             $errors++;
1179             print {$transcript} "Failed to give $ref a summary: ".cleanup_eval_fail($@,$debug)."\n";
1180         }
1181
1182     } elsif (m/^owner\s+\#?(-?\d+)\s+((?:\S.*\S)|\!)\s*$/i) {
1183         $ok++;
1184         $ref = $1;
1185         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1186         my $newowner = $2;
1187         if ($newowner eq '!') {
1188             $newowner = $replyto;
1189         }
1190         $bug_affected{$ref} = 1;
1191         eval {
1192             owner(@common_control_options,
1193                   bug          => $ref,
1194                   owner        => $newowner,
1195                  );
1196         };
1197         if ($@) {
1198             $errors++;
1199             print {$transcript} "Failed to mark $ref as having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1200         }
1201     } elsif (m/^noowner\s+\#?(-?\d+)\s*$/i) {
1202         $ok++;
1203         $ref = $1;
1204         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1205         $bug_affected{$ref} = 1;
1206         eval {
1207             owner(@common_control_options,
1208                   bug          => $ref,
1209                   owner        => undef,
1210                  );
1211         };
1212         if ($@) {
1213             $errors++;
1214             print {$transcript} "Failed to mark $ref as not having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1215         }
1216     } elsif (m/^unarchive\s+#?(\d+)$/i) {
1217          $ok++;
1218          $ref = $1;
1219          $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1220          $bug_affected{$ref} = 1;
1221          eval {
1222               bug_unarchive(@common_control_options,
1223                             bug        => $ref,
1224                             recipients => \%recipients,
1225                            );
1226          };
1227          if ($@) {
1228               $errors++;
1229          }
1230     } elsif (m/^archive\s+#?(\d+)$/i) {
1231          $ok++;
1232          $ref = $1;
1233          $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1234          $bug_affected{$ref} = 1;
1235          eval {
1236               bug_archive(@common_control_options,
1237                           bug => $ref,
1238                           ignore_time => 1,
1239                           archive_unarchived => 0,
1240                          );
1241          };
1242          if ($@) {
1243               $errors++;
1244          }
1245     } else {
1246         print {$transcript} "Unknown command or malformed arguments to command.\n\n";
1247         $errors++;
1248         if (++$unknowns >= 5) {
1249             print {$transcript} "Too many unknown commands, stopping here.\n\n";
1250             last;
1251         }
1252     }
1253 }
1254 if ($procline>$#bodylines) {
1255     print {$transcript} ">\nEnd of message, stopping processing here.\n\n";
1256 }
1257 if (!$ok && !$quickabort) {
1258     $errors++;
1259     print {$transcript} "No commands successfully parsed; sending the help text(s).\n";
1260     &sendhelp;
1261     print {$transcript} "\n";
1262 }
1263
1264 my @maintccs = determine_recipients(recipients => \%recipients,
1265                                     address_only => 1,
1266                                     cc => 1,
1267                                    );
1268 my $maintccs = 'Cc: '.join(",\n    ",
1269                     determine_recipients(recipients => \%recipients,
1270                                          cc => 1,
1271                                         )
1272                    )."\n";
1273
1274 my $packagepr = '';
1275 $packagepr = "X-${gProject}-PR-Package: " . join(keys %affected_packages) . "\n" if keys %affected_packages;
1276
1277 # Add Bcc's to subscribed bugs
1278 # now handled by Debbugs::Recipients
1279 #push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
1280
1281 if (!defined $header{'subject'} || $header{'subject'} eq "") {
1282   $header{'subject'} = "your mail";
1283 }
1284
1285 # Error text here advertises how many errors there were
1286 my $error_text = $errors > 0 ? " (with $errors errors)":'';
1287
1288 my $reply= <<END;
1289 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1290 To: $replyto
1291 ${maintccs}Subject: Processed${error_text}: $header{'subject'}
1292 In-Reply-To: $header{'message-id'}
1293 END
1294 $reply .= <<END;
1295 References: $header{'message-id'}
1296 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
1297 Precedence: bulk
1298 ${packagepr}X-$gProject-PR-Message: transcript
1299
1300 ${transcript_scalar}Please contact me if you need assistance.
1301
1302 $gMaintainer
1303 (administrator, $gProject $gBugs database)
1304 END
1305
1306 my $repliedshow= join(', ',$replyto,
1307                       determine_recipients(recipients => \%recipients,
1308                                            cc => 1,
1309                                            address_only => 1,
1310                                           )
1311                      );
1312 # -1 is the service.in log
1313 &filelock("lock/-1");
1314 open(AP,">>db-h/-1.log") || die "open db-h/-1.log: $!";
1315 print(AP
1316       "\2\n$repliedshow\n\5\n$reply\n\3\n".
1317       "\6\n".
1318       "<strong>Request received</strong> from <code>".
1319       html_escape($header{'from'})."</code>\n".
1320       "to <code>".html_escape($controlrequestaddr)."</code>\n".
1321       "\3\n".
1322       "\7\n",escape_log(@log),"\n\3\n") || die "writing db-h/-1.log: $!";
1323 close(AP) || die "open db-h/-1.log: $!";
1324 &unfilelock;
1325 utime(time,time,"db-h");
1326
1327 &sendmailmessage($reply,
1328                  exists $header{'x-debbugs-no-ack'}?():$replyto,
1329                  make_list(values %{{determine_recipients(recipients => \%recipients,
1330                                                           address_only => 1,
1331                                                          )}}
1332                           ),
1333                 );
1334
1335 unlink("incoming/P$nn") || die "unlinking incoming/P$nn: $!";
1336
1337 sub sendmailmessage {
1338     my ($message,@recips) = @_;
1339     $message = "X-Loop: $gMaintainerEmail\n" . $message;
1340     send_mail_message(message    => $message,
1341                       recipients => \@recips,
1342                      );
1343     $midix++;
1344 }
1345
1346 sub fill_template{
1347      my ($template,$extra_var) = @_;
1348      $extra_var ||={};
1349      my $variables = {config => \%config,
1350                       defined($ref)?(ref    => $ref):(),
1351                       defined($data)?(data  => $data):(),
1352                       %{$extra_var},
1353                      };
1354      my $hole_var = {'&bugurl' =>
1355                      sub{"$_[0]: ".
1356                               'http://'.$config{cgi_domain}.'/'.
1357                                    Debbugs::CGI::bug_url($_[0]);
1358                     }
1359                     };
1360      return fill_in_template(template => $template,
1361                              variables => $variables,
1362                              hole_var  => $hole_var,
1363                             );
1364 }
1365
1366 =head2 message_body_template
1367
1368      message_body_template('mail/ack',{ref=>'foo'});
1369
1370 Creates a message body using a template
1371
1372 =cut
1373
1374 sub message_body_template{
1375      my ($template,$extra_var) = @_;
1376      $extra_var ||={};
1377      my $body = fill_template($template,$extra_var);
1378      return fill_template('mail/message_body',
1379                           {%{$extra_var},
1380                            body => $body,
1381                           },
1382                          );
1383 }
1384
1385 sub sendhelp {
1386      if ($control) {
1387           &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
1388      }
1389      else {
1390           &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
1391      }
1392 }
1393
1394 #sub unimplemented {
1395 #    print {$transcript} "Sorry, command $_[0] not yet implemented.\n\n";
1396 #}
1397 our %checkmatch_values;
1398 sub checkmatch {
1399     my ($string,$mvarname,$svarvalue,@newmergelist) = @_;
1400     my ($mvarvalue);
1401     if (@newmergelist) {
1402         $mvarvalue = $checkmatch_values{$mvarname};
1403         print {$transcript} "D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n"
1404             if $dl;
1405         $mismatch .=
1406             "Values for \`$string' don't match:\n".
1407             " #$newmergelist[0] has \`$mvarvalue';\n".
1408             " #$ref has \`$svarvalue'\n"
1409             if $mvarvalue ne $svarvalue;
1410     } else {
1411          print {$transcript} "D| setupmatch \`$string' /$mvarname/$svarvalue/\n"
1412               if $dl;
1413          $checkmatch_values{$mvarname} = $svarvalue;
1414     }
1415 }
1416
1417 sub checkpkglimit {
1418     if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1419         print {$transcript} "$gBug number $ref belongs to package $data->{package}, skipping.\n\n";
1420         $errors++;
1421         return 0;
1422     }
1423     return 1;
1424 }
1425
1426 sub manipset {
1427     my $list = shift;
1428     my $elt = shift;
1429     my $add = shift;
1430
1431     my %h = map { $_ => 1 } split ' ', $list;
1432     if ($add) {
1433         $h{$elt}=1;
1434     }
1435     else {
1436         delete $h{$elt};
1437     }
1438     return join ' ', sort keys %h;
1439 }
1440
1441 # High-level bug manipulation calls
1442 # Do announcements themselves
1443 #
1444 # Possible calling sequences:
1445 #    setbug (returns 0)
1446 #    
1447 #    setbug (returns 1)
1448 #    &transcript(something)
1449 #    nochangebug
1450 #
1451 #    setbug (returns 1)
1452 #    $action= (something)
1453 #    do {
1454 #      (modify s_* variables)
1455 #    } while (getnextbug);
1456
1457 our $manybugs;
1458
1459 sub nochangebug {
1460     &dlen("nochangebug");
1461     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1462     &cancelbug;
1463     &endmerge if $manybugs;
1464     $state= 'idle';
1465     &dlex("nochangebug");
1466 }
1467
1468 our $sref;
1469 our @thisbugmergelist;
1470
1471 sub setbug {
1472     &dlen("setbug $ref");
1473     if ($ref =~ m/^-\d+/) {
1474         if (!defined $clonebugs{$ref}) {
1475             &notfoundbug;
1476             &dlex("setbug => noclone");
1477             return 0;
1478         }
1479         $ref = $clonebugs{$ref};
1480     }
1481     $state eq 'idle' || die "$state ?";
1482     if (!&getbug) {
1483         &notfoundbug;
1484         &dlex("setbug => 0s");
1485         return 0;
1486     }
1487
1488     if (!&checkpkglimit) {
1489         &cancelbug;
1490         return 0;
1491     }
1492
1493     @thisbugmergelist= split(/ /,$data->{mergedwith});
1494     if (!@thisbugmergelist) {
1495         &foundbug;
1496         $manybugs= 0;
1497         $state= 'single';
1498         $sref=$ref;
1499         &dlex("setbug => 1s");
1500         return 1;
1501     }
1502     &cancelbug;
1503     &getmerge;
1504     $manybugs= 1;
1505     if (!&getbug) {
1506         &notfoundbug;
1507         &endmerge;
1508         &dlex("setbug => 0mc");
1509         return 0;
1510     }
1511     &foundbug;
1512     $state= 'multiple'; $sref=$ref;
1513     &dlex("setbug => 1m");
1514     return 1;
1515 }
1516
1517 sub getnextbug {
1518     &dlen("getnextbug");
1519     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1520     &savebug;
1521     if (!$manybugs || !@thisbugmergelist) {
1522         length($action) || die;
1523         print {$transcript} "$action\n$extramessage\n";
1524         &endmerge if $manybugs;
1525         $state= 'idle';
1526         &dlex("getnextbug => 0");
1527         return 0;
1528     }
1529     $ref= shift(@thisbugmergelist);
1530     &getbug || die "bug $ref disappeared";
1531     &foundbug;
1532     &dlex("getnextbug => 1");
1533     return 1;
1534 }
1535
1536 # Low-level bug-manipulation calls
1537 # Do no announcements
1538 #
1539 #    getbug (returns 0)
1540 #
1541 #    getbug (returns 1)
1542 #    cancelbug
1543 #
1544 #    getmerge
1545 #    $action= (something)
1546 #    getbug (returns 1)
1547 #    savebug/cancelbug
1548 #    getbug (returns 1)
1549 #    savebug/cancelbug
1550 #    [getbug (returns 0)]
1551 #    &transcript("$action\n\n")
1552 #    endmerge
1553
1554 sub notfoundbug { print {$transcript} "$gBug number $ref not found. (Is it archived?)\n\n"; }
1555 sub foundbug { print {$transcript} "$gBug#$ref: $data->{subject}\n"; }
1556
1557 sub getmerge {
1558     &dlen("getmerge");
1559     $mergelowstate eq 'idle' || die "$mergelowstate ?";
1560     &filelock('lock/merge');
1561     $mergelowstate='locked';
1562     &dlex("getmerge");
1563 }
1564
1565 sub endmerge {
1566     &dlen("endmerge");
1567     $mergelowstate eq 'locked' || die "$mergelowstate ?";
1568     &unfilelock;
1569     $mergelowstate='idle';
1570     &dlex("endmerge");
1571 }
1572
1573 sub getbug {
1574     &dlen("getbug $ref");
1575     $lowstate eq 'idle' || die "$state ?";
1576     # Only use unmerged bugs here
1577     if (($data = &lockreadbug($ref,'db-h'))) {
1578         $sref= $ref;
1579         $lowstate= "open";
1580         &dlex("getbug => 1");
1581         $extramessage='';
1582         return 1;
1583     }
1584     $lowstate= 'idle';
1585     &dlex("getbug => 0");
1586     return 0;
1587 }
1588
1589 sub cancelbug {
1590     &dlen("cancelbug");
1591     $lowstate eq 'open' || die "$state ?";
1592     &unfilelock;
1593     $lowstate= 'idle';
1594     &dlex("cancelbug");
1595 }
1596
1597 sub savebug {
1598     &dlen("savebug $ref");
1599     $lowstate eq 'open' || die "$lowstate ?";
1600     length($action) || die;
1601     $ref == $sref || die "read $sref but saving $ref ?";
1602     append_action_to_log(bug => $ref,
1603                          action => $action,
1604                          requester => $header{from},
1605                          request_addr => $controlrequestaddr,
1606                          message => \@log,
1607                          get_lock => 0,
1608                         );
1609     unlockwritebug($ref, $data);
1610     $lowstate= "idle";
1611     &dlex("savebug");
1612 }
1613
1614 sub dlen {
1615     return if !$dl;
1616     print {$transcript} "C> @_ ($state $lowstate $mergelowstate)\n";
1617 }
1618
1619 sub dlex {
1620     return if !$dl;
1621     print {$transcript} "R> @_ ($state $lowstate $mergelowstate)\n";
1622 }
1623
1624 sub urlsanit {
1625     my $url = shift;
1626     $url =~ s/%/%25/g;
1627     $url =~ s/\+/%2b/g;
1628     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1629     $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1630     return $url;
1631 }
1632
1633 sub sendlynxdoc {
1634     &sendlynxdocraw;
1635     print {$transcript} "\n";
1636     $ok++;
1637 }
1638
1639 sub sendtxthelp {
1640     &sendtxthelpraw;
1641     print {$transcript} "\n";
1642     $ok++;
1643 }
1644
1645
1646 our $doc;
1647 sub sendtxthelpraw {
1648     my ($relpath,$description) = @_;
1649     $doc='';
1650     if (not -e "$gDocDir/$relpath") {
1651         print {$transcript} "Unfortunatly, the help text doesn't exist, so it wasn't sent.\n";
1652         warn "Help text $gDocDir/$relpath not found";
1653         return;
1654     }
1655     open(D,"$gDocDir/$relpath") || die "open doc file $relpath: $!";
1656     while(<D>) { $doc.=$_; }
1657     close(D);
1658     print {$transcript} "Sending $description in separate message.\n";
1659     &sendmailmessage(<<END.$doc,$replyto);
1660 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1661 To: $replyto
1662 Subject: $gProject $gBug help: $description
1663 References: $header{'message-id'}
1664 In-Reply-To: $header{'message-id'}
1665 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1666 Precedence: bulk
1667 X-$gProject-PR-Message: doc-text $relpath
1668
1669 END
1670     $ok++;
1671 }
1672
1673 sub sendlynxdocraw {
1674     my ($relpath,$description) = @_;
1675     $doc='';
1676     open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || die "fork for lynx: $!";
1677     while(<L>) { $doc.=$_; }
1678     $!=0; close(L);
1679     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1680         print {$transcript} "Information ($description) is not available -\n".
1681              "perhaps the $gBug does not exist or is not on the WWW yet.\n";
1682          $ok++;
1683     } elsif ($?) {
1684         print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1685     } else {
1686         print {$transcript} "Sending $description.\n";
1687         &sendmailmessage(<<END.$doc,$replyto);
1688 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1689 To: $replyto
1690 Subject: $gProject $gBugs information: $description
1691 References: $header{'message-id'}
1692 In-Reply-To: $header{'message-id'}
1693 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1694 Precedence: bulk
1695 X-$gProject-PR-Message: doc-html $relpath
1696
1697 END
1698          $ok++;
1699     }
1700 }
1701
1702
1703 sub sendinfo {
1704     my ($wherefrom,$path,$description) = @_;
1705     if ($wherefrom eq "ftp.d.o") {
1706       $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or die "fork for lynx/gunzip: $!";
1707       $! = 0;
1708       if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1709           print {$transcript} "$description is not available.\n";
1710           $ok++; return;
1711       } elsif ($?) {
1712           print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1713           return;
1714       }
1715     } elsif ($wherefrom eq "local") {
1716       open P, "$path";
1717       $doc = do { local $/; <P> };
1718       close P;
1719     } else {
1720       print {$transcript} "internal errror: info files location unknown.\n";
1721       $ok++; return;
1722     }
1723     print {$transcript} "Sending $description.\n";
1724     &sendmailmessage(<<END.$doc,$replyto);
1725 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1726 To: $replyto
1727 Subject: $gProject $gBugs information: $description
1728 References: $header{'message-id'}
1729 In-Reply-To: $header{'message-id'}
1730 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1731 Precedence: bulk
1732 X-$gProject-PR-Message: getinfo
1733
1734 $description follows:
1735
1736 END
1737     $ok++;
1738     print {$transcript} "\n";
1739 }