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