]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
* Don't ask for more bugs if there is no maintainer (closes: #355190)
[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\s*$/i || m/^quit\s*$/i || m/^--\s*$/ || m/^thank(?:s|\s*you)?\s*$/i || m/^kthxbye\s*$/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             &nochangebug;
339             my %ut;
340             Debbugs::User::read_usertags(\%ut, $user);
341             my @oldtags = (); my @newtags = (); my @badtags = ();
342             my %chtags;
343             for my $t (split /[,\s]+/, $tags) {
344                 if ($t =~ m/^[a-zA-Z0-9.+\@-]+$/) {
345                     $chtags{$t} = 1;
346                 } else {
347                     push @badtags, $t;
348                 }
349             }
350             if (@badtags) {
351                 &transcript("Ignoring illegal tag/s: ".join(', ', @badtags).".\nPlease use only alphanumerics, at, dot, plus and dash.\n");
352             }
353             for my $t (keys %chtags) {
354                 $ut{$t} = [] unless defined $ut{$t};
355             }
356             for my $t (keys %ut) {
357                 my %res = map { ($_, 1) } @{$ut{$t}};
358                 push @oldtags, $t if defined $res{$ref};
359                 my $addop = ($addsubcode eq "+" or $addsubcode eq "=");
360                 my $del = (defined $chtags{$t} ? $addsubcode eq "-" 
361                                                : $addsubcode eq "=");
362                 $res{$ref} = 1 if ($addop && defined $chtags{$t});
363                 delete $res{$ref} if ($del);
364                 push @newtags, $t if defined $res{$ref};
365                 $ut{$t} = [ sort { $a <=> $b } (keys %res) ];
366             }
367             if (@oldtags == 0) {
368                 &transcript("There were no usertags set.\n");
369             } else {
370                 &transcript("Usertags were: " . join(" ", @oldtags) . ".\n");
371             }
372             &transcript("Usertags are now: " . join(" ", @newtags) . ".\n");
373             Debbugs::User::write_usertags(\%ut, $user);
374         }
375     } elsif (!$control) {
376         &transcript(<<END);
377 Unknown command or malformed arguments to command.
378 (Use control\@$gEmailDomain to manipulate reports.)
379
380 END
381         if (++$unknowns >= 3) {
382             &transcript("Too many unknown commands, stopping here.\n\n");
383             last;
384         }
385 #### "developer only" ones start here
386     } elsif (m/^close\s+\#?(-?\d+)(?:\s+(\d.*))?$/i) {
387         $ok++;
388         $ref= $1;
389         $bug_affected{$ref}=1;
390         $version= $2;
391         if (&setbug) {
392             &transcript("'close' is deprecated; see http://$gWebDomain/Developer$gHTMLSuffix#closing.\n");
393             if (length($data->{done}) and not defined($version)) {
394                 &transcript("$gBug is already closed, cannot re-close.\n\n");
395                 &nochangebug;
396             } else {
397                 $action= "$gBug " .
398                     (defined($version) ?
399                         "marked as fixed in version $version" :
400                         "closed") .
401                     ", send any further explanations to $data->{originator}";
402                 do {
403                     &addmaintainers($data);
404                                         if ( length( $gDoneList ) > 0 && length( $gListDomain ) >
405                                         0 ) { &addccaddress("$gDoneList\@$gListDomain"); }
406                     $data->{done}= $replyto;
407                     my @keywords= split ' ', $data->{keywords};
408                     if (grep $_ eq 'pending', @keywords) {
409                         $extramessage= "Removed pending tag.\n";
410                         $data->{keywords}= join ' ', grep $_ ne 'pending',
411                                                 @keywords;
412                     }
413                     addfixedversions($data, $data->{package}, $version, 'binary');
414
415                     $message= <<END;
416 From: $gMaintainerEmail ($gProject $gBug Tracking System)
417 To: $data->{originator}
418 Subject: $gBug#$ref acknowledged by developer
419          ($header{'subject'})
420 References: $header{'message-id'} $data->{msgid}
421 In-Reply-To: $data->{msgid}
422 Message-ID: <handler.$ref.$nn.notifdonectrl.$midix\@$gEmailDomain>
423 Reply-To: $ref\@$gEmailDomain
424 X-$gProject-PR-Message: they-closed-control $ref
425
426 This is an automatic notification regarding your $gBug report
427 #$ref: $data->{subject},
428 which was filed against the $data->{package} package.
429
430 It has been marked as closed by one of the developers, namely
431 $replyto.
432
433 You should be hearing from them with a substantive response shortly,
434 in case you haven't already. If not, please contact them directly.
435
436 $gMaintainer
437 (administrator, $gProject $gBugs database)
438
439 END
440                     &sendmailmessage($message,$data->{originator});
441                 } while (&getnextbug);
442             }
443         }
444     } elsif (m/^reassign\s+\#?(-?\d+)\s+(\S+)(?:\s+(\d.*))?$/i) {
445         $ok++;
446         $ref= $1; $newpackage= $2;
447         $bug_affected{$ref}=1;
448         $version= $3;
449         $newpackage =~ y/A-Z/a-z/;
450         if (&setbug) {
451             if (length($data->{package})) {
452                 $action= "$gBug reassigned from package \`$data->{package}'".
453                          " to \`$newpackage'.";
454             } else {
455                 $action= "$gBug assigned to package \`$newpackage'.";
456             }
457             do {
458                 &addmaintainers($data);
459                 $data->{package}= $newpackage;
460                 $data->{found_versions}= [];
461                 $data->{fixed_versions}= [];
462                 # TODO: what if $newpackage is a source package?
463                 addfoundversions($data, $data->{package}, $version, 'binary');
464                 &addmaintainers($data);
465             } while (&getnextbug);
466         }
467     } elsif (m/^reopen\s+\#?(-?\d+)$/i ? ($noriginator='', 1) :
468              m/^reopen\s+\#?(-?\d+)\s+\=$/i ? ($noriginator='', 1) :
469              m/^reopen\s+\#?(-?\d+)\s+\!$/i ? ($noriginator=$replyto, 1) :
470              m/^reopen\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($noriginator=$2, 1) : 0) {
471         $ok++;
472         $ref= $1;
473         $bug_affected{$ref}=1;
474         if (&setbug) {
475             if (@{$data->{fixed_versions}}) {
476                 &transcript("'reopen' is deprecated when a bug has been closed with a version;\nuse 'found' or 'submitter' as appropriate instead.\n");
477             }
478             if (!length($data->{done})) {
479                 &transcript("$gBug is already open, cannot reopen.\n\n");
480                 &nochangebug;
481             } else {
482                 $action=
483                     $noriginator eq '' ? "$gBug reopened, originator not changed." :
484                         "$gBug reopened, originator set to $noriginator.";
485                 do {
486                     &addmaintainers($data);
487                     $data->{originator}= $noriginator eq '' ?  $data->{originator} : $noriginator;
488                     $data->{fixed_versions}= [];
489                     $data->{done}= '';
490                 } while (&getnextbug);
491             }
492         }
493     } elsif (m/^found\s+\#?(-?\d+)(?:\s+(\d.*))?$/i) {
494         $ok++;
495         $ref= $1;
496         $version= $2;
497         if (&setbug) {
498             if (!length($data->{done}) and not defined($version)) {
499                 &transcript("$gBug is already open, cannot reopen.\n\n");
500                 &nochangebug;
501             } else {
502                 $action=
503                     defined($version) ?
504                         "$gBug marked as found in version $version." :
505                         "$gBug reopened.";
506                 do {
507                     &addmaintainers($data);
508                     # The 'done' field gets a bit weird with version
509                     # tracking, because a bug may be closed by multiple
510                     # people in different branches. Until we have something
511                     # more flexible, we set it every time a bug is fixed,
512                     # and clear it precisely when a found command is
513                     # received for the rightmost fixed-in version, which
514                     # equates to the most recent fixing of the bug, or when
515                     # a versionless found command is received.
516                     if (defined $version) {
517                         my $lastfixed = $data->{fixed_versions}[-1];
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         if ($bugnum =~ m/^-\d+$/ && defined $clonebugs{$bugnum}) {
724              $bugnum = $clonebugs{$bugnum};
725         }
726
727         my @okayblockers;
728         my @badblockers;
729         foreach my $b (split /[\s,]+/, $blockers) {
730             $b=~s/^\#//;
731             if ($b=~/[0-9]+/) {
732                 $ref=$b;
733                 if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
734                      $ref = $clonebugs{$ref};
735                 }
736                 if (&getbug) {
737                     push @okayblockers, $ref;
738
739                     # add to the list all bugs that are merged with $b,
740                     # because all of their data must be kept in sync
741                     @thisbugmergelist= split(/ /,$data->{mergedwith});
742                     &cancelbug;
743
744                     foreach $ref (@thisbugmergelist) {
745                         if (&getbug) {
746                            push @okayblockers, $ref;
747                            &cancelbug;
748                         }
749                     }
750                 }
751                 else {
752                     &notfoundbug;
753                     push @badblockers, $ref;
754                 }
755             }
756             else {
757                 push @badblockers, $b;
758             }
759         }
760         if (@badblockers) {
761             &transcript("Unknown blocking bug/s: ".join(', ', @badblockers).".\n");
762         }
763         
764         $ref=$bugnum;
765         if (&setbug) {
766             if ($data->{blockedby} eq '') {
767                 &transcript("Was not blocked by any bugs.\n");
768             } else {
769                 &transcript("Was blocked by: $data->{blockedby}\n");
770             }
771             if ($addsub eq "set") {
772                 $action= "Blocking bugs of $bugnum set to: " . join(", ", @okayblockers);
773             } elsif ($addsub eq "add") {
774                 $action= "Blocking bugs of $bugnum added: " . join(", ", @okayblockers);
775             } elsif ($addsub eq "sub") {
776                 $action= "Blocking bugs of $bugnum removed: " . join(", ", @okayblockers);
777             }
778             my %removedblocks;
779             my %addedblocks;
780             do {
781                 &addmaintainers($data);
782                 my @oldblockerlist = split ' ', $data->{blockedby};
783                 $data->{blockedby} = '' if ($addsub eq "set");
784                 foreach my $b (@okayblockers) {
785                         $data->{blockedby} = manipset($data->{blockedby}, $b,
786                                 ($addsub ne "sub"));
787                 }
788
789                 foreach my $b (@oldblockerlist) {
790                         if (! grep { $_ eq $b } split ' ', $data->{blockedby}) {
791                                 push @{$removedblocks{$b}}, $ref;
792                         }
793                 }
794                 foreach my $b (split ' ', $data->{blockedby}) {
795                         if (! grep { $_ eq $b } @oldblockerlist) {
796                                 push @{$addedblocks{$b}}, $ref;
797                         }
798                 }
799             } while (&getnextbug);
800
801             # Now that the blockedby data is updated, change blocks data
802             # to match the changes.
803             foreach $ref (keys %addedblocks) {
804                 if (&getbug) {
805                     foreach my $b (@{$addedblocks{$ref}}) {
806                         $data->{blocks} = manipset($data->{blocks}, $b, 1);
807                     }
808                     &savebug;
809                 }
810             }
811             foreach $ref (keys %removedblocks) {
812                 if (&getbug) {
813                     foreach my $b (@{$removedblocks{$ref}}) {
814                         $data->{blocks} = manipset($data->{blocks}, $b, 0);
815                     }
816                     &savebug;
817                 }
818             }
819         }
820     } elsif (m/^retitle\s+\#?(-?\d+)\s+(\S.*\S)\s*$/i) {
821         $ok++;
822         $ref= $1; $newtitle= $2;
823         $bug_affected{$ref}=1;
824         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
825             $ref = $clonebugs{$ref};
826         }
827         if (&getbug) {
828             if (&checkpkglimit) {
829                 &foundbug;
830                 &addmaintainers($data);
831                 $data->{subject}= $newtitle;
832                 $action= "Changed $gBug title.";
833                 &savebug;
834                 &transcript("$action\n");
835                 if (length($data->{done})) {
836                     &transcript("(By the way, that $gBug is currently marked as done.)\n");
837                 }
838                 &transcript("\n");
839             } else {
840                 &cancelbug;
841             }
842         } else {
843             &notfoundbug;
844         }
845     } elsif (m/^unmerge\s+\#?(-?\d+)$/i) {
846         $ok++;
847         $ref= $1;
848         $bug_affected{$ref} = 1;
849         if (&setbug) {
850             if (!length($data->{mergedwith})) {
851                 &transcript("$gBug is not marked as being merged with any others.\n\n");
852                 &nochangebug;
853             } else {
854                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
855                 $action= "Disconnected #$ref from all other report(s).";
856                 @newmergelist= split(/ /,$data->{mergedwith});
857                 $discref= $ref;
858                 @bug_affected{@newmergelist} = 1 x @newmergelist;
859                 do {
860                     &addmaintainers($data);
861                     $data->{mergedwith}= ($ref == $discref) ? ''
862                         : join(' ',grep($_ ne $ref,@newmergelist));
863                 } while (&getnextbug);
864             }
865         }
866     } elsif (m/^merge\s+#?(-?\d+(\s+#?-?\d+)+)\s*$/i) {
867         $ok++;
868         my @tomerge= sort { $a <=> $b } split(/\s+#?/,$1);
869         my @newmergelist= ();
870         my %tags = ();
871         my %found = ();
872         my %fixed = ();
873         &getmerge;
874         while (defined($ref= shift(@tomerge))) {
875             &transcript("D| checking merge $ref\n") if $dl;
876             $ref+= 0;
877             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
878                 $ref = $clonebugs{$ref};
879             }
880             next if grep($_ == $ref,@newmergelist);
881             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
882             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
883             &foundbug;
884             &transcript("D| adding $ref ($data->{mergedwith})\n") if $dl;
885             $mismatch= '';
886             &checkmatch('package','m_package',$data->{package},@newmergelist);
887             &checkmatch('forwarded addr','m_forwarded',$data->{forwarded},@newmergelist);
888             $data->{severity} = '$gDefaultSeverity' if $data->{severity} eq '';
889             &checkmatch('severity','m_severity',$data->{severity},@newmergelist);
890             &checkmatch('blocks','m_blocks',$data->{blocks},@newmergelist);
891             &checkmatch('blocked-by','m_blockedby',$data->{blockedby},@newmergelist);
892             &checkmatch('done mark','m_done',length($data->{done}) ? 'done' : 'open',@newmergelist);
893             &checkmatch('owner','m_owner',$data->{owner},@newmergelist);
894             foreach my $t (split /\s+/, $data->{keywords}) { $tags{$t} = 1; }
895             foreach my $f (@{$data->{found_versions}}) { $found{$f} = 1; }
896             foreach my $f (@{$data->{fixed_versions}}) { $fixed{$f} = 1; }
897             if (length($mismatch)) {
898                 &transcript("Mismatch - only $gBugs in same state can be merged:\n".
899                             $mismatch."\n");
900                 &cancelbug; @newmergelist=(); last;
901             }
902             push(@newmergelist,$ref);
903             push(@tomerge,split(/ /,$data->{mergedwith}));
904             &cancelbug;
905         }
906         if (@newmergelist) {
907             @newmergelist= sort { $a <=> $b } @newmergelist;
908             $action= "Merged @newmergelist.";
909             delete @fixed{keys %found};
910             for $ref (@newmergelist) {
911                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
912                 &addmaintainers($data);
913                 @bug_affected{@newmergelist} = 1 x @newmergelist;
914                 $data->{mergedwith}= join(' ',grep($_ != $ref,@newmergelist));
915                 $data->{keywords}= join(' ', keys %tags);
916                 $data->{found_versions}= [sort keys %found];
917                 $data->{fixed_versions}= [sort keys %fixed];
918                 &savebug;
919             }
920             &transcript("$action\n\n");
921         }
922         &endmerge;
923     } elsif (m/^forcemerge\s+\#?(-?\d+(?:\s+\#?-?\d+)+)\s*$/i) {
924         $ok++;
925         my @temp = split /\s+\#?/,$1;
926         my $master_bug = shift @temp;
927         my $master_bug_data;
928         my @tomerge = sort { $a <=> $b } @temp;
929         unshift @tomerge,$master_bug;
930         &transcript("D| force merging ".join(',',@tomerge)."\n") if $dl;
931         my @newmergelist= ();
932         my %tags = ();
933         my %found = ();
934         my %fixed = ();
935         # Here we try to do the right thing.
936         # First, if the bugs are in the same package, we merge all of the found, fixed, and tags.
937         # If not, we discard the found and fixed.
938         # Everything else we set to the values of the first bug.
939         &getmerge;
940         while (defined($ref= shift(@tomerge))) {
941             &transcript("D| checking merge $ref\n") if $dl;
942             $ref+= 0;
943             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
944                 $ref = $clonebugs{$ref};
945             }
946             next if grep($_ == $ref,@newmergelist);
947             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
948             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
949             &foundbug;
950             &transcript("D| adding $ref ($data->{mergedwith})\n") if $dl;
951             $master_bug_data = $data if not defined $master_bug_data;
952             if ($data->{package} ne $master_bug_data->{package}) {
953                 &transcript("Mismatch - only $gBugs in the same package can be forcibly merged:\n".
954                             "$gBug $ref is not in the same package as $master_bug\n");
955                 &cancelbug; @newmergelist=(); last;
956             }
957             for my $t (split /\s+/,$data->{keywords}) {
958                  $tags{$t} = 1;
959             }
960             @found{@{$data->{found_versions}}} = (1) x @{$data->{found_versions}};
961             @fixed{@{$data->{fixed_versions}}} = (1) x @{$data->{fixed_versions}};
962             push(@newmergelist,$ref);
963             push(@tomerge,split(/ /,$data->{mergedwith}));
964             &cancelbug;
965         }
966         if (@newmergelist) {
967             @newmergelist= sort { $a <=> $b } @newmergelist;
968             $action= "Forcibly Merged @newmergelist.";
969             delete @fixed{keys %found};
970             for $ref (@newmergelist) {
971                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
972                 &addmaintainers($data);
973                 @bug_affected{@newmergelist} = 1 x @newmergelist;
974                 $data->{mergedwith}= join(' ',grep($_ != $ref,@newmergelist));
975                 $data->{keywords}= join(' ', keys %tags);
976                 $data->{found_versions}= [sort keys %found];
977                 $data->{fixed_versions}= [sort keys %fixed];
978                 my @field_list = qw(forwarded package severity blocks blockedby owner done);
979                 @{$data}{@field_list} = @{$master_bug_data}{@field_list};
980                 &savebug;
981             }
982             &transcript("$action\n\n");
983         }
984         &endmerge;
985     } elsif (m/^clone\s+#?(\d+)\s+((-\d+\s+)*-\d+)\s*$/i) {
986         $ok++;
987
988         $origref = $1;
989         @newclonedids = split /\s+/, $2;
990         $newbugsneeded = scalar(@newclonedids);
991
992         $ref = $origref;
993         $bug_affected{$ref} = 1;
994         if (&setbug) {
995             if (length($data->{mergedwith})) {
996                 &transcript("$gBug is marked as being merged with others.\n\n");
997                 &nochangebug;
998             } else {
999                 &filelock("nextnumber.lock");
1000                 open(N,"nextnumber") || &quit("nextnumber: read: $!");
1001                 $v=<N>; $v =~ s/\n$// || &quit("nextnumber bad format");
1002                 $firstref= $v+0;  $v += $newbugsneeded;
1003                 open(NN,">nextnumber"); print NN "$v\n"; close(NN);
1004                 &unfilelock;
1005
1006                 $lastref = $firstref + $newbugsneeded - 1;
1007
1008                 if ($newbugsneeded == 1) {
1009                     $action= "$gBug $origref cloned as bug $firstref.";
1010                 } else {
1011                     $action= "$gBug $origref cloned as bugs $firstref-$lastref.";
1012                 }
1013
1014                 my $blocks = $data->{blocks};
1015                 my $blockedby = $data->{blockedby};
1016                 
1017                 &getnextbug;
1018                 my $ohash = get_hashname($origref);
1019                 my $clone = $firstref;
1020                 @bug_affected{@newclonedids} = 1 x @newclonedids;
1021                 for $newclonedid (@newclonedids) {
1022                     $clonebugs{$newclonedid} = $clone;
1023             
1024                     my $hash = get_hashname($clone);
1025                     copy("db-h/$ohash/$origref.log", "db-h/$hash/$clone.log");
1026                     copy("db-h/$ohash/$origref.status", "db-h/$hash/$clone.status");
1027                     copy("db-h/$ohash/$origref.summary", "db-h/$hash/$clone.summary");
1028                     copy("db-h/$ohash/$origref.report", "db-h/$hash/$clone.report");
1029                     &bughook('new', $clone, $data);
1030                 
1031                     # Update blocking info of bugs blocked by or blocking the
1032                     # cloned bug.
1033                     foreach $ref (split ' ', $blocks) {
1034                         &getbug;
1035                         $data->{blockedby} = manipset($data->{blockedby}, $clone, 1);
1036                         &savebug;
1037                     }
1038                     foreach $ref (split ' ', $blockedby) {
1039                         &getbug;
1040                         $data->{blocks} = manipset($data->{blocks}, $clone, 1);
1041                         &savebug;
1042                     }
1043
1044                     $clone++;
1045                 }
1046             }
1047         }
1048     } elsif (m/^package\s+(\S.*\S)?\s*$/i) {
1049         $ok++;
1050         my @pkgs = split /\s+/, $1;
1051         if (scalar(@pkgs) > 0) {
1052                 %limit_pkgs = map { ($_, 1) } @pkgs;
1053                 &transcript("Ignoring bugs not assigned to: " . 
1054                         join(" ", keys(%limit_pkgs)) . "\n\n");
1055         } else {
1056                 %limit_pkgs = ();
1057                 &transcript("Not ignoring any bugs.\n\n");
1058         }
1059     } elsif (m/^owner\s+\#?(-?\d+)\s+!$/i ? ($newowner = $replyto, 1) :
1060              m/^owner\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($newowner = $2, 1) : 0) {
1061         $ok++;
1062         $ref = $1;
1063         $bug_affected{$ref} = 1;
1064         if (&setbug) {
1065             if (length $data->{owner}) {
1066                 $action = "Owner changed from $data->{owner} to $newowner.";
1067             } else {
1068                 $action = "Owner recorded as $newowner.";
1069             }
1070             if (length $data->{done}) {
1071                 $extramessage = "(By the way, this $gBug is currently " .
1072                                 "marked as done.)\n";
1073             }
1074             do {
1075                 &addmaintainers($data);
1076                 $data->{owner} = $newowner;
1077             } while (&getnextbug);
1078         }
1079     } elsif (m/^noowner\s+\#?(-?\d+)$/i) {
1080         $ok++;
1081         $ref = $1;
1082         $bug_affected{$ref} = 1;
1083         if (&setbug) {
1084             if (length $data->{owner}) {
1085                 $action = "Removed annotation that $gBug was owned by " .
1086                           "$data->{owner}.";
1087                 do {
1088                     &addmaintainers($data);
1089                     $data->{owner} = '';
1090                 } while (&getnextbug);
1091             } else {
1092                 &transcript("$gBug is not marked as having an owner.\n\n");
1093                 &nochangebug;
1094             }
1095         }
1096     } else {
1097         &transcript("Unknown command or malformed arguments to command.\n\n");
1098         if (++$unknowns >= 5) {
1099             &transcript("Too many unknown commands, stopping here.\n\n");
1100             last;
1101         }
1102     }
1103 }
1104 if ($procline>$#bodylines) {
1105     &transcript(">\nEnd of message, stopping processing here.\n\n");
1106 }
1107 if (!$ok && !quickabort) {
1108     &transcript("No commands successfully parsed; sending the help text(s).\n");
1109     &sendhelp;
1110     &transcript("\n");
1111 }
1112
1113 &transcript("MC\n") if $dl>1;
1114 @maintccs= ();
1115 for $maint (keys %maintccreasons) {
1116 &transcript("MM|$maint|\n") if $dl>1;
1117     next if $maint eq $replyto;
1118     $reasonstring= '';
1119     $reasonsref= $maintccreasons{$maint};
1120 &transcript("MY|$maint|\n") if $dl>2;
1121     for $p (sort keys %$reasonsref) {
1122 &transcript("MP|$p|\n") if $dl>2;
1123         $reasonstring.= ', ' if length($reasonstring);
1124         $reasonstring.= $p.' ' if length($p);
1125         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
1126     }
1127     if (length($reasonstring) > 40) {
1128         (substr $reasonstring, 37) = "...";
1129     }
1130     $reasonstring = "" if (!defined($reasonstring));
1131     push(@maintccs,"$maint ($reasonstring)");
1132     push(@maintccaddrs,"$maint");
1133 }
1134
1135 $maintccs = ""; 
1136 if (@maintccs) {
1137     &transcript("MC|@maintccs|\n") if $dl>2;
1138     $maintccs .= "Cc: " . join(",\n    ",@maintccs) . "\n";
1139 }
1140
1141 # Add Bcc's to subscribed bugs
1142 push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
1143
1144 if (!defined $header{'subject'} || $header{'subject'} eq "") {
1145   $header{'subject'} = "your mail";
1146 }
1147
1148 $reply= <<END;
1149 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1150 To: $replyto
1151 ${maintccs}Subject: Processed: $header{'subject'}
1152 In-Reply-To: $header{'message-id'}
1153 References: $header{'message-id'}
1154 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
1155 Precedence: bulk
1156 X-$gProject-PR-Message: transcript
1157
1158 ${transcript}Please contact me if you need assistance.
1159
1160 $gMaintainer
1161 (administrator, $gProject $gBugs database)
1162 $extras
1163 END
1164
1165 $repliedshow= join(', ',$replyto,@maintccaddrs);
1166 &filelock("lock/-1");
1167 open(AP,">>db-h/-1.log") || &quit("open db-h/-1.log: $!");
1168 print(AP
1169       "\2\n$repliedshow\n\5\n$reply\n\3\n".
1170       "\6\n".
1171       "<strong>Request received</strong> from <code>".
1172       &sani($header{'from'})."</code>\n".
1173       "to <code>".&sani($controlrequestaddr)."</code>\n".
1174       "\3\n".
1175       "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/-1.log: $!");
1176 close(AP) || &quit("open db-h/-1.log: $!");
1177 &unfilelock;
1178 utime(time,time,"db-h");
1179
1180 &sendmailmessage($reply,exists $header{'x-debbugs-no-ack'}?():$replyto,@maintccaddrs,@bcc);
1181
1182 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
1183
1184 sub sendmailmessage {
1185     local ($message,@recips) = @_;
1186     $message = "X-Loop: $gMaintainerEmail\n" . $message;
1187     send_mail_message(message    => $message,
1188                       recipients => \@recips,
1189                      );
1190     $midix++;
1191 }
1192
1193 sub sendhelp {
1194         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
1195         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
1196             if $control;
1197 }
1198
1199 #sub unimplemented {
1200 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
1201 #}
1202
1203 sub checkmatch {
1204     local ($string,$mvarname,$svarvalue,@newmergelist) = @_;
1205     local ($mvarvalue);
1206     if (@newmergelist) {
1207         eval "\$mvarvalue= \$$mvarname";
1208         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
1209             if $dl;
1210         $mismatch .=
1211             "Values for \`$string' don't match:\n".
1212             " #$newmergelist[0] has \`$mvarvalue';\n".
1213             " #$ref has \`$svarvalue'\n"
1214             if $mvarvalue ne $svarvalue;
1215     } else {
1216         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
1217             if $dl;
1218         eval "\$$mvarname= \$svarvalue";
1219     }
1220 }
1221
1222 sub checkpkglimit {
1223     if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1224         &transcript("$gBug number $ref belongs to package $data->{package}, skipping.\n\n");
1225         return 0;
1226     }
1227     return 1;
1228 }
1229
1230 sub manipset {
1231     my $list = shift;
1232     my $elt = shift;
1233     my $add = shift;
1234
1235     my %h = map { $_ => 1 } split ' ', $list;
1236     if ($add) {
1237         $h{$elt}=1;
1238     }
1239     else {
1240         delete $h{$elt};
1241     }
1242     return join ' ', sort keys %h;
1243 }
1244
1245 # High-level bug manipulation calls
1246 # Do announcements themselves
1247 #
1248 # Possible calling sequences:
1249 #    setbug (returns 0)
1250 #    
1251 #    setbug (returns 1)
1252 #    &transcript(something)
1253 #    nochangebug
1254 #
1255 #    setbug (returns 1)
1256 #    $action= (something)
1257 #    do {
1258 #      (modify s_* variables)
1259 #    } while (getnextbug);
1260
1261 sub nochangebug {
1262     &dlen("nochangebug");
1263     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1264     &cancelbug;
1265     &endmerge if $manybugs;
1266     $state= 'idle';
1267     &dlex("nochangebug");
1268 }
1269
1270 sub setbug {
1271     &dlen("setbug $ref");
1272     if ($ref =~ m/^-\d+/) {
1273         if (!defined $clonebugs{$ref}) {
1274             &notfoundbug;
1275             &dlex("setbug => noclone");
1276             return 0;
1277         }
1278         $ref = $clonebugs{$ref};
1279     }
1280     $state eq 'idle' || die "$state ?";
1281     if (!&getbug) {
1282         &notfoundbug;
1283         &dlex("setbug => 0s");
1284         return 0;
1285     }
1286
1287     if (!&checkpkglimit) {
1288         &cancelbug;
1289         return 0;
1290     }
1291
1292     @thisbugmergelist= split(/ /,$data->{mergedwith});
1293     if (!@thisbugmergelist) {
1294         &foundbug;
1295         $manybugs= 0;
1296         $state= 'single';
1297         $sref=$ref;
1298         &dlex("setbug => 1s");
1299         return 1;
1300     }
1301     &cancelbug;
1302     &getmerge;
1303     $manybugs= 1;
1304     if (!&getbug) {
1305         &notfoundbug;
1306         &endmerge;
1307         &dlex("setbug => 0mc");
1308         return 0;
1309     }
1310     &foundbug;
1311     $state= 'multiple'; $sref=$ref;
1312     &dlex("setbug => 1m");
1313     return 1;
1314 }
1315
1316 sub getnextbug {
1317     &dlen("getnextbug");
1318     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1319     &savebug;
1320     if (!$manybugs || !@thisbugmergelist) {
1321         length($action) || die;
1322         &transcript("$action\n$extramessage\n");
1323         &endmerge if $manybugs;
1324         $state= 'idle';
1325         &dlex("getnextbug => 0");
1326         return 0;
1327     }
1328     $ref= shift(@thisbugmergelist);
1329     &getbug || die "bug $ref disappeared";
1330     &foundbug;
1331     &dlex("getnextbug => 1");
1332     return 1;
1333 }
1334
1335 # Low-level bug-manipulation calls
1336 # Do no announcements
1337 #
1338 #    getbug (returns 0)
1339 #
1340 #    getbug (returns 1)
1341 #    cancelbug
1342 #
1343 #    getmerge
1344 #    $action= (something)
1345 #    getbug (returns 1)
1346 #    savebug/cancelbug
1347 #    getbug (returns 1)
1348 #    savebug/cancelbug
1349 #    [getbug (returns 0)]
1350 #    &transcript("$action\n\n")
1351 #    endmerge
1352
1353 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
1354 sub foundbug { &transcript("$gBug#$ref: $data->{subject}\n"); }
1355
1356 sub getmerge {
1357     &dlen("getmerge");
1358     $mergelowstate eq 'idle' || die "$mergelowstate ?";
1359     &filelock('lock/merge');
1360     $mergelowstate='locked';
1361     &dlex("getmerge");
1362 }
1363
1364 sub endmerge {
1365     &dlen("endmerge");
1366     $mergelowstate eq 'locked' || die "$mergelowstate ?";
1367     &unfilelock;
1368     $mergelowstate='idle';
1369     &dlex("endmerge");
1370 }
1371
1372 sub getbug {
1373     &dlen("getbug $ref");
1374     $lowstate eq 'idle' || die "$state ?";
1375     if (($data = &lockreadbug($ref))) {
1376         $sref= $ref;
1377         $lowstate= "open";
1378         &dlex("getbug => 1");
1379         $extramessage='';
1380         return 1;
1381     }
1382     $lowstate= 'idle';
1383     &dlex("getbug => 0");
1384     return 0;
1385 }
1386
1387 sub cancelbug {
1388     &dlen("cancelbug");
1389     $lowstate eq 'open' || die "$state ?";
1390     &unfilelock;
1391     $lowstate= 'idle';
1392     &dlex("cancelbug");
1393 }
1394
1395 sub savebug {
1396     &dlen("savebug $ref");
1397     $lowstate eq 'open' || die "$lowstate ?";
1398     length($action) || die;
1399     $ref == $sref || die "read $sref but saving $ref ?";
1400     my $hash = get_hashname($ref);
1401     open(L,">>db-h/$hash/$ref.log") || &quit("opening db-h/$hash/$ref.log: $!");
1402     print(L
1403           "\6\n".
1404           "<strong>".&sani($action)."</strong>\n".
1405           "Request was from <code>".&sani($header{'from'})."</code>\n".
1406           "to <code>".&sani($controlrequestaddr)."</code>. \n".
1407           "\3\n".
1408           "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/$hash/$ref.log: $!");
1409     close(L) || &quit("closing db-h/$hash/$ref.log: $!");
1410     unlockwritebug($ref, $data);
1411     $lowstate= "idle";
1412     &dlex("savebug");
1413 }
1414
1415 sub dlen {
1416     return if !$dl;
1417     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
1418 }
1419
1420 sub dlex {
1421     return if !$dl;
1422     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
1423 }
1424
1425 sub transcript {
1426     print $_[0] if $debug;
1427     $transcript.= $_[0];
1428 }
1429
1430 sub urlsanit {
1431     my $url = shift;
1432     $url =~ s/%/%25/g;
1433     $url =~ s/\+/%2b/g;
1434     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1435     $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1436     return $url;
1437 }
1438
1439 sub sendlynxdoc {
1440     &sendlynxdocraw;
1441     &transcript("\n");
1442     $ok++;
1443 }
1444
1445 sub sendtxthelp {
1446     &sendtxthelpraw;
1447     &transcript("\n");
1448     $ok++;
1449 }
1450
1451 sub sendtxthelpraw {
1452     local ($relpath,$description) = @_;
1453     $doc='';
1454     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
1455     while(<D>) { $doc.=$_; }
1456     close(D);
1457     &transcript("Sending $description in separate message.\n");
1458     &sendmailmessage(<<END.$doc,$replyto);
1459 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1460 To: $replyto
1461 Subject: $gProject $gBug help: $description
1462 References: $header{'message-id'}
1463 In-Reply-To: $header{'message-id'}
1464 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1465 Precedence: bulk
1466 X-$gProject-PR-Message: doc-text $relpath
1467
1468 END
1469     $ok++;
1470 }
1471
1472 sub sendlynxdocraw {
1473     local ($relpath,$description) = @_;
1474     $doc='';
1475     open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || &quit("fork for lynx: $!");
1476     while(<L>) { $doc.=$_; }
1477     $!=0; close(L);
1478     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1479         &transcript("Information ($description) is not available -\n".
1480                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
1481          $ok++;
1482     } elsif ($?) {
1483         &transcript("Error getting $description (code $? $!):\n$doc\n");
1484     } else {
1485         &transcript("Sending $description.\n");
1486         &sendmailmessage(<<END.$doc,$replyto);
1487 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1488 To: $replyto
1489 Subject: $gProject $gBugs information: $description
1490 References: $header{'message-id'}
1491 In-Reply-To: $header{'message-id'}
1492 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1493 Precedence: bulk
1494 X-$gProject-PR-Message: doc-html $relpath
1495
1496 END
1497          $ok++;
1498     }
1499 }
1500
1501 sub addccaddress {
1502     my ($cca) = @_;
1503     $maintccreasons{$cca}{''}{$ref}= 1;
1504 }
1505
1506 sub addmaintainers {
1507     # Data structure is:
1508     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
1509     my $data = shift;
1510     my ($p, $addmaint);
1511     &ensuremaintainersloaded;
1512     $anymaintfound=0; $anymaintnotfound=0;
1513     for $p (split(m/[ \t?,():]+/, $data->{package})) {
1514         $p =~ y/A-Z/a-z/;
1515         $p =~ /([a-z0-9.+-]+)/;
1516         $p = $1;
1517         next unless defined $p;
1518         if (defined $gSubscriptionDomain) {
1519             if (defined($pkgsrc{$p})) {
1520                 addbcc("$pkgsrc{$p}\@$gSubscriptionDomain");
1521             } else {
1522                 addbcc("$p\@$gSubscriptionDomain");
1523             }
1524         }
1525         if (defined $data->{severity} and defined $gStrongList and
1526                 isstrongseverity($data->{severity})) {
1527             addbcc("$gStrongList\@$gListDomain");
1528         }
1529         if (defined($maintainerof{$p})) {
1530             $addmaint= $maintainerof{$p};
1531             &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
1532             $maintccreasons{$addmaint}{$p}{$ref}= 1;
1533             print "maintainer add >$p|$addmaint<\n" if $debug;
1534         } else { 
1535             print "maintainer none >$p<\n" if $debug; 
1536             &transcript("Warning: Unknown package '$p'\n");
1537             &transcript("MR|unknown-package|$p|$ref|\n") if $dl>2;
1538             $maintccreasons{$gUnknownMaintainerEmail}{$p}{$ref}= 1;
1539         }
1540     }
1541
1542     if (length $data->{owner}) {
1543         $addmaint = $data->{owner};
1544         &transcript("MO|$addmaint|$data->{package}|$ref|\n") if $dl>2;
1545         $maintccreasons{$addmaint}{$data->{package}}{$ref} = 1;
1546         print "owner add >$data->{package}|$addmaint<\n" if $debug;
1547     }
1548 }
1549
1550 sub ensuremaintainersloaded {
1551     my ($a,$b);
1552     return if $maintainersloaded++;
1553     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
1554     while (<MAINT>) {
1555         m/^\n$/ && next;
1556         m/^\s*$/ && next;
1557         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers bogus \`$_'");
1558         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1559         $maintainerof{$a}= $2;
1560     }
1561     close(MAINT);
1562     open(MAINT,"$gMaintainerFileOverride") || die &quit("maintainers.override open: $!");
1563     while (<MAINT>) {
1564         m/^\n$/ && next;
1565         m/^\s*$/ && next;
1566         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers.override bogus \`$_'");
1567         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1568         $maintainerof{$a}= $2;
1569     }
1570
1571     open(SOURCES, "$gPackageSource") || &quit("pkgsrc open: $!");
1572     while (<SOURCES>) {
1573         next unless m/^(\S+)\s+\S+\s+(\S.*\S)\s*$/;
1574         my ($a, $b) = ($1, $2);
1575         $pkgsrc{lc($a)} = $b;
1576     }
1577     close(SOURCES);
1578 }
1579
1580 sub sendinfo {
1581     local ($wherefrom,$path,$description) = @_;
1582     if ($wherefrom eq "ftp.d.o") {
1583       $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or &quit("fork for lynx/gunzip: $!");
1584       $! = 0;
1585       if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1586           &transcript("$description is not available.\n");
1587           $ok++; return;
1588       } elsif ($?) {
1589           &transcript("Error getting $description (code $? $!):\n$doc\n");
1590           return;
1591       }
1592     } elsif ($wherefrom eq "local") {
1593       open P, "$path";
1594       $doc = do { local $/; <P> };
1595       close P;
1596     } else {
1597       &transcript("internal errror: info files location unknown.\n");
1598       $ok++; return;
1599     }
1600     &transcript("Sending $description.\n");
1601     &sendmailmessage(<<END.$doc,$replyto);
1602 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1603 To: $replyto
1604 Subject: $gProject $gBugs information: $description
1605 References: $header{'message-id'}
1606 In-Reply-To: $header{'message-id'}
1607 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1608 Precedence: bulk
1609 X-$gProject-PR-Message: getinfo
1610
1611 $description follows:
1612
1613 END
1614     $ok++;
1615     &transcript("\n");
1616 }