]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
3cd5e524c6db41b2b0f8a7ba216400f3ffef5583
[debbugs.git] / scripts / service.in
1 #!/usr/bin/perl
2 # $Id: service.in,v 1.67 2003/02/15 00:44:20 cjwatson Exp $
3 # ^ more or less ^
4 #
5 # Usage: service <code>.nn
6 # Temps:  incoming/P<code>.nn
7
8 use Mail::Address;
9 use File::Copy;
10 use MIME::Parser;
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
52 if ($entity and $entity->head->tags) {
53     @headerlines = @{$entity->head->header};
54     chomp @headerlines;
55
56     my $entity_body = getmailbody($entity);
57     @bodylines = $entity_body ? $entity_body->as_lines() : ();
58     chomp @bodylines;
59 } else {
60     # Legacy pre-MIME code, kept around in case MIME::Parser fails.
61     my $i;
62     for ($i = 0; $i <= $#msg; $i++) {
63         $_ = $msg[$i];
64         last unless length($_);
65         while ($msg[$i+1] =~ m/^\s/) {
66             $i++;
67             $_ .= "\n".$msg[$i];
68         }
69         push @headerlines, $_;
70     }
71
72     @bodylines = @msg[$i..$#msg];
73 }
74
75 for (@headerlines) {
76     s/\n\s/ /g;
77     print ">$_<\n" if $debug;
78     if (s/^(\S+):\s*//) {
79         my $v = lc $1;
80         print ">$v=$_<\n" if $debug;
81         $header{$v} = $_;
82     } else {
83         print "!>$_<\n" if $debug;
84     }
85 }
86
87 # Strip off RFC2440-style PGP clearsigning.
88 if (@bodylines and $bodylines[0] =~ /^-----BEGIN PGP SIGNED/) {
89     shift @bodylines while @bodylines and length $bodylines[0];
90     shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
91     for my $findsig (0 .. $#bodylines) {
92         if ($bodylines[$findsig] =~ /^-----BEGIN PGP SIGNATURE/) {
93             $#bodylines = $findsig - 1;
94             last;
95         }
96     }
97     map { s/^- // } @bodylines;
98 }
99
100 grep(s/\s+$//,@bodylines);
101
102 print "***\n",join("\n",@bodylines),"\n***\n" if $debug;
103
104 if (defined $header{'resent-from'} && !defined $header{'from'}) {
105     $header{'from'} = $header{'resent-from'};
106 }
107
108 defined($header{'from'}) || &quit("no From header");
109
110 delete $header{'reply-to'} 
111         if ( defined($header{'reply-to'}) && $header{'reply-to'} =~ m/^\s*$/ );
112
113 if ( defined($header{'reply-to'}) && $header{'reply-to'} ne "" ) {
114     $replyto = $header{'reply-to'};
115 } else {
116     $replyto = $header{'from'};
117 }
118
119 $controlrequestaddr= $control ? "control\@$gEmailDomain" : "request\@$gEmailDomain";
120 $transcript='';
121 &transcript("Processing commands for $controlrequestaddr:\n\n");
122
123 $dl= 0;
124 $state= 'idle';
125 $lowstate= 'idle';
126 $mergelowstate= 'idle';
127 $midix=0;    
128 $extras="";
129
130 my $quickabort = 0;
131
132 my $fuckheads = "(" . join("|", @gFuckheads) . ")";
133 if (@gFuckheads and $replyto =~ m/$fuckheads/) {
134         &transcript("This service is unavailable.\n\n");
135         $quickabort = 1;
136 }
137
138 my %clonebugs = ();
139 my @bcc = ();
140
141 for ($procline=0; $procline<=$#bodylines; $procline++) {
142     $state eq 'idle' || print "$state ?\n";
143     $lowstate eq 'idle' || print "$lowstate ?\n";
144     $mergelowstate eq 'idle' || print "$mergelowstate ?\n";
145     if ($quickabort) {
146          &transcript("Stopping processing here.\n\n");
147          last;
148     }
149     $_= $bodylines[$procline]; s/\s+$//;
150     next unless m/\S/; next if m/^\s*\#/;
151     &transcript("> $_\n");
152     $action= '';
153     if (m/^stop/i || m/^quit/i || m/^--/ || m/^thank/i) {
154         &transcript("Stopping processing here.\n\n");
155         last;
156     } elsif (m/^debug\s+(\d+)$/i && $1 >= 0 && $1 <= 1000) {
157         $dl= $1+0;
158         &transcript("Debug level $dl.\n\n");
159     } elsif (m/^(send|get)\s+\#?(\d{2,})$/i) {
160         $ref= $2+0;
161         &sendlynxdoc("$ref","logs for $gBug#$ref");
162     } elsif (m/^send-detail\s+\#?(\d+)$/i) {
163         &transcript("This BTS function is currently disabled, sorry.\n\n");
164         $ok++; # well, it's not really ok, but it fixes #81224 :)
165     } elsif (m/^index(\s+full)?$/i) {
166         &transcript("This BTS function is currently disabled, sorry.\n\n");
167         $ok++; # well, it's not really ok, but it fixes #81224 :)
168     } elsif (m/^index-summary\s+by-package$/i) {
169         &transcript("This BTS function is currently disabled, sorry.\n\n");
170         $ok++; # well, it's not really ok, but it fixes #81224 :)
171     } elsif (m/^index-summary(\s+by-number)?$/i) {
172         &transcript("This BTS function is currently disabled, sorry.\n\n");
173         $ok++; # well, it's not really ok, but it fixes #81224 :)
174     } elsif (m/^index(\s+|-)pack(age)?s?$/i) {
175         &sendlynxdoc("cgi-bin/pkgindex.cgi?indexon=pkg",'index of packages');
176     } elsif (m/^index(\s+|-)maints?$/i) {
177         &sendlynxdoc("cgi-bin/pkgindex.cgi?indexon=maint",'index of maintainers');
178     } elsif (m/^index(\s+|-)maint\s+(\S+)$/i) {
179         $maint = $2;
180 # check for crap in $maint
181         if ($maint =~ m/^\./ || $maint !~ m/^[\d\w-+.@]+$/) {
182                 &transcript("Invalid request $maint!\n");
183                 next;
184         }
185         &sendlynxdoc("$maint","$gBug list for maintainer \`$maint'");
186         $ok++;
187     } elsif (m/^index(\s+|-)pack(age)?s?\s+(\S.*\S)$/i) {
188         $package = $+;
189 # check for crap in $package
190         if ($package =~ m/^\./ || $package !~ m/^[\d\w-+.@]+$/) {
191                 &transcript("Invalid request $package!\n");
192                 next;
193         }
194         &sendlynxdoc("$package","$gBug list for package $package sent.\n\n");
195         $ok++;
196     } elsif (m/^send-unmatched(\s+this|\s+-?0)?$/i) {
197         &transcript("This BTS function is currently disabled, sorry.\n\n");
198         $ok++; # well, it's not really ok, but it fixes #81224 :)
199     } elsif (m/^send-unmatched\s+(last|-1)$/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+(old|-2)$/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/^getinfo\s+([\w-.]+)$/i) {
206         # the following is basically a Debian-specific kludge, but who cares
207         $req = $1;
208         if ($req =~ /^maintainers$/i && -f "$gConfigDir/Maintainers") {
209             &sendinfo("local", "$gConfigDir/Maintainers", "Maintainers file");
210         } elsif ($req =~ /^override\.(\w+)\.([\w-.]+)$/i) {
211             $req =~ s/.gz$//;
212             &sendinfo("ftp.d.o", "$req", "override file for $2 part of $1 distribution");
213         } elsif ($req =~ /^pseudo-packages\.(description|maintainers)$/i && -f "$gConfigDir/$req") {
214             &sendinfo("local", "$gConfigDir/$req", "$req file");
215         } else {
216             &transcript("Info file $req does not exist.\n\n");
217         }
218     } elsif (m/^help/i) {
219         &sendhelp;
220         &transcript("\n");
221         $ok++;
222     } elsif (m/^refcard/i) {
223         &sendtxthelp("bug-mailserver-refcard.txt","mail servers' reference card");
224     } elsif (m/^subscribe/i) {
225         &transcript(<<END);
226 There is no $gProject $gBug mailing list.  If you wish to review bug reports
227 please do so via http://$gWebDomain/ or ask this mail server
228 to send them to you.
229 soon: MAILINGLISTS_TEXT
230 END
231     } elsif (m/^unsubscribe/i) {
232         &transcript(<<END);
233 soon: UNSUBSCRIBE_TEXT
234 soon: MAILINGLISTS_TEXT
235 END
236     } elsif (!$control) {
237         &transcript(<<END);
238 Unknown command or malformed arguments to command.
239 (Use control\@$gEmailDomain to manipulate reports.)
240
241 END
242         if (++$unknowns >= 3) {
243             &transcript("Too many unknown commands, stopping here.\n\n");
244             last;
245         }
246 #### interesting ones start here
247     } elsif (m/^close\s+\#?(-?\d+)$/i) {
248         $ok++;
249         $ref= $1;
250         if (&setbug) {
251             &transcript("'close' is deprecated; see http://$gWebDomain/Developer$gHTMLSuffix#closing.\n");
252             if (length($s_done)) {
253                 &transcript("$gBug is already closed, cannot re-close.\n\n");
254                 &nochangebug;
255             } else {
256                 $action= "$gBug closed, send any further explanations to $s_originator";
257                 do {
258                     &addmaintainers($s_package);
259                                         if ( length( $gDoneList ) > 0 && length( $gListDomain ) >
260                                         0 ) { &addccaddress("$gDoneList\@$gListDomain"); }
261                     $s_done= $replyto;
262                     $message= <<END;
263 From: $gMaintainerEmail ($gProject $gBug Tracking System)
264 To: $s_originator
265 Subject: $gBug#$ref acknowledged by developer
266          ($header{'subject'})
267 References: $header{'message-id'} $s_msgid
268 In-Reply-To: $s_msgid
269 Message-ID: <handler.$ref.$nn.notifdonectrl.$midix\@$gEmailDomain>
270 Reply-To: $ref\@$gEmailDomain
271
272 This is an automatic notification regarding your $gBug report
273 #$ref: $s_subject,
274 which was filed against the $s_package package.
275
276 It has been marked as closed by one of the developers, namely
277 $replyto.
278
279 You should be hearing from them with a substantive response shortly,
280 in case you haven't already. If not, please contact them directly.
281
282 $gMaintainer
283 (administrator, $gProject $gBugs database)
284
285 END
286                     &sendmailmessage($message,$s_originator);
287                 } while (&getnextbug);
288             }
289         }
290     } elsif (m/^reassign\s+\#?(-?\d+)\s+(\S.*\S)$/i) {
291         $ok++;
292         $ref= $1; $newpackage= $2;
293         $newpackage =~ y/A-Z/a-z/;
294         if (&setbug) {
295             if (length($s_package)) {
296                 $action= "$gBug reassigned from package \`$s_package'".
297                          " to \`$newpackage'.";
298             } else {
299                 $action= "$gBug assigned to package \`$newpackage'.";
300             }
301             do {
302                 &addmaintainers($s_package);
303                 &addmaintainers($newpackage);
304                 $s_package= $newpackage;
305             } while (&getnextbug);
306         }
307     } elsif (m/^reopen\s+\#?(-?\d+)$/i ? ($noriginator='', 1) :
308              m/^reopen\s+\#?(-?\d+)\s+\=$/i ? ($noriginator='', 1) :
309              m/^reopen\s+\#?(-?\d+)\s+\!$/i ? ($noriginator=$replyto, 1) :
310              m/^reopen\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($noriginator=$2, 1) : 0) {
311         $ok++;
312         $ref= $1;
313         if (&setbug) {
314             if (!length($s_done)) {
315                 &transcript("$gBug is already open, cannot reopen.\n\n");
316                 &nochangebug;
317             } else {
318                 $action=
319                     $noriginator eq '' ? "$gBug reopened, originator not changed." :
320                         "$gBug reopened, originator set to $noriginator.";
321                 do {
322                     &addmaintainers($s_package);
323                     $s_originator= $noriginator eq '' ?  $s_originator : $noriginator;
324                     $s_done= '';
325                 } while (&getnextbug);
326             }
327         }
328     } elsif (m/^submitter\s+\#?(-?\d+)\s+\!$/i ? ($newsubmitter=$replyto, 1) :
329              m/^submitter\s+\#?(-?\d+)\s+(\S.*\S)$/i ? ($newsubmitter=$2, 1) : 0) {
330         $ok++;
331         $ref= $1;
332         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
333             $ref = $clonebugs{$ref};
334         }
335         if (&getbug) {
336             &foundbug;
337             &addmaintainers($s_package);
338             $oldsubmitter= $s_originator;
339             $s_originator= $newsubmitter;
340             $action= "Changed $gBug submitter from $oldsubmitter to $newsubmitter.";
341             &savebug;
342             &transcript("$action\n");
343             if (length($s_done)) {
344                 &transcript("(By the way, that $gBug is currently marked as done.)\n");
345             }
346             &transcript("\n");
347             $message= <<END;
348 From: $gMaintainerEmail ($gProject $gBug Tracking System)
349 To: $oldsubmitter
350 Subject: $gBug#$ref submitter address changed
351          ($header{'subject'})
352 References: $header{'message-id'} $s_msgid
353 In-Reply-To: $s_msgid
354 Message-ID: <handler.$ref.$nn.newsubmitter.$midix\@$gEmailDomain>
355 Reply-To: $ref\@$gEmailDomain
356
357 The submitter address recorded for your $gBug report
358 #$ref: $s_subject
359 has been changed.
360
361 The new submitter address for this report is
362 $newsubmitter.
363
364 This change was made by
365 $replyto.
366 If it was incorrect, please contact them directly.
367
368 $gMaintainer
369 (administrator, $gProject $gBugs database)
370
371 END
372             &sendmailmessage($message,$oldsubmitter);
373         } else {
374             &notfoundbug;
375         }
376     } elsif (m/^forwarded\s+\#?(-?\d+)\s+(\S.*\S)$/i) {
377         $ok++;
378         $ref= $1; $whereto= $2;
379         if (&setbug) {
380             if (length($s_forwarded)) {
381     $action= "Forwarded-to-address changed from $s_forwarded to $whereto.";
382             } else {
383     $action= "Noted your statement that $gBug has been forwarded to $whereto.";
384             }
385             if (length($s_done)) {
386                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
387             }
388             do {
389                 &addmaintainers($s_package);
390                 if (length($gFowardList)>0 && length($gListDomain)>0 ) {
391                      &addccaddress("$gFowardList\@$gListDomain"); 
392                 }
393                 $s_forwarded= $whereto;
394             } while (&getnextbug);
395         }
396     } elsif (m/^notforwarded\s+\#?(-?\d+)$/i) {
397         $ok++;
398         $ref= $1;
399         if (&setbug) {
400             if (!length($s_forwarded)) {
401                 &transcript("$gBug is not marked as having been forwarded.\n\n");
402                 &nochangebug;
403             } else {
404     $action= "Removed annotation that $gBug had been forwarded to $s_forwarded.";
405                 do {
406                     &addmaintainers($s_package);
407                     $s_forwarded= '';
408                 } while (&getnextbug);
409             }
410         }
411     } elsif (m/^severity\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i ||
412         m/^priority\s+\#?(-?\d+)\s+([-0-9a-z]+)$/i) {
413         $ok++;
414         $ref= $1;
415         $newseverity= $2;
416         if (!grep($_ eq $newseverity, @gSeverityList, "$gDefaultSeverity")) {
417             &transcript("Severity level \`$newseverity' is not known.\n".
418                         "Recognized are: $gShowSeverities.\n\n");
419         } elsif (&setbug) {
420             $printseverity= $s_severity;
421             $printseverity= "$gDefaultSeverity" if $printseverity eq '';
422             $action= "Severity set to \`$newseverity'.";
423             do {
424                 &addmaintainers($s_package);
425                 $s_severity= $newseverity;
426             } while (&getnextbug);
427         }
428     } elsif (m/^tags?\s+\#?(-?\d+)\s+(([=+-])\s*)?(\S.*)?$/i) {
429         $ok++;
430         $ref = $1; $addsubcode = $3; $tags = $4;
431         $addsub = "add";
432         if (defined $addsubcode) {
433             $addsub = "sub" if ($addsubcode eq "-");
434             $addsub = "add" if ($addsubcode eq "+");
435             $addsub = "set" if ($addsubcode eq "=");
436         }
437         my @okaytags = ();
438         my @badtags = ();
439         foreach my $t (split /[\s,]+/, $tags) {
440             if (!grep($_ eq $t, @gTags)) {
441                 push @badtags, $t;
442             } else {
443                 push @okaytags, $t;
444             }
445         }
446         if (@badtags) {
447             &transcript("Unknown tag/s: ".join(', ', @badtags).".\n".
448                         "Recognized are: ".join(' ', @gTags).".\n\n");
449         }
450         if (&setbug) {
451             if ($s_keywords eq '') {
452                 &transcript("There were no tags set.\n");
453             } else {
454                 &transcript("Tags were: $s_keywords\n");
455             }
456             if ($addsub eq "set") {
457                 $action= "Tags set to: " . join(", ", @okaytags);
458             } elsif ($addsub eq "add") {
459                 $action= "Tags added: " . join(", ", @okaytags);
460             } elsif ($addsub eq "sub") {
461                 $action= "Tags removed: " . join(", ", @okaytags);
462             }
463             do {
464                 &addmaintainers($s_package);
465                 $s_keywords = '' if ($addsub eq "set");
466                 if ($addsub eq "sub") {
467                     foreach my $t (@badtags) {
468                         $s_keywords = join ' ', grep $_ ne $t, 
469                             split ' ', $s_keywords;
470                     }
471                 }
472                 foreach my $t (@okaytags) {
473                     $s_keywords = join ' ', grep $_ ne $t, 
474                         split ' ', $s_keywords;
475                     $s_keywords = "$t $s_keywords" unless($addsub eq "sub");
476                 }
477                 $s_keywords =~ s/\s*$//;
478             } while (&getnextbug);
479         }
480     } elsif (m/^retitle\s+\#?(-?\d+)\s+(\S.*\S)\s*$/i) {
481         $ok++;
482         $ref= $1; $newtitle= $2;
483         if ($ref =~ m/^-\d+$/ && defined $clonebugs{$ref}) {
484             $ref = $clonebugs{$ref};
485         }
486         if (&getbug) {
487             &foundbug;
488             &addmaintainers($s_package);
489             $s_subject= $newtitle;
490             $action= "Changed $gBug title.";
491             &savebug;
492             &transcript("$action\n");
493             if (length($s_done)) {
494                 &transcript("(By the way, that $gBug is currently marked as done.)\n");
495             }
496             &transcript("\n");
497         } else {
498             &notfoundbug;
499         }
500     } elsif (m/^unmerge\s+\#?(-?\d+)$/i) {
501         $ok++;
502         $ref= $1;
503         if (&setbug) {
504             if (!length($s_mergedwith)) {
505                 &transcript("$gBug is not marked as being merged with any others.\n\n");
506                 &nochangebug;
507             } else {
508                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
509                 $action= "Disconnected #$ref from all other report(s).";
510                 @newmergelist= split(/ /,$s_mergedwith);
511                 $discref= $ref;
512                 do {
513                     &addmaintainers($s_package);
514                     $s_mergedwith= ($ref == $discref) ? ''
515                         : join(' ',grep($_ ne $ref,@newmergelist));
516                 } while (&getnextbug);
517             }
518         }
519     } elsif (m/^merge\s+(\d+(\s+\d+)+)\s*$/i) {
520         $ok++;
521         @tomerge= sort { $a <=> $b } split(/\s+/,$1);
522         @newmergelist= ();
523         my %tags = ();
524         &getmerge;
525         while (defined($ref= shift(@tomerge))) {
526             &transcript("D| checking merge $ref\n") if $dl;
527             $ref+= 0;
528             next if grep($_ eq $ref,@newmergelist);
529             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
530             &foundbug;
531             &transcript("D| adding $ref ($s_mergedwith)\n") if $dl;
532             $mismatch= '';
533             &checkmatch('package','m_package',$s_package);
534             &checkmatch('forwarded addr','m_forwarded',$s_forwarded);
535             $s_severity = '$gDefaultSeverity' if $s_severity eq '';
536             &checkmatch('severity','m_severity',$s_severity);
537             &checkmatch('done mark','m_done',length($s_done) ? 'done' : 'open');
538             foreach my $t (split /\s+/, $s_keywords) { $tags{$t} = 1; }
539             if (length($mismatch)) {
540                 &transcript("Mismatch - only $gBugs in same state can be merged:\n".
541                             $mismatch."\n");
542                 &cancelbug; @newmergelist=(); last;
543             }
544             push(@newmergelist,$ref);
545             push(@tomerge,split(/ /,$s_mergedwith));
546             &cancelbug;
547         }
548         if (@newmergelist) {
549             @newmergelist= sort { $a <=> $b } @newmergelist;
550             $action= "Merged @newmergelist.";
551             for $ref (@newmergelist) {
552                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
553                 &addmaintainers($s_package);
554                 $s_mergedwith= join(' ',grep($_ ne $ref,@newmergelist));
555                 $s_keywords= join(' ', keys %tags);
556                 &savebug;
557             }
558             &transcript("$action\n\n");
559         }
560         &endmerge;
561     } elsif (m/^clone\s+#?(\d+)\s+((-\d+\s+)*-\d+)\s*$/i) {
562         $ok++;
563
564         $origref = $1;
565         @newclonedids = split /\s+/, $2;
566         $newbugsneeded = scalar(@newclonedids);
567
568         $ref = $origref;
569         if (&setbug) {
570             if (length($s_mergedwith)) {
571                 &transcript("$gBug is marked as being merged with others.\n\n");
572                 &nochangebug;
573             } else {
574                 &filelock("nextnumber.lock");
575                 open(N,"nextnumber") || &quit("nextnumber: read: $!");
576                 $v=<N>; $v =~ s/\n$// || &quit("nextnumber bad format");
577                 $firstref= $v+0;  $v += $newbugsneeded;
578                 open(NN,">nextnumber"); print NN "$v\n"; close(NN);
579                 &unfilelock;
580
581                 $lastref = $firstref + $newbugsneeded - 1;
582
583                 if ($newbugsneeded == 1) {
584                     $action= "$gBug $origref cloned as bug $firstref.";
585                 } else {
586                     $action= "$gBug $origref cloned as bugs $firstref-$lastref.";
587                 }
588                 &getnextbug;
589                 my $ohash = get_hashname($origref);
590                 $ref = $firstref;
591                 for $newclonedid (@newclonedids) {
592                     $clonebugs{$newclonedid} = $ref;
593             
594                     my $hash = get_hashname($ref);
595                     copy("db-h/$ohash/$origref.log", "db-h/$hash/$ref.log");
596                     copy("db-h/$ohash/$origref.status", "db-h/$hash/$ref.status");
597                     copy("db-h/$ohash/$origref.report", "db-h/$hash/$ref.report");
598                     &bughook('new', $ref, "$s_originator\n$s_date\n$s_subject\n$s_msgid\n$s_package\n$s_keywords\n$s_done\n$s_forwarded\n$s_mergedwith\n$s_severity\n");
599
600                     $ref++;
601                 }
602             }
603         }
604     } else {
605         &transcript("Unknown command or malformed arguments to command.\n\n");
606         if (++$unknowns >= 5) {
607             &transcript("Too many unknown commands, stopping here.\n\n");
608             last;
609         }
610     }
611 }
612 if ($procline>$#bodylines) {
613     &transcript(">\nEnd of message, stopping processing here.\n\n");
614 }
615 if (!$ok && !quickabort) {
616     &transcript("No commands successfully parsed; sending the help text(s).\n");
617     &sendhelp;
618     &transcript("\n");
619 }
620
621 &transcript("MC\n") if $dl>1;
622 @maintccs= ();
623 for $maint (keys %maintccreasons) {
624 &transcript("MM|$maint|\n") if $dl>1;
625     next if $maint eq $replyto;
626     $reasonstring= '';
627     $reasonsref= $maintccreasons{$maint};
628 &transcript("MY|$maint|\n") if $dl>2;
629     for $p (sort keys %$reasonsref) {
630 &transcript("MP|$p|\n") if $dl>2;
631         $reasonstring.= ', ' if length($reasonstring);
632         $reasonstring.= $p.' ' if length($p);
633         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
634     }
635     if (length($reasonstring) > 40) {
636         (substr $reasonstring, 37) = "...";
637     }
638     $reasonstring = "" if (!defined($reasonstring));
639     push(@maintccs,"$maint ($reasonstring)");
640     push(@maintccaddrs,"$maint");
641 }
642
643 $maintccs = ""; 
644 if (@maintccs) {
645     &transcript("MC|@maintccs|\n") if $dl>2;
646     $maintccs .= "Cc: " . join(",\n    ",@maintccs) . "\n";
647 }
648
649 if (!defined $header{'subject'} || $header{'subject'} eq "") {
650   $header{'subject'} = "your mail";
651 }
652
653 $reply= <<END;
654 From: $gMaintainerEmail ($gProject $gBug Tracking System)
655 To: $replyto
656 ${maintccs}Subject: Processed: $header{'subject'}
657 In-Reply-To: $header{'message-id'}
658 References: $header{'message-id'}
659 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
660
661 ${transcript}Please contact me if you need assistance.
662
663 $gMaintainer
664 (administrator, $gProject $gBugs database)
665 $extras
666 END
667
668 $repliedshow= join(', ',$replyto,@maintccaddrs);
669 &filelock("lock/-1");
670 open(AP,">>db-h/-1.log") || &quit("open db-h/-1.log: $!");
671 print(AP
672       "\2\n$repliedshow\n\5\n$reply\n\3\n".
673       "\6\n".
674       "<strong>Request received</strong> from <code>".
675       &sani($header{'from'})."</code>\n".
676       "to <code>".&sani($controlrequestaddr)."</code>\n".
677       "\3\n".
678       "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/-1.log: $!");
679 close(AP) || &quit("open db-h/-1.log: $!");
680 &unfilelock;
681 utime(time,time,"db-h");
682
683 &sendmailmessage($reply,$replyto,@maintccaddrs,@bcc);
684
685 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
686
687 sub get_addresses {
688     return
689        map { $_->address() }
690        map { Mail::Address->parse($_) } @_;
691 }
692
693 sub sendmailmessage {
694     local ($message,@recips) = @_;
695     $message = "X-Loop: $gMaintainerEmail\n" . $message;
696     print "mailing to >@recips<\n" if $debug;
697     $c= open(D,"|-");
698     defined($c) || &quit("mailing forking for sendmail: $!");
699     if (!$c) { # ie, we are the child process
700         exec '/usr/lib/sendmail','-f'."$gMaintainerEmail",'-odb','-oem','-oi',get_addresses(@recips);
701         die $!;
702     }
703     print(D $message) || &quit("writing to sendmail process: $!");
704     $!=0; close(D); $? && &quit("sendmail gave exit status $? ($!)");
705     $midix++;
706 }
707
708 sub sendhelp {
709         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
710         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
711             if $control;
712 }
713
714 #sub unimplemented {
715 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
716 #}
717
718 sub checkmatch {
719     local ($string,$mvarname,$svarvalue) = @_;
720     local ($mvarvalue);
721     if (@newmergelist) {
722         eval "\$mvarvalue= \$$mvarname";
723         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
724             if $dl;
725         $mismatch .=
726             "Values for \`$string' don't match:\n".
727             " #$newmergelist[0] has \`$mvarvalue';\n".
728             " #$ref has \`$svarvalue'\n"
729             if $mvarvalue ne $svarvalue;
730     } else {
731         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
732             if $dl;
733         eval "\$$mvarname= \$svarvalue";
734     }
735 }
736
737 # High-level bug manipulation calls
738 # Do announcements themselves
739 #
740 # Possible calling sequences:
741 #    setbug (returns 0)
742 #    
743 #    setbug (returns 1)
744 #    &transcript(something)
745 #    nochangebug
746 #
747 #    setbug (returns 1)
748 #    $action= (something)
749 #    do {
750 #      (modify s_* variables)
751 #    } while (getnextbug);
752
753 sub nochangebug {
754     &dlen("nochangebug");
755     $state eq 'single' || $state eq 'multiple' || die "$state ?";
756     &cancelbug;
757     &endmerge if $manybugs;
758     $state= 'idle';
759     &dlex("nochangebug");
760 }
761
762 sub setbug {
763     &dlen("setbug $ref");
764     if ($ref =~ m/^-\d+/) {
765         if (!defined $clonebugs{$ref}) {
766             &notfoundbug;
767             &dlex("setbug => noclone");
768             return 0;
769         }
770         $ref = $clonebugs{$ref};
771     }
772     $state eq 'idle' || die "$state ?";
773     if (!&getbug) {
774         &notfoundbug;
775         &dlex("setbug => 0s");
776         return 0;
777     }
778     @thisbugmergelist= split(/ /,$s_mergedwith);
779     if (!@thisbugmergelist) {
780         &foundbug;
781         $manybugs= 0;
782         $state= 'single';
783         $sref=$ref;
784         &dlex("setbug => 1s");
785         return 1;
786     }
787     &cancelbug;
788     &getmerge;
789     $manybugs= 1;
790     if (!&getbug) {
791         &notfoundbug;
792         &endmerge;
793         &dlex("setbug => 0mc");
794         return 0;
795     }
796     &foundbug;
797     $state= 'multiple'; $sref=$ref;
798     &dlex("setbug => 1m");
799     return 1;
800 }
801
802 sub getnextbug {
803     &dlen("getnextbug");
804     $state eq 'single' || $state eq 'multiple' || die "$state ?";
805     &savebug;
806     if (!$manybugs || !@thisbugmergelist) {
807         length($action) || die;
808         &transcript("$action\n$extramessage\n");
809         &endmerge if $manybugs;
810         $state= 'idle';
811         &dlex("getnextbug => 0");
812         return 0;
813     }
814     $ref= shift(@thisbugmergelist);
815     &getbug || die "bug $ref disappeared";
816     &foundbug;
817     &dlex("getnextbug => 1");
818     return 1;
819 }
820
821 # Low-level bug-manipulation calls
822 # Do no announcements
823 #
824 #    getbug (returns 0)
825 #
826 #    getbug (returns 1)
827 #    cancelbug
828 #
829 #    getmerge
830 #    $action= (something)
831 #    getbug (returns 1)
832 #    savebug/cancelbug
833 #    getbug (returns 1)
834 #    savebug/cancelbug
835 #    [getbug (returns 0)]
836 #    &transcript("$action\n\n")
837 #    endmerge
838
839 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
840 sub foundbug { &transcript("$gBug#$ref: $s_subject\n"); }
841
842 sub getmerge {
843     &dlen("getmerge");
844     $mergelowstate eq 'idle' || die "$mergelowstate ?";
845     &filelock('lock/merge');
846     $mergelowstate='locked';
847     &dlex("getmerge");
848 }
849
850 sub endmerge {
851     &dlen("endmerge");
852     $mergelowstate eq 'locked' || die "$mergelowstate ?";
853     &unfilelock;
854     $mergelowstate='idle';
855     &dlex("endmerge");
856 }
857
858 sub getbug {
859     &dlen("getbug $ref");
860     $lowstate eq 'idle' || die "$state ?";
861     if (&lockreadbug($ref)) {
862         $sref= $ref;
863         $lowstate= "open";
864         &dlex("getbug => 1");
865         $extramessage='';
866         return 1;
867     }
868     $lowstate= 'idle';
869     &dlex("getbug => 0");
870     return 0;
871 }
872
873 sub cancelbug {
874     &dlen("cancelbug");
875     $lowstate eq 'open' || die "$state ?";
876     &unfilelock;
877     $lowstate= 'idle';
878     &dlex("cancelbug");
879 }
880
881 sub savebug {
882     &dlen("savebug $ref");
883     $lowstate eq 'open' || die "$lowstate ?";
884     length($action) || die;
885     $ref == $sref || die "read $sref but saving $ref ?";
886     my $hash = get_hashname($ref);
887     open(L,">>db-h/$hash/$ref.log") || &quit("opening db-h/$hash/$ref.log: $!");
888     print(L
889           "\6\n".
890           "<strong>".&sani($action)."</strong>\n".
891           "Request was from <code>".&sani($header{'from'})."</code>\n".
892           "to <code>".&sani($controlrequestaddr)."</code>. \n".
893           "\3\n".
894           "\7\n",@{escapelog(@log)},"\n\3\n") || &quit("writing db-h/$hash/$ref.log: $!");
895     close(L) || &quit("closing db-h/$hash/$ref.log: $!");
896     open(S,">db-h/$hash/$ref.status.new") || &quit("opening db-h/$hash/$ref.status.new: $!");
897     print(S
898           "$s_originator\n".
899           "$s_date\n".
900           "$s_subject\n".
901           "$s_msgid\n".
902           "$s_package\n".
903           "$s_keywords\n".
904           "$s_done\n".
905           "$s_forwarded\n".
906           "$s_mergedwith\n".
907           "$s_severity\n".
908           "$s_versions\n".
909           "$s_fixed_versions\n") || &quit("writing db-h/$hash/$ref.status.new: $!");
910     close(S) || &quit("closing db-h/$hash/$ref.status.new: $!");
911     rename("db-h/$hash/$ref.status.new","db-h/$hash/$ref.status") ||
912         &quit("installing new db-h/$hash/$ref.status: $!");
913         &bughook('change',$ref,
914           "$s_originator\n".
915           "$s_date\n".
916           "$s_subject\n".
917           "$s_msgid\n".
918           "$s_package\n".
919           "$s_keywords\n".
920           "$s_done\n".
921           "$s_forwarded\n".
922           "$s_mergedwith\n".
923           "$s_severity\n".
924           "$s_versions\n".
925           "$s_fixed_versions\n");
926     &unfilelock;
927     $lowstate= "idle";
928     &dlex("savebug");
929 }
930
931 sub dlen {
932     return if !$dl;
933     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
934 }
935
936 sub dlex {
937     return if !$dl;
938     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
939 }
940
941 sub transcript {
942     print $_[0] if $debug;
943     $transcript.= $_[0];
944 }
945
946 sub sendlynxdoc {
947     &sendlynxdocraw;
948     &transcript("\n");
949     $ok++;
950 }
951
952 sub sendtxthelp {
953     &sendtxthelpraw;
954     &transcript("\n");
955     $ok++;
956 }
957
958 sub sendtxthelpraw {
959     local ($relpath,$description) = @_;
960     $doc='';
961     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
962     while(<D>) { $doc.=$_; }
963     close(D);
964     &transcript("Sending $description in separate message.\n");
965     &sendmailmessage(<<END.$doc,$replyto);
966 From: $gMaintainerEmail ($gProject $gBug Tracking System)
967 To: $replyto
968 Subject: $gProject $gBug help: $description
969 References: $header{'message-id'}
970 In-Reply-To: $header{'message-id'}
971 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
972
973 END
974     $ok++;
975 }
976
977 sub sendlynxdocraw {
978     local ($relpath,$description) = @_;
979     $doc='';
980 # the below oughta use $gWebDomain but it can't
981     open(L,"lynx -nolist -dump http://bugs.debian.org/$relpath 2>&1 |") || &quit("fork for lynx: $!");
982     while(<L>) { $doc.=$_; }
983     $!=0; close(L);
984     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
985         &transcript("Information ($description) is not available -\n".
986                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
987          $ok++;
988     } elsif ($?) {
989         &transcript("Error getting $description (code $? $!):\n$doc\n");
990     } else {
991         &transcript("Sending $description.\n");
992         &sendmailmessage(<<END.$doc,$replyto);
993 From: $gMaintainerEmail ($gProject $gBug Tracking System)
994 To: $replyto
995 Subject: $gProject $gBugs information: $description
996 References: $header{'message-id'}
997 In-Reply-To: $header{'message-id'}
998 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
999
1000 END
1001          $ok++;
1002     }
1003 }
1004
1005 sub addccaddress {
1006     my ($cca) = @_;
1007     $maintccreasons{$cca}{''}{$ref}= 1;
1008 }
1009
1010 sub addmaintainers {
1011     # Data structure is:
1012     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
1013     my ($p, $addmaint, $pshow);
1014     &ensuremaintainersloaded;
1015     $anymaintfound=0; $anymaintnotfound=0;
1016     for $p (split(m/[ \t?,()]+/,$_[0])) {
1017         $p =~ y/A-Z/a-z/;
1018         $pshow= ($p =~ m/[-+.a-z0-9]+/ ? $& : '');
1019         if (defined $gSubscriptionDomain) {
1020             if (defined($pkgsrc{$p})) {
1021                 push @bcc, "$pkgsrc{$p}\@$gSubscriptionDomain";
1022             } else {
1023                 push @bcc, "$p\@$gSubscriptionDomain";
1024             }
1025         }
1026         if (defined($maintainerof{$p})) {
1027             $addmaint= $maintainerof{$p};
1028             &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
1029             $maintccreasons{$addmaint}{$p}{$ref}= 1;
1030             print "maintainer add >$p|$addmaint<\n" if $debug;
1031         } else { 
1032             print "maintainer none >$p<\n" if $debug; 
1033             &transcript("MR|unknown-package|$p|$ref|\n") if $dl>2;
1034             $maintccreasons{$gUnknownMaintainerEmail}{$p}{$ref}= 1;
1035         }
1036     }
1037 }
1038
1039 sub ensuremaintainersloaded {
1040     my ($a,$b);
1041     return if $maintainersloaded++;
1042     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
1043     while (<MAINT>) {
1044         m/^\n$/ && next;
1045         m/^\s*$/ && next;
1046         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers bogus \`$_'");
1047         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1048         $maintainerof{$1}= $2;
1049     }
1050     close(MAINT);
1051     open(MAINT,"$gMaintainerFileOverride") || die &quit("maintainers.override open: $!");
1052     while (<MAINT>) {
1053         m/^\n$/ && next;
1054         m/^\s*$/ && next;
1055         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers.override bogus \`$_'");
1056         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
1057         $maintainerof{$1}= $2;
1058     }
1059
1060     open(SOURCES, "$gPackageSource") || &quit("pkgsrc open: $!");
1061     while (<SOURCES>) {
1062         next unless m/^(\S+)\s+\S+\s+(\S.*\S)\s*$/;
1063         my ($a, $b) = ($1, $2);
1064         $pkgsrc{lc($a)} = $b;
1065     }
1066     close(SOURCES);
1067 }
1068
1069 sub sendinfo {
1070     local ($wherefrom,$path,$description) = @_;
1071     if ($wherefrom eq "ftp.d.o") {
1072       $doc = `lynx -nolist -dump http://ftp.debian.org/debian/indices/$path.gz 2>&1 | gunzip -cf` or &quit("fork for lynx/gunzip: $!");
1073       $! = 0;
1074       if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
1075           &transcript("$description is not available.\n");
1076           $ok++; return;
1077       } elsif ($?) {
1078           &transcript("Error getting $description (code $? $!):\n$doc\n");
1079           return;
1080       }
1081     } elsif ($wherefrom eq "local") {
1082       open P, "$path";
1083       $doc = do { local $/; <P> };
1084       close P;
1085     } else {
1086       &transcript("internal errror: info files location unknown.\n");
1087       $ok++; return;
1088     }
1089     &transcript("Sending $description.\n");
1090     &sendmailmessage(<<END.$doc,$replyto);
1091 From: $gMaintainerEmail ($gProject $gBug Tracking System)
1092 To: $replyto
1093 Subject: $gProject $gBugs information: $description
1094 References: $header{'message-id'}
1095 In-Reply-To: $header{'message-id'}
1096 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
1097
1098 $description follows:
1099
1100 END
1101     $ok++;
1102     &transcript("\n");
1103 }