]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
[project @ 2000-10-06 05:44:45 by doogie]
[debbugs.git] / scripts / service.in
1 #!/usr/bin/perl -w
2 # $Id: service.in,v 1.16 2000/10/06 05:44:45 doogie Exp $
3 #
4 # Usage: service <code>.nn
5 # Temps:  incoming/P<code>.nn
6
7 use Mail::Address;
8 require('/etc/debbugs/config');
9 require('/usr/lib/debbugs/errorlib');
10 $ENV{'PATH'}= '/usr/lib/debbugs'.$ENV{'PATH'};;
11 chdir("$gSpoolDir") || die "chdir spool: $!\n";
12
13 # open(DEBUG,">&4");
14 $debug = 0;
15 $wwwbase= "$gWebDir";
16
17 $_=shift;
18 m/^[RC]\.\d+$/ || &quit("bad argument");
19 $control= m/C/;
20 $nn= $_;
21 if (!rename("incoming/G$nn","incoming/P$nn")) 
22 {       $_=$!.'';  m/no such file or directory/i && exit 0;
23     &quit("renaming to lock: $!");
24 }    
25
26 open(M,"incoming/P$nn");
27 @log=<M>;
28 @msg=@log;
29 close(M);
30
31 grep((s/\n$//,s/\s+$//),@msg);
32
33 print "###\n",join("##\n",@msg),"\n###\n" if $debug;
34
35 chop($tdate= `date -u '+%a, %d %h %Y %T GMT'`);
36 $fwd= <<END;
37 Received: via spool for service; $tdate
38 END
39
40 for ($i=0; $i<=$#msg; $i++) 
41 {       $_ = $msg[$i];
42     last unless length($_);
43     $fwd .= $_."\n";
44     while ($msg[$i+1] =~ m/^\s/) 
45         {       $i++;
46         $fwd .= $msg[$i]."\n" if $ins; # Huh ? Where is ins set ?
47         $_ .= ' '.$msg[$i];
48     }
49         print ">$_<\n" if $debug;
50     if (s/^(\S+):\s*//) 
51         {       $v= $1; $v =~ y/A-Z/a-z/;
52                 print ">$v=$_<\n" if $debug;
53         $header{$v}= $_;
54     } else { print "!>$_<\n" if $debug; }
55 }
56
57 defined($header{'from'}) || &quit("no From header");
58 $replyto= defined($header{'reply-to'}) ? $header{'reply-to'} : $header{'from'};
59
60 $controlrequestaddr= $control ? "control\@$gEmailDomain" : "request\@$gEmailDomain";
61 $transcript='';
62 &transcript("Processing commands for $controlrequestaddr:\n\n");
63
64 $dl= 0;
65 $state= 'idle';
66 $lowstate= 'idle';
67 $mergelowstate= 'idle';
68 $midix=0;    
69 $extras="";
70
71 #strip blank line(s) after header
72 while ( $i <= $#msg && !length( $msg[$i] ) ) { $fwd .= "\n"; $i++; }
73
74 #strip, if exists, mime header
75 if ( $msg[$i] =~ /^This is a multi-part message in MIME format./ )
76 {       while ( $i <= $#msg && length( $msg[$i] ) ) { $fwd .= $msg[$i] . "\n"; $i++; }
77         while ( $i <= $#msg && !length( $msg[$i] ) ) { $fwd .= "\n"; $i++; }
78 }
79 if ( $msg[$i] =~ /^--/ || $msg[$i] =~ /^\s*$/ )
80 {       while ( $i <= $#msg && length( $msg[$i] ) ) { $fwd .= $msg[$i]; $i++; }
81         while ( $i <= $#msg && !length( $msg[$i] ) ) { $fwd .= "\n"; $i++; }
82 }       
83
84
85 for ($procline=$i; $procline<=$#msg; $procline++) 
86 {       $state eq 'idle' || print "$state ?\n";
87     $lowstate eq 'idle' || print "$lowstate ?\n";
88     $mergelowstate eq 'idle' || print "$mergelowstate ?\n";
89     $_= $msg[$procline]; s/\s+$//;
90     next unless m/\S/; next if m/^\s*\#/;
91     &transcript("> $_\n");
92     $action= '';
93     if (m/^stop\s/i || m/^quit\s/i || m/^--/ || m/^thank\s/i) 
94         {       &transcript("Stopping processing here.\n\n");
95         last;
96     } elsif (m/^debug\s+(\d+)$/i && $1 >= 0 && $1 <= 1000) {
97         $dl= $1+0;
98         &transcript("Debug level $dl.\n\n");
99     } elsif (m/^(send|get)\s+\#?(\d{2,})$/i) {
100         $ref= $2+0;
101         &sendlynxdoc("$ref","logs for $gBug#$ref");
102     } elsif (m/^send-detail\s+\#?(\d+)$/i) {
103         $ref= $1+0; $reffile= $ref; $reffile =~ s,^..,$&/$&,;
104         &sendlynxdoc("db/$reffile-b.html","additional logs for $gBug#$ref");
105     } elsif (m/^index(\s+full)?$/i) {
106         &sendlynxdoc("db/ix/full.html",'full index');
107     } elsif (m/^index-summary\s+by-package$/i) {
108         &sendlynxdoc("db/ix/psummary.html",'summary index sorted by package/title');
109     } elsif (m/^index-summary(\s+by-number)?$/i) {
110         &sendlynxdoc("db/ix/summary.html",'summary index sorted by number/date');
111     } elsif (m/^index(\s+|-)pack(age)?s?$/i) {
112         &sendlynxdoc("db/ix/packages.html",'index of packages');
113     } elsif (m/^index(\s+|-)maints?$/i) {
114         &sendlynxdoc("db/ix/maintainers.html",'index of maintainers');
115     } elsif (m/^index(\s+|-)maint\s+(\S.*\S)$/i) {
116         $substrg= $2; $matches=0;
117         opendir(DBD,"$gWebDir/db/ma") || die $!;
118         while (defined($_=readdir(DBD))) {
119             next unless m/^l/ && m/\.html$/;
120             &transcript("F|$_\n") if $dl>1;
121             $filename= $_; s/^l//; s/\.html$//;
122             &transcript("P|$_\n") if $dl>2;
123             while (s/-(..)([^_])/-$1_-$2/) { }
124             &transcript("P|$_\n") if $dl>2;
125             s/^(.{0,2})_/$1-20_/g; while (s/([^-]..)_/$1-20_/) { };
126             &transcript("P|$_\n") if $dl>2;
127             s/^,(.*),(.*),([^,]+)$/$1-40_$2-20_-28_$3-29_/;
128             &transcript("P|$_\n") if $dl>2;
129             s/^([^,]+),(.*),(.*),$/$1-20_-3c_$2-40_$3-3e_/;
130             &transcript("P|$_\n") if $dl>2;
131             s/\./-2e_/g;
132             &transcript("P|$_\n") if $dl>2;
133             $out='';
134             while (m/-(..)_/) { $out.= $`.sprintf("%c",hex($1)); $_=$'; }
135             $out.=$_;
136             &transcript("M|$out\n") if $dl>1;
137             next unless index(lc $out, lc $substrg)>=0;
138             &transcript("S|$filename\n") if $dl>0;
139             &transcript("S|$out\n") if $dl>0;
140             $matches++;
141             &sendlynxdocraw("db/ma/$filename","$gBug list for maintainer \`$out'");
142         }
143         if ($matches) {
144             &transcript("$gBug list(s) for $matches maintainer(s) sent.\n\n");
145         } else {
146             &transcript("No maintainers found containing \`$substrg'.\n".
147                         "Use \`index-maint' to get list of maintainers.\n\n");
148         }
149         $ok++;
150     } elsif (m/^index(\s+|-)pack(age)?s?\s+(\S.*\S)$/i) {
151         $substrg= $+; $matches=0;
152         opendir(DBD,"$gWebDir/db/pa") || die $!;
153         while (defined($_=readdir(DBD))) {
154             next unless m/^l/ && m/\.html$/;
155             &transcript("F|$_\n") if $dl>1;
156             $filename= $_; s/^l//; s/\.html$//;
157             next unless index(lc $_, lc $substrg)>=0;
158             &transcript("S|$filename\n") if $dl>0;
159             &transcript("S|$out\n") if $dl>0;
160             $matches++;
161             &sendlynxdocraw("db/pa/$filename","$gBug list for package \`$_'");
162         }
163         if ($matches) {
164             &transcript("$gBug list(s) for $matches package(s) sent.\n\n");
165         } else {
166             &transcript("No packages found containing \`$substrg'.\n".
167                         "Use \`index-packages' to get list of packages.\n\n");
168         }
169         $ok++;
170     } elsif (m/^send-unmatched(\s+this|\s+-?0)?$/i) {
171         &sendlynxdoc("db/ju/unmatched-1.html","junk (this week)");
172     } elsif (m/^send-unmatched\s+(last|-1)$/i) {
173         &sendlynxdoc("db/ju/unmatched-2.html","junk (last week)");
174     } elsif (m/^send-unmatched\s+(old|-2)$/i) {
175         &sendlynxdoc("db/ju/unmatched-3.html","junk (two weeks ago)");
176     } elsif (m/^getinfo\s+(\S+)$/i) {
177         $file= $1;
178         if ($file =~ m/^\./ || $file !~ m/^[-.0-9a-z]+$/ || $file =~ m/\.gz$/) {
179             &transcript("Filename $file is badly formatted.\n\n");
180         } elsif (open(P,"$gDocDir/$file")) {
181             $ok++;
182             &transcript("Info file $file appears below.\n\n");
183             $extras.= "\n---------- Info file $file follows:\n\n";
184             while(<P>) { $extras.= $_; }
185             close(P);
186         } else {
187             &transcript("Info file $file does not exist.\n\n");
188          }
189     } elsif (m/^help$/i) {
190         &sendhelp;
191         &transcript("\n");
192         $ok++;
193     } elsif (m/^refcard$/i) {
194         &sendtxthelp("bug-mailserver-refcard.txt","mailservers' reference card");
195     } elsif (m/^subscribe/i) {
196         &transcript(<<END);
197 There is no $gProject $gBug mailing list.  If you wish to review bug reports
198 please do so via http://$gWebUrl/ or ask this mailserver
199 to send them to you.
200 soon: MAILINGLISTS_TEXT
201 END
202     } elsif (m/^unsubscribe/i) {
203         &transcript(<<END);
204 soon: UNSUBSCRIBE_TEXT
205 soon: MAILINGLISTS_TEXT
206 END
207     } elsif (!$control) {
208         &transcript(<<END);
209 Unknown command or malformed arguments to command.
210 (Use control\@$gEmailDomain to manipulate reports.)
211
212 END
213         if (++$unknowns >= 3) {
214             &transcript("Too many unknown commands, stopping here.\n\n");
215             last;
216         }
217     } elsif (m/^close\s+\#?(\d+)$/i) {
218         $ok++;
219         $ref= $1;
220         if (&setbug) {
221             if (length($s_done)) {
222                 &transcript("$gBug is already closed, cannot re-close.\n\n");
223                 &nochangebug;
224             } else {
225                 $action= "$gBug closed, send any further explanations to $s_originator";
226                 do {
227                     &addmaintainers($s_package);
228                                         if ( length( $gDoneList ) > 0 && length( $gListDomain ) >
229                                         0 ) { &addccaddress("$gDoneList\@$gListDomain"); }
230                     $s_done= $replyto;
231                     $message= <<END;
232 From: $gMaintainerEmail ($gProject $gBug Tracking System)
233 To: $s_originator
234 Subject: $gBug#$ref acknowledged by developer
235          ($header{'subject'})
236 References: $header{'message-id'} $s_msgid
237 In-Reply-To: $s_msgid
238 Message-ID: <handler.$ref.$nn.notifdonectrl.$midix\@$gEmailDomain>
239
240 This is an automatic notification regarding your $gBug report
241 #$ref: $s_subject.
242
243 It has been marked as closed by one of the developers, namely
244 $replyto.
245
246 You should be hearing from them with a substantive response shortly,
247 if you have not already done so.  If not, please contact them
248 directly or myself.
249
250 $gMaintainer
251 (administrator, $gProject $gBugs database)
252 END
253                     &sendmailmessage($message,$s_originator);
254                 } while (&getnextbug);
255             }
256         }
257     } elsif (m/^reassign\s+\#?(\d+)\s+(\S.*\S)$/i) {
258         $ok++;
259         $ref= $1; $newpackage= $2;
260         $newpackage =~ y/A-Z/a-z/;
261         if (&setbug) {
262             if (length($s_package)) {
263                 $action= "$gBug reassigned from package \`$s_package'".
264                          " to \`$newpackage'.";
265             } else {
266                 $action= "$gBug assigned to package \`$newpackage'.";
267             }
268             do {
269                 &addmaintainers($s_package);
270                 &addmaintainers($newpackage);
271                 $s_package= $newpackage;
272             } while (&getnextbug);
273         }
274     } elsif (m/^reopen\s+\#?(\d+)$/i ? ($noriginator='', 1) :
275              m/^reopen\s+\#?(\d+)\s+\=$/i ? ($noriginator='', 1) :
276              m/^reopen\s+\#?(\d+)\s+\!$/i ? ($noriginator=$replyto, 1) :
277              m/^reopen\s+\#?(\d+)\s+(\S.*\S)$/i ? ($noriginator=$2, 1) : 0) {
278         $ok++;
279         $ref= $1;
280         if (&setbug) {
281             if (!length($s_done)) {
282                 &transcript("$gBug is already open, cannot reopen.\n\n");
283                 &nochangebug;
284             } else {
285                 $action=
286                     $noriginator eq '' ? "$gBug reopened, originator not changed." :
287                         "$gBug reopened, originator set to $noriginator.";
288                 do {
289                     &addmaintainers($s_package);
290                     $s_originator= $noriginator eq '' ?  $s_originator : $noriginator;
291                     $s_done= '';
292                 } while (&getnextbug);
293             }
294         }
295     } elsif (m/^forwarded\s+\#?(\d+)\s+(\S.*\S)$/i) {
296         $ok++;
297         $ref= $1; $whereto= $2;
298         if (&setbug) {
299             if (length($s_forwarded)) {
300     $action= "Forwarded-to-address changed from $s_forwarded to $whereto.";
301             } else {
302     $action= "Noted your statement that $gBug has been forwarded to $whereto.";
303             }
304             if (length($s_done)) {
305                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
306             }
307             do {
308                 &addmaintainers($s_package);
309                                 if (length($gFowardList)>0 && length($gListDomain)>0 )
310                 { &addccaddress("$gFowardList\@$gListDomain"); }
311                 $s_forwarded= $whereto;
312             } while (&getnextbug);
313         }
314     } elsif (m/^notforwarded\s+\#?(\d+)$/i) {
315         $ok++;
316         $ref= $1;
317         if (&setbug) {
318             if (!length($s_forwarded)) {
319                 &transcript("$gBug is not marked as having been forwarded.\n\n");
320                 &nochangebug;
321             } else {
322     $action= "Removed annotation that $gBug had been forwarded to $s_forwarded.";
323                 do {
324                     &addmaintainers($s_package);
325                     $s_forwarded= '';
326                 } while (&getnextbug);
327             }
328         }
329     } elsif (m/^severity\s+\#?(\d+)\s+([-0-9a-z]+)$/i ||
330         m/^priority\s+\#?(\d+)\s+([-0-9a-z]+)$/i) {
331         $ok++;
332         $ref= $1;
333         $newseverity= $2;
334         if (!grep($_ eq $newseverity, @severities, "$gDefaultSeverity")) {
335             &transcript("Severity level \`$newseverity' is not known.\n".
336                         "Recognised are: ".join(' ',@showseverities).".\n\n");
337         } elsif (&setbug) {
338             $printseverity= $s_severity;
339             $printseverity= "$gDefaultSeverity" if $printseverity eq '';
340             $action= "Severity set to \`$newseverity'.";
341             do {
342                 &addmaintainers($s_package);
343                 $s_severity= $newseverity;
344             } while (&getnextbug);
345         }
346     } elsif (m/^retitle\s+\#?(\d+)\s+(\S.*\S)\s*$/i) {
347         $ok++;
348         $ref= $1; $newtitle= $2;
349         if (&getbug) {
350             &foundbug;
351             &addmaintainers($s_package);
352             $s_subject= $newtitle;
353             $action= "Changed $gBug title.";
354             &savebug;
355             &transcript("$action\n");
356             if (length($s_done)) {
357                 &transcript("(By the way, that $gBug is currently marked as done.)\n");
358             }
359             &transcript("\n");
360         } else {
361             &notfoundbug;
362         }
363     } elsif (m/^unmerge\s+\#?(\d+)$/i) {
364         $ok++;
365         $ref= $1;
366         if (&setbug) {
367             if (!length($s_mergedwith)) {
368                 &transcript("$gBug is not marked as being merged with any others.\n\n");
369                 &nochangebug;
370             } else {
371                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
372                 $action= "Disconnected #$ref from all other report(s).";
373                 @newmergelist= split(/ /,$s_mergedwith);
374                 $discref= $ref;
375                 do {
376                     &addmaintainers($s_package);
377                     $s_mergedwith= ($ref == $discref) ? ''
378                         : join(' ',grep($_ ne $ref,@newmergelist));
379                 } while (&getnextbug);
380             }
381         }
382     } elsif (m/^merge\s+(\d+(\s+\d+)+)\s*$/i) {
383         $ok++;
384         @tomerge= sort { $a <=> $b } split(/\s+/,$1);
385         @newmergelist= ();
386         &getmerge;
387         while (defined($ref= shift(@tomerge))) {
388             &transcript("D| checking merge $ref\n") if $dl;
389             $ref+= 0;
390             next if grep($_ eq $ref,@newmergelist);
391             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
392             &foundbug;
393             &transcript("D| adding $ref ($s_mergewith)\n") if $dl;
394             $mismatch= '';
395             &checkmatch('package','m_package',$s_package);
396             &checkmatch('forwarded addr','m_forwarded',$s_forwarded);
397             &checkmatch('severity','m_severity',$s_severity);
398             &checkmatch('done mark','m_done',length($s_done) ? 'done' : 'open');
399             if (length($mismatch)) {
400                 &transcript("Mismatch - only $gBugs in same state can be merged:\n".
401                             $mismatch."\n");
402                 &cancelbug; @newmergelist=(); last;
403             }
404             push(@newmergelist,$ref);
405             push(@tomerge,split(/ /,$s_mergedwith));
406             &cancelbug;
407         }
408         if (@newmergelist) {
409             @newmergelist= sort { $a <=> $b } @newmergelist;
410             $action= "Merged @newmergelist.";
411             for $ref (@newmergelist) {
412                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
413                 &addmaintainers($s_package);
414                 $s_mergedwith= join(' ',grep($_ ne $ref,@newmergelist));
415                 &savebug;
416             }
417             &transcript("$action\n\n");
418         }
419         &endmerge;
420     } else {
421         &transcript("Unknown command or malformed arguments to command.\n\n");
422         if (++$unknowns >= 5) {
423             &transcript("Too many unknown commands, stopping here.\n\n");
424             last;
425         }
426     }
427 }
428 if ($procline>$#msg) {
429     &transcript(">\nEnd of message, stopping processing here.\n\n");
430 }
431 if (!$ok) {
432     &transcript("No commands successfully parsed; sending the help text(s).\n");
433     &sendhelp;
434     &transcript("\n");
435 }
436
437 &transcript("MC\n") if $dl>1;
438 @maintccs= ();
439 for $maint (keys %maintccreasons) {
440 &transcript("MM|$maint|\n") if $dl>1;
441     next if $maint eq $replyto;
442     $reasonstring= '';
443     $reasonsref= $maintccreasons{$maint};
444 &transcript("MY|$maint|\n") if $dl>2;
445     for $p (sort keys %$reasonsref) {
446 &transcript("MP|$p|\n") if $dl>2;
447         $reasonstring.= ', ' if length($reasonstring);
448         $reasonstring.= $p.' ' if length($p);
449         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
450     }
451     push(@maintccs,"$maint ($reasonstring)");
452     push(@maintccaddrs,"$maint");
453 }
454 if (@maintccs) {
455     &transcript("MC|@maintccs|\n") if $dl>2;
456     $maintccs= "Cc: ".join(",\n    ",@maintccs)."\n";
457 } else { $maintccs = ""; }
458
459 $reply= <<END;
460 From: $gMaintainerEmail ($gProject $gBug Tracking System)
461 To: $replyto
462 ${maintccs}Subject: Processed: $header{'subject'}
463 In-Reply-To: $header{'message-id'}
464 References: $header{'message-id'}
465 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
466
467 ${transcript}Please contact me if you need assistance.
468
469 $gMaintainer
470 (administrator, $gProject $gBugs database)
471 $extras
472 END
473
474 $repliedshow= join(', ',$replyto,@maintccaddrs);
475 &filelock("lock/-1");
476 open(AP,">>db/-1.log") || &quit("open db/-1.log: $!");
477 print(AP
478       "\2\n$repliedshow\n\5\n$reply\n\3\n".
479       "\6\n".
480       "<strong>Request received</strong> from <code>".
481       &sani($header{'from'})."</code>\n".
482       "to <code>".&sani($controlrequestaddr)."</code>\n".
483       "\3\n".
484       "\7\n",@log,"\n\3\n") || &quit("writing db/-1.log: $!");
485 close(AP) || &quit("open db/-1.log: $!");
486 &unfilelock;
487 utime(time,time,"db");
488
489 &sendmailmessage($reply,$replyto,@maintccaddrs);
490
491 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
492
493 sub get_addresses {
494     return
495        map { $_->address() }
496        map { Mail::Address->parse($_) } @_;
497 }
498
499 sub sendmailmessage {
500     local ($message,@recips) = @_;
501     print "mailing to >@recips<\n" if $debug;
502     $c= open(D,"|-");
503     defined($c) || &quit("mailing forking for sendmail: $!");
504     if (!$c) { # ie, we are the child process
505         exec '/usr/lib/sendmail','-f'."$gMaintainerEmail",'-odi','-oem','-oi',get_addresses(@recips);
506         die $!;
507     }
508     print(D $message) || &quit("writing to sendmail process: $!");
509     $!=0; close(D); $? && &quit("sendmail gave exit status $? ($!)");
510     $midix++;
511 }
512
513 sub sendhelp {
514         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
515         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
516             if $control;
517 }
518
519 #sub unimplemented {
520 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
521 #}
522
523 sub checkmatch {
524     local ($string,$mvarname,$svarvalue) = @_;
525     local ($mvarvalue);
526     if (@newmergelist) {
527         eval "\$mvarvalue= \$$mvarname";
528         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
529             if $dl;
530         $mismatch .=
531             "Values for \`$string' don't match:\n".
532             " #$newmergelist[0] has \`$mvarvalue';\n".
533             " #$ref has \`$svarvalue'\n"
534             if $mvarvalue ne $svarvalue;
535     } else {
536         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
537             if $dl;
538         eval "\$$mvarname= \$svarvalue";
539     }
540 }
541
542 # High-level bug manipulation calls
543 # Do announcements themselves
544 #
545 # Possible calling sequences:
546 #    setbug (returns 0)
547 #    
548 #    setbug (returns 1)
549 #    &transcript(something)
550 #    nochangebug
551 #
552 #    setbug (returns 1)
553 #    $action= (something)
554 #    do {
555 #      (modify s_* variables)
556 #    } while (getnextbug);
557
558 sub nochangebug {
559     &dlen("nochangebug");
560     $state eq 'single' || $state eq 'multiple' || die "$state ?";
561     &cancelbug;
562     &endmerge if $manybugs;
563     $state= 'idle';
564     &dlex("nochangebug");
565 }
566
567 sub setbug {
568     &dlen("setbug $ref");
569     $state eq 'idle' || die "$state ?";
570     if (!&getbug) {
571         &notfoundbug;
572         &dlex("setbug => 0s");
573         return 0;
574     }
575     @thisbugmergelist= split(/ /,$s_mergedwith);
576     if (!@thisbugmergelist) {
577         &foundbug;
578         $manybugs= 0;
579         $state= 'single';
580         $sref=$ref;
581         &dlex("setbug => 1s");
582         return 1;
583     }
584     &cancelbug;
585     &getmerge;
586     $manybugs= 1;
587     if (!&getbug) {
588         &notfoundbug;
589         &endmerge;
590         &dlex("setbug => 0mc");
591         return 0;
592     }
593     &foundbug;
594     $state= 'multiple'; $sref=$ref;
595     &dlex("setbug => 1m");
596     return 1;
597 }
598
599 sub getnextbug {
600     &dlen("getnextbug");
601     $state eq 'single' || $state eq 'multiple' || die "$state ?";
602     &savebug;
603     if (!$manybugs || !@thisbugmergelist) {
604         length($action) || die;
605         &transcript("$action\n$extramessage\n");
606         &endmerge if $manybugs;
607         $state= 'idle';
608         &dlex("getnextbug => 0");
609         return 0;
610     }
611     $ref= shift(@thisbugmergelist);
612     &getbug || die "bug $ref disappeared";
613     &foundbug;
614     &dlex("getnextbug => 1");
615     return 1;
616 }
617
618 # Low-level bug-manipulation calls
619 # Do no announcements
620 #
621 #    getbug (returns 0)
622 #
623 #    getbug (returns 1)
624 #    cancelbug
625 #
626 #    getmerge
627 #    $action= (something)
628 #    getbug (returns 1)
629 #    savebug/cancelbug
630 #    getbug (returns 1)
631 #    savebug/cancelbug
632 #    [getbug (returns 0)]
633 #    &transcript("$action\n\n")
634 #    endmerge
635
636 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
637 sub foundbug { &transcript("$gBug#$ref: $s_subject\n"); }
638
639 sub getmerge {
640     &dlen("getmerge");
641     $mergelowstate eq 'idle' || die "$mergelowstate ?";
642     &filelock('lock/merge');
643     $mergelowstate='locked';
644     &dlex("getmerge");
645 }
646
647 sub endmerge {
648     &dlen("endmerge");
649     $mergelowstate eq 'locked' || die "$mergelowstate ?";
650     &unfilelock;
651     $mergelowstate='idle';
652     &dlex("endmerge");
653 }
654
655 sub getbug {
656     &dlen("getbug $ref");
657     $lowstate eq 'idle' || die "$state ?";
658     if (&lockreadbug($ref)) {
659         $sref= $ref;
660         $lowstate= "open";
661         &dlex("getbug => 1");
662         $extramessage='';
663         return 1;
664     }
665     $lowstate= 'idle';
666     &dlex("getbug => 0");
667     return 0;
668 }
669
670 sub cancelbug {
671     &dlen("cancelbug");
672     $lowstate eq 'open' || die "$state ?";
673     &unfilelock;
674     $lowstate= 'idle';
675     &dlex("cancelbug");
676 }
677
678 sub savebug {
679     &dlen("savebug $ref");
680     $lowstate eq 'open' || die "$lowstate ?";
681     length($action) || die;
682     $ref == $sref || die "read $sref but saving $ref ?";
683     open(L,">>db/$ref.log") || &quit("opening db/$ref.log: $!");
684     print(L
685           "\6\n".
686           "<strong>".&sani($action)."</strong>\n".
687           "Request was from <code>".&sani($header{'from'})."</code>\n".
688           "to <code>".&sani($controlrequestaddr)."</code>. \n".
689           "\3\n".
690           "\7\n",@log,"\n\3\n") || &quit("writing db/$ref.log: $!");
691     close(L) || &quit("closing db/$ref.log: $!");
692     open(S,">db/$ref.status.new") || &quit("opening db/$ref.status.new: $!");
693     print(S
694           "$s_originator\n".
695           "$s_date\n".
696           "$s_subject\n".
697           "$s_msgid\n".
698           "$s_package\n".
699           "$s_keywords\n".
700           "$s_done\n".
701           "$s_forwarded\n".
702           "$s_mergedwith\n".
703           "$s_severity\n") || &quit("writing db/$ref.status.new: $!");
704     close(S) || &quit("closing db/$ref.status.new: $!");
705     rename("db/$ref.status.new","db/$ref.status") ||
706         &quit("installing new db/$ref.status: $!");
707     &unfilelock;
708     $lowstate= "idle";
709     &dlex("savebug");
710 }
711
712 sub dlen {
713     return if !$dl;
714     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
715 }
716
717 sub dlex {
718     return if !$dl;
719     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
720 }
721
722 sub transcript {
723     print $_[0] if $debug;
724     $transcript.= $_[0];
725 }
726
727 sub sendlynxdoc {
728     &sendlynxdocraw;
729     &transcript("\n");
730     $ok++;
731 }
732
733 sub sendtxthelp {
734     &sendtxthelpraw;
735     &transcript("\n");
736     $ok++;
737 }
738
739 sub sendtxthelpraw {
740     local ($relpath,$description) = @_;
741     $doc='';
742     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
743     while(<D>) { $doc.=$_; }
744     close(D);
745     &transcript("Sending $description in separate message.\n");
746     &sendmailmessage(<<END.$doc,$replyto);
747 From: $gMaintainerEmail ($gProject $gBug Tracking System)
748 To: $replyto
749 Subject: $gProject $gBug help: $description
750 References: $header{'message-id'}
751 In-Reply-To: $header{'message-id'}
752 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
753
754 END
755     $ok++;
756 }
757
758 sub sendlynxdocraw {
759     local ($relpath,$description) = @_;
760     $doc='';
761     open(L,"lynx -nolist -dump http://$gWebDomain/$relpath 2>&1 |") || &quit("fork for lynx: $!");
762     while(<L>) { $doc.=$_; }
763     $!=0; close(L);
764     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
765         &transcript("Information ($description) is not available -\n".
766                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
767          $ok++;
768     } elsif ($?) {
769         &transcript("Error getting $description (code $? $!):\n$doc\n");
770     } else {
771         &transcript("Sending $description.\n");
772         &sendmailmessage(<<END.$doc,$replyto);
773 From: $gMaintainerEmail ($gProject $gBug Tracking System)
774 To: $replyto
775 Subject: $gProject $gBugs information: $description
776 References: $header{'message-id'}
777 In-Reply-To: $header{'message-id'}
778 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
779
780 END
781          $ok++;
782     }
783 }
784
785 sub addccaddress {
786     my ($cca) = @_;
787     $maintccreasons{$cca}{''}{$ref}= 1;
788 }
789
790 sub addmaintainers 
791 {       # Data structure is:
792     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
793     my ($p, $addmaint, $pshow);
794     &ensuremaintainersloaded;
795     $anymaintfound=0; $anymaintnotfound=0;
796     for $p (split(m/[ \t?,()]+/,$_[0])) 
797         {       $p =~ y/A-Z/a-z/;
798         $pshow= ($p =~ m/[-+.a-z0-9]+/ ? $& : '');
799         if (defined($maintainerof{$p})) 
800                 {       $addmaint= $maintainerof{$p};
801                         &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
802             $maintccreasons{$addmaint}{$p}{$ref}= 1;
803                         print "maintainer add >$p|$addmaint<\n" if $debug;
804         } else { print "maintainer none >$p<\n" if $debug; }
805     }
806 }
807
808 sub ensuremaintainersloaded {
809     my ($a,$b);
810     return if $maintainersloaded++;
811     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
812     while (<MAINT>) {
813         m/^\n$/ && next;
814         m/^\s*$/ && next;
815         m/^(\S+)\s+(\S.*\S)\n$/ || &quit("maintainers bogus \`$_'");
816         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
817         $maintainerof{$1}= $2;
818     }
819     close(MAINT);
820 }