2 # $Id: service.in,v 1.118 2005/10/19 01:22:14 don Exp $
4 # Usage: service <code>.nn
5 # Temps: incoming/P<code>.nn
11 use Debbugs::Config qw(:globals :config);
16 use Params::Validate qw(:types validate_with);
18 use Debbugs::Common qw(:util :quit :misc :lock);
20 use Debbugs::Status qw(:read :status :write :versions :hook);
21 use Debbugs::Packages qw(binary_to_source);
23 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522 create_mime_message);
24 use Debbugs::Mail qw(send_mail_message);
26 use Debbugs::Recipients qw(:all);
27 use HTML::Entities qw(encode_entities);
28 use Debbugs::Versions::Dpkg;
30 use Debbugs::Status qw(splitpackages);
32 use Debbugs::CGI qw(html_escape);
33 use Debbugs::Control qw(:all);
34 use Debbugs::Log qw(:misc);
35 use Debbugs::Text qw(:templates);
37 use Scalar::Util qw(looks_like_number);
39 use List::Util qw(first);
41 use Mail::RFC822::Address;
42 use Encode qw(decode encode);
44 chdir($config{spool_dir}) or
45 die "Unable to chdir to spool_dir '$config{spool_dir}': $!";
50 my ($nn,$control) = $ARGV[0] =~ m/^(([RC])\.\d+)$/;
51 if (not defined $control or not defined $nn) {
52 die "Bad argument to service.in";
54 if (!rename("incoming/G$nn","incoming/P$nn")) {
55 defined $! and $! =~ m/no such file or directory/i and exit 0;
56 die "Failed to rename incoming/G$nn to incoming/P$nn: $!";
59 my $log_fh = IO::File->new("incoming/P$nn",'r') or
60 die "Unable to open incoming/P$nn for reading: $!";
67 print "###\n",join("##\n",@msg),"\n###\n" if $debug;
69 # Bug numbers to send e-mail to, hash so that we don't send to the
73 my (@headerlines,@bodylines);
75 my $parse_output = Debbugs::MIME::parse(join('',@log));
76 @headerlines = @{$parse_output->{header}};
77 @bodylines = @{$parse_output->{body}};
81 $_ = decode_rfc1522($_);
83 print ">$_<\n" if $debug;
86 print ">$v=$_<\n" if $debug;
89 print "!>$_<\n" if $debug;
92 $header{'message-id'} ||= '';
93 $header{subject} ||= '';
95 grep(s/\s+$//,@bodylines);
97 print "***\n",join("\n",@bodylines),"\n***\n" if $debug;
99 if (defined $header{'resent-from'} && !defined $header{'from'}) {
100 $header{'from'} = $header{'resent-from'};
103 defined($header{'from'}) || die "no From header";
105 delete $header{'reply-to'}
106 if ( defined($header{'reply-to'}) && $header{'reply-to'} =~ m/^\s*$/ );
109 if ( defined($header{'reply-to'}) && $header{'reply-to'} ne "" ) {
110 $replyto = $header{'reply-to'};
112 $replyto = $header{'from'};
115 # This is an error counter which should be incremented every time there is an error.
117 my $controlrequestaddr= ($control ? 'control' : 'request').'@'.$config{email_domain};
118 my $transcript_scalar = '';
119 open my $transcript, ">:scalar:utf8", \$transcript_scalar or
120 die "Unable to create transcript scalar: $!";
121 print {$transcript} "Processing commands for $controlrequestaddr:\n\n";
125 my %affected_packages;
127 # this is the hashref which is passed to all control calls
131 my @common_control_options =
132 (transcript => $transcript,
133 requester => $header{from},
134 request_addr => $controlrequestaddr,
135 request_msgid => $header{'message-id'},
136 request_subject => $header{subject},
138 request_replyto => $replyto,
140 affected_bugs => \%bug_affected,
141 affected_packages => \%affected_packages,
142 recipients => \%recipients,
147 my $lowstate= 'idle';
148 my $mergelowstate= 'idle';
153 $user =~ s/^.*<(.*)>.*$/$1/;
154 $user =~ s/[(].*[)]//;
155 $user =~ s/^\s*(\S+)\s+.*$/$1/;
156 $user = "" unless (Debbugs::User::is_valid_user($user));
157 my $indicated_user = 0;
162 if (@gExcludeFromControl and grep {$replyto =~ m/\Q$_\E/} @gExcludeFromControl) {
163 print {$transcript} fill_template('mail/excluded_from_control');
174 push @bcc, $_[0] unless grep { $_ eq $_[0] } @bcc;
189 for ($procline=0; $procline<=$#bodylines; $procline++) {
194 $state eq 'idle' || print "state: $state ?\n";
195 $lowstate eq 'idle' || print "lowstate: $lowstate ?\n";
196 $mergelowstate eq 'idle' || print "mergelowstate: $mergelowstate ?\n";
198 print {$transcript} "Stopping processing here.\n\n";
201 $_= $bodylines[$procline]; s/\s+$//;
202 # Remove BOM markers from UTF-8 strings
207 my $temp = decode("utf8",$_,Encode::FB_CROAK);
210 print {$transcript} "> $_\n";
213 if (m/^(?:stop|quit|--|thank(?:s|\s*you)?|kthxbye)\.*\s*$/i) {
214 print {$transcript} "Stopping processing here.\n\n";
216 } elsif (m/^debug\s+(\d+)$/i && $1 >= 0 && $1 <= 1000) {
218 if ($dl > 0 and not grep /debug/,@common_control_options) {
219 push @common_control_options,(debug => $transcript);
221 print {$transcript} "Debug level $dl.\n\n";
222 } elsif (m/^(send|get)\s+\#?(\d{2,})$/i) {
224 &sendlynxdoc("bugreport.cgi?bug=$ref","logs for $gBug#$ref");
225 } elsif (m/^send-detail\s+\#?(\d{2,})$/i) {
227 &sendlynxdoc("bugreport.cgi?bug=$ref&boring=yes",
228 "detailed logs for $gBug#$ref");
229 } elsif (m/^index(\s+full)?$/i) {
230 print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
232 $ok++; # well, it's not really ok, but it fixes #81224 :)
233 } elsif (m/^index-summary\s+by-package$/i) {
234 print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
236 $ok++; # well, it's not really ok, but it fixes #81224 :)
237 } elsif (m/^index-summary(\s+by-number)?$/i) {
238 print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
240 $ok++; # well, it's not really ok, but it fixes #81224 :)
241 } elsif (m/^index(\s+|-)pack(age)?s?$/i) {
242 &sendlynxdoc("pkgindex.cgi?indexon=pkg",'index of packages');
243 } elsif (m/^index(\s+|-)maints?$/i) {
244 &sendlynxdoc("pkgindex.cgi?indexon=maint",'index of maintainers');
245 } elsif (m/^index(\s+|-)maint\s+(\S+)$/i) {
247 &sendlynxdoc("pkgreport.cgi?maint=" . urlsanit($maint),
248 "$gBug list for maintainer \`$maint'");
250 } elsif (m/^index(\s+|-)pack(age)?s?\s+(\S.*\S)$/i) {
252 &sendlynxdoc("pkgreport.cgi?pkg=" . urlsanit($package),
253 "$gBug list for package $package");
255 } elsif (m/^send-unmatched(\s+this|\s+-?0)?$/i) {
256 print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
258 $ok++; # well, it's not really ok, but it fixes #81224 :)
259 } elsif (m/^send-unmatched\s+(last|-1)$/i) {
260 print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
262 $ok++; # well, it's not really ok, but it fixes #81224 :)
263 } elsif (m/^send-unmatched\s+(old|-2)$/i) {
264 print {$transcript} "This BTS function is currently disabled, sorry.\n\n";
266 $ok++; # well, it's not really ok, but it fixes #81224 :)
267 } elsif (m/^getinfo\s+([\w.-]+)$/i) {
268 # the following is basically a Debian-specific kludge, but who cares
270 if ($req =~ /^maintainers$/i && -f "$gConfigDir/Maintainers") {
271 &sendinfo("local", "$gConfigDir/Maintainers", "Maintainers file");
272 } elsif ($req =~ /^override\.(\w+)\.([\w.-]+)$/i) {
274 &sendinfo("ftp.d.o", "$req", "override file for $2 part of $1 distribution");
275 } elsif ($req =~ /^pseudo-packages\.(description|maintainers)$/i && -f "$gConfigDir/$req") {
276 &sendinfo("local", "$gConfigDir/$req", "$req file");
278 print {$transcript} "Info file $req does not exist.\n\n";
280 } elsif (m/^help/i) {
282 print {$transcript} "\n";
284 } elsif (m/^refcard/i) {
285 &sendtxthelp("bug-mailserver-refcard.txt","mail servers' reference card");
286 } elsif (m/^subscribe/i) {
287 print {$transcript} <<END;
288 There is no $gProject $gBug mailing list. If you wish to review bug reports
289 please do so via http://$gWebDomain/ or ask this mail server
291 soon: MAILINGLISTS_TEXT
293 } elsif (m/^unsubscribe/i) {
294 print {$transcript} <<END;
295 soon: UNSUBSCRIBE_TEXT
296 soon: MAILINGLISTS_TEXT
298 } elsif (m/^user\s+(\S+)\s*$/i) {
300 if (Debbugs::User::is_valid_user($newuser)) {
301 my $olduser = ($user ne "" ? " (was $user)" : "");
302 print {$transcript} "Setting user to $newuser$olduser.\n";
306 print {$transcript} "Selected user id ($newuser) invalid, sorry\n";
311 } elsif (m/^usercategory\s+(\S+)(\s+\[hidden\])?\s*$/i) {
314 my $hidden = (defined $2 and $2 ne "");
321 print {$transcript} "No valid user selected\n";
325 if (not $indicated_user and defined $user) {
326 print {$transcript} "User is $user\n";
330 while (++$procline <= $#bodylines) {
331 unless ($bodylines[$procline] =~ m/^\s*([*+])\s*(\S.*)$/) {
335 print {$transcript} "> $bodylines[$procline]\n";
337 my ($o, $txt) = ($1, $2);
338 if ($#cats == -1 && $o eq "+") {
339 print {$transcript} "User defined category specification must start with a category name. Skipping.\n\n";
345 unless (ref($cats[-1]) eq "HASH") {
346 $cats[-1] = { "nam" => $cats[-1],
347 "pri" => [], "ttl" => [] };
350 my ($desc, $ord, $op);
351 if ($txt =~ m/^(.*\S)\s*\[((\d+):\s*)?\]\s*$/) {
352 $desc = $1; $ord = $3; $op = "";
353 } elsif ($txt =~ m/^(.*\S)\s*\[((\d+):\s*)?(\S+)\]\s*$/) {
354 $desc = $1; $ord = $3; $op = $4;
355 } elsif ($txt =~ m/^([^[\s]+)\s*$/) {
356 $desc = ""; $op = $1;
358 print {$transcript} "Unrecognised syntax for category section. Skipping.\n\n";
363 $ord = 999 unless defined $ord;
366 push @{$cats[-1]->{"pri"}}, $prefix . $op;
367 push @{$cats[-1]->{"ttl"}}, $desc;
368 push @ords, "$ord $catsec";
370 $cats[-1]->{"def"} = $desc;
371 push @ords, "$ord DEF";
375 my ($a1, $a2, $b1, $b2) = split / /, "$a $b";
376 ((looks_like_number($a1) and looks_like_number($a2))?$a1 <=> $b1:$a1 cmp $b1) ||
377 ((looks_like_number($a2) and looks_like_number($b2))?$a2 <=> $b2:$a2 cmp $b2);
379 $cats[-1]->{"ord"} = [map { m/^.* (\S+)/; $1 eq "DEF" ? $catsec + 1 : $1 } @ords];
380 } elsif ($o eq "*") {
383 if ($txt =~ m/^(.*\S)(\s*\[(\S+)\])\s*$/) {
384 $name = $1; $prefix = $3;
386 $name = $txt; $prefix = "";
391 # XXX: got @cats, now do something with it
392 my $u = Debbugs::User::get_user($user);
394 print {$transcript} "Added usercategory $catname.\n\n";
395 $u->{"categories"}->{$catname} = [ @cats ];
397 push @{$u->{visible_cats}},$catname;
400 print {$transcript} "Removed usercategory $catname.\n\n";
401 delete $u->{"categories"}->{$catname};
402 @{$u->{visible_cats}} = grep {$_ ne $catname} @{$u->{visible_cats}};
405 } elsif (m/^usertags?\s+\#?(-?\d+)\s+(([=+-])\s*)?(\S.*)?$/i) {
408 my $addsubcode = $3 || "+";
410 if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
411 $ref = $clonebugs{$ref};
414 print {$transcript} "No valid user selected\n";
418 if (not $indicated_user and defined $user) {
419 print {$transcript} "User is $user\n";
424 Debbugs::User::read_usertags(\%ut, $user);
425 my @oldtags = (); my @newtags = (); my @badtags = ();
427 if (defined $tags and length $tags) {
428 for my $t (split /[,\s]+/, $tags) {
429 if ($t =~ m/^[a-zA-Z0-9.+\@-]+$/) {
437 print {$transcript} "Ignoring illegal tag/s: ".join(', ', @badtags).".\nPlease use only alphanumerics, at, dot, plus and dash.\n";
440 for my $t (keys %chtags) {
441 $ut{$t} = [] unless defined $ut{$t};
443 for my $t (keys %ut) {
444 my %res = map { ($_, 1) } @{$ut{$t}};
445 push @oldtags, $t if defined $res{$ref};
446 my $addop = ($addsubcode eq "+" or $addsubcode eq "=");
447 my $del = (defined $chtags{$t} ? $addsubcode eq "-"
448 : $addsubcode eq "=");
449 $res{$ref} = 1 if ($addop && defined $chtags{$t});
450 delete $res{$ref} if ($del);
451 push @newtags, $t if defined $res{$ref};
452 $ut{$t} = [ sort { $a <=> $b } (keys %res) ];
455 print {$transcript} "There were no usertags set.\n";
457 print {$transcript} "Usertags were: " . join(" ", @oldtags) . ".\n";
459 print {$transcript} "Usertags are now: " . join(" ", @newtags) . ".\n";
460 Debbugs::User::write_usertags(\%ut, $user);
462 } elsif (!$control) {
463 print {$transcript} <<END;
464 Unknown command or malformed arguments to command.
465 (Use control\@$gEmailDomain to manipulate reports.)
469 if (++$unknowns >= 3) {
470 print {$transcript} "Too many unknown commands, stopping here.\n\n";
473 #### "developer only" ones start here
474 } elsif (m/^close\s+\#?(-?\d+)(?:\s+(\d.*))?$/i) {
477 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
480 set_fixed(@common_control_options,
488 print {$transcript} "Failed to add fixed version '$2' to $ref: ".cleanup_eval_fail($@,$debug)."\n";
492 set_done(@common_control_options,
496 notify_submitter => 1,
502 print {$transcript} "Failed to mark $ref as done: ".cleanup_eval_fail($@,$debug)."\n";
504 } elsif (m/^reassign\s+\#?(-?\d+)\s+ # bug and command
505 (?:(?:((?:src:|source:)?$config{package_name_re}) # new package
506 (?:\s+((?:$config{package_name_re}\/)?
507 $config{package_version_re}))?)| # optional version
508 ((?:src:|source:)?$config{package_name_re} # multiple package form
509 (?:\s*\,\s*(?:src:|source:)?$config{package_name_re})+))
514 if (not defined $2) {
515 push @new_packages, split /\s*\,\s*/,$4;
518 push @new_packages, $2;
520 @new_packages = map {y/A-Z/a-z/; s/^(?:src|source):/src:/; $_;} @new_packages;
521 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
524 set_package(@common_control_options,
526 package => \@new_packages,
528 # if there is a version passed, we make an internal call
530 if (defined($version) && length $version) {
531 set_found(@common_control_options,
539 print {$transcript} "Failed to clear fixed versions and reopen on $ref: ".cleanup_eval_fail($@,$debug)."\n";
541 } elsif (m/^reopen\s+\#?(-?\d+)(?:\s+([\=\!]|(?:\S.*\S)))?$/i) {
544 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
545 my $new_submitter = $2;
546 if (defined $new_submitter) {
547 if ($new_submitter eq '=') {
548 undef $new_submitter;
550 elsif ($new_submitter eq '!') {
551 $new_submitter = $replyto;
555 set_done(@common_control_options,
558 defined $new_submitter? (submitter => $new_submitter):(),
563 print {$transcript} "Failed to reopen $ref: ".cleanup_eval_fail($@,$debug)."\n";
565 } elsif (m{^(?:(?i)found)\s+\#?(-?\d+)
566 (?:\s+((?:$config{package_name_re}\/)?
567 $config{package_version_re}
568 # allow for multiple packages
569 (?:\s*,\s*(?:$config{package_name_re}\/)?
570 $config{package_version_re})*)
574 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
577 @versions = split /\s*,\s*/,$2;
579 set_found(@common_control_options,
587 print {$transcript} "Failed to add found on $ref: ".cleanup_eval_fail($@,$debug)."\n";
592 set_fixed(@common_control_options,
600 print {$transcript} "Failed to clear fixed versions and reopen on $ref: ".cleanup_eval_fail($@,$debug)."\n";
604 elsif (m{^(?:(?i)notfound)\s+\#?(-?\d+)
605 \s+((?:$config{package_name_re}\/)?
606 $config{package_version_re}
607 # allow for multiple packages
608 (?:\s*,\s*(?:$config{package_name_re}\/)?
609 $config{package_version_re})*
613 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
615 @versions = split /\s*,\s*/,$2;
617 set_found(@common_control_options,
625 print {$transcript} "Failed to remove found on $ref: ".cleanup_eval_fail($@,$debug)."\n";
628 elsif (m{^(?:(?i)fixed)\s+\#?(-?\d+)
629 \s+((?:$config{package_name_re}\/)?
630 $config{package_version_re}
631 # allow for multiple packages
632 (?:\s*,\s*(?:$config{package_name_re}\/)?
633 $config{package_version_re})*)
637 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
639 @versions = split /\s*,\s*/,$2;
641 set_fixed(@common_control_options,
649 print {$transcript} "Failed to add fixed on $ref: ".cleanup_eval_fail($@,$debug)."\n";
652 elsif (m{^(?:(?i)notfixed)\s+\#?(-?\d+)
653 \s+((?:$config{package_name_re}\/)?
654 $config{package_version_re}
655 # allow for multiple packages
656 (?:\s*,\s*(?:$config{package_name_re}\/)?
657 $config{package_version_re})*)
661 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
663 @versions = split /\s*,\s*/,$2;
665 set_fixed(@common_control_options,
673 print {$transcript} "Failed to remove fixed on $ref: ".cleanup_eval_fail($@,$debug)."\n";
676 elsif (m/^submitter\s+\#?(-?\d+)\s+(\!|\S.*\S)$/i) {
679 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
680 my $newsubmitter = $2 eq '!' ? $replyto : $2;
681 if (not Mail::RFC822::Address::valid($newsubmitter)) {
682 print {$transcript} "$newsubmitter is not a valid e-mail address; not changing submitter\n";
687 set_submitter(@common_control_options,
689 submitter => $newsubmitter,
694 print {$transcript} "Failed to set submitter on $ref: ".cleanup_eval_fail($@,$debug)."\n";
697 } elsif (m/^forwarded\s+\#?(-?\d+)\s+(\S.*\S)$/i) {
701 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
703 set_forwarded(@common_control_options,
705 forwarded => $forward_to,
710 print {$transcript} "Failed to set the forwarded-to-address of $ref: ".cleanup_eval_fail($@,$debug)."\n";
712 } elsif (m/^notforwarded\s+\#?(-?\d+)$/i) {
715 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
717 set_forwarded(@common_control_options,
724 print {$transcript} "Failed to clear the forwarded-to-address of $ref: ".cleanup_eval_fail($@,$debug)."\n";
726 } elsif (m/^(?:severity|priority)\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i) {
729 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
731 if (exists $gObsoleteSeverities{$newseverity}) {
732 print {$transcript} "Severity level \`$newseverity' is obsolete. " .
733 "Use $gObsoleteSeverities{$newseverity} instead.\n\n";
735 } elsif (not defined first {$_ eq $newseverity}
736 (@gSeverityList, "$gDefaultSeverity")) {
737 print {$transcript} "Severity level \`$newseverity' is not known.\n".
738 "Recognized are: $gShowSeverities.\n\n";
742 set_severity(@common_control_options,
744 severity => $newseverity,
749 print {$transcript} "Failed to set severity of $config{bug} $ref to $newseverity: ".cleanup_eval_fail($@,$debug)."\n";
752 } elsif (m/^tags?\s+\#?(-?\d+)\s+(\S.*)$/i) {
755 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
757 my @tags = map {m/^([+=-])(.+)/ ? ($1,$2):($_)} split /[\s,]+/, $tags;
758 # this is an array of hashrefs which contain two elements, the
759 # first of which is the array of tags, the second is the
760 # option to pass to set_tag (we use a hashref here to make it
761 # more obvious what is happening)
764 for my $tag (@tags) {
765 if ($tag =~ /^[=+-]$/) {
767 @tag_operations = {tags => [],
771 elsif ($tag eq '-') {
772 push @tag_operations,
774 option => [remove => 1],
777 elsif ($tag eq '+') {
778 push @tag_operations,
780 option => [add => 1],
785 if (not defined first {$_ eq $tag} @{$config{tags}}) {
789 if (not @tag_operations) {
790 @tag_operations = {tags => [],
791 option => [add => 1],
794 push @{$tag_operations[-1]{tags}},$tag;
797 print {$transcript} "Unknown tag/s: ".join(', ', @badtags).".\n".
798 "Recognized are: ".join(' ', @gTags).".\n\n";
802 for my $operation (@tag_operations) {
803 set_tag(@common_control_options,
805 tag => [@{$operation->{tags}}],
806 warn_on_bad_tags => 0, # don't warn on bad tags,
807 # 'cause we do that above
808 @{$operation->{option}},
813 # we intentionally have two errors here if there is a bad
814 # tag and the above fails for some reason
816 print {$transcript} "Failed to alter tags of $config{bug} $ref: ".cleanup_eval_fail($@,$debug)."\n";
818 } elsif (m/^(un)?block\s+\#?(-?\d+)\s+(?:by|with)\s+(\S.*)?$/i) {
821 my $add_remove = defined $1 && $1 eq 'un';
822 my @blockers = map {exists $clonebugs{$_}?$clonebugs{$_}:$_} split /[\s,]+/, $3;
823 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
825 set_blocks(@common_control_options,
828 $add_remove ? (remove => 1):(add => 1),
833 print {$transcript} "Failed to set blocking bugs of $ref: ".cleanup_eval_fail($@,$debug)."\n";
835 } elsif (m/^retitle\s+\#?(-?\d+)\s+(\S.*\S)\s*$/i) {
837 $ref= $1; my $newtitle= $2;
838 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
840 set_title(@common_control_options,
847 print {$transcript} "Failed to set the title of $ref: ".cleanup_eval_fail($@,$debug)."\n";
849 } elsif (m/^unmerge\s+\#?(-?\d+)$/i) {
852 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
854 set_merged(@common_control_options,
860 print {$transcript} "Failed to unmerge $ref: ".cleanup_eval_fail($@,$debug)."\n";
862 } elsif (m/^merge\s+#?(-?\d+(\s+#?-?\d+)+)\s*$/i) {
865 ($ref,@tomerge) = map {exists $clonebugs{$_}?$clonebugs{$_}:$_}
868 set_merged(@common_control_options,
870 merge_with => \@tomerge,
875 print {$transcript} "Failed to merge $ref: ".cleanup_eval_fail($@,$debug)."\n";
877 } elsif (m/^forcemerge\s+\#?(-?\d+(?:\s+\#?-?\d+)+)\s*$/i) {
880 ($ref,@tomerge) = map {exists $clonebugs{$_}?$clonebugs{$_}:$_}
883 set_merged(@common_control_options,
885 merge_with => \@tomerge,
892 print {$transcript} "Failed to forcibly merge $ref: ".cleanup_eval_fail($@,$debug)."\n";
894 } elsif (m/^clone\s+#?(\d+)\s+((-\d+\s+)*-\d+)\s*$/i) {
898 my @newclonedids = split /\s+/, $2;
899 my $newbugsneeded = scalar(@newclonedids);
902 if (exists $clonebugs{$ref}) {
903 $ref = $clonebugs{$ref};
905 $bug_affected{$ref} = 1;
908 clone_bug(@common_control_options,
910 new_bugs => \@newclonedids,
911 new_clones => \%new_clones,
913 %clonebugs = (%clonebugs,
918 print {$transcript} "Failed to clone $ref: ".cleanup_eval_fail($@,$debug)."\n";
920 } elsif (m/^package\:?\s+(\S.*\S)?\s*$/i) {
922 my @pkgs = split /\s+/, $1;
923 if (scalar(@pkgs) > 0) {
924 %limit_pkgs = map { ($_, 1) } @pkgs;
925 $limit{package} = [@pkgs];
926 print {$transcript} "Limiting to bugs with field 'package' containing at least one of ".join(', ',map {qq('$_')} @pkgs)."\n";
927 print {$transcript} "Limit currently set to";
928 for my $limit_field (keys %limit) {
929 print {$transcript} " '$limit_field':".join(', ',map {qq('$_')} @{$limit{$limit_field}})."\n";
931 print {$transcript} "\n";
934 $limit{package} = [];
935 print {$transcript} "Limit cleared.\n\n";
937 } elsif (m/^limit\:?\s+(\S.*\S)\s*$/) {
939 my ($field,@options) = split /\s+/, $1;
941 if ($field =~ /^(?:clear|unset|blank)$/) {
943 print {$transcript} "Limit cleared.\n\n";
945 elsif (exists $Debbugs::Status::fields{$field} or $field eq 'source') {
946 # %limit can actually contain regexes, but because they're
947 # not evaluated in Safe, DO NOT allow them through without
949 $limit{$field} = [@options];
950 print {$transcript} "Limiting to bugs with field '$field' containing at least one of ".join(', ',map {qq('$_')} @options)."\n";
951 print {$transcript} "Limit currently set to";
952 for my $limit_field (keys %limit) {
953 print {$transcript} " '$limit_field':".join(', ',map {qq('$_')} @{$limit{$limit_field}})."\n";
955 print {$transcript} "\n";
958 print {$transcript} "Limit key $field not understood. Stopping processing here.\n\n";
962 } elsif (m/^affects?\s+\#?(-?\d+)(?:\s+((?:[=+-])?)\s*(\S.*)?)?\s*$/i) {
967 # if there isn't a package given, assume that we should unset
968 # affects; otherwise default to adding
969 if (not defined $packages or
970 not length $packages) {
974 elsif (not defined $add_remove or
975 not length $add_remove) {
978 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
980 affects(@common_control_options,
982 package => [splitpackages($3)],
983 ($add_remove eq '+'?(add => 1):()),
984 ($add_remove eq '-'?(remove => 1):()),
989 print {$transcript} "Failed to mark $ref as affecting package(s): ".cleanup_eval_fail($@,$debug)."\n";
992 } elsif (m/^summary\s+\#?(-?\d+)\s*(\d+|)\s*$/i) {
995 my $summary_msg = length($2)?$2:undef;
996 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
998 summary(@common_control_options,
1000 summary => $summary_msg,
1005 print {$transcript} "Failed to give $ref a summary: ".cleanup_eval_fail($@,$debug)."\n";
1008 } elsif (m/^owner\s+\#?(-?\d+)\s+((?:\S.*\S)|\!)\s*$/i) {
1011 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1013 if ($newowner eq '!') {
1014 $newowner = $replyto;
1017 owner(@common_control_options,
1024 print {$transcript} "Failed to mark $ref as having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1026 } elsif (m/^noowner\s+\#?(-?\d+)\s*$/i) {
1029 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1031 owner(@common_control_options,
1038 print {$transcript} "Failed to mark $ref as not having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1040 } elsif (m/^unarchive\s+#?(\d+)$/i) {
1043 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1045 bug_unarchive(@common_control_options,
1047 recipients => \%recipients,
1053 } elsif (m/^archive\s+#?(\d+)$/i) {
1056 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1058 bug_archive(@common_control_options,
1061 archive_unarchived => 0,
1068 print {$transcript} "Unknown command or malformed arguments to command.\n\n";
1070 if (++$unknowns >= 5) {
1071 print {$transcript} "Too many unknown commands, stopping here.\n\n";
1076 if ($procline>$#bodylines) {
1077 print {$transcript} ">\nEnd of message, stopping processing here.\n\n";
1079 if (!$ok && !$quickabort) {
1081 print {$transcript} "No commands successfully parsed; sending the help text(s).\n";
1083 print {$transcript} "\n";
1086 my @maintccs = determine_recipients(recipients => \%recipients,
1090 my $maintccs = 'Cc: '.join(",\n ",
1091 determine_recipients(recipients => \%recipients,
1097 $packagepr = "X-${gProject}-PR-Package: " . join(keys %affected_packages) . "\n" if keys %affected_packages;
1099 # Add Bcc's to subscribed bugs
1100 # now handled by Debbugs::Recipients
1101 #push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
1103 if (!defined $header{'subject'} || $header{'subject'} eq "") {
1104 $header{'subject'} = "your mail";
1107 # Error text here advertises how many errors there were
1108 my $error_text = $errors > 0 ? " (with $errors errors)":'';
1111 push @common_headers, 'X-Loop',$gMaintainerEmail;
1113 my $temp_transcript = ${transcript_scalar};
1115 $temp_transcript = decode("utf8",$temp_transcript,Encode::FB_CROAK);
1118 create_mime_message([From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
1120 @maintccs ? (Cc => join(', ',@maintccs)):(),
1121 Subject => "Processed${error_text}: $header{subject}",
1122 'Message-ID' => "<handler.s.$nn.transcript\@$gEmailDomain>",
1123 'In-Reply-To' => $header{'message-id'},
1124 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
1125 Precedence => 'bulk',
1126 keys %affected_packages ?("X-${gProject}-PR-Package" => join(' ',keys %affected_packages)):(),
1127 keys %affected_packages ?("X-${gProject}-PR-Source" =>
1129 map {defined $_ ?(ref($_)?@{$_}:$_):()}
1130 binary_to_source(binary => [keys %affected_packages],
1131 source_only => 1))):(),
1132 "X-$gProject-PR-Message" => 'transcript',
1135 fill_template('mail/message_body',
1136 {body => "${temp_transcript}Please contact me if you need assistance."},
1139 my $repliedshow= join(', ',$replyto,
1140 determine_recipients(recipients => \%recipients,
1146 # -1 is the service.in log
1147 &filelock("lock/-1");
1148 open(AP,">>db-h/-1.log") || die "open db-h/-1.log: $!";
1150 "\2\n$repliedshow\n\5\n$reply\n\3\n".
1152 "<strong>Request received</strong> from <code>".
1153 html_escape($header{'from'})."</code>\n".
1154 "to <code>".html_escape($controlrequestaddr)."</code>\n".
1156 "\7\n",escape_log(@log),"\n\3\n") || die "writing db-h/-1.log: $!";
1157 close(AP) || die "open db-h/-1.log: $!";
1159 utime(time,time,"db-h");
1161 &sendmailmessage($reply,
1162 exists $header{'x-debbugs-no-ack'}?():$replyto,
1163 make_list(values %{{determine_recipients(recipients => \%recipients,
1169 unlink("incoming/P$nn") || die "unlinking incoming/P$nn: $!";
1171 sub sendmailmessage {
1172 my ($message,@recips) = @_;
1173 $message = "X-Loop: $gMaintainerEmail\n" . $message;
1174 send_mail_message(message => $message,
1175 recipients => \@recips,
1181 my ($template,$extra_var) = @_;
1183 my $variables = {config => \%config,
1184 defined($ref)?(ref => $ref):(),
1185 defined($data)?(data => $data):(),
1186 refs => [map {exists $clonebugs{$_}?$clonebugs{$_}:$_} keys %bug_affected],
1189 my $hole_var = {'&bugurl' =>
1191 'http://'.$config{cgi_domain}.'/'.
1192 Debbugs::CGI::bug_links(bug=>$_[0],
1197 return fill_in_template(template => $template,
1198 variables => $variables,
1199 hole_var => $hole_var,
1203 =head2 message_body_template
1205 message_body_template('mail/ack',{ref=>'foo'});
1207 Creates a message body using a template
1211 sub message_body_template{
1212 my ($template,$extra_var) = @_;
1214 my $body = fill_template($template,$extra_var);
1215 return fill_template('mail/message_body',
1224 &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
1227 &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
1231 #sub unimplemented {
1232 # print {$transcript} "Sorry, command $_[0] not yet implemented.\n\n";
1234 our %checkmatch_values;
1236 my ($string,$mvarname,$svarvalue,@newmergelist) = @_;
1238 if (@newmergelist) {
1239 $mvarvalue = $checkmatch_values{$mvarname};
1240 print {$transcript} "D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n"
1243 "Values for \`$string' don't match:\n".
1244 " #$newmergelist[0] has \`$mvarvalue';\n".
1245 " #$ref has \`$svarvalue'\n"
1246 if $mvarvalue ne $svarvalue;
1248 print {$transcript} "D| setupmatch \`$string' /$mvarname/$svarvalue/\n"
1250 $checkmatch_values{$mvarname} = $svarvalue;
1255 if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1256 print {$transcript} "$gBug number $ref belongs to package $data->{package}, skipping.\n\n";
1268 my %h = map { $_ => 1 } split ' ', $list;
1275 return join ' ', sort keys %h;
1278 # High-level bug manipulation calls
1279 # Do announcements themselves
1281 # Possible calling sequences:
1282 # setbug (returns 0)
1284 # setbug (returns 1)
1285 # &transcript(something)
1288 # setbug (returns 1)
1289 # $action= (something)
1291 # (modify s_* variables)
1292 # } while (getnextbug);
1297 &dlen("nochangebug");
1298 $state eq 'single' || $state eq 'multiple' || die "$state ?";
1300 &endmerge if $manybugs;
1302 &dlex("nochangebug");
1306 our @thisbugmergelist;
1309 &dlen("setbug $ref");
1310 if ($ref =~ m/^-\d+/) {
1311 if (!defined $clonebugs{$ref}) {
1313 &dlex("setbug => noclone");
1316 $ref = $clonebugs{$ref};
1318 $state eq 'idle' || die "$state ?";
1321 &dlex("setbug => 0s");
1325 if (!&checkpkglimit) {
1330 @thisbugmergelist= split(/ /,$data->{mergedwith});
1331 if (!@thisbugmergelist) {
1336 &dlex("setbug => 1s");
1345 &dlex("setbug => 0mc");
1349 $state= 'multiple'; $sref=$ref;
1350 &dlex("setbug => 1m");
1355 &dlen("getnextbug");
1356 $state eq 'single' || $state eq 'multiple' || die "$state ?";
1358 if (!$manybugs || !@thisbugmergelist) {
1359 length($action) || die;
1360 print {$transcript} "$action\n$extramessage\n";
1361 &endmerge if $manybugs;
1363 &dlex("getnextbug => 0");
1366 $ref= shift(@thisbugmergelist);
1367 &getbug || die "bug $ref disappeared";
1369 &dlex("getnextbug => 1");
1373 # Low-level bug-manipulation calls
1374 # Do no announcements
1376 # getbug (returns 0)
1378 # getbug (returns 1)
1382 # $action= (something)
1383 # getbug (returns 1)
1385 # getbug (returns 1)
1387 # [getbug (returns 0)]
1388 # &transcript("$action\n\n")
1391 sub notfoundbug { print {$transcript} "$gBug number $ref not found. (Is it archived?)\n\n"; }
1392 sub foundbug { print {$transcript} "$gBug#$ref: $data->{subject}\n"; }
1396 $mergelowstate eq 'idle' || die "$mergelowstate ?";
1397 &filelock('lock/merge');
1398 $mergelowstate='locked';
1404 $mergelowstate eq 'locked' || die "$mergelowstate ?";
1406 $mergelowstate='idle';
1411 &dlen("getbug $ref");
1412 $lowstate eq 'idle' || die "$state ?";
1413 # Only use unmerged bugs here
1414 if (($data = &lockreadbug($ref,'db-h'))) {
1417 &dlex("getbug => 1");
1422 &dlex("getbug => 0");
1428 $lowstate eq 'open' || die "$state ?";
1435 &dlen("savebug $ref");
1436 $lowstate eq 'open' || die "$lowstate ?";
1437 length($action) || die;
1438 $ref == $sref || die "read $sref but saving $ref ?";
1439 append_action_to_log(bug => $ref,
1441 requester => $header{from},
1442 request_addr => $controlrequestaddr,
1446 unlockwritebug($ref, $data);
1453 print {$transcript} "C> @_ ($state $lowstate $mergelowstate)\n";
1458 print {$transcript} "R> @_ ($state $lowstate $mergelowstate)\n";
1465 my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1466 $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1472 print {$transcript} "\n";
1478 print {$transcript} "\n";
1484 sub sendtxthelpraw {
1485 my ($relpath,$description) = @_;
1487 if (not -e "$gDocDir/$relpath") {
1488 print {$transcript} "Unfortunatly, the help text doesn't exist, so it wasn't sent.\n";
1489 warn "Help text $gDocDir/$relpath not found";
1492 open(D,"$gDocDir/$relpath") || die "open doc file $relpath: $!";
1493 while(<D>) { $doc.=$_; }
1495 print {$transcript} "Sending $description in separate message.\n";
1496 &sendmailmessage(<<END.$doc,$replyto);
1497 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1499 Subject: $gProject $gBug help: $description
1500 References: $header{'message-id'}
1501 In-Reply-To: $header{'message-id'}
1502 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1504 X-$gProject-PR-Message: doc-text $relpath
1510 sub sendlynxdocraw {
1511 my ($relpath,$description) = @_;
1513 open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || die "fork for lynx: $!";
1514 while(<L>) { $doc.=$_; }
1516 if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1517 print {$transcript} "Information ($description) is not available -\n".
1518 "perhaps the $gBug does not exist or is not on the WWW yet.\n";
1521 print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1523 print {$transcript} "Sending $description.\n";
1524 &sendmailmessage(<<END.$doc,$replyto);
1525 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1527 Subject: $gProject $gBugs information: $description
1528 References: $header{'message-id'}
1529 In-Reply-To: $header{'message-id'}
1530 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1532 X-$gProject-PR-Message: doc-html $relpath
1541 my ($wherefrom,$path,$description) = @_;
1542 if ($wherefrom eq "ftp.d.o") {
1543 $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or die "fork for lynx/gunzip: $!";
1545 if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1546 print {$transcript} "$description is not available.\n";
1549 print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1552 } elsif ($wherefrom eq "local") {
1554 $doc = do { local $/; <P> };
1557 print {$transcript} "internal errror: info files location unknown.\n";
1560 print {$transcript} "Sending $description.\n";
1561 &sendmailmessage(<<END.$doc,$replyto);
1562 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1564 Subject: $gProject $gBugs information: $description
1565 References: $header{'message-id'}
1566 In-Reply-To: $header{'message-id'}
1567 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1569 X-$gProject-PR-Message: getinfo
1571 $description follows:
1575 print {$transcript} "\n";