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