]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
merge changes from da tree
[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         } else {
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 =
517                             (reverse @{$data->{fixed_versions}})[0];
518                         # TODO: what if $data->{package} is a source package?
519                         addfoundversions($data, $data->{package}, $version, 'binary');
520                         if (defined $lastfixed and not grep { $_ eq $lastfixed } @{$data->{fixed_versions}}) {
521                             $data->{done} = '';
522                         }
523                     } else {
524                         # Versionless found; assume old-style "not fixed at
525                         # all".
526                         $data->{fixed_versions} = [];
527                         $data->{done} = '';
528                     }
529                 } while (&getnextbug);
530             }
531         }
532     } elsif (m/^notfound\s+\#?(-?\d+)\s+(\d.*)$/i) {
533         $ok++;
534         $ref= $1;
535         $version= $2;
536         if (&setbug) {
537             $action= "$gBug marked as not found in version $version.";
538             if (length($data->{done})) {
539                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
540             }
541             do {
542                 &addmaintainers($data);
543                 removefoundversions($data, $data->{package}, $version, 'binary');
544             } while (&getnextbug);
545         }
546     } elsif (m/^submitter\s+\#?(-?\d+)\s+\!$/i ? ($newsubmitter=$replyto, 1) :
547              m/^submitter\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($newsubmitter=$2, 1) : 0) {
548         $ok++;
549         $ref= $1;
550         $bug_affected{$ref}=1;
551         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
552             $ref = $clonebugs{$ref};
553         }
554         if (&getbug) {
555             if (&checkpkglimit) {
556                 &foundbug;
557                 &addmaintainers($data);
558                 $oldsubmitter= $data->{originator};
559                 $data->{originator}= $newsubmitter;
560                 $action= "Changed $gBug submitter from $oldsubmitter to $newsubmitter.";
561                 &savebug;
562                 &transcript("$action\n");
563                 if (length($data->{done})) {
564                     &transcript("(By the way, that $gBug is currently marked as done.)\n");
565                 }
566                 &transcript("\n");
567                 $message= <<END;
568 From: $gMaintainerEmail ($gProject $gBug Tracking System)
569 To: $oldsubmitter
570 Subject: $gBug#$ref submitter address changed
571          ($header{'subject'})
572 References: $header{'message-id'} $data->{msgid}
573 In-Reply-To: $data->{msgid}
574 Message-ID: <handler.$ref.$nn.newsubmitter.$midix\@$gEmailDomain>
575 Reply-To: $ref\@$gEmailDomain
576 X-$gProject-PR-Message: submitter-changed $ref
577
578 The submitter address recorded for your $gBug report
579 #$ref: $data->{subject}
580 has been changed.
581
582 The old submitter address for this report was
583 $oldsubmitter.
584 The new submitter address is
585 $newsubmitter.
586
587 This change was made by
588 $replyto.
589 If it was incorrect, please contact them directly.
590
591 $gMaintainer
592 (administrator, $gProject $gBugs database)
593
594 END
595                 &sendmailmessage($message,$oldsubmitter);
596             } else {
597                 &cancelbug;
598             }
599         } else {
600             &notfoundbug;
601         }
602     } elsif (m/^forwarded\s+\#?(-?\d+)\s+(\S.*\S)$/i) {
603         $ok++;
604         $ref= $1; $whereto= $2;
605         $bug_affected{$ref}=1;
606         if (&setbug) {
607             if (length($data->{forwarded})) {
608     $action= "Forwarded-to-address changed from $data->{forwarded} to $whereto.";
609             } else {
610     $action= "Noted your statement that $gBug has been forwarded to $whereto.";
611             }
612             if (length($data->{done})) {
613                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
614             }
615             do {
616                 &addmaintainers($data);
617                 if (length($gForwardList)>0 && length($gListDomain)>0 ) {
618                      &addccaddress("$gForwardList\@$gListDomain"); 
619                 }
620                 $data->{forwarded}= $whereto;
621             } while (&getnextbug);
622         }
623     } elsif (m/^notforwarded\s+\#?(-?\d+)$/i) {
624         $ok++;
625         $ref= $1;
626         $bug_affected{$ref}=1;
627         if (&setbug) {
628             if (!length($data->{forwarded})) {
629                 &transcript("$gBug is not marked as having been forwarded.\n\n");
630                 &nochangebug;
631             } else {
632     $action= "Removed annotation that $gBug had been forwarded to $data->{forwarded}.";
633                 do {
634                     &addmaintainers($data);
635                     $data->{forwarded}= '';
636                 } while (&getnextbug);
637             }
638         }
639     } elsif (m/^severity\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i ||
640         m/^priority\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i) {
641         $ok++;
642         $ref= $1;
643         $bug_affected{$ref}=1;
644         $newseverity= $2;
645         if (!grep($_ eq $newseverity, @gSeverityList, "$gDefaultSeverity")) {
646             &transcript("Severity level \`$newseverity' is not known.\n".
647                         "Recognized are: $gShowSeverities.\n\n");
648         } elsif (exists $gObsoleteSeverities{$newseverity}) {
649             &transcript("Severity level \`$newseverity' is obsolete. " .
650                         "$gObsoleteSeverities{$newseverity}\n\n");
651         } elsif (&setbug) {
652             $printseverity= $data->{severity};
653             $printseverity= "$gDefaultSeverity" if $printseverity eq '';
654             $action= "Severity set to \`$newseverity' from \`$printseverity'";
655             do {
656                 &addmaintainers($data);
657                 if (defined $gStrongList and isstrongseverity($newseverity)) {
658                     addbcc("$gStrongList\@$gListDomain");
659                 }
660                 $data->{severity}= $newseverity;
661             } while (&getnextbug);
662         }
663     } elsif (m/^tags?\s+\#?(-?\d+)\s+(([=+-])\s*)?(\S.*)?$/i) {
664         $ok++;
665         $ref = $1; $addsubcode = $3; $tags = $4;
666         $bug_affected{$ref}=1;
667         $addsub = "add";
668         if (defined $addsubcode) {
669             $addsub = "sub" if ($addsubcode eq "-");
670             $addsub = "add" if ($addsubcode eq "+");
671             $addsub = "set" if ($addsubcode eq "=");
672         }
673         my @okaytags = ();
674         my @badtags = ();
675         foreach my $t (split /[\s,]+/, $tags) {
676             if (!grep($_ eq $t, @gTags)) {
677                 push @badtags, $t;
678             } else {
679                 push @okaytags, $t;
680             }
681         }
682         if (@badtags) {
683             &transcript("Unknown tag/s: ".join(', ', @badtags).".\n".
684                         "Recognized are: ".join(' ', @gTags).".\n\n");
685         }
686         if (&setbug) {
687             if ($data->{keywords} eq '') {
688                 &transcript("There were no tags set.\n");
689             } else {
690                 &transcript("Tags were: $data->{keywords}\n");
691             }
692             if ($addsub eq "set") {
693                 $action= "Tags set to: " . join(", ", @okaytags);
694             } elsif ($addsub eq "add") {
695                 $action= "Tags added: " . join(", ", @okaytags);
696             } elsif ($addsub eq "sub") {
697                 $action= "Tags removed: " . join(", ", @okaytags);
698             }
699             do {
700                 &addmaintainers($data);
701                 $data->{keywords} = '' if ($addsub eq "set");
702                 # Allow removing obsolete tags.
703                 if ($addsub eq "sub") {
704                     foreach my $t (@badtags) {
705                         $data->{keywords} = join ' ', grep $_ ne $t, 
706                             split ' ', $data->{keywords};
707                     }
708                 }
709                 # Now process all other additions and subtractions.
710                 foreach my $t (@okaytags) {
711                     $data->{keywords} = join ' ', grep $_ ne $t, 
712                         split ' ', $data->{keywords};
713                     $data->{keywords} = "$t $data->{keywords}" unless($addsub eq "sub");
714                 }
715                 $data->{keywords} =~ s/\s*$//;
716             } while (&getnextbug);
717         }
718     } elsif (m/^(un)?block\s+\#?(-?\d+)\s+(by|with)\s+\s*(\S.*)?$/i) {
719         $ok++;
720         my $bugnum = $2; my $blockers = $4;
721         $addsub = "add";
722         $addsub = "sub" if ($1 eq "un");
723         
724         my @okayblockers;
725         my @badblockers;
726         foreach my $b (split /[\s,]+/, $blockers) {
727             $b=~s/^\#//;
728             if ($b=~/[0-9]+/) {
729                 $ref=$b;
730                 if (&getbug) {
731                     push @okayblockers, $b;
732
733                     # add to the list all bugs that are merged with $b,
734                     # because all of their data must be kept in sync
735                     @thisbugmergelist= split(/ /,$data->{mergedwith});
736                     &cancelbug;
737
738                     foreach $ref (@thisbugmergelist) {
739                         if (&getbug) {
740                            push @okayblockers, $ref;
741                            &cancelbug;
742                         }
743                     }
744                 }
745                 else {
746                     &notfoundbug;
747                     push @badblockers, $b;
748                 }
749             }
750             else {
751                 push @badblockers, $b;
752             }
753         }
754         if (@badblockers) {
755             &transcript("Unknown blocking bug/s: ".join(', ', @badblockers).".\n");
756         }
757         
758         $ref=$bugnum;
759         if (&setbug) {
760             if ($data->{blockedby} eq '') {
761                 &transcript("Was not blocked by any bugs.\n");
762             } else {
763                 &transcript("Was blocked by: $data->{blockedby}\n");
764             }
765             if ($addsub eq "set") {
766                 $action= "Blocking bugs set to: " . join(", ", @okayblockers);
767             } elsif ($addsub eq "add") {
768                 $action= "Blocking bugs added: " . join(", ", @okayblockers);
769             } elsif ($addsub eq "sub") {
770                 $action= "Blocking bugs removed: " . join(", ", @okayblockers);
771             }
772             my %removedblocks;
773             my %addedblocks;
774             do {
775                 &addmaintainers($data);
776                 my @oldblockerlist = split ' ', $data->{blockedby};
777                 $data->{blockedby} = '' if ($addsub eq "set");
778                 foreach my $b (@okayblockers) {
779                         $data->{blockedby} = manipset($data->{blockedby}, $b,
780                                 ($addsub ne "sub"));
781                 }
782
783                 foreach my $b (@oldblockerlist) {
784                         if (! grep { $_ eq $b } split ' ', $data->{blockedby}) {
785                                 push @{$removedblocks{$b}}, $ref;
786                         }
787                 }
788                 foreach my $b (split ' ', $data->{blockedby}) {
789                         if (! grep { $_ eq $b } @oldblockerlist) {
790                                 push @{$addedblocks{$b}}, $ref;
791                         }
792                 }
793             } while (&getnextbug);
794
795             # Now that the blockedby data is updated, change blocks data
796             # to match the changes.
797             foreach $ref (keys %addedblocks) {
798                 if (&getbug) {
799                     foreach my $b (@{$addedblocks{$ref}}) {
800                         $data->{blocks} = manipset($data->{blocks}, $b, 1);
801                     }
802                     &savebug;
803                 }
804             }
805             foreach $ref (keys %removedblocks) {
806                 if (&getbug) {
807                     foreach my $b (@{$removedblocks{$ref}}) {
808                         $data->{blocks} = manipset($data->{blocks}, $b, 0);
809                     }
810                     &savebug;
811                 }
812             }
813         }
814     } elsif (m/^retitle\s+\#?(-?\d+)\s+(\S.*\S)\s*$/i) {
815         $ok++;
816         $ref= $1; $newtitle= $2;
817         $bug_affected{$ref}=1;
818         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
819             $ref = $clonebugs{$ref};
820         }
821         if (&getbug) {
822             if (&checkpkglimit) {
823                 &foundbug;
824                 &addmaintainers($data);
825                 $data->{subject}= $newtitle;
826                 $action= "Changed $gBug title.";
827                 &savebug;
828                 &transcript("$action\n");
829                 if (length($data->{done})) {
830                     &transcript("(By the way, that $gBug is currently marked as done.)\n");
831                 }
832                 &transcript("\n");
833             } else {
834                 &cancelbug;
835             }
836         } else {
837             &notfoundbug;
838         }
839     } elsif (m/^unmerge\s+\#?(-?\d+)$/i) {
840         $ok++;
841         $ref= $1;
842         $bug_affected{$ref} = 1;
843         if (&setbug) {
844             if (!length($data->{mergedwith})) {
845                 &transcript("$gBug is not marked as being merged with any others.\n\n");
846                 &nochangebug;
847             } else {
848                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
849                 $action= "Disconnected #$ref from all other report(s).";
850                 @newmergelist= split(/ /,$data->{mergedwith});
851                 $discref= $ref;
852                 @bug_affected{@newmergelist} = 1 x @newmergelist;
853                 do {
854                     &addmaintainers($data);
855                     $data->{mergedwith}= ($ref == $discref) ? ''
856                         : join(' ',grep($_ ne $ref,@newmergelist));
857                 } while (&getnextbug);
858             }
859         }
860     } elsif (m/^merge\s+#?(-?\d+(\s+#?-?\d+)+)\s*$/i) {
861         $ok++;
862         my @tomerge= sort { $a <=> $b } split(/\s+#?/,$1);
863         my @newmergelist= ();
864         my %tags = ();
865         my %found = ();
866         my %fixed = ();
867         &getmerge;
868         while (defined($ref= shift(@tomerge))) {
869             &transcript("D| checking merge $ref\n") if $dl;
870             $ref+= 0;
871             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
872                 $ref = $clonebugs{$ref};
873             }
874             next if grep($_ == $ref,@newmergelist);
875             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
876             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
877             &foundbug;
878             &transcript("D| adding $ref ($data->{mergedwith})\n") if $dl;
879             $mismatch= '';
880             &checkmatch('package','m_package',$data->{package});
881             &checkmatch('forwarded addr','m_forwarded',$data->{forwarded});
882             $data->{severity} = '$gDefaultSeverity' if $data->{severity} eq '';
883             &checkmatch('severity','m_severity',$data->{severity});
884             &checkmatch('blocks','m_blocks',$data->{blocks});
885             &checkmatch('blocked-by','m_blockedby',$data->{blockedby});
886             &checkmatch('done mark','m_done',length($data->{done}) ? 'done' : 'open');
887             &checkmatch('owner','m_owner',$data->{owner});
888             foreach my $t (split /\s+/, $data->{keywords}) { $tags{$t} = 1; }
889             foreach my $f (@{$data->{found_versions}}) { $found{$f} = 1; }
890             foreach my $f (@{$data->{fixed_versions}}) { $fixed{$f} = 1; }
891             if (length($mismatch)) {
892                 &transcript("Mismatch - only $gBugs in same state can be merged:\n".
893                             $mismatch."\n");
894                 &cancelbug; @newmergelist=(); last;
895             }
896             push(@newmergelist,$ref);
897             push(@tomerge,split(/ /,$data->{mergedwith}));
898             &cancelbug;
899         }
900         if (@newmergelist) {
901             @newmergelist= sort { $a <=> $b } @newmergelist;
902             $action= "Merged @newmergelist.";
903             delete @fixed{keys %found};
904             for $ref (@newmergelist) {
905                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
906                 &addmaintainers($data);
907                 @bug_affected{@newmergelist} = 1 x @newmergelist;
908                 $data->{mergedwith}= join(' ',grep($_ != $ref,@newmergelist));
909                 $data->{keywords}= join(' ', keys %tags);
910                 $data->{found_versions}= [sort keys %found];
911                 $data->{fixed_versions}= [sort keys %fixed];
912                 &savebug;
913             }
914             &transcript("$action\n\n");
915         }
916         &endmerge;
917     } elsif (m/^forcemerge\s+\#?(-?\d+(?:\s+\#?-?\d+)+)\s*$/i) {
918         $ok++;
919         my @temp = split /\s+\#?/,$1;
920         my $master_bug = shift @temp;
921         my $master_bug_data;
922         my @tomerge = sort { $a <=> $b } @temp;
923         unshift @tomerge,$master_bug;
924         &transcript("D| force merging ".join(',',@tomerge)."\n") if $dl;
925         my @newmergelist= ();
926         my %tags = ();
927         my %found = ();
928         my %fixed = ();
929         # Here we try to do the right thing.
930         # First, if the bugs are in the same package, we merge all of the found, fixed, and tags.
931         # If not, we discard the found and fixed.
932         # Everything else we set to the values of the first bug.
933         &getmerge;
934         while (defined($ref= shift(@tomerge))) {
935             &transcript("D| checking merge $ref\n") if $dl;
936             $ref+= 0;
937             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
938                 $ref = $clonebugs{$ref};
939             }
940             next if grep($_ == $ref,@newmergelist);
941             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
942             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
943             &foundbug;
944             &transcript("D| adding $ref ($data->{mergedwith})\n") if $dl;
945             $master_bug_data = $data if not defined $master_bug_data;
946             if ($data->{package} ne $master_bug_data->{package}) {
947                 &transcript("Mismatch - only $gBugs in the same package can be forcibly merged:\n".
948                             "$gBug $ref is not in the same package as $master_bug\n");
949                 &cancelbug; @newmergelist=(); last;
950             }
951             for my $t (split /\s+/,$data->{keywords}) {
952                  $tags{$t} = 1;
953             }
954             @found{@{$data->{found_versions}}} = (1) x @{$data->{found_versions}};
955             @fixed{@{$data->{fixed_versions}}} = (1) x @{$data->{fixed_versions}};
956             push(@newmergelist,$ref);
957             push(@tomerge,split(/ /,$data->{mergedwith}));
958             &cancelbug;
959         }
960         if (@newmergelist) {
961             @newmergelist= sort { $a <=> $b } @newmergelist;
962             $action= "Forcibly Merged @newmergelist.";
963             delete @fixed{keys %found};
964             for $ref (@newmergelist) {
965                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
966                 &addmaintainers($data);
967                 @bug_affected{@newmergelist} = 1 x @newmergelist;
968                 $data->{mergedwith}= join(' ',grep($_ != $ref,@newmergelist));
969                 $data->{keywords}= join(' ', keys %tags);
970                 $data->{found_versions}= [sort keys %found];
971                 $data->{fixed_versions}= [sort keys %fixed];
972                 my @field_list = qw(forwarded package severity blocks blockedby owner done);
973                 @{$data}{@field_list} = @{$master_bug_data}{@field_list};
974                 &savebug;
975             }
976             &transcript("$action\n\n");
977         }
978         &endmerge;
979     } elsif (m/^clone\s+#?(\d+)\s+((-\d+\s+)*-\d+)\s*$/i) {
980         $ok++;
981
982         $origref = $1;
983         @newclonedids = split /\s+/, $2;
984         $newbugsneeded = scalar(@newclonedids);
985
986         $ref = $origref;
987         $bug_affected{$ref} = 1;
988         if (&setbug) {
989             if (length($data->{mergedwith})) {
990                 &transcript("$gBug is marked as being merged with others.\n\n");
991                 &nochangebug;
992             } else {
993                 &filelock("nextnumber.lock");
994                 open(N,"nextnumber") || &quit("nextnumber: read: $!");
995                 $v=<N>; $v =~ s/\n$// || &quit("nextnumber bad format");
996                 $firstref= $v+0;  $v += $newbugsneeded;
997                 open(NN,">nextnumber"); print NN "$v\n"; close(NN);
998                 &unfilelock;
999
1000                 $lastref = $firstref + $newbugsneeded - 1;
1001
1002                 if ($newbugsneeded == 1) {
1003                     $action= "$gBug $origref cloned as bug $firstref.";
1004                 } else {
1005                     $action= "$gBug $origref cloned as bugs $firstref-$lastref.";
1006                 }
1007
1008                 my $blocks = $data->{blocks};
1009                 my $blockedby = $data->{blockedby};
1010                 
1011                 &getnextbug;
1012                 my $ohash = get_hashname($origref);
1013                 my $clone = $firstref;
1014                 @bug_affected{@newclonedids} = 1 x @newclonedids;
1015                 for $newclonedid (@newclonedids) {
1016                     $clonebugs{$newclonedid} = $clone;
1017             
1018                     my $hash = get_hashname($clone);
1019                     copy("db-h/$ohash/$origref.log", "db-h/$hash/$clone.log");
1020                     copy("db-h/$ohash/$origref.status", "db-h/$hash/$clone.status");
1021                     copy("db-h/$ohash/$origref.summary", "db-h/$hash/$clone.summary");
1022                     copy("db-h/$ohash/$origref.report", "db-h/$hash/$clone.report");
1023                     &bughook('new', $clone, $data);
1024                 
1025                     # Update blocking info of bugs blocked by or blocking the
1026                     # cloned bug.
1027                     foreach $ref (split ' ', $blocks) {
1028                         &getbug;
1029                         $data->{blockedby} = manipset($data->{blockedby}, $clone, 1);
1030                         &savebug;
1031                     }
1032                     foreach $ref (split ' ', $blockedby) {
1033                         &getbug;
1034                         $data->{blocks} = manipset($data->{blocks}, $clone, 1);
1035                         &savebug;
1036                     }
1037
1038                     $clone++;
1039                 }
1040             }
1041         }
1042     } elsif (m/^package\s+(\S.*\S)?\s*$/i) {
1043         $ok++;
1044         my @pkgs = split /\s+/, $1;
1045         if (scalar(@pkgs) > 0) {
1046                 %limit_pkgs = map { ($_, 1) } @pkgs;
1047                 &transcript("Ignoring bugs not assigned to: " . 
1048                         join(" ", keys(%limit_pkgs)) . "\n\n");
1049         } else {
1050                 %limit_pkgs = ();
1051                 &transcript("Not ignoring any bugs.\n\n");
1052         }
1053     } elsif (m/^owner\s+\#?(-?\d+)\s+!$/i ? ($newowner = $replyto, 1) :
1054              m/^owner\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($newowner = $2, 1) : 0) {
1055         $ok++;
1056         $ref = $1;
1057         $bug_affected{$ref} = 1;
1058         if (&setbug) {
1059             if (length $data->{owner}) {
1060                 $action = "Owner changed from $data->{owner} to $newowner.";
1061             } else {
1062                 $action = "Owner recorded as $newowner.";
1063             }
1064             if (length $data->{done}) {
1065                 $extramessage = "(By the way, this $gBug is currently " .
1066                                 "marked as done.)\n";
1067             }
1068             do {
1069                 &addmaintainers($data);
1070                 $data->{owner} = $newowner;
1071             } while (&getnextbug);
1072         }
1073     } elsif (m/^noowner\s+\#?(-?\d+)$/i) {
1074         $ok++;
1075         $ref = $1;
1076         $bug_affected{$ref} = 1;
1077         if (&setbug) {
1078             if (length $data->{owner}) {
1079                 $action = "Removed annotation that $gBug was owned by " .
1080                           "$data->{owner}.";
1081                 do {
1082                     &addmaintainers($data);
1083                     $data->{owner} = '';
1084                 } while (&getnextbug);
1085             } else {
1086                 &transcript("$gBug is not marked as having an owner.\n\n");
1087                 &nochangebug;
1088             }
1089         }
1090     } else {
1091         &transcript("Unknown command or malformed arguments to command.\n\n");
1092         if (++$unknowns >= 5) {
1093             &transcript("Too many unknown commands, stopping here.\n\n");
1094             last;
1095         }
1096     }
1097 }
1098 if ($procline>$#bodylines) {
1099     &transcript(">\nEnd of message, stopping processing here.\n\n");
1100 }
1101 if (!$ok && !quickabort) {
1102     &transcript("No commands successfully parsed; sending the help text(s).\n");
1103     &sendhelp;
1104     &transcript("\n");
1105 }
1106
1107 &transcript("MC\n") if $dl>1;
1108 @maintccs= ();
1109 for $maint (keys %maintccreasons) {
1110 &transcript("MM|$maint|\n") if $dl>1;
1111     next if $maint eq $replyto;
1112     $reasonstring= '';
1113     $reasonsref= $maintccreasons{$maint};
1114 &transcript("MY|$maint|\n") if $dl>2;
1115     for $p (sort keys %$reasonsref) {
1116 &transcript("MP|$p|\n") if $dl>2;
1117         $reasonstring.= ', ' if length($reasonstring);
1118         $reasonstring.= $p.' ' if length($p);
1119         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
1120     }
1121     if (length($reasonstring) > 40) {
1122         (substr $reasonstring, 37) = "...";
1123     }
1124     $reasonstring = "" if (!defined($reasonstring));
1125     push(@maintccs,"$maint ($reasonstring)");
1126     push(@maintccaddrs,"$maint");
1127 }
1128
1129 $maintccs = ""; 
1130 if (@maintccs) {
1131     &transcript("MC|@maintccs|\n") if $dl>2;
1132     $maintccs .= "Cc: " . join(",\n    ",@maintccs) . "\n";
1133 }
1134
1135 # Add Bcc's to subscribed bugs
1136 push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
1137
1138 if (!defined $header{'subject'} || $header{'subject'} eq "") {
1139   $header{'subject'} = "your mail";
1140 }
1141
1142 $reply= <<END;
1143 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1144 To: $replyto
1145 ${maintccs}Subject: Processed: $header{'subject'}
1146 In-Reply-To: $header{'message-id'}
1147 References: $header{'message-id'}
1148 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
1149 Precedence: bulk
1150 X-$gProject-PR-Message: transcript
1151
1152 ${transcript}Please contact me if you need assistance.
1153
1154 $gMaintainer
1155 (administrator, $gProject $gBugs database)
1156 $extras
1157 END
1158
1159 $repliedshow= join(', ',$replyto,@maintccaddrs);
1160 &filelock("lock/-1");
1161 open(AP,">>db-h/-1.log") || &quit("open db-h/-1.log: $!");
1162 print(AP
1163       "\2\n$repliedshow\n\5\n$reply\n\3\n".
1164       "\6\n".
1165       "<strong>Request received</strong> from <code>".
1166       &sani($header{'from'})."</code>\n".
1167       "to <code>".&sani($controlrequestaddr)."</code>\n".
1168       "\3\n".
1169       "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/-1.log: $!");
1170 close(AP) || &quit("open db-h/-1.log: $!");
1171 &unfilelock;
1172 utime(time,time,"db-h");
1173
1174 &sendmailmessage($reply,exists $header{'x-debbugs-no-ack'}?():$replyto,@maintccaddrs,@bcc);
1175
1176 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
1177
1178 sub sendmailmessage {
1179     local ($message,@recips) = @_;
1180     $message = "X-Loop: $gMaintainerEmail\n" . $message;
1181     send_mail_message(message    => $message,
1182                       recipients => \@recips,
1183                      );
1184     $midix++;
1185 }
1186
1187 sub sendhelp {
1188         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
1189         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
1190             if $control;
1191 }
1192
1193 #sub unimplemented {
1194 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
1195 #}
1196
1197 sub checkmatch {
1198     local ($string,$mvarname,$svarvalue) = @_;
1199     local ($mvarvalue);
1200     if (@newmergelist) {
1201         eval "\$mvarvalue= \$$mvarname";
1202         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
1203             if $dl;
1204         $mismatch .=
1205             "Values for \`$string' don't match:\n".
1206             " #$newmergelist[0] has \`$mvarvalue';\n".
1207             " #$ref has \`$svarvalue'\n"
1208             if $mvarvalue ne $svarvalue;
1209     } else {
1210         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
1211             if $dl;
1212         eval "\$$mvarname= \$svarvalue";
1213     }
1214 }
1215
1216 sub checkpkglimit {
1217     if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1218         &transcript("$gBug number $ref belongs to package $data->{package}, skipping.\n\n");
1219         return 0;
1220     }
1221     return 1;
1222 }
1223
1224 sub manipset {
1225     my $list = shift;
1226     my $elt = shift;
1227     my $add = shift;
1228
1229     my %h = map { $_ => 1 } split ' ', $list;
1230     if ($add) {
1231         $h{$elt}=1;
1232     }
1233     else {
1234         delete $h{$elt};
1235     }
1236     return join ' ', sort keys %h;
1237 }
1238
1239 # High-level bug manipulation calls
1240 # Do announcements themselves
1241 #
1242 # Possible calling sequences:
1243 #    setbug (returns 0)
1244 #    
1245 #    setbug (returns 1)
1246 #    &transcript(something)
1247 #    nochangebug
1248 #
1249 #    setbug (returns 1)
1250 #    $action= (something)
1251 #    do {
1252 #      (modify s_* variables)
1253 #    } while (getnextbug);
1254
1255 sub nochangebug {
1256     &dlen("nochangebug");
1257     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1258     &cancelbug;
1259     &endmerge if $manybugs;
1260     $state= 'idle';
1261     &dlex("nochangebug");
1262 }
1263
1264 sub setbug {
1265     &dlen("setbug $ref");
1266     if ($ref =~ m/^-\d+/) {
1267         if (!defined $clonebugs{$ref}) {
1268             &notfoundbug;
1269             &dlex("setbug => noclone");
1270             return 0;
1271         }
1272         $ref = $clonebugs{$ref};
1273     }
1274     $state eq 'idle' || die "$state ?";
1275     if (!&getbug) {
1276         &notfoundbug;
1277         &dlex("setbug => 0s");
1278         return 0;
1279     }
1280
1281     if (!&checkpkglimit) {
1282         &cancelbug;
1283         return 0;
1284     }
1285
1286     @thisbugmergelist= split(/ /,$data->{mergedwith});
1287     if (!@thisbugmergelist) {
1288         &foundbug;
1289         $manybugs= 0;
1290         $state= 'single';
1291         $sref=$ref;
1292         &dlex("setbug => 1s");
1293         return 1;
1294     }
1295     &cancelbug;
1296     &getmerge;
1297     $manybugs= 1;
1298     if (!&getbug) {
1299         &notfoundbug;
1300         &endmerge;
1301         &dlex("setbug => 0mc");
1302         return 0;
1303     }
1304     &foundbug;
1305     $state= 'multiple'; $sref=$ref;
1306     &dlex("setbug => 1m");
1307     return 1;
1308 }
1309
1310 sub getnextbug {
1311     &dlen("getnextbug");
1312     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1313     &savebug;
1314     if (!$manybugs || !@thisbugmergelist) {
1315         length($action) || die;
1316         &transcript("$action\n$extramessage\n");
1317         &endmerge if $manybugs;
1318         $state= 'idle';
1319         &dlex("getnextbug => 0");
1320         return 0;
1321     }
1322     $ref= shift(@thisbugmergelist);
1323     &getbug || die "bug $ref disappeared";
1324     &foundbug;
1325     &dlex("getnextbug => 1");
1326     return 1;
1327 }
1328
1329 # Low-level bug-manipulation calls
1330 # Do no announcements
1331 #
1332 #    getbug (returns 0)
1333 #
1334 #    getbug (returns 1)
1335 #    cancelbug
1336 #
1337 #    getmerge
1338 #    $action= (something)
1339 #    getbug (returns 1)
1340 #    savebug/cancelbug
1341 #    getbug (returns 1)
1342 #    savebug/cancelbug
1343 #    [getbug (returns 0)]
1344 #    &transcript("$action\n\n")
1345 #    endmerge
1346
1347 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
1348 sub foundbug { &transcript("$gBug#$ref: $data->{subject}\n"); }
1349
1350 sub getmerge {
1351     &dlen("getmerge");
1352     $mergelowstate eq 'idle' || die "$mergelowstate ?";
1353     &filelock('lock/merge');
1354     $mergelowstate='locked';
1355     &dlex("getmerge");
1356 }
1357
1358 sub endmerge {
1359     &dlen("endmerge");
1360     $mergelowstate eq 'locked' || die "$mergelowstate ?";
1361     &unfilelock;
1362     $mergelowstate='idle';
1363     &dlex("endmerge");
1364 }
1365
1366 sub getbug {
1367     &dlen("getbug $ref");
1368     $lowstate eq 'idle' || die "$state ?";
1369     if (($data = &lockreadbug($ref))) {
1370         $sref= $ref;
1371         $lowstate= "open";
1372         &dlex("getbug => 1");
1373         $extramessage='';
1374         return 1;
1375     }
1376     $lowstate= 'idle';
1377     &dlex("getbug => 0");
1378     return 0;
1379 }
1380
1381 sub cancelbug {
1382     &dlen("cancelbug");
1383     $lowstate eq 'open' || die "$state ?";
1384     &unfilelock;
1385     $lowstate= 'idle';
1386     &dlex("cancelbug");
1387 }
1388
1389 sub savebug {
1390     &dlen("savebug $ref");
1391     $lowstate eq 'open' || die "$lowstate ?";
1392     length($action) || die;
1393     $ref == $sref || die "read $sref but saving $ref ?";
1394     my $hash = get_hashname($ref);
1395     open(L,">>db-h/$hash/$ref.log") || &quit("opening db-h/$hash/$ref.log: $!");
1396     print(L
1397           "\6\n".
1398           "<strong>".&sani($action)."</strong>\n".
1399           "Request was from <code>".&sani($header{'from'})."</code>\n".
1400           "to <code>".&sani($controlrequestaddr)."</code>. \n".
1401           "\3\n".
1402           "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/$hash/$ref.log: $!");
1403     close(L) || &quit("closing db-h/$hash/$ref.log: $!");
1404     unlockwritebug($ref, $data);
1405     $lowstate= "idle";
1406     &dlex("savebug");
1407 }
1408
1409 sub dlen {
1410     return if !$dl;
1411     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
1412 }
1413
1414 sub dlex {
1415     return if !$dl;
1416     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
1417 }
1418
1419 sub transcript {
1420     print $_[0] if $debug;
1421     $transcript.= $_[0];
1422 }
1423
1424 sub urlsanit {
1425     my $url = shift;
1426     $url =~ s/%/%25/g;
1427     $url =~ s/\+/%2b/g;
1428     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1429     $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1430     return $url;
1431 }
1432
1433 sub sendlynxdoc {
1434     &sendlynxdocraw;
1435     &transcript("\n");
1436     $ok++;
1437 }
1438
1439 sub sendtxthelp {
1440     &sendtxthelpraw;
1441     &transcript("\n");
1442     $ok++;
1443 }
1444
1445 sub sendtxthelpraw {
1446     local ($relpath,$description) = @_;
1447     $doc='';
1448     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
1449     while(<D>) { $doc.=$_; }
1450     close(D);
1451     &transcript("Sending $description in separate message.\n");
1452     &sendmailmessage(<<END.$doc,$replyto);
1453 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1454 To: $replyto
1455 Subject: $gProject $gBug help: $description
1456 References: $header{'message-id'}
1457 In-Reply-To: $header{'message-id'}
1458 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1459 Precedence: bulk
1460 X-$gProject-PR-Message: doc-text $relpath
1461
1462 END
1463     $ok++;
1464 }
1465
1466 sub sendlynxdocraw {
1467     local ($relpath,$description) = @_;
1468     $doc='';
1469     open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || &quit("fork for lynx: $!");
1470     while(<L>) { $doc.=$_; }
1471     $!=0; close(L);
1472     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1473         &transcript("Information ($description) is not available -\n".
1474                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
1475          $ok++;
1476     } elsif ($?) {
1477         &transcript("Error getting $description (code $? $!):\n$doc\n");
1478     } else {
1479         &transcript("Sending $description.\n");
1480         &sendmailmessage(<<END.$doc,$replyto);
1481 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1482 To: $replyto
1483 Subject: $gProject $gBugs information: $description
1484 References: $header{'message-id'}
1485 In-Reply-To: $header{'message-id'}
1486 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1487 Precedence: bulk
1488 X-$gProject-PR-Message: doc-html $relpath
1489
1490 END
1491          $ok++;
1492     }
1493 }
1494
1495 sub addccaddress {
1496     my ($cca) = @_;
1497     $maintccreasons{$cca}{''}{$ref}= 1;
1498 }
1499
1500 sub addmaintainers {
1501     # Data structure is:
1502     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
1503     my $data = shift;
1504     my ($p, $addmaint);
1505     &ensuremaintainersloaded;
1506     $anymaintfound=0; $anymaintnotfound=0;
1507     for $p (split(m/[ \t?,():]+/, $data->{package})) {
1508         $p =~ y/A-Z/a-z/;
1509         $p =~ /([a-z0-9.+-]+)/;
1510         $p = $1;
1511         next unless defined $p;
1512         if (defined $gSubscriptionDomain) {
1513             if (defined($pkgsrc{$p})) {
1514                 addbcc("$pkgsrc{$p}\@$gSubscriptionDomain");
1515             } else {
1516                 addbcc("$p\@$gSubscriptionDomain");
1517             }
1518         }
1519         if (defined $data->{severity} and defined $gStrongList and
1520                 isstrongseverity($data->{severity})) {
1521             addbcc("$gStrongList\@$gListDomain");
1522         }
1523         if (defined($maintainerof{$p})) {
1524             $addmaint= $maintainerof{$p};
1525             &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
1526             $maintccreasons{$addmaint}{$p}{$ref}= 1;
1527             print "maintainer add >$p|$addmaint<\n" if $debug;
1528         } else { 
1529             print "maintainer none >$p<\n" if $debug; 
1530             &transcript("Warning: Unknown package '$p'\n");
1531             &transcript("MR|unknown-package|$p|$ref|\n") if $dl>2;
1532             $maintccreasons{$gUnknownMaintainerEmail}{$p}{$ref}= 1;
1533         }
1534     }
1535
1536     if (length $data->{owner}) {
1537         $addmaint = $data->{owner};
1538         &transcript("MO|$addmaint|$data->{package}|$ref|\n") if $dl>2;
1539         $maintccreasons{$addmaint}{$data->{package}}{$ref} = 1;
1540         print "owner add >$data->{package}|$addmaint<\n" if $debug;
1541     }
1542 }
1543
1544 sub ensuremaintainersloaded {
1545     my ($a,$b);
1546     return if $maintainersloaded++;
1547     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
1548     while (<MAINT>) {
1549         m/^\n$/ && next;
1550         m/^\s*$/ && next;
1551         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers bogus \`$_'");
1552         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1553         $maintainerof{$a}= $2;
1554     }
1555     close(MAINT);
1556     open(MAINT,"$gMaintainerFileOverride") || die &quit("maintainers.override open: $!");
1557     while (<MAINT>) {
1558         m/^\n$/ && next;
1559         m/^\s*$/ && next;
1560         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers.override bogus \`$_'");
1561         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1562         $maintainerof{$a}= $2;
1563     }
1564
1565     open(SOURCES, "$gPackageSource") || &quit("pkgsrc open: $!");
1566     while (<SOURCES>) {
1567         next unless m/^(\S+)\s+\S+\s+(\S.*\S)\s*$/;
1568         my ($a, $b) = ($1, $2);
1569         $pkgsrc{lc($a)} = $b;
1570     }
1571     close(SOURCES);
1572 }
1573
1574 sub sendinfo {
1575     local ($wherefrom,$path,$description) = @_;
1576     if ($wherefrom eq "ftp.d.o") {
1577       $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or &quit("fork for lynx/gunzip: $!");
1578       $! = 0;
1579       if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1580           &transcript("$description is not available.\n");
1581           $ok++; return;
1582       } elsif ($?) {
1583           &transcript("Error getting $description (code $? $!):\n$doc\n");
1584           return;
1585       }
1586     } elsif ($wherefrom eq "local") {
1587       open P, "$path";
1588       $doc = do { local $/; <P> };
1589       close P;
1590     } else {
1591       &transcript("internal errror: info files location unknown.\n");
1592       $ok++; return;
1593     }
1594     &transcript("Sending $description.\n");
1595     &sendmailmessage(<<END.$doc,$replyto);
1596 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1597 To: $replyto
1598 Subject: $gProject $gBugs information: $description
1599 References: $header{'message-id'}
1600 In-Reply-To: $header{'message-id'}
1601 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1602 Precedence: bulk
1603 X-$gProject-PR-Message: getinfo
1604
1605 $description follows:
1606
1607 END
1608     $ok++;
1609     &transcript("\n");
1610 }