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