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