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