]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
[project @ 1999-12-17 19:44:07 by gecko]
[debbugs.git] / scripts / service.in
1 #!/usr/bin/perl -w
2 # $Id: service.in,v 1.10 1999/12/17 19:44:08 gecko 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; $reffile= $ref; $reffile =~ s,^..,$&/$&,;
101         &sendlynxdoc("db/$reffile.html","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, ack sent to submitter - they'd better know why !";
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          ($s_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
242 It has been marked as closed by one of the developers, namely
243 $replyto.
244
245 You should be hearing from them with a substantive response shortly,
246 if you have not already done so.  If not, please contact them
247 directly or myself.
248
249 $gMaintainer
250 (administrator, $gProject $gBugs database)
251 END
252                     &sendmailmessage($message,$s_originator);
253                 } while (&getnextbug);
254             }
255         }
256     } elsif (m/^reassign\s+\#?(\d+)\s+(\S.*\S)$/i) {
257         $ok++;
258         $ref= $1; $newpackage= $2;
259         $newpackage =~ y/A-Z/a-z/;
260         if (&setbug) {
261             if (length($s_package)) {
262                 $action= "$gBug reassigned from package \`$s_package'".
263                          " to \`$newpackage'.";
264             } else {
265                 $action= "$gBug assigned to package \`$newpackage'.";
266             }
267             do {
268                 &addmaintainers($s_package);
269                 &addmaintainers($newpackage);
270                 $s_package= $newpackage;
271             } while (&getnextbug);
272         }
273     } elsif (m/^reopen\s+\#?(\d+)$/i ? ($noriginator='', 1) :
274              m/^reopen\s+\#?(\d+)\s+\=$/i ? ($noriginator='', 1) :
275              m/^reopen\s+\#?(\d+)\s+\!$/i ? ($noriginator=$replyto, 1) :
276              m/^reopen\s+\#?(\d+)\s+(\S.*\S)$/i ? ($noriginator=$2, 1) : 0) {
277         $ok++;
278         $ref= $1;
279         if (&setbug) {
280             if (!length($s_done)) {
281                 &transcript("$gByg is already open, cannot reopen.\n\n");
282                 &nochangebug;
283             } else {
284                 $action=
285                     $noriginator eq '' ? "$gBug reopened, originator not changed." :
286                         "$gBug reopened, originator set to $noriginator.";
287                 do {
288                     &addmaintainers($s_package);
289                     $s_originator= $noriginator eq '' ?  $s_originator : $noriginator;
290                     $s_done= '';
291                 } while (&getnextbug);
292             }
293         }
294     } elsif (m/^forwarded\s+\#?(\d+)\s+(\S.*\S)$/i) {
295         $ok++;
296         $ref= $1; $whereto= $2;
297         if (&setbug) {
298             if (length($s_forwarded)) {
299     $action= "Forwarded-to-address changed from $s_forwarded to $whereto.";
300             } else {
301     $action= "Noted your statement that $gBug has been forwarded to $whereto.";
302             }
303             if (length($s_done)) {
304                 $extramessage= "(By the way, this $gBug is currently marked as done.)\n";
305             }
306             do {
307                 &addmaintainers($s_package);
308                                 if (length($gFowardList)>0 && length($gListDomain)>0 )
309                 { &addccaddress("$gFowardList\@$gListDomain"); }
310                 $s_forwarded= $whereto;
311             } while (&getnextbug);
312         }
313     } elsif (m/^notforwarded\s+\#?(\d+)$/i) {
314         $ok++;
315         $ref= $1;
316         if (&setbug) {
317             if (!length($s_forwarded)) {
318                 &transcript("$gBug is not marked as having been forwarded.\n\n");
319                 &nochangebug;
320             } else {
321     $action= "Removed annotation that $gBug had been forwarded to $s_forwarded.";
322                 do {
323                     &addmaintainers($s_package);
324                     $s_forwarded= '';
325                 } while (&getnextbug);
326             }
327         }
328     } elsif (m/^severity\s+\#?(\d+)\s+([-0-9a-z]+)$/i ||
329         m/^priority\s+\#?(\d+)\s+([-0-9a-z]+)$/i) {
330         $ok++;
331         $ref= $1;
332         $newseverity= $2;
333         if (!grep($_ eq $newseverity, @severities, "$gDefaultSeverity")) {
334             &transcript("Severity level \`$newseverity' is not known.\n".
335                         "Recognised are: ".join(' ',@showseverities).".\n\n");
336         } elsif (&setbug) {
337             $printseverity= $s_severity;
338             $printseverity= "$gDefaultSeverity" if $printseverity eq '';
339             $action= "Severity set to \`$newseverity'.";
340             do {
341                 &addmaintainers($s_package);
342                 $s_severity= $newseverity;
343             } while (&getnextbug);
344         }
345     } elsif (m/^retitle\s+\#?(\d+)\s+(\S.*\S)\s*$/i) {
346         $ok++;
347         $ref= $1; $newtitle= $2;
348         if (&getbug) {
349             &foundbug;
350             &addmaintainers($s_package);
351             $s_subject= $newtitle;
352             $action= "Changed $gBug title.";
353             &savebug;
354             &transcript("$action\n");
355             if (length($s_done)) {
356                 &transcript("(By the way, that $gBug is currently marked as done.)\n");
357             }
358             &transcript("\n");
359         } else {
360             &notfoundbug;
361         }
362     } elsif (m/^unmerge\s+\#?(\d+)$/i) {
363         $ok++;
364         $ref= $1;
365         if (&setbug) {
366             if (!length($s_mergedwith)) {
367                 &transcript("$gBug is not marked as being merged with any others.\n\n");
368                 &nochangebug;
369             } else {
370                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
371                 $action= "Disconnected #$ref from all other report(s).";
372                 @newmergelist= split(/ /,$s_mergedwith);
373                 $discref= $ref;
374                 do {
375                     &addmaintainers($s_package);
376                     $s_mergedwith= ($ref == $discref) ? ''
377                         : join(' ',grep($_ ne $ref,@newmergelist));
378                 } while (&getnextbug);
379             }
380         }
381     } elsif (m/^merge\s+(\d+(\s+\d+)+)\s*$/i) {
382         $ok++;
383         @tomerge= sort { $a <=> $b } split(/\s+/,$1);
384         @newmergelist= ();
385         &getmerge;
386         while (defined($ref= shift(@tomerge))) {
387             &transcript("D| checking merge $ref\n") if $dl;
388             $ref+= 0;
389             next if grep($_ eq $ref,@newmergelist);
390             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
391             &foundbug;
392             &transcript("D| adding $ref ($s_mergewith)\n") if $dl;
393             $mismatch= '';
394             &checkmatch('package','m_package',$s_package);
395             &checkmatch('forwarded addr','m_forwarded',$s_forwarded);
396             &checkmatch('severity','m_severity',$s_severity);
397             &checkmatch('done mark','m_done',length($s_done) ? 'done' : 'open');
398             if (length($mismatch)) {
399                 &transcript("Mismatch - only $Bugs in same state can be merged:\n".
400                             $mismatch."\n");
401                 &cancelbug; @newmergelist=(); last;
402             }
403             push(@newmergelist,$ref);
404             push(@tomerge,split(/ /,$s_mergedwith));
405             &cancelbug;
406         }
407         if (@newmergelist) {
408             @newmergelist= sort { $a <=> $b } @newmergelist;
409             $action= "Merged @newmergelist.";
410             for $ref (@newmergelist) {
411                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
412                 &addmaintainers($s_package);
413                 $s_mergedwith= join(' ',grep($_ ne $ref,@newmergelist));
414                 &savebug;
415             }
416             &transcript("$action\n\n");
417         }
418         &endmerge;
419     } else {
420         &transcript("Unknown command or malformed arguments to command.\n\n");
421         if (++$unknowns >= 5) {
422             &transcript("Too many unknown commands, stopping here.\n\n");
423             last;
424         }
425     }
426 }
427 if ($procline>$#msg) {
428     &transcript(">\nEnd of message, stopping processing here.\n\n");
429 }
430 if (!$ok) {
431     &transcript("No commands successfully parsed; sending the help text(s).\n");
432     &sendhelp;
433     &transcript("\n");
434 }
435
436 &transcript("MC\n") if $dl>1;
437 @maintccs= ();
438 for $maint (keys %maintccreasons) {
439 &transcript("MM|$maint|\n") if $dl>1;
440     next if $maint eq $replyto;
441     $reasonstring= '';
442     $reasonsref= $maintccreasons{$maint};
443 &transcript("MY|$maint|\n") if $dl>2;
444     for $p (sort keys %$reasonsref) {
445 &transcript("MP|$p|\n") if $dl>2;
446         $reasonstring.= ', ' if length($reasonstring);
447         $reasonstring.= $p.' ' if length($p);
448         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
449     }
450     push(@maintccs,"$maint ($reasonstring)");
451     push(@maintccaddrs,"$maint");
452 }
453 if (@maintccs) {
454     &transcript("MC|@maintccs|\n") if $dl>2;
455     $maintccs= "Cc: ".join(",\n    ",@maintccs)."\n";
456 } else { $maintccs = ""; }
457
458 $reply= <<END;
459 From: $gMaintainerEmail ($gProject $gBug Tracking System)
460 To: $replyto
461 ${maintccs}Subject: Processed: $header{'subject'}
462 In-Reply-To: $header{'message-id'}
463 References: $header{'message-id'}
464 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
465
466 ${transcript}Please contact me if you need assistance.
467
468 $gMaintainer
469 (administrator, $gProject $gBugs database)
470 $extras
471 END
472
473 $repliedshow= join(', ',$replyto,@maintccaddrs);
474 &filelock("lock/-1");
475 open(AP,">>db/-1.log") || &quit("open db/-1.log: $!");
476 print(AP
477       "\2\n$repliedshow\n\5\n$reply\n\3\n".
478       "\6\n".
479       "<strong>Request received</strong> from <code>".
480       &sani($header{'from'})."</code>\n".
481       "to <code>".&sani($controlrequestaddr)."</code>\n".
482       "\3\n".
483       "\7\n",@log,"\n\3\n") || &quit("writing db/-1.log: $!");
484 close(AP) || &quit("open db/-1.log: $!");
485 &unfilelock;
486 utime(time,time,"db");
487
488 &sendmailmessage($reply,$replyto,@maintccaddrs);
489
490 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
491
492 sub get_addresses {
493     return
494        map { $_->address() }
495        map { Mail::Address->parse($_) } @_;
496 }
497
498 sub sendmailmessage {
499     local ($message,@recips) = @_;
500     print "mailing to >@recips<\n" if $debug;
501     $c= open(D,"|-");
502     defined($c) || &quit("mailing forking for sendmail: $!");
503     if (!$c) { # ie, we are the child process
504         exec '/usr/lib/sendmail','-f'."$gMaintainerEmail",'-odi','-oem','-oi',get_addresses(@recips);
505         die $!;
506     }
507     print(D $message) || &quit("writing to sendmail process: $!");
508     $!=0; close(D); $? && &quit("sendmail gave exit status $? ($!)");
509     $midix++;
510 }
511
512 sub sendhelp {
513         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
514         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
515             if $control;
516 }
517
518 #sub unimplemented {
519 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
520 #}
521
522 sub checkmatch {
523     local ($string,$mvarname,$svarvalue) = @_;
524     local ($mvarvalue);
525     if (@newmergelist) {
526         eval "\$mvarvalue= \$$mvarname";
527         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
528             if $dl;
529         $mismatch .=
530             "Values for \`$string' don't match:\n".
531             " #$newmergelist[0] has \`$mvarvalue';\n".
532             " #$ref has \`$svarvalue'\n"
533             if $mvarvalue ne $svarvalue;
534     } else {
535         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
536             if $dl;
537         eval "\$$mvarname= \$svarvalue";
538     }
539 }
540
541 # High-level bug manipulation calls
542 # Do announcements themselves
543 #
544 # Possible calling sequences:
545 #    setbug (returns 0)
546 #    
547 #    setbug (returns 1)
548 #    &transcript(something)
549 #    nochangebug
550 #
551 #    setbug (returns 1)
552 #    $action= (something)
553 #    do {
554 #      (modify s_* variables)
555 #    } while (getnextbug);
556
557 sub nochangebug {
558     &dlen("nochangebug");
559     $state eq 'single' || $state eq 'multiple' || die "$state ?";
560     &cancelbug;
561     &endmerge if $manybugs;
562     $state= 'idle';
563     &dlex("nochangebug");
564 }
565
566 sub setbug {
567     &dlen("setbug $ref");
568     $state eq 'idle' || die "$state ?";
569     if (!&getbug) {
570         &notfoundbug;
571         &dlex("setbug => 0s");
572         return 0;
573     }
574     @thisbugmergelist= split(/ /,$s_mergedwith);
575     if (!@thisbugmergelist) {
576         &foundbug;
577         $manybugs= 0;
578         $state= 'single';
579         $sref=$ref;
580         &dlex("setbug => 1s");
581         return 1;
582     }
583     &cancelbug;
584     &getmerge;
585     $manybugs= 1;
586     if (!&getbug) {
587         &notfoundbug;
588         &endmerge;
589         &dlex("setbug => 0mc");
590         return 0;
591     }
592     &foundbug;
593     $state= 'multiple'; $sref=$ref;
594     &dlex("setbug => 1m");
595     return 1;
596 }
597
598 sub getnextbug {
599     &dlen("getnextbug");
600     $state eq 'single' || $state eq 'multiple' || die "$state ?";
601     &savebug;
602     if (!$manybugs || !@thisbugmergelist) {
603         length($action) || die;
604         &transcript("$action\n$extramessage\n");
605         &endmerge if $manybugs;
606         $state= 'idle';
607         &dlex("getnextbug => 0");
608         return 0;
609     }
610     $ref= shift(@thisbugmergelist);
611     &getbug || die "bug $ref disappeared";
612     &foundbug;
613     &dlex("getnextbug => 1");
614     return 1;
615 }
616
617 # Low-level bug-manipulation calls
618 # Do no announcements
619 #
620 #    getbug (returns 0)
621 #
622 #    getbug (returns 1)
623 #    cancelbug
624 #
625 #    getmerge
626 #    $action= (something)
627 #    getbug (returns 1)
628 #    savebug/cancelbug
629 #    getbug (returns 1)
630 #    savebug/cancelbug
631 #    [getbug (returns 0)]
632 #    &transcript("$action\n\n")
633 #    endmerge
634
635 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
636 sub foundbug { &transcript("$gBug#$ref: $s_subject\n"); }
637
638 sub getmerge {
639     &dlen("getmerge");
640     $mergelowstate eq 'idle' || die "$mergelowstate ?";
641     &filelock('lock/merge');
642     $mergelowstate='locked';
643     &dlex("getmerge");
644 }
645
646 sub endmerge {
647     &dlen("endmerge");
648     $mergelowstate eq 'locked' || die "$mergelowstate ?";
649     &unfilelock;
650     $mergelowstate='idle';
651     &dlex("endmerge");
652 }
653
654 sub getbug {
655     &dlen("getbug $ref");
656     $lowstate eq 'idle' || die "$state ?";
657     if (&lockreadbug($ref)) {
658         $sref= $ref;
659         $lowstate= "open";
660         &dlex("getbug => 1");
661         $extramessage='';
662         return 1;
663     }
664     $lowstate= 'idle';
665     &dlex("getbug => 0");
666     return 0;
667 }
668
669 sub cancelbug {
670     &dlen("cancelbug");
671     $lowstate eq 'open' || die "$state ?";
672     &unfilelock;
673     $lowstate= 'idle';
674     &dlex("cancelbug");
675 }
676
677 sub savebug {
678     &dlen("savebug $ref");
679     $lowstate eq 'open' || die "$lowstate ?";
680     length($action) || die;
681     $ref == $sref || die "read $sref but saving $ref ?";
682     open(L,">>db/$ref.log") || &quit("opening db/$ref.log: $!");
683     print(L
684           "\6\n".
685           "<strong>".&sani($action)."</strong>\n".
686           "Request was from <code>".&sani($header{'from'})."</code>\n".
687           "to <code>".&sani($controlrequestaddr)."</code>. \n".
688           "\3\n".
689           "\7\n",@log,"\n\3\n") || &quit("writing db/$ref.log: $!");
690     close(L) || &quit("closing db/$ref.log: $!");
691     open(S,">db/$ref.status.new") || &quit("opening db/$ref.status.new: $!");
692     print(S
693           "$s_originator\n".
694           "$s_date\n".
695           "$s_subject\n".
696           "$s_msgid\n".
697           "$s_package\n".
698           "$s_keywords\n".
699           "$s_done\n".
700           "$s_forwarded\n".
701           "$s_mergedwith\n".
702           "$s_severity\n") || &quit("writing db/$ref.status.new: $!");
703     close(S) || &quit("closing db/$ref.status.new: $!");
704     rename("db/$ref.status.new","db/$ref.status") ||
705         &quit("installing new db/$ref.status: $!");
706     &unfilelock;
707     $lowstate= "idle";
708     &dlex("savebug");
709 }
710
711 sub dlen {
712     return if !$dl;
713     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
714 }
715
716 sub dlex {
717     return if !$dl;
718     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
719 }
720
721 sub transcript {
722     print $_[0] if $debug;
723     $transcript.= $_[0];
724 }
725
726 sub sendlynxdoc {
727     &sendlynxdocraw;
728     &transcript("\n");
729     $ok++;
730 }
731
732 sub sendtxthelp {
733     &sendtxthelpraw;
734     &transcript("\n");
735     $ok++;
736 }
737
738 sub sendtxthelpraw {
739     local ($relpath,$description) = @_;
740     $doc='';
741     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
742     while(<D>) { $doc.=$_; }
743     close(D);
744     &transcript("Sending $description in separate message.\n");
745     &sendmailmessage(<<END.$doc,$replyto);
746 From: $gMaintainerEmail ($gProject $gBug Tracking System)
747 To: $replyto
748 Subject: $gProject $gBug help: $description
749 References: $header{'message-id'}
750 In-Reply-To: $header{'message-id'}
751 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
752
753 END
754     $ok++;
755 }
756
757 sub sendlynxdocraw {
758     local ($relpath,$description) = @_;
759     $doc='';
760     open(L,"lynx -nolist -dump $wwwbase/$relpath 2>&1 |") || &quit("fork for lynx: $!");
761     while(<L>) { $doc.=$_; }
762     $!=0; close(L);
763     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
764         &transcript("Information ($description) is not available -\n".
765                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
766          $ok++;
767     } elsif ($?) {
768         &transcript("Error getting $description (code $? $!):\n$doc\n");
769     } else {
770         &transcript("Sending $description.\n");
771         &sendmailmessage(<<END.$doc,$replyto);
772 From: $gMaintainerEmail ($gProject $gBug Tracking System)
773 To: $replyto
774 Subject: $gProject $gBugs information: $description
775 References: $header{'message-id'}
776 In-Reply-To: $header{'message-id'}
777 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
778
779 END
780          $ok++;
781     }
782 }
783
784 sub addccaddress {
785     my ($cca) = @_;
786     $maintccreasons{$cca}{''}{$ref}= 1;
787 }
788
789 sub addmaintainers 
790 {       # Data structure is:
791     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
792     my ($p, $addmaint, $pshow);
793     &ensuremaintainersloaded;
794     $anymaintfound=0; $anymaintnotfound=0;
795     for $p (split(m/[ \t?,()]+/,$_[0])) 
796         {       $p =~ y/A-Z/a-z/;
797         $pshow= ($p =~ m/[-+.a-z0-9]+/ ? $& : '');
798         if (defined($maintainerof{$p})) 
799                 {       $addmaint= $maintainerof{$p};
800                         &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
801             $maintccreasons{$addmaint}{$p}{$ref}= 1;
802                         print "maintainer add >$p|$addmaint<\n" if $debug;
803         } else { print "maintainer none >$p<\n" if $debug; }
804     }
805 }
806
807 sub ensuremaintainersloaded {
808     my ($a,$b);
809     return if $maintainersloaded++;
810     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
811     while (<MAINT>) {
812         m/^(\S+)\s+(\S.*\S)\n$/ || &quit("maintainers bogus \`$_'");
813         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
814         $maintainerof{$1}= $2;
815     }
816     close(MAINT);
817 }