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