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) {
965 my $add_remove = $2 || '';
966 my $packages = $3 || '';
967 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
969 affects(@common_control_options,
971 package => [splitpackages($3)],
972 ($add_remove eq '+'?(add => 1):()),
973 ($add_remove eq '-'?(remove => 1):()),
978 print {$transcript} "Failed to mark $ref as affecting package(s): ".cleanup_eval_fail($@,$debug)."\n";
981 } elsif (m/^summary\s+\#?(-?\d+)\s*(\d+|)\s*$/i) {
984 my $summary_msg = length($2)?$2:undef;
985 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
987 summary(@common_control_options,
989 summary => $summary_msg,
994 print {$transcript} "Failed to give $ref a summary: ".cleanup_eval_fail($@,$debug)."\n";
997 } elsif (m/^owner\s+\#?(-?\d+)\s+((?:\S.*\S)|\!)\s*$/i) {
1000 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1002 if ($newowner eq '!') {
1003 $newowner = $replyto;
1006 owner(@common_control_options,
1013 print {$transcript} "Failed to mark $ref as having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1015 } elsif (m/^noowner\s+\#?(-?\d+)\s*$/i) {
1018 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1020 owner(@common_control_options,
1027 print {$transcript} "Failed to mark $ref as not having an owner: ".cleanup_eval_fail($@,$debug)."\n";
1029 } elsif (m/^unarchive\s+#?(\d+)$/i) {
1032 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1034 bug_unarchive(@common_control_options,
1036 recipients => \%recipients,
1042 } elsif (m/^archive\s+#?(\d+)$/i) {
1045 $ref = $clonebugs{$ref} if exists $clonebugs{$ref};
1047 bug_archive(@common_control_options,
1050 archive_unarchived => 0,
1057 print {$transcript} "Unknown command or malformed arguments to command.\n\n";
1059 if (++$unknowns >= 5) {
1060 print {$transcript} "Too many unknown commands, stopping here.\n\n";
1065 if ($procline>$#bodylines) {
1066 print {$transcript} ">\nEnd of message, stopping processing here.\n\n";
1068 if (!$ok && !$quickabort) {
1070 print {$transcript} "No commands successfully parsed; sending the help text(s).\n";
1072 print {$transcript} "\n";
1075 my @maintccs = determine_recipients(recipients => \%recipients,
1079 my $maintccs = 'Cc: '.join(",\n ",
1080 determine_recipients(recipients => \%recipients,
1086 $packagepr = "X-${gProject}-PR-Package: " . join(keys %affected_packages) . "\n" if keys %affected_packages;
1088 # Add Bcc's to subscribed bugs
1089 # now handled by Debbugs::Recipients
1090 #push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
1092 if (!defined $header{'subject'} || $header{'subject'} eq "") {
1093 $header{'subject'} = "your mail";
1096 # Error text here advertises how many errors there were
1097 my $error_text = $errors > 0 ? " (with $errors errors)":'';
1100 push @common_headers, 'X-Loop',$gMaintainerEmail;
1102 my $temp_transcript = ${transcript_scalar};
1104 $temp_transcript = decode("utf8",$temp_transcript,Encode::FB_CROAK);
1107 create_mime_message([From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
1109 @maintccs ? (Cc => join(', ',@maintccs)):(),
1110 Subject => "Processed${error_text}: $header{subject}",
1111 'Message-ID' => "<handler.s.$nn.transcript\@$gEmailDomain>",
1112 'In-Reply-To' => $header{'message-id'},
1113 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
1114 Precedence => 'bulk',
1115 keys %affected_packages ?("X-${gProject}-PR-Package" => join(' ',keys %affected_packages)):(),
1116 keys %affected_packages ?("X-${gProject}-PR-Source" =>
1117 join(' ',grep {defined $_} map {binary_to_source(binary => $_)} keys %affected_packages)):(),
1118 "X-$gProject-PR-Message" => 'transcript',
1121 fill_template('mail/message_body',
1122 {body => "${temp_transcript}Please contact me if you need assistance."},
1125 my $repliedshow= join(', ',$replyto,
1126 determine_recipients(recipients => \%recipients,
1132 # -1 is the service.in log
1133 &filelock("lock/-1");
1134 open(AP,">>db-h/-1.log") || die "open db-h/-1.log: $!";
1136 "\2\n$repliedshow\n\5\n$reply\n\3\n".
1138 "<strong>Request received</strong> from <code>".
1139 html_escape($header{'from'})."</code>\n".
1140 "to <code>".html_escape($controlrequestaddr)."</code>\n".
1142 "\7\n",escape_log(@log),"\n\3\n") || die "writing db-h/-1.log: $!";
1143 close(AP) || die "open db-h/-1.log: $!";
1145 utime(time,time,"db-h");
1147 &sendmailmessage($reply,
1148 exists $header{'x-debbugs-no-ack'}?():$replyto,
1149 make_list(values %{{determine_recipients(recipients => \%recipients,
1155 unlink("incoming/P$nn") || die "unlinking incoming/P$nn: $!";
1157 sub sendmailmessage {
1158 my ($message,@recips) = @_;
1159 $message = "X-Loop: $gMaintainerEmail\n" . $message;
1160 send_mail_message(message => $message,
1161 recipients => \@recips,
1167 my ($template,$extra_var) = @_;
1169 my $variables = {config => \%config,
1170 defined($ref)?(ref => $ref):(),
1171 defined($data)?(data => $data):(),
1172 refs => [map {exists $clonebugs{$_}?$clonebugs{$_}:$_} keys %bug_affected],
1175 my $hole_var = {'&bugurl' =>
1177 'http://'.$config{cgi_domain}.'/'.
1178 Debbugs::CGI::bug_links(bug=>$_[0],
1183 return fill_in_template(template => $template,
1184 variables => $variables,
1185 hole_var => $hole_var,
1189 =head2 message_body_template
1191 message_body_template('mail/ack',{ref=>'foo'});
1193 Creates a message body using a template
1197 sub message_body_template{
1198 my ($template,$extra_var) = @_;
1200 my $body = fill_template($template,$extra_var);
1201 return fill_template('mail/message_body',
1210 &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
1213 &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
1217 #sub unimplemented {
1218 # print {$transcript} "Sorry, command $_[0] not yet implemented.\n\n";
1220 our %checkmatch_values;
1222 my ($string,$mvarname,$svarvalue,@newmergelist) = @_;
1224 if (@newmergelist) {
1225 $mvarvalue = $checkmatch_values{$mvarname};
1226 print {$transcript} "D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n"
1229 "Values for \`$string' don't match:\n".
1230 " #$newmergelist[0] has \`$mvarvalue';\n".
1231 " #$ref has \`$svarvalue'\n"
1232 if $mvarvalue ne $svarvalue;
1234 print {$transcript} "D| setupmatch \`$string' /$mvarname/$svarvalue/\n"
1236 $checkmatch_values{$mvarname} = $svarvalue;
1241 if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1242 print {$transcript} "$gBug number $ref belongs to package $data->{package}, skipping.\n\n";
1254 my %h = map { $_ => 1 } split ' ', $list;
1261 return join ' ', sort keys %h;
1264 # High-level bug manipulation calls
1265 # Do announcements themselves
1267 # Possible calling sequences:
1268 # setbug (returns 0)
1270 # setbug (returns 1)
1271 # &transcript(something)
1274 # setbug (returns 1)
1275 # $action= (something)
1277 # (modify s_* variables)
1278 # } while (getnextbug);
1283 &dlen("nochangebug");
1284 $state eq 'single' || $state eq 'multiple' || die "$state ?";
1286 &endmerge if $manybugs;
1288 &dlex("nochangebug");
1292 our @thisbugmergelist;
1295 &dlen("setbug $ref");
1296 if ($ref =~ m/^-\d+/) {
1297 if (!defined $clonebugs{$ref}) {
1299 &dlex("setbug => noclone");
1302 $ref = $clonebugs{$ref};
1304 $state eq 'idle' || die "$state ?";
1307 &dlex("setbug => 0s");
1311 if (!&checkpkglimit) {
1316 @thisbugmergelist= split(/ /,$data->{mergedwith});
1317 if (!@thisbugmergelist) {
1322 &dlex("setbug => 1s");
1331 &dlex("setbug => 0mc");
1335 $state= 'multiple'; $sref=$ref;
1336 &dlex("setbug => 1m");
1341 &dlen("getnextbug");
1342 $state eq 'single' || $state eq 'multiple' || die "$state ?";
1344 if (!$manybugs || !@thisbugmergelist) {
1345 length($action) || die;
1346 print {$transcript} "$action\n$extramessage\n";
1347 &endmerge if $manybugs;
1349 &dlex("getnextbug => 0");
1352 $ref= shift(@thisbugmergelist);
1353 &getbug || die "bug $ref disappeared";
1355 &dlex("getnextbug => 1");
1359 # Low-level bug-manipulation calls
1360 # Do no announcements
1362 # getbug (returns 0)
1364 # getbug (returns 1)
1368 # $action= (something)
1369 # getbug (returns 1)
1371 # getbug (returns 1)
1373 # [getbug (returns 0)]
1374 # &transcript("$action\n\n")
1377 sub notfoundbug { print {$transcript} "$gBug number $ref not found. (Is it archived?)\n\n"; }
1378 sub foundbug { print {$transcript} "$gBug#$ref: $data->{subject}\n"; }
1382 $mergelowstate eq 'idle' || die "$mergelowstate ?";
1383 &filelock('lock/merge');
1384 $mergelowstate='locked';
1390 $mergelowstate eq 'locked' || die "$mergelowstate ?";
1392 $mergelowstate='idle';
1397 &dlen("getbug $ref");
1398 $lowstate eq 'idle' || die "$state ?";
1399 # Only use unmerged bugs here
1400 if (($data = &lockreadbug($ref,'db-h'))) {
1403 &dlex("getbug => 1");
1408 &dlex("getbug => 0");
1414 $lowstate eq 'open' || die "$state ?";
1421 &dlen("savebug $ref");
1422 $lowstate eq 'open' || die "$lowstate ?";
1423 length($action) || die;
1424 $ref == $sref || die "read $sref but saving $ref ?";
1425 append_action_to_log(bug => $ref,
1427 requester => $header{from},
1428 request_addr => $controlrequestaddr,
1432 unlockwritebug($ref, $data);
1439 print {$transcript} "C> @_ ($state $lowstate $mergelowstate)\n";
1444 print {$transcript} "R> @_ ($state $lowstate $mergelowstate)\n";
1451 my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1452 $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1458 print {$transcript} "\n";
1464 print {$transcript} "\n";
1470 sub sendtxthelpraw {
1471 my ($relpath,$description) = @_;
1473 if (not -e "$gDocDir/$relpath") {
1474 print {$transcript} "Unfortunatly, the help text doesn't exist, so it wasn't sent.\n";
1475 warn "Help text $gDocDir/$relpath not found";
1478 open(D,"$gDocDir/$relpath") || die "open doc file $relpath: $!";
1479 while(<D>) { $doc.=$_; }
1481 print {$transcript} "Sending $description in separate message.\n";
1482 &sendmailmessage(<<END.$doc,$replyto);
1483 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1485 Subject: $gProject $gBug help: $description
1486 References: $header{'message-id'}
1487 In-Reply-To: $header{'message-id'}
1488 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1490 X-$gProject-PR-Message: doc-text $relpath
1496 sub sendlynxdocraw {
1497 my ($relpath,$description) = @_;
1499 open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || die "fork for lynx: $!";
1500 while(<L>) { $doc.=$_; }
1502 if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1503 print {$transcript} "Information ($description) is not available -\n".
1504 "perhaps the $gBug does not exist or is not on the WWW yet.\n";
1507 print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1509 print {$transcript} "Sending $description.\n";
1510 &sendmailmessage(<<END.$doc,$replyto);
1511 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1513 Subject: $gProject $gBugs information: $description
1514 References: $header{'message-id'}
1515 In-Reply-To: $header{'message-id'}
1516 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1518 X-$gProject-PR-Message: doc-html $relpath
1527 my ($wherefrom,$path,$description) = @_;
1528 if ($wherefrom eq "ftp.d.o") {
1529 $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or die "fork for lynx/gunzip: $!";
1531 if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1532 print {$transcript} "$description is not available.\n";
1535 print {$transcript} "Error getting $description (code $? $!):\n$doc\n";
1538 } elsif ($wherefrom eq "local") {
1540 $doc = do { local $/; <P> };
1543 print {$transcript} "internal errror: info files location unknown.\n";
1546 print {$transcript} "Sending $description.\n";
1547 &sendmailmessage(<<END.$doc,$replyto);
1548 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1550 Subject: $gProject $gBugs information: $description
1551 References: $header{'message-id'}
1552 In-Reply-To: $header{'message-id'}
1553 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1555 X-$gProject-PR-Message: getinfo
1557 $description follows:
1561 print {$transcript} "\n";