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