]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service
89ff785c4152234c72c62f02fb30ac9e17a755c1
[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 = map {m/^([+=-])(.+)/ ? ($1,$2):($_)} 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 = map {exists $clonebugs{$_}?$clonebugs{$_}:$_} 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} "Limiting to bugs with field 'package' containing at least one of ".join(', ',map {qq('$_')} @pkgs)."\n";
1115                 print {$transcript} "Limit currently set to";
1116                 for my $limit_field (keys %limit) {
1117                     print {$transcript} " '$limit_field':".join(', ',map {qq('$_')} @{$limit{$limit_field}})."\n";
1118                 }
1119                 print {$transcript} "\n";
1120         } else {
1121             %limit_pkgs = ();
1122             $limit{package} = [];
1123             print {$transcript} "Limit cleared.\n\n";
1124         }
1125     } elsif (m/^limit\:?\s+(\S.*\S)\s*$/) {
1126         $ok++;
1127         my ($field,@options) = split /\s+/, $1;
1128         $field = lc($field);
1129         if ($field =~ /^(?:clear|unset|blank)$/) {
1130             %limit = ();
1131             print {$transcript} "Limit cleared.\n\n";
1132         }
1133         elsif (exists $Debbugs::Status::fields{$field} or $field eq 'source') {
1134             # %limit can actually contain regexes, but because they're
1135             # not evaluated in Safe, DO NOT allow them through without
1136             # fixing this.
1137             $limit{$field} = [@options];
1138             print {$transcript} "Limiting to bugs with field '$field' containing at least one of ".join(', ',map {qq('$_')} @options)."\n";
1139             print {$transcript} "Limit currently set to";
1140             for my $limit_field (keys %limit) {
1141                 print {$transcript} " '$limit_field':".join(', ',map {qq('$_')} @{$limit{$limit_field}})."\n";
1142             }
1143             print {$transcript} "\n";
1144         }
1145         else {
1146             print {$transcript} "Limit key $field not understood. Stopping processing here.\n\n";
1147             $errors++;
1148             last;
1149         }
1150     } elsif (m/^affects?\s+\#?(-?\d+)(?:\s+((?:[=+-])?)\s*(\S.*)?)?\s*$/i) {
1151         $ok++;
1152         $ref = $1;
1153         my $add_remove = $2 || '';
1154         my $packages = $3 || '';
1155         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1156         $bug_affected{$ref} = 1;
1157         eval {
1158              affects(@common_control_options,
1159                      bug => $ref,
1160                      packages     => [splitpackages($3)],
1161                      ($add_remove eq '+'?(add => 1):()),
1162                      ($add_remove eq '-'?(remove => 1):()),
1163                     );
1164         };
1165         if ($@) {
1166             $errors++;
1167             print {$transcript} "Failed to mark $ref as affecting package(s): ".cleanup_eval_fail($@,$debug)."\n";
1168         }
1169
1170     } elsif (m/^summary\s+\#?(-?\d+)\s*(\d+|)\s*$/i) {
1171         $ok++;
1172         $ref = $1;
1173         my $summary_msg = length($2)?$2:undef;
1174         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1175         $bug_affected{$ref} = 1;
1176         eval {
1177             summary(@common_control_options,
1178                     bug          => $ref,
1179                     summary      => $summary_msg,
1180                    );
1181         };
1182         if ($@) {
1183             $errors++;
1184             print {$transcript} "Failed to give $ref a summary: ".cleanup_eval_fail($@,$debug)."\n";
1185         }
1186
1187     } elsif (m/^owner\s+\#?(-?\d+)\s+((?:\S.*\S)|\!)\s*$/i) {
1188         $ok++;
1189         $ref = $1;
1190         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1191         my $newowner = $2;
1192         if ($newowner eq '!') {
1193             $newowner = $replyto;
1194         }
1195         $bug_affected{$ref} = 1;
1196         eval {
1197             owner(@common_control_options,
1198                   bug          => $ref,
1199                   owner        => $newowner,
1200                  );
1201         };
1202         if ($@) {
1203             $errors++;
1204             print {$transcript} "Failed to mark $ref as having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1205         }
1206     } elsif (m/^noowner\s+\#?(-?\d+)\s*$/i) {
1207         $ok++;
1208         $ref = $1;
1209         $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1210         $bug_affected{$ref} = 1;
1211         eval {
1212             owner(@common_control_options,
1213                   bug          => $ref,
1214                   owner        => undef,
1215                  );
1216         };
1217         if ($@) {
1218             $errors++;
1219             print {$transcript} "Failed to mark $ref as not having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1220         }
1221     } elsif (m/^unarchive\s+#?(\d+)$/i) {
1222          $ok++;
1223          $ref = $1;
1224          $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1225          $bug_affected{$ref} = 1;
1226          eval {
1227               bug_unarchive(@common_control_options,
1228                             bug        => $ref,
1229                             recipients => \%recipients,
1230                            );
1231          };
1232          if ($@) {
1233               $errors++;
1234          }
1235     } elsif (m/^archive\s+#?(\d+)$/i) {
1236          $ok++;
1237          $ref = $1;
1238          $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1239          $bug_affected{$ref} = 1;
1240          eval {
1241               bug_archive(@common_control_options,
1242                           bug => $ref,
1243                           ignore_time => 1,
1244                           archive_unarchived => 0,
1245                          );
1246          };
1247          if ($@) {
1248               $errors++;
1249          }
1250     } else {
1251         print {$transcript} "Unknown command or malformed arguments to command.\n\n";
1252         $errors++;
1253         if (++$unknowns >= 5) {
1254             print {$transcript} "Too many unknown commands, stopping here.\n\n";
1255             last;
1256         }
1257     }
1258 }
1259 if ($procline>$#bodylines) {
1260     print {$transcript} ">\nEnd of message, stopping processing here.\n\n";
1261 }
1262 if (!$ok && !$quickabort) {
1263     $errors++;
1264     print {$transcript} "No commands successfully parsed; sending the help text(s).\n";
1265     &sendhelp;
1266     print {$transcript} "\n";
1267 }
1268
1269 my @maintccs = determine_recipients(recipients => \%recipients,
1270                                     address_only => 1,
1271                                     cc => 1,
1272                                    );
1273 my $maintccs = 'Cc: '.join(",\n    ",
1274                     determine_recipients(recipients => \%recipients,
1275                                          cc => 1,
1276                                         )
1277                    )."\n";
1278
1279 my $packagepr = '';
1280 $packagepr = "X-${gProject}-PR-Package: " . join(keys %affected_packages) . "\n" if keys %affected_packages;
1281
1282 # Add Bcc's to subscribed bugs
1283 # now handled by Debbugs::Recipients
1284 #push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
1285
1286 if (!defined $header{'subject'} || $header{'subject'} eq "") {
1287   $header{'subject'} = "your mail";
1288 }
1289
1290 # Error text here advertises how many errors there were
1291 my $error_text = $errors > 0 ? " (with $errors errors)":'';
1292
1293 my $reply= <<END;
1294 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1295 To: $replyto
1296 ${maintccs}Subject: Processed${error_text}: $header{'subject'}
1297 In-Reply-To: $header{'message-id'}
1298 END
1299 $reply .= <<END;
1300 References: $header{'message-id'}
1301 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
1302 Precedence: bulk
1303 ${packagepr}X-$gProject-PR-Message: transcript
1304 END
1305
1306 $reply .= fill_template('mail/message_body',
1307                           {body => "${transcript_scalar}Please contact me if you need assistance."},
1308                         );
1309
1310 my $repliedshow= join(', ',$replyto,
1311                       determine_recipients(recipients => \%recipients,
1312                                            cc => 1,
1313                                            address_only => 1,
1314                                           )
1315                      );
1316 # -1 is the service.in log
1317 &filelock("lock/-1");
1318 open(AP,">>db-h/-1.log") || die "open db-h/-1.log: $!";
1319 print(AP
1320       "\2\n$repliedshow\n\5\n$reply\n\3\n".
1321       "\6\n".
1322       "<strong>Request received</strong> from <code>".
1323       html_escape($header{'from'})."</code>\n".
1324       "to <code>".html_escape($controlrequestaddr)."</code>\n".
1325       "\3\n".
1326       "\7\n",escape_log(@log),"\n\3\n") || die "writing db-h/-1.log: $!";
1327 close(AP) || die "open db-h/-1.log: $!";
1328 &unfilelock;
1329 utime(time,time,"db-h");
1330
1331 &sendmailmessage($reply,
1332                  exists $header{'x-debbugs-no-ack'}?():$replyto,
1333                  make_list(values %{{determine_recipients(recipients => \%recipients,
1334                                                           address_only => 1,
1335                                                          )}}
1336                           ),
1337                 );
1338
1339 unlink("incoming/P$nn") || die "unlinking incoming/P$nn: $!";
1340
1341 sub sendmailmessage {
1342     my ($message,@recips) = @_;
1343     $message = "X-Loop: $gMaintainerEmail\n" . $message;
1344     send_mail_message(message    => $message,
1345                       recipients => \@recips,
1346                      );
1347     $midix++;
1348 }
1349
1350 sub fill_template{
1351      my ($template,$extra_var) = @_;
1352      $extra_var ||={};
1353      my $variables = {config => \%config,
1354                       defined($ref)?(ref    => $ref):(),
1355                       defined($data)?(data  => $data):(),
1356                       refs => [keys %bug_affected],
1357                       %{$extra_var},
1358                      };
1359      my $hole_var = {'&bugurl' =>
1360                      sub{"$_[0]: ".
1361                               'http://'.$config{cgi_domain}.'/'.
1362                                    Debbugs::CGI::bug_links(bug=>$_[0],
1363                                                            links_only => 1,
1364                                                           );
1365                     }
1366                     };
1367      return fill_in_template(template => $template,
1368                              variables => $variables,
1369                              hole_var  => $hole_var,
1370                             );
1371 }
1372
1373 =head2 message_body_template
1374
1375      message_body_template('mail/ack',{ref=>'foo'});
1376
1377 Creates a message body using a template
1378
1379 =cut
1380
1381 sub message_body_template{
1382      my ($template,$extra_var) = @_;
1383      $extra_var ||={};
1384      my $body = fill_template($template,$extra_var);
1385      return fill_template('mail/message_body',
1386                           {%{$extra_var},
1387                            body => $body,
1388                           },
1389                          );
1390 }
1391
1392 sub sendhelp {
1393      if ($control) {
1394           &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
1395      }
1396      else {
1397           &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
1398      }
1399 }
1400
1401 #sub unimplemented {
1402 #    print {$transcript} "Sorry, command $_[0] not yet implemented.\n\n";
1403 #}
1404 our %checkmatch_values;
1405 sub checkmatch {
1406     my ($string,$mvarname,$svarvalue,@newmergelist) = @_;
1407     my ($mvarvalue);
1408     if (@newmergelist) {
1409         $mvarvalue = $checkmatch_values{$mvarname};
1410         print {$transcript} "D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n"
1411             if $dl;
1412         $mismatch .=
1413             "Values for \`$string' don't match:\n".
1414             " #$newmergelist[0] has \`$mvarvalue';\n".
1415             " #$ref has \`$svarvalue'\n"
1416             if $mvarvalue ne $svarvalue;
1417     } else {
1418          print {$transcript} "D| setupmatch \`$string' /$mvarname/$svarvalue/\n"
1419               if $dl;
1420          $checkmatch_values{$mvarname} = $svarvalue;
1421     }
1422 }
1423
1424 sub checkpkglimit {
1425     if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1426         print {$transcript} "$gBug number $ref belongs to package $data->{package}, skipping.\n\n";
1427         $errors++;
1428         return 0;
1429     }
1430     return 1;
1431 }
1432
1433 sub manipset {
1434     my $list = shift;
1435     my $elt = shift;
1436     my $add = shift;
1437
1438     my %h = map { $_ => 1 } split ' ', $list;
1439     if ($add) {
1440         $h{$elt}=1;
1441     }
1442     else {
1443         delete $h{$elt};
1444     }
1445     return join ' ', sort keys %h;
1446 }
1447
1448 # High-level bug manipulation calls
1449 # Do announcements themselves
1450 #
1451 # Possible calling sequences:
1452 #    setbug (returns 0)
1453 #    
1454 #    setbug (returns 1)
1455 #    &transcript(something)
1456 #    nochangebug
1457 #
1458 #    setbug (returns 1)
1459 #    $action= (something)
1460 #    do {
1461 #      (modify s_* variables)
1462 #    } while (getnextbug);
1463
1464 our $manybugs;
1465
1466 sub nochangebug {
1467     &dlen("nochangebug");
1468     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1469     &cancelbug;
1470     &endmerge if $manybugs;
1471     $state= 'idle';
1472     &dlex("nochangebug");
1473 }
1474
1475 our $sref;
1476 our @thisbugmergelist;
1477
1478 sub setbug {
1479     &dlen("setbug $ref");
1480     if ($ref =~ m/^-\d+/) {
1481         if (!defined $clonebugs{$ref}) {
1482             &notfoundbug;
1483             &dlex("setbug => noclone");
1484             return 0;
1485         }
1486         $ref = $clonebugs{$ref};
1487     }
1488     $state eq 'idle' || die "$state ?";
1489     if (!&getbug) {
1490         &notfoundbug;
1491         &dlex("setbug => 0s");
1492         return 0;
1493     }
1494
1495     if (!&checkpkglimit) {
1496         &cancelbug;
1497         return 0;
1498     }
1499
1500     @thisbugmergelist= split(/ /,$data->{mergedwith});
1501     if (!@thisbugmergelist) {
1502         &foundbug;
1503         $manybugs= 0;
1504         $state= 'single';
1505         $sref=$ref;
1506         &dlex("setbug => 1s");
1507         return 1;
1508     }
1509     &cancelbug;
1510     &getmerge;
1511     $manybugs= 1;
1512     if (!&getbug) {
1513         &notfoundbug;
1514         &endmerge;
1515         &dlex("setbug => 0mc");
1516         return 0;
1517     }
1518     &foundbug;
1519     $state= 'multiple'; $sref=$ref;
1520     &dlex("setbug => 1m");
1521     return 1;
1522 }
1523
1524 sub getnextbug {
1525     &dlen("getnextbug");
1526     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1527     &savebug;
1528     if (!$manybugs || !@thisbugmergelist) {
1529         length($action) || die;
1530         print {$transcript} "$action\n$extramessage\n";
1531         &endmerge if $manybugs;
1532         $state= 'idle';
1533         &dlex("getnextbug => 0");
1534         return 0;
1535     }
1536     $ref= shift(@thisbugmergelist);
1537     &getbug || die "bug $ref disappeared";
1538     &foundbug;
1539     &dlex("getnextbug => 1");
1540     return 1;
1541 }
1542
1543 # Low-level bug-manipulation calls
1544 # Do no announcements
1545 #
1546 #    getbug (returns 0)
1547 #
1548 #    getbug (returns 1)
1549 #    cancelbug
1550 #
1551 #    getmerge
1552 #    $action= (something)
1553 #    getbug (returns 1)
1554 #    savebug/cancelbug
1555 #    getbug (returns 1)
1556 #    savebug/cancelbug
1557 #    [getbug (returns 0)]
1558 #    &transcript("$action\n\n")
1559 #    endmerge
1560
1561 sub notfoundbug { print {$transcript} "$gBug number $ref not found. (Is it archived?)\n\n"; }
1562 sub foundbug { print {$transcript} "$gBug#$ref: $data->{subject}\n"; }
1563
1564 sub getmerge {
1565     &dlen("getmerge");
1566     $mergelowstate eq 'idle' || die "$mergelowstate ?";
1567     &filelock('lock/merge');
1568     $mergelowstate='locked';
1569     &dlex("getmerge");
1570 }
1571
1572 sub endmerge {
1573     &dlen("endmerge");
1574     $mergelowstate eq 'locked' || die "$mergelowstate ?";
1575     &unfilelock;
1576     $mergelowstate='idle';
1577     &dlex("endmerge");
1578 }
1579
1580 sub getbug {
1581     &dlen("getbug $ref");
1582     $lowstate eq 'idle' || die "$state ?";
1583     # Only use unmerged bugs here
1584     if (($data = &lockreadbug($ref,'db-h'))) {
1585         $sref= $ref;
1586         $lowstate= "open";
1587         &dlex("getbug => 1");
1588         $extramessage='';
1589         return 1;
1590     }
1591     $lowstate= 'idle';
1592     &dlex("getbug => 0");
1593     return 0;
1594 }
1595
1596 sub cancelbug {
1597     &dlen("cancelbug");
1598     $lowstate eq 'open' || die "$state ?";
1599     &unfilelock;
1600     $lowstate= 'idle';
1601     &dlex("cancelbug");
1602 }
1603
1604 sub savebug {
1605     &dlen("savebug $ref");
1606     $lowstate eq 'open' || die "$lowstate ?";
1607     length($action) || die;
1608     $ref == $sref || die "read $sref but saving $ref ?";
1609     append_action_to_log(bug => $ref,
1610                          action => $action,
1611                          requester => $header{from},
1612                          request_addr => $controlrequestaddr,
1613                          message => \@log,
1614                          get_lock => 0,
1615                         );
1616     unlockwritebug($ref, $data);
1617     $lowstate= "idle";
1618     &dlex("savebug");
1619 }
1620
1621 sub dlen {
1622     return if !$dl;
1623     print {$transcript} "C> @_ ($state $lowstate $mergelowstate)\n";
1624 }
1625
1626 sub dlex {
1627     return if !$dl;
1628     print {$transcript} "R> @_ ($state $lowstate $mergelowstate)\n";
1629 }
1630
1631 sub urlsanit {
1632     my $url = shift;
1633     $url =~ s/%/%25/g;
1634     $url =~ s/\+/%2b/g;
1635     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1636     $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1637     return $url;
1638 }
1639
1640 sub sendlynxdoc {
1641     &sendlynxdocraw;
1642     print {$transcript} "\n";
1643     $ok++;
1644 }
1645
1646 sub sendtxthelp {
1647     &sendtxthelpraw;
1648     print {$transcript} "\n";
1649     $ok++;
1650 }
1651
1652
1653 our $doc;
1654 sub sendtxthelpraw {
1655     my ($relpath,$description) = @_;
1656     $doc='';
1657     if (not -e "$gDocDir/$relpath") {
1658         print {$transcript} "Unfortunatly, the help text doesn't exist, so it wasn't sent.\n";
1659         warn "Help text $gDocDir/$relpath not found";
1660         return;
1661     }
1662     open(D,"$gDocDir/$relpath") || die "open doc file $relpath: $!";
1663     while(<D>) { $doc.=$_; }
1664     close(D);
1665     print {$transcript} "Sending $description in separate message.\n";
1666     &sendmailmessage(<<END.$doc,$replyto);
1667 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1668 To: $replyto
1669 Subject: $gProject $gBug help: $description
1670 References: $header{'message-id'}
1671 In-Reply-To: $header{'message-id'}
1672 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1673 Precedence: bulk
1674 X-$gProject-PR-Message: doc-text $relpath
1675
1676 END
1677     $ok++;
1678 }
1679
1680 sub sendlynxdocraw {
1681     my ($relpath,$description) = @_;
1682     $doc='';
1683     open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || die "fork for lynx: $!";
1684     while(<L>) { $doc.=$_; }
1685     $!=0; close(L);
1686     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1687         print {$transcript} "Information ($description) is not available -\n".
1688              "perhaps the $gBug does not exist or is not on the WWW yet.\n";
1689          $ok++;
1690     } elsif ($?) {
1691         print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1692     } else {
1693         print {$transcript} "Sending $description.\n";
1694         &sendmailmessage(<<END.$doc,$replyto);
1695 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1696 To: $replyto
1697 Subject: $gProject $gBugs information: $description
1698 References: $header{'message-id'}
1699 In-Reply-To: $header{'message-id'}
1700 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1701 Precedence: bulk
1702 X-$gProject-PR-Message: doc-html $relpath
1703
1704 END
1705          $ok++;
1706     }
1707 }
1708
1709
1710 sub sendinfo {
1711     my ($wherefrom,$path,$description) = @_;
1712     if ($wherefrom eq "ftp.d.o") {
1713       $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or die "fork for lynx/gunzip: $!";
1714       $! = 0;
1715       if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1716           print {$transcript} "$description is not available.\n";
1717           $ok++; return;
1718       } elsif ($?) {
1719           print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1720           return;
1721       }
1722     } elsif ($wherefrom eq "local") {
1723       open P, "$path";
1724       $doc = do { local $/; <P> };
1725       close P;
1726     } else {
1727       print {$transcript} "internal errror: info files location unknown.\n";
1728       $ok++; return;
1729     }
1730     print {$transcript} "Sending $description.\n";
1731     &sendmailmessage(<<END.$doc,$replyto);
1732 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1733 To: $replyto
1734 Subject: $gProject $gBugs information: $description
1735 References: $header{'message-id'}
1736 In-Reply-To: $header{'message-id'}
1737 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1738 Precedence: bulk
1739 X-$gProject-PR-Message: getinfo
1740
1741 $description follows:
1742
1743 END
1744     $ok++;
1745     print {$transcript} "\n";
1746 }