]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
[project @ 2005-10-06 03:32:13 by ajt]
[debbugs.git] / scripts / service.in
1 #!/usr/bin/perl
2 # $Id: service.in,v 1.113 2005/10/06 03:32:13 ajt 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
12 $config_path = '/etc/debbugs';
13 $lib_path = '/usr/lib/debbugs';
14
15 require "$config_path/config";
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 $controlrequestaddr= $control ? "control\@$gEmailDomain" : "request\@$gEmailDomain";
124 $transcript='';
125 &transcript("Processing commands for $controlrequestaddr:\n\n");
126
127 $dl= 0;
128 $state= 'idle';
129 $lowstate= 'idle';
130 $mergelowstate= 'idle';
131 $midix=0;    
132 $extras="";
133
134 my $quickabort = 0;
135
136 my $fuckheads = "(" . join("|", @gFuckheads) . ")";
137 if (@gFuckheads and $replyto =~ m/$fuckheads/) {
138         &transcript("This service is unavailable.\n\n");
139         $quickabort = 1;
140 }
141
142 my %limit_pkgs = ();
143 my %clonebugs = ();
144 my @bcc = ();
145
146 sub addbcc {
147     push @bcc, $_[0] unless grep { $_ eq $_[0] } @bcc;
148 }
149
150 for ($procline=0; $procline<=$#bodylines; $procline++) {
151     $state eq 'idle' || print "$state ?\n";
152     $lowstate eq 'idle' || print "$lowstate ?\n";
153     $mergelowstate eq 'idle' || print "$mergelowstate ?\n";
154     if ($quickabort) {
155          &transcript("Stopping processing here.\n\n");
156          last;
157     }
158     $_= $bodylines[$procline]; s/\s+$//;
159     next unless m/\S/;
160     &transcript("> $_\n");
161     next if m/^\s*\#/;
162     $action= '';
163     if (m/^stop/i || m/^quit/i || m/^--/ || m/^thank/i || m/^kthxbye/i) {
164         &transcript("Stopping processing here.\n\n");
165         last;
166     } elsif (m/^debug\s+(\d+)$/i && $1 >= 0 && $1 <= 1000) {
167         $dl= $1+0;
168         &transcript("Debug level $dl.\n\n");
169     } elsif (m/^(send|get)\s+\#?(\d{2,})$/i) {
170         $ref= $2+0;
171         &sendlynxdoc("bugreport.cgi?bug=$ref","logs for $gBug#$ref");
172     } elsif (m/^send-detail\s+\#?(\d{2,})$/i) {
173         $ref= $1+0;
174         &sendlynxdoc("bugreport.cgi?bug=$ref&boring=yes",
175                      "detailed logs for $gBug#$ref");
176     } elsif (m/^index(\s+full)?$/i) {
177         &transcript("This BTS function is currently disabled, sorry.\n\n");
178         $ok++; # well, it's not really ok, but it fixes #81224 :)
179     } elsif (m/^index-summary\s+by-package$/i) {
180         &transcript("This BTS function is currently disabled, sorry.\n\n");
181         $ok++; # well, it's not really ok, but it fixes #81224 :)
182     } elsif (m/^index-summary(\s+by-number)?$/i) {
183         &transcript("This BTS function is currently disabled, sorry.\n\n");
184         $ok++; # well, it's not really ok, but it fixes #81224 :)
185     } elsif (m/^index(\s+|-)pack(age)?s?$/i) {
186         &sendlynxdoc("pkgindex.cgi?indexon=pkg",'index of packages');
187     } elsif (m/^index(\s+|-)maints?$/i) {
188         &sendlynxdoc("pkgindex.cgi?indexon=maint",'index of maintainers');
189     } elsif (m/^index(\s+|-)maint\s+(\S+)$/i) {
190         $maint = $2;
191         &sendlynxdoc("pkgreport.cgi?maint=" . urlsanit($maint),
192                      "$gBug list for maintainer \`$maint'");
193         $ok++;
194     } elsif (m/^index(\s+|-)pack(age)?s?\s+(\S.*\S)$/i) {
195         $package = $+;
196         &sendlynxdoc("pkgreport.cgi?pkg=" . urlsanit($package),
197                      "$gBug list for package $package");
198         $ok++;
199     } elsif (m/^send-unmatched(\s+this|\s+-?0)?$/i) {
200         &transcript("This BTS function is currently disabled, sorry.\n\n");
201         $ok++; # well, it's not really ok, but it fixes #81224 :)
202     } elsif (m/^send-unmatched\s+(last|-1)$/i) {
203         &transcript("This BTS function is currently disabled, sorry.\n\n");
204         $ok++; # well, it's not really ok, but it fixes #81224 :)
205     } elsif (m/^send-unmatched\s+(old|-2)$/i) {
206         &transcript("This BTS function is currently disabled, sorry.\n\n");
207         $ok++; # well, it's not really ok, but it fixes #81224 :)
208     } elsif (m/^getinfo\s+([\w-.]+)$/i) {
209         # the following is basically a Debian-specific kludge, but who cares
210         $req = $1;
211         if ($req =~ /^maintainers$/i && -f "$gConfigDir/Maintainers") {
212             &sendinfo("local", "$gConfigDir/Maintainers", "Maintainers file");
213         } elsif ($req =~ /^override\.(\w+)\.([\w-.]+)$/i) {
214             $req =~ s/.gz$//;
215             &sendinfo("ftp.d.o", "$req", "override file for $2 part of $1 distribution");
216         } elsif ($req =~ /^pseudo-packages\.(description|maintainers)$/i && -f "$gConfigDir/$req") {
217             &sendinfo("local", "$gConfigDir/$req", "$req file");
218         } else {
219             &transcript("Info file $req does not exist.\n\n");
220         }
221     } elsif (m/^help/i) {
222         &sendhelp;
223         &transcript("\n");
224         $ok++;
225     } elsif (m/^refcard/i) {
226         &sendtxthelp("bug-mailserver-refcard.txt","mail servers' reference card");
227     } elsif (m/^subscribe/i) {
228         &transcript(<<END);
229 There is no $gProject $gBug mailing list.  If you wish to review bug reports
230 please do so via http://$gWebDomain/ or ask this mail server
231 to send them to you.
232 soon: MAILINGLISTS_TEXT
233 END
234     } elsif (m/^unsubscribe/i) {
235         &transcript(<<END);
236 soon: UNSUBSCRIBE_TEXT
237 soon: MAILINGLISTS_TEXT
238 END
239     } elsif (!$control) {
240         &transcript(<<END);
241 Unknown command or malformed arguments to command.
242 (Use control\@$gEmailDomain to manipulate reports.)
243
244 END
245         if (++$unknowns >= 3) {
246             &transcript("Too many unknown commands, stopping here.\n\n");
247             last;
248         }
249 #### interesting ones start here
250     } elsif (m/^close\s+\#?(-?\d+)(?:\s+(\d.*))?$/i) {
251         $ok++;
252         $ref= $1;
253         $bug_affected{$ref}=1;
254         $version= $2;
255         if (&setbug) {
256             &transcript("'close' is deprecated; see http://$gWebDomain/Developer$gHTMLSuffix#closing.\n");
257             if (length($data->{done}) and not defined($version)) {
258                 &transcript("$gBug is already closed, cannot re-close.\n\n");
259                 &nochangebug;
260             } else {
261                 $action= "$gBug " .
262                     (defined($version) ?
263                         "marked as fixed in version $version" :
264                         "closed") .
265                     ", send any further explanations to $data->{originator}";
266                 do {
267                     &addmaintainers($data);
268                                         if ( length( $gDoneList ) > 0 && length( $gListDomain ) >
269                                         0 ) { &addccaddress("$gDoneList\@$gListDomain"); }
270                     $data->{done}= $replyto;
271                     my @keywords= split ' ', $data->{keywords};
272                     if (grep $_ eq 'pending', @keywords) {
273                         $extramessage= "Removed pending tag.\n";
274                         $data->{keywords}= join ' ', grep $_ ne 'pending',
275                                                 @keywords;
276                     }
277                     addfixedversions($data, $data->{package}, $version, 'binary');
278
279                     $message= <<END;
280 From: $gMaintainerEmail ($gProject $gBug Tracking System)
281 To: $data->{originator}
282 Subject: $gBug#$ref acknowledged by developer
283          ($header{'subject'})
284 References: $header{'message-id'} $data->{msgid}
285 In-Reply-To: $data->{msgid}
286 Message-ID: <handler.$ref.$nn.notifdonectrl.$midix\@$gEmailDomain>
287 Reply-To: $ref\@$gEmailDomain
288 X-$gProject-PR-Message: they-closed-control $ref
289
290 This is an automatic notification regarding your $gBug report
291 #$ref: $data->{subject},
292 which was filed against the $data->{package} package.
293
294 It has been marked as closed by one of the developers, namely
295 $replyto.
296
297 You should be hearing from them with a substantive response shortly,
298 in case you haven't already. If not, please contact them directly.
299
300 $gMaintainer
301 (administrator, $gProject $gBugs database)
302
303 END
304                     &sendmailmessage($message,$data->{originator});
305                 } while (&getnextbug);
306             }
307         }
308     } elsif (m/^reassign\s+\#?(-?\d+)\s+(\S+)(?:\s+(\d.*))?$/i) {
309         $ok++;
310         $ref= $1; $newpackage= $2;
311         $bug_affected{$ref}=1;
312         $version= $3;
313         $newpackage =~ y/A-Z/a-z/;
314         if (&setbug) {
315             if (length($data->{package})) {
316                 $action= "$gBug reassigned from package \`$data->{package}'".
317                          " to \`$newpackage'.";
318             } else {
319                 $action= "$gBug assigned to package \`$newpackage'.";
320             }
321             do {
322                 &addmaintainers($data);
323                 $data->{package}= $newpackage;
324                 $data->{found_versions}= [];
325                 $data->{fixed_versions}= [];
326                 # TODO: what if $newpackage is a source package?
327                 addfoundversions($data, $data->{package}, $version, 'binary');
328                 &addmaintainers($data);
329             } while (&getnextbug);
330         }
331     } elsif (m/^reopen\s+\#?(-?\d+)$/i ? ($noriginator='', 1) :
332              m/^reopen\s+\#?(-?\d+)\s+\=$/i ? ($noriginator='', 1) :
333              m/^reopen\s+\#?(-?\d+)\s+\!$/i ? ($noriginator=$replyto, 1) :
334              m/^reopen\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($noriginator=$2, 1) : 0) {
335         $ok++;
336         $ref= $1;
337         $bug_affected{$ref}=1;
338         if (&setbug) {
339             if (@{$data->{fixed_versions}}) {
340                 &transcript("'reopen' is deprecated when a bug has been closed with a version;\nuse 'found' or 'submitter' as appropriate instead.\n");
341             }
342             if (!length($data->{done})) {
343                 &transcript("$gBug is already open, cannot reopen.\n\n");
344                 &nochangebug;
345             } else {
346                 $action=
347                     $noriginator eq '' ? "$gBug reopened, originator not changed." :
348                         "$gBug reopened, originator set to $noriginator.";
349                 do {
350                     &addmaintainers($data);
351                     $data->{originator}= $noriginator eq '' ?  $data->{originator} : $noriginator;
352                     $data->{fixed_versions}= [];
353                     $data->{done}= '';
354                 } while (&getnextbug);
355             }
356         }
357     } elsif (m/^found\s+\#?(-?\d+)(?:\s+(\d.*))?$/i) {
358         $ok++;
359         $ref= $1;
360         $version= $2;
361         if (&setbug) {
362             if (!length($data->{done}) and not defined($version)) {
363                 &transcript("$gBug is already open, cannot reopen.\n\n");
364                 &nochangebug;
365             } else {
366                 $action=
367                     defined($version) ?
368                         "$gBug marked as found in version $version." :
369                         "$gBug reopened.";
370                 do {
371                     &addmaintainers($data);
372                     # The 'done' field gets a bit weird with version
373                     # tracking, because a bug may be closed by multiple
374                     # people in different branches. Until we have something
375                     # more flexible, we set it every time a bug is fixed,
376                     # and clear it precisely when a found command is
377                     # received for the rightmost fixed-in version, which
378                     # equates to the most recent fixing of the bug, or when
379                     # a versionless found command is received.
380                     if (defined $version) {
381                         my $lastfixed =
382                             (reverse @{$data->{fixed_versions}})[0];
383                         # TODO: what if $data->{package} is a source package?
384                         addfoundversions($data, $data->{package}, $version, 'binary');
385                         if (defined $lastfixed and not grep { $_ eq $lastfixed } @{$data->{fixed_versions}}) {
386                             $data->{done} = '';
387                         }
388                     } else {
389                         # Versionless found; assume old-style "not fixed at
390                         # all".
391                         $data->{fixed_versions} = [];
392                         $data->{done} = '';
393                     }
394                 } while (&getnextbug);
395             }
396         }
397     } elsif (m/^notfound\s+\#?(-?\d+)\s+(\d.*)$/i) {
398         $ok++;
399         $ref= $1;
400         $version= $2;
401         if (&setbug) {
402             $action= "$gBug marked as not found in version $version.";
403             if (length($data->{done})) {
404                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
405             }
406             do {
407                 &addmaintainers($data);
408                 removefoundversions($data, $data->{package}, $version, 'binary');
409             } while (&getnextbug);
410         }
411     } elsif (m/^submitter\s+\#?(-?\d+)\s+\!$/i ? ($newsubmitter=$replyto, 1) :
412              m/^submitter\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($newsubmitter=$2, 1) : 0) {
413         $ok++;
414         $ref= $1;
415         $bug_affected{$ref}=1;
416         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
417             $ref = $clonebugs{$ref};
418         }
419         if (&getbug) {
420             if (&checkpkglimit) {
421                 &foundbug;
422                 &addmaintainers($data);
423                 $oldsubmitter= $data->{originator};
424                 $data->{originator}= $newsubmitter;
425                 $action= "Changed $gBug submitter from $oldsubmitter to $newsubmitter.";
426                 &savebug;
427                 &transcript("$action\n");
428                 if (length($data->{done})) {
429                     &transcript("(By the way, that $gBug is currently marked as done.)\n");
430                 }
431                 &transcript("\n");
432                 $message= <<END;
433 From: $gMaintainerEmail ($gProject $gBug Tracking System)
434 To: $oldsubmitter
435 Subject: $gBug#$ref submitter address changed
436          ($header{'subject'})
437 References: $header{'message-id'} $data->{msgid}
438 In-Reply-To: $data->{msgid}
439 Message-ID: <handler.$ref.$nn.newsubmitter.$midix\@$gEmailDomain>
440 Reply-To: $ref\@$gEmailDomain
441 X-$gProject-PR-Message: submitter-changed $ref
442
443 The submitter address recorded for your $gBug report
444 #$ref: $data->{subject}
445 has been changed.
446
447 The old submitter address for this report was
448 $oldsubmitter.
449 The new submitter address is
450 $newsubmitter.
451
452 This change was made by
453 $replyto.
454 If it was incorrect, please contact them directly.
455
456 $gMaintainer
457 (administrator, $gProject $gBugs database)
458
459 END
460                 &sendmailmessage($message,$oldsubmitter);
461             } else {
462                 &cancelbug;
463             }
464         } else {
465             &notfoundbug;
466         }
467     } elsif (m/^forwarded\s+\#?(-?\d+)\s+(\S.*\S)$/i) {
468         $ok++;
469         $ref= $1; $whereto= $2;
470         $bug_affected{$ref}=1;
471         if (&setbug) {
472             if (length($data->{forwarded})) {
473     $action= "Forwarded-to-address changed from $data->{forwarded} to $whereto.";
474             } else {
475     $action= "Noted your statement that $gBug has been forwarded to $whereto.";
476             }
477             if (length($data->{done})) {
478                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
479             }
480             do {
481                 &addmaintainers($data);
482                 if (length($gForwardList)>0 && length($gListDomain)>0 ) {
483                      &addccaddress("$gForwardList\@$gListDomain"); 
484                 }
485                 $data->{forwarded}= $whereto;
486             } while (&getnextbug);
487         }
488     } elsif (m/^notforwarded\s+\#?(-?\d+)$/i) {
489         $ok++;
490         $ref= $1;
491         $bug_affected{$ref}=1;
492         if (&setbug) {
493             if (!length($data->{forwarded})) {
494                 &transcript("$gBug is not marked as having been forwarded.\n\n");
495                 &nochangebug;
496             } else {
497     $action= "Removed annotation that $gBug had been forwarded to $data->{forwarded}.";
498                 do {
499                     &addmaintainers($data);
500                     $data->{forwarded}= '';
501                 } while (&getnextbug);
502             }
503         }
504     } elsif (m/^severity\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i ||
505         m/^priority\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i) {
506         $ok++;
507         $ref= $1;
508         $bug_affected{$ref}=1;
509         $newseverity= $2;
510         if (!grep($_ eq $newseverity, @gSeverityList, "$gDefaultSeverity")) {
511             &transcript("Severity level \`$newseverity' is not known.\n".
512                         "Recognized are: $gShowSeverities.\n\n");
513         } elsif (exists $gObsoleteSeverities{$newseverity}) {
514             &transcript("Severity level \`$newseverity' is obsolete. " .
515                         "$gObsoleteSeverities{$newseverity}\n\n");
516         } elsif (&setbug) {
517             $printseverity= $data->{severity};
518             $printseverity= "$gDefaultSeverity" if $printseverity eq '';
519             $action= "Severity set to \`$newseverity' from \`$printseverity'";
520             do {
521                 &addmaintainers($data);
522                 if (defined $gStrongList and isstrongseverity($newseverity)) {
523                     addbcc("$gStrongList\@$gListDomain");
524                 }
525                 $data->{severity}= $newseverity;
526             } while (&getnextbug);
527         }
528     } elsif (m/^tags?\s+\#?(-?\d+)\s+(([=+-])\s*)?(\S.*)?$/i) {
529         $ok++;
530         $ref = $1; $addsubcode = $3; $tags = $4;
531         $bug_affected{$ref}=1;
532         $addsub = "add";
533         if (defined $addsubcode) {
534             $addsub = "sub" if ($addsubcode eq "-");
535             $addsub = "add" if ($addsubcode eq "+");
536             $addsub = "set" if ($addsubcode eq "=");
537         }
538         my @okaytags = ();
539         my @badtags = ();
540         foreach my $t (split /[\s,]+/, $tags) {
541             if (!grep($_ eq $t, @gTags)) {
542                 push @badtags, $t;
543             } else {
544                 push @okaytags, $t;
545             }
546         }
547         if (@badtags) {
548             &transcript("Unknown tag/s: ".join(', ', @badtags).".\n".
549                         "Recognized are: ".join(' ', @gTags).".\n\n");
550         }
551         if (&setbug) {
552             if ($data->{keywords} eq '') {
553                 &transcript("There were no tags set.\n");
554             } else {
555                 &transcript("Tags were: $data->{keywords}\n");
556             }
557             if ($addsub eq "set") {
558                 $action= "Tags set to: " . join(", ", @okaytags);
559             } elsif ($addsub eq "add") {
560                 $action= "Tags added: " . join(", ", @okaytags);
561             } elsif ($addsub eq "sub") {
562                 $action= "Tags removed: " . join(", ", @okaytags);
563             }
564             do {
565                 &addmaintainers($data);
566                 $data->{keywords} = '' if ($addsub eq "set");
567                 # Allow removing obsolete tags.
568                 if ($addsub eq "sub") {
569                     foreach my $t (@badtags) {
570                         $data->{keywords} = join ' ', grep $_ ne $t, 
571                             split ' ', $data->{keywords};
572                     }
573                 }
574                 # Now process all other additions and subtractions.
575                 foreach my $t (@okaytags) {
576                     $data->{keywords} = join ' ', grep $_ ne $t, 
577                         split ' ', $data->{keywords};
578                     $data->{keywords} = "$t $data->{keywords}" unless($addsub eq "sub");
579                 }
580                 $data->{keywords} =~ s/\s*$//;
581             } while (&getnextbug);
582         }
583     } elsif (m/^(un)?block\s+\#?(-?\d+)\s+(by|with)\s+\s*(\S.*)?$/i) {
584         $ok++;
585         my $bugnum = $2; my $blockers = $4;
586         $addsub = "add";
587         $addsub = "sub" if ($1 eq "un");
588         
589         my @okayblockers;
590         my @badblockers;
591         foreach my $b (split /[\s,]+/, $blockers) {
592             $b=~s/^\#//;
593             if ($b=~/[0-9]+/) {
594                 $ref=$b;
595                 if (&getbug) {
596                     push @okayblockers, $b;
597
598                     # add to the list all bugs that are merged with $b,
599                     # because all of their data must be kept in sync
600                     @thisbugmergelist= split(/ /,$data->{mergedwith});
601                     &cancelbug;
602
603                     foreach $ref (@thisbugmergelist) {
604                         if (&getbug) {
605                            push @okayblockers, $ref;
606                            &cancelbug;
607                         }
608                     }
609                 }
610                 else {
611                     &notfoundbug;
612                     push @badblockers, $b;
613                 }
614             }
615             else {
616                 push @badblockers, $b;
617             }
618         }
619         if (@badblockers) {
620             &transcript("Unknown blocking bug/s: ".join(', ', @badblockers).".\n");
621         }
622         
623         $ref=$bugnum;
624         if (&setbug) {
625             if ($data->{blockedby} eq '') {
626                 &transcript("Was not blocked by any bugs.\n");
627             } else {
628                 &transcript("Was blocked by: $data->{blockedby}\n");
629             }
630             if ($addsub eq "set") {
631                 $action= "Blocking bugs set to: " . join(", ", @okayblockers);
632             } elsif ($addsub eq "add") {
633                 $action= "Blocking bugs added: " . join(", ", @okayblockers);
634             } elsif ($addsub eq "sub") {
635                 $action= "Blocking bugs removed: " . join(", ", @okayblockers);
636             }
637             my %removedblocks;
638             my %addedblocks;
639             do {
640                 &addmaintainers($data);
641                 my @oldblockerlist = split ' ', $data->{blockedby};
642                 $data->{blockedby} = '' if ($addsub eq "set");
643                 foreach my $b (@okayblockers) {
644                         $data->{blockedby} = manipset($data->{blockedby}, $b,
645                                 ($addsub ne "sub"));
646                 }
647
648                 foreach my $b (@oldblockerlist) {
649                         if (! grep { $_ eq $b } split ' ', $data->{blockedby}) {
650                                 push @{$removedblocks{$b}}, $ref;
651                         }
652                 }
653                 foreach my $b (split ' ', $data->{blockedby}) {
654                         if (! grep { $_ eq $b } @oldblockerlist) {
655                                 push @{$addedblocks{$b}}, $ref;
656                         }
657                 }
658             } while (&getnextbug);
659
660             # Now that the blockedby data is updated, change blocks data
661             # to match the changes.
662             foreach $ref (keys %addedblocks) {
663                 if (&getbug) {
664                     foreach my $b (@{$addedblocks{$ref}}) {
665                         $data->{blocks} = manipset($data->{blocks}, $b, 1);
666                     }
667                     &savebug;
668                 }
669             }
670             foreach $ref (keys %removedblocks) {
671                 if (&getbug) {
672                     foreach my $b (@{$removedblocks{$ref}}) {
673                         $data->{blocks} = manipset($data->{blocks}, $b, 0);
674                     }
675                     &savebug;
676                 }
677             }
678         }
679     } elsif (m/^retitle\s+\#?(-?\d+)\s+(\S.*\S)\s*$/i) {
680         $ok++;
681         $ref= $1; $newtitle= $2;
682         $bug_affected{$ref}=1;
683         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
684             $ref = $clonebugs{$ref};
685         }
686         if (&getbug) {
687             if (&checkpkglimit) {
688                 &foundbug;
689                 &addmaintainers($data);
690                 $data->{subject}= $newtitle;
691                 $action= "Changed $gBug title.";
692                 &savebug;
693                 &transcript("$action\n");
694                 if (length($data->{done})) {
695                     &transcript("(By the way, that $gBug is currently marked as done.)\n");
696                 }
697                 &transcript("\n");
698             } else {
699                 &cancelbug;
700             }
701         } else {
702             &notfoundbug;
703         }
704     } elsif (m/^unmerge\s+\#?(-?\d+)$/i) {
705         $ok++;
706         $ref= $1;
707         $bug_affected{$ref} = 1;
708         if (&setbug) {
709             if (!length($data->{mergedwith})) {
710                 &transcript("$gBug is not marked as being merged with any others.\n\n");
711                 &nochangebug;
712             } else {
713                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
714                 $action= "Disconnected #$ref from all other report(s).";
715                 @newmergelist= split(/ /,$data->{mergedwith});
716                 $discref= $ref;
717                 @bug_affected{@newmergelist} = 1 x @newmergelist;
718                 do {
719                     &addmaintainers($data);
720                     $data->{mergedwith}= ($ref == $discref) ? ''
721                         : join(' ',grep($_ ne $ref,@newmergelist));
722                 } while (&getnextbug);
723             }
724         }
725     } elsif (m/^merge\s+#?(-?\d+(\s+#?-?\d+)+)\s*$/i) {
726         $ok++;
727         @tomerge= sort { $a <=> $b } split(/\s+#?/,$1);
728         @newmergelist= ();
729         my %tags = ();
730         my %found = ();
731         my %fixed = ();
732         &getmerge;
733         while (defined($ref= shift(@tomerge))) {
734             &transcript("D| checking merge $ref\n") if $dl;
735             $ref+= 0;
736             if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
737                 $ref = $clonebugs{$ref};
738             }
739             next if grep($_ eq $ref,@newmergelist);
740             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
741             if (!&checkpkglimit) { &cancelbug; @newmergelist=(); last; }
742             &foundbug;
743             &transcript("D| adding $ref ($data->{mergedwith})\n") if $dl;
744             $mismatch= '';
745             &checkmatch('package','m_package',$data->{package});
746             &checkmatch('forwarded addr','m_forwarded',$data->{forwarded});
747             $data->{severity} = '$gDefaultSeverity' if $data->{severity} eq '';
748             &checkmatch('severity','m_severity',$data->{severity});
749             &checkmatch('blocks','m_blocks',$data->{blocks});
750             &checkmatch('blocked-by','m_blockedby',$data->{blockedby});
751             &checkmatch('done mark','m_done',length($data->{done}) ? 'done' : 'open');
752             &checkmatch('owner','m_owner',$data->{owner});
753             foreach my $t (split /\s+/, $data->{keywords}) { $tags{$t} = 1; }
754             foreach my $f (@{$data->{found_versions}}) { $found{$f} = 1; }
755             foreach my $f (@{$data->{fixed_versions}}) { $fixed{$f} = 1; }
756             if (length($mismatch)) {
757                 &transcript("Mismatch - only $gBugs in same state can be merged:\n".
758                             $mismatch."\n");
759                 &cancelbug; @newmergelist=(); last;
760             }
761             push(@newmergelist,$ref);
762             push(@tomerge,split(/ /,$data->{mergedwith}));
763             &cancelbug;
764         }
765         if (@newmergelist) {
766             @newmergelist= sort { $a <=> $b } @newmergelist;
767             $action= "Merged @newmergelist.";
768             delete @fixed{keys %found};
769             for $ref (@newmergelist) {
770                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
771                 &addmaintainers($data);
772                 @bug_affected{@newmergelist} = 1 x @newmergelist;
773                 $data->{mergedwith}= join(' ',grep($_ ne $ref,@newmergelist));
774                 $data->{keywords}= join(' ', keys %tags);
775                 $data->{found_versions}= [sort keys %found];
776                 $data->{fixed_versions}= [sort keys %fixed];
777                 &savebug;
778             }
779             &transcript("$action\n\n");
780         }
781         &endmerge;
782     } elsif (m/^clone\s+#?(\d+)\s+((-\d+\s+)*-\d+)\s*$/i) {
783         $ok++;
784
785         $origref = $1;
786         @newclonedids = split /\s+/, $2;
787         $newbugsneeded = scalar(@newclonedids);
788
789         $ref = $origref;
790         $bug_affected{$ref} = 1;
791         if (&setbug) {
792             if (length($data->{mergedwith})) {
793                 &transcript("$gBug is marked as being merged with others.\n\n");
794                 &nochangebug;
795             } else {
796                 &filelock("nextnumber.lock");
797                 open(N,"nextnumber") || &quit("nextnumber: read: $!");
798                 $v=<N>; $v =~ s/\n$// || &quit("nextnumber bad format");
799                 $firstref= $v+0;  $v += $newbugsneeded;
800                 open(NN,">nextnumber"); print NN "$v\n"; close(NN);
801                 &unfilelock;
802
803                 $lastref = $firstref + $newbugsneeded - 1;
804
805                 if ($newbugsneeded == 1) {
806                     $action= "$gBug $origref cloned as bug $firstref.";
807                 } else {
808                     $action= "$gBug $origref cloned as bugs $firstref-$lastref.";
809                 }
810
811                 my $blocks = $data->{blocks};
812                 my $blockedby = $data->{blockedby};
813                 
814                 &getnextbug;
815                 my $ohash = get_hashname($origref);
816                 my $clone = $firstref;
817                 @bug_affected{@newclonedids} = 1 x @newclonedids;
818                 for $newclonedid (@newclonedids) {
819                     $clonebugs{$newclonedid} = $clone;
820             
821                     my $hash = get_hashname($clone);
822                     copy("db-h/$ohash/$origref.log", "db-h/$hash/$clone.log");
823                     copy("db-h/$ohash/$origref.status", "db-h/$hash/$clone.status");
824                     copy("db-h/$ohash/$origref.summary", "db-h/$hash/$clone.summary");
825                     copy("db-h/$ohash/$origref.report", "db-h/$hash/$clone.report");
826                     &bughook('new', $clone, $data);
827                 
828                     # Update blocking info of bugs blocked by or blocking the
829                     # cloned bug.
830                     foreach $ref (split ' ', $blocks) {
831                         &getbug;
832                         $data->{blockedby} = manipset($data->{blockedby}, $clone, 1);
833                         &savebug;
834                     }
835                     foreach $ref (split ' ', $blockedby) {
836                         &getbug;
837                         $data->{blocks} = manipset($data->{blocks}, $clone, 1);
838                         &savebug;
839                     }
840
841                     $clone++;
842                 }
843             }
844         }
845     } elsif (m/^package\s+(\S.*\S)?\s*$/i) {
846         $ok++;
847         my @pkgs = split /\s+/, $1;
848         if (scalar(@pkgs) > 0) {
849                 %limit_pkgs = map { ($_, 1) } @pkgs;
850                 &transcript("Ignoring bugs not assigned to: " . 
851                         join(" ", keys(%limit_pkgs)) . "\n\n");
852         } else {
853                 %limit_pkgs = ();
854                 &transcript("Not ignoring any bugs.\n\n");
855         }
856     } elsif (m/^owner\s+\#?(-?\d+)\s+!$/i ? ($newowner = $replyto, 1) :
857              m/^owner\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($newowner = $2, 1) : 0) {
858         $ok++;
859         $ref = $1;
860         $bug_affected{$ref} = 1;
861         if (&setbug) {
862             if (length $data->{owner}) {
863                 $action = "Owner changed from $data->{owner} to $newowner.";
864             } else {
865                 $action = "Owner recorded as $newowner.";
866             }
867             if (length $data->{done}) {
868                 $extramessage = "(By the way, this $gBug is currently " .
869                                 "marked as done.)\n";
870             }
871             do {
872                 &addmaintainers($data);
873                 $data->{owner} = $newowner;
874             } while (&getnextbug);
875         }
876     } elsif (m/^noowner\s+\#?(-?\d+)$/i) {
877         $ok++;
878         $ref = $1;
879         $bug_affected{$ref} = 1;
880         if (&setbug) {
881             if (length $data->{owner}) {
882                 $action = "Removed annotation that $gBug was owned by " .
883                           "$data->{owner}.";
884                 do {
885                     &addmaintainers($data);
886                     $data->{owner} = '';
887                 } while (&getnextbug);
888             } else {
889                 &transcript("$gBug is not marked as having an owner.\n\n");
890                 &nochangebug;
891             }
892         }
893     } else {
894         &transcript("Unknown command or malformed arguments to command.\n\n");
895         if (++$unknowns >= 5) {
896             &transcript("Too many unknown commands, stopping here.\n\n");
897             last;
898         }
899     }
900 }
901 if ($procline>$#bodylines) {
902     &transcript(">\nEnd of message, stopping processing here.\n\n");
903 }
904 if (!$ok && !quickabort) {
905     &transcript("No commands successfully parsed; sending the help text(s).\n");
906     &sendhelp;
907     &transcript("\n");
908 }
909
910 &transcript("MC\n") if $dl>1;
911 @maintccs= ();
912 for $maint (keys %maintccreasons) {
913 &transcript("MM|$maint|\n") if $dl>1;
914     next if $maint eq $replyto;
915     $reasonstring= '';
916     $reasonsref= $maintccreasons{$maint};
917 &transcript("MY|$maint|\n") if $dl>2;
918     for $p (sort keys %$reasonsref) {
919 &transcript("MP|$p|\n") if $dl>2;
920         $reasonstring.= ', ' if length($reasonstring);
921         $reasonstring.= $p.' ' if length($p);
922         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
923     }
924     if (length($reasonstring) > 40) {
925         (substr $reasonstring, 37) = "...";
926     }
927     $reasonstring = "" if (!defined($reasonstring));
928     push(@maintccs,"$maint ($reasonstring)");
929     push(@maintccaddrs,"$maint");
930 }
931
932 $maintccs = ""; 
933 if (@maintccs) {
934     &transcript("MC|@maintccs|\n") if $dl>2;
935     $maintccs .= "Cc: " . join(",\n    ",@maintccs) . "\n";
936 }
937
938 # Add Bcc's to subscribed bugs
939 push @bcc, map {"bugs=$_\@$gListDomain"} keys %bug_affected;
940
941 if (!defined $header{'subject'} || $header{'subject'} eq "") {
942   $header{'subject'} = "your mail";
943 }
944
945 $reply= <<END;
946 From: $gMaintainerEmail ($gProject $gBug Tracking System)
947 To: $replyto
948 ${maintccs}Subject: Processed: $header{'subject'}
949 In-Reply-To: $header{'message-id'}
950 References: $header{'message-id'}
951 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
952 Precedence: bulk
953 X-$gProject-PR-Message: transcript
954
955 ${transcript}Please contact me if you need assistance.
956
957 $gMaintainer
958 (administrator, $gProject $gBugs database)
959 $extras
960 END
961
962 $repliedshow= join(', ',$replyto,@maintccaddrs);
963 &filelock("lock/-1");
964 open(AP,">>db-h/-1.log") || &quit("open db-h/-1.log: $!");
965 print(AP
966       "\2\n$repliedshow\n\5\n$reply\n\3\n".
967       "\6\n".
968       "<strong>Request received</strong> from <code>".
969       &sani($header{'from'})."</code>\n".
970       "to <code>".&sani($controlrequestaddr)."</code>\n".
971       "\3\n".
972       "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/-1.log: $!");
973 close(AP) || &quit("open db-h/-1.log: $!");
974 &unfilelock;
975 utime(time,time,"db-h");
976
977 &sendmailmessage($reply,$replyto,@maintccaddrs,@bcc);
978
979 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
980
981 sub sendmailmessage {
982     local ($message,@recips) = @_;
983     $message = "X-Loop: $gMaintainerEmail\n" . $message;
984     send_mail_message(message    => $message,
985                       recipients => \@recips,
986                      );
987     $midix++;
988 }
989
990 sub sendhelp {
991         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
992         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
993             if $control;
994 }
995
996 #sub unimplemented {
997 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
998 #}
999
1000 sub checkmatch {
1001     local ($string,$mvarname,$svarvalue) = @_;
1002     local ($mvarvalue);
1003     if (@newmergelist) {
1004         eval "\$mvarvalue= \$$mvarname";
1005         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
1006             if $dl;
1007         $mismatch .=
1008             "Values for \`$string' don't match:\n".
1009             " #$newmergelist[0] has \`$mvarvalue';\n".
1010             " #$ref has \`$svarvalue'\n"
1011             if $mvarvalue ne $svarvalue;
1012     } else {
1013         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
1014             if $dl;
1015         eval "\$$mvarname= \$svarvalue";
1016     }
1017 }
1018
1019 sub checkpkglimit {
1020     if (keys %limit_pkgs and not defined $limit_pkgs{$data->{package}}) {
1021         &transcript("$gBug number $ref belongs to package $data->{package}, skipping.\n\n");
1022         return 0;
1023     }
1024     return 1;
1025 }
1026
1027 sub manipset {
1028     my $list = shift;
1029     my $elt = shift;
1030     my $add = shift;
1031
1032     my %h = map { $_ => 1 } split ' ', $list;
1033     if ($add) {
1034         $h{$elt}=1;
1035     }
1036     else {
1037         delete $h{$elt};
1038     }
1039     return join ' ', sort keys %h;
1040 }
1041
1042 # High-level bug manipulation calls
1043 # Do announcements themselves
1044 #
1045 # Possible calling sequences:
1046 #    setbug (returns 0)
1047 #    
1048 #    setbug (returns 1)
1049 #    &transcript(something)
1050 #    nochangebug
1051 #
1052 #    setbug (returns 1)
1053 #    $action= (something)
1054 #    do {
1055 #      (modify s_* variables)
1056 #    } while (getnextbug);
1057
1058 sub nochangebug {
1059     &dlen("nochangebug");
1060     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1061     &cancelbug;
1062     &endmerge if $manybugs;
1063     $state= 'idle';
1064     &dlex("nochangebug");
1065 }
1066
1067 sub setbug {
1068     &dlen("setbug $ref");
1069     if ($ref =~ m/^-\d+/) {
1070         if (!defined $clonebugs{$ref}) {
1071             &notfoundbug;
1072             &dlex("setbug => noclone");
1073             return 0;
1074         }
1075         $ref = $clonebugs{$ref};
1076     }
1077     $state eq 'idle' || die "$state ?";
1078     if (!&getbug) {
1079         &notfoundbug;
1080         &dlex("setbug => 0s");
1081         return 0;
1082     }
1083
1084     if (!&checkpkglimit) {
1085         &cancelbug;
1086         return 0;
1087     }
1088
1089     @thisbugmergelist= split(/ /,$data->{mergedwith});
1090     if (!@thisbugmergelist) {
1091         &foundbug;
1092         $manybugs= 0;
1093         $state= 'single';
1094         $sref=$ref;
1095         &dlex("setbug => 1s");
1096         return 1;
1097     }
1098     &cancelbug;
1099     &getmerge;
1100     $manybugs= 1;
1101     if (!&getbug) {
1102         &notfoundbug;
1103         &endmerge;
1104         &dlex("setbug => 0mc");
1105         return 0;
1106     }
1107     &foundbug;
1108     $state= 'multiple'; $sref=$ref;
1109     &dlex("setbug => 1m");
1110     return 1;
1111 }
1112
1113 sub getnextbug {
1114     &dlen("getnextbug");
1115     $state eq 'single' || $state eq 'multiple' || die "$state ?";
1116     &savebug;
1117     if (!$manybugs || !@thisbugmergelist) {
1118         length($action) || die;
1119         &transcript("$action\n$extramessage\n");
1120         &endmerge if $manybugs;
1121         $state= 'idle';
1122         &dlex("getnextbug => 0");
1123         return 0;
1124     }
1125     $ref= shift(@thisbugmergelist);
1126     &getbug || die "bug $ref disappeared";
1127     &foundbug;
1128     &dlex("getnextbug => 1");
1129     return 1;
1130 }
1131
1132 # Low-level bug-manipulation calls
1133 # Do no announcements
1134 #
1135 #    getbug (returns 0)
1136 #
1137 #    getbug (returns 1)
1138 #    cancelbug
1139 #
1140 #    getmerge
1141 #    $action= (something)
1142 #    getbug (returns 1)
1143 #    savebug/cancelbug
1144 #    getbug (returns 1)
1145 #    savebug/cancelbug
1146 #    [getbug (returns 0)]
1147 #    &transcript("$action\n\n")
1148 #    endmerge
1149
1150 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
1151 sub foundbug { &transcript("$gBug#$ref: $data->{subject}\n"); }
1152
1153 sub getmerge {
1154     &dlen("getmerge");
1155     $mergelowstate eq 'idle' || die "$mergelowstate ?";
1156     &filelock('lock/merge');
1157     $mergelowstate='locked';
1158     &dlex("getmerge");
1159 }
1160
1161 sub endmerge {
1162     &dlen("endmerge");
1163     $mergelowstate eq 'locked' || die "$mergelowstate ?";
1164     &unfilelock;
1165     $mergelowstate='idle';
1166     &dlex("endmerge");
1167 }
1168
1169 sub getbug {
1170     &dlen("getbug $ref");
1171     $lowstate eq 'idle' || die "$state ?";
1172     if (($data = &lockreadbug($ref))) {
1173         $sref= $ref;
1174         $lowstate= "open";
1175         &dlex("getbug => 1");
1176         $extramessage='';
1177         return 1;
1178     }
1179     $lowstate= 'idle';
1180     &dlex("getbug => 0");
1181     return 0;
1182 }
1183
1184 sub cancelbug {
1185     &dlen("cancelbug");
1186     $lowstate eq 'open' || die "$state ?";
1187     &unfilelock;
1188     $lowstate= 'idle';
1189     &dlex("cancelbug");
1190 }
1191
1192 sub savebug {
1193     &dlen("savebug $ref");
1194     $lowstate eq 'open' || die "$lowstate ?";
1195     length($action) || die;
1196     $ref == $sref || die "read $sref but saving $ref ?";
1197     my $hash = get_hashname($ref);
1198     open(L,">>db-h/$hash/$ref.log") || &quit("opening db-h/$hash/$ref.log: $!");
1199     print(L
1200           "\6\n".
1201           "<strong>".&sani($action)."</strong>\n".
1202           "Request was from <code>".&sani($header{'from'})."</code>\n".
1203           "to <code>".&sani($controlrequestaddr)."</code>. \n".
1204           "\3\n".
1205           "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/$hash/$ref.log: $!");
1206     close(L) || &quit("closing db-h/$hash/$ref.log: $!");
1207     unlockwritebug($ref, $data);
1208     $lowstate= "idle";
1209     &dlex("savebug");
1210 }
1211
1212 sub dlen {
1213     return if !$dl;
1214     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
1215 }
1216
1217 sub dlex {
1218     return if !$dl;
1219     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
1220 }
1221
1222 sub transcript {
1223     print $_[0] if $debug;
1224     $transcript.= $_[0];
1225 }
1226
1227 sub urlsanit {
1228     my $url = shift;
1229     $url =~ s/%/%25/g;
1230     $url =~ s/\+/%2b/g;
1231     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
1232     $url =~ s/([<>&"])/\&$saniarray{$1};/g;
1233     return $url;
1234 }
1235
1236 sub sendlynxdoc {
1237     &sendlynxdocraw;
1238     &transcript("\n");
1239     $ok++;
1240 }
1241
1242 sub sendtxthelp {
1243     &sendtxthelpraw;
1244     &transcript("\n");
1245     $ok++;
1246 }
1247
1248 sub sendtxthelpraw {
1249     local ($relpath,$description) = @_;
1250     $doc='';
1251     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
1252     while(<D>) { $doc.=$_; }
1253     close(D);
1254     &transcript("Sending $description in separate message.\n");
1255     &sendmailmessage(<<END.$doc,$replyto);
1256 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1257 To: $replyto
1258 Subject: $gProject $gBug help: $description
1259 References: $header{'message-id'}
1260 In-Reply-To: $header{'message-id'}
1261 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
1262 Precedence: bulk
1263 X-$gProject-PR-Message: doc-text $relpath
1264
1265 END
1266     $ok++;
1267 }
1268
1269 sub sendlynxdocraw {
1270     local ($relpath,$description) = @_;
1271     $doc='';
1272     open(L,"lynx -nolist -dump http://$gCGIDomain/\Q$relpath\E 2>&1 |") || &quit("fork for lynx: $!");
1273     while(<L>) { $doc.=$_; }
1274     $!=0; close(L);
1275     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1276         &transcript("Information ($description) is not available -\n".
1277                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
1278          $ok++;
1279     } elsif ($?) {
1280         &transcript("Error getting $description (code $? $!):\n$doc\n");
1281     } else {
1282         &transcript("Sending $description.\n");
1283         &sendmailmessage(<<END.$doc,$replyto);
1284 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1285 To: $replyto
1286 Subject: $gProject $gBugs information: $description
1287 References: $header{'message-id'}
1288 In-Reply-To: $header{'message-id'}
1289 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1290 Precedence: bulk
1291 X-$gProject-PR-Message: doc-html $relpath
1292
1293 END
1294          $ok++;
1295     }
1296 }
1297
1298 sub addccaddress {
1299     my ($cca) = @_;
1300     $maintccreasons{$cca}{''}{$ref}= 1;
1301 }
1302
1303 sub addmaintainers {
1304     # Data structure is:
1305     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
1306     my $data = shift;
1307     my ($p, $addmaint);
1308     &ensuremaintainersloaded;
1309     $anymaintfound=0; $anymaintnotfound=0;
1310     for $p (split(m/[ \t?,():]+/, $data->{package})) {
1311         $p =~ y/A-Z/a-z/;
1312         $p =~ /([a-z0-9.+-]+)/;
1313         $p = $1;
1314         next unless defined $p;
1315         if (defined $gSubscriptionDomain) {
1316             if (defined($pkgsrc{$p})) {
1317                 addbcc("$pkgsrc{$p}\@$gSubscriptionDomain");
1318             } else {
1319                 addbcc("$p\@$gSubscriptionDomain");
1320             }
1321         }
1322         if (defined $data->{severity} and defined $gStrongList and
1323                 isstrongseverity($data->{severity})) {
1324             addbcc("$gStrongList\@$gListDomain");
1325         }
1326         if (defined($maintainerof{$p})) {
1327             $addmaint= $maintainerof{$p};
1328             &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
1329             $maintccreasons{$addmaint}{$p}{$ref}= 1;
1330             print "maintainer add >$p|$addmaint<\n" if $debug;
1331         } else { 
1332             print "maintainer none >$p<\n" if $debug; 
1333             &transcript("Warning: Unknown package '$p'\n");
1334             &transcript("MR|unknown-package|$p|$ref|\n") if $dl>2;
1335             $maintccreasons{$gUnknownMaintainerEmail}{$p}{$ref}= 1;
1336         }
1337     }
1338
1339     if (length $data->{owner}) {
1340         $addmaint = $data->{owner};
1341         &transcript("MO|$addmaint|$data->{package}|$ref|\n") if $dl>2;
1342         $maintccreasons{$addmaint}{$data->{package}}{$ref} = 1;
1343         print "owner add >$data->{package}|$addmaint<\n" if $debug;
1344     }
1345 }
1346
1347 sub ensuremaintainersloaded {
1348     my ($a,$b);
1349     return if $maintainersloaded++;
1350     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
1351     while (<MAINT>) {
1352         m/^\n$/ && next;
1353         m/^\s*$/ && next;
1354         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers bogus \`$_'");
1355         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1356         $maintainerof{$1}= $2;
1357     }
1358     close(MAINT);
1359     open(MAINT,"$gMaintainerFileOverride") || die &quit("maintainers.override open: $!");
1360     while (<MAINT>) {
1361         m/^\n$/ && next;
1362         m/^\s*$/ && next;
1363         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers.override bogus \`$_'");
1364         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1365         $maintainerof{$1}= $2;
1366     }
1367
1368     open(SOURCES, "$gPackageSource") || &quit("pkgsrc open: $!");
1369     while (<SOURCES>) {
1370         next unless m/^(\S+)\s+\S+\s+(\S.*\S)\s*$/;
1371         my ($a, $b) = ($1, $2);
1372         $pkgsrc{lc($a)} = $b;
1373     }
1374     close(SOURCES);
1375 }
1376
1377 sub sendinfo {
1378     local ($wherefrom,$path,$description) = @_;
1379     if ($wherefrom eq "ftp.d.o") {
1380       $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or &quit("fork for lynx/gunzip: $!");
1381       $! = 0;
1382       if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1383           &transcript("$description is not available.\n");
1384           $ok++; return;
1385       } elsif ($?) {
1386           &transcript("Error getting $description (code $? $!):\n$doc\n");
1387           return;
1388       }
1389     } elsif ($wherefrom eq "local") {
1390       open P, "$path";
1391       $doc = do { local $/; <P> };
1392       close P;
1393     } else {
1394       &transcript("internal errror: info files location unknown.\n");
1395       $ok++; return;
1396     }
1397     &transcript("Sending $description.\n");
1398     &sendmailmessage(<<END.$doc,$replyto);
1399 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1400 To: $replyto
1401 Subject: $gProject $gBugs information: $description
1402 References: $header{'message-id'}
1403 In-Reply-To: $header{'message-id'}
1404 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1405 Precedence: bulk
1406 X-$gProject-PR-Message: getinfo
1407
1408 $description follows:
1409
1410 END
1411     $ok++;
1412     &transcript("\n");
1413 }