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