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