]> git.donarmstrong.com Git - debbugs.git/blob - scripts/service.in
8eca1324bc076a405e286465c979708843ce585a
[debbugs.git] / scripts / service.in
1 #!/usr/bin/perl -w
2 # $Id: service.in,v 1.19 2000/10/07 15:16:18 joy Exp $
3 #
4 # Usage: service <code>.nn
5 # Temps:  incoming/P<code>.nn
6
7 use Mail::Address;
8 require('/etc/debbugs/config');
9 require('/usr/lib/debbugs/errorlib');
10 $ENV{'PATH'}= '/usr/lib/debbugs'.$ENV{'PATH'};;
11 chdir("$gSpoolDir") || die "chdir spool: $!\n";
12
13 # open(DEBUG,">&4");
14 $debug = 0;
15 $wwwbase= "$gWebDir";
16
17 $_=shift;
18 m/^[RC]\.\d+$/ || &quit("bad argument");
19 $control= m/C/;
20 $nn= $_;
21 if (!rename("incoming/G$nn","incoming/P$nn")) 
22 {       $_=$!.'';  m/no such file or directory/i && exit 0;
23     &quit("renaming to lock: $!");
24 }    
25
26 open(M,"incoming/P$nn");
27 @log=<M>;
28 @msg=@log;
29 close(M);
30
31 grep((s/\n$//,s/\s+$//),@msg);
32
33 print "###\n",join("##\n",@msg),"\n###\n" if $debug;
34
35 chop($tdate= `date -u '+%a, %d %h %Y %T GMT'`);
36 $fwd= <<END;
37 Received: via spool for service; $tdate
38 END
39
40 for ($i=0; $i<=$#msg; $i++) 
41 {       $_ = $msg[$i];
42     last unless length($_);
43     $fwd .= $_."\n";
44     while ($msg[$i+1] =~ m/^\s/) 
45         {       $i++;
46         $fwd .= $msg[$i]."\n" if $ins; # Huh ? Where is ins set ?
47         $_ .= ' '.$msg[$i];
48     }
49         print ">$_<\n" if $debug;
50     if (s/^(\S+):\s*//) 
51         {       $v= $1; $v =~ y/A-Z/a-z/;
52                 print ">$v=$_<\n" if $debug;
53         $header{$v}= $_;
54     } else { print "!>$_<\n" if $debug; }
55 }
56
57 defined($header{'from'}) || &quit("no From header");
58 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\s/i || m/^quit\s/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://$gWebUrl/ 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, @severities, "$gDefaultSeverity")) {
340             &transcript("Severity level \`$newseverity' is not known.\n".
341                         "Recognised are: ".join(' ',@showseverities).".\n\n");
342         } elsif (&setbug) {
343             $printseverity= $s_severity;
344             $printseverity= "$gDefaultSeverity" if $printseverity eq '';
345             $action= "Severity set to \`$newseverity'.";
346             do {
347                 &addmaintainers($s_package);
348                 $s_severity= $newseverity;
349             } while (&getnextbug);
350         }
351     } elsif (m/^retitle\s+\#?(\d+)\s+(\S.*\S)\s*$/i) {
352         $ok++;
353         $ref= $1; $newtitle= $2;
354         if (&getbug) {
355             &foundbug;
356             &addmaintainers($s_package);
357             $s_subject= $newtitle;
358             $action= "Changed $gBug title.";
359             &savebug;
360             &transcript("$action\n");
361             if (length($s_done)) {
362                 &transcript("(By the way, that $gBug is currently marked as done.)\n");
363             }
364             &transcript("\n");
365         } else {
366             &notfoundbug;
367         }
368     } elsif (m/^unmerge\s+\#?(\d+)$/i) {
369         $ok++;
370         $ref= $1;
371         if (&setbug) {
372             if (!length($s_mergedwith)) {
373                 &transcript("$gBug is not marked as being merged with any others.\n\n");
374                 &nochangebug;
375             } else {
376                 $mergelowstate eq 'locked' || die "$mergelowstate ?";
377                 $action= "Disconnected #$ref from all other report(s).";
378                 @newmergelist= split(/ /,$s_mergedwith);
379                 $discref= $ref;
380                 do {
381                     &addmaintainers($s_package);
382                     $s_mergedwith= ($ref == $discref) ? ''
383                         : join(' ',grep($_ ne $ref,@newmergelist));
384                 } while (&getnextbug);
385             }
386         }
387     } elsif (m/^merge\s+(\d+(\s+\d+)+)\s*$/i) {
388         $ok++;
389         @tomerge= sort { $a <=> $b } split(/\s+/,$1);
390         @newmergelist= ();
391         &getmerge;
392         while (defined($ref= shift(@tomerge))) {
393             &transcript("D| checking merge $ref\n") if $dl;
394             $ref+= 0;
395             next if grep($_ eq $ref,@newmergelist);
396             if (!&getbug) { &notfoundbug; @newmergelist=(); last }
397             &foundbug;
398             &transcript("D| adding $ref ($s_mergewith)\n") if $dl;
399             $mismatch= '';
400             &checkmatch('package','m_package',$s_package);
401             &checkmatch('forwarded addr','m_forwarded',$s_forwarded);
402             $s_severity = '$gDefaultSeverity' if $s_severity eq '';
403             &checkmatch('severity','m_severity',$s_severity);
404             &checkmatch('done mark','m_done',length($s_done) ? 'done' : 'open');
405             if (length($mismatch)) {
406                 &transcript("Mismatch - only $gBugs in same state can be merged:\n".
407                             $mismatch."\n");
408                 &cancelbug; @newmergelist=(); last;
409             }
410             push(@newmergelist,$ref);
411             push(@tomerge,split(/ /,$s_mergedwith));
412             &cancelbug;
413         }
414         if (@newmergelist) {
415             @newmergelist= sort { $a <=> $b } @newmergelist;
416             $action= "Merged @newmergelist.";
417             for $ref (@newmergelist) {
418                 &getbug || die "huh ?  $gBug $ref disappeared during merge";
419                 &addmaintainers($s_package);
420                 $s_mergedwith= join(' ',grep($_ ne $ref,@newmergelist));
421                 &savebug;
422             }
423             &transcript("$action\n\n");
424         }
425         &endmerge;
426     } else {
427         &transcript("Unknown command or malformed arguments to command.\n\n");
428         if (++$unknowns >= 5) {
429             &transcript("Too many unknown commands, stopping here.\n\n");
430             last;
431         }
432     }
433 }
434 if ($procline>$#msg) {
435     &transcript(">\nEnd of message, stopping processing here.\n\n");
436 }
437 if (!$ok) {
438     &transcript("No commands successfully parsed; sending the help text(s).\n");
439     &sendhelp;
440     &transcript("\n");
441 }
442
443 &transcript("MC\n") if $dl>1;
444 @maintccs= ();
445 for $maint (keys %maintccreasons) {
446 &transcript("MM|$maint|\n") if $dl>1;
447     next if $maint eq $replyto;
448     $reasonstring= '';
449     $reasonsref= $maintccreasons{$maint};
450 &transcript("MY|$maint|\n") if $dl>2;
451     for $p (sort keys %$reasonsref) {
452 &transcript("MP|$p|\n") if $dl>2;
453         $reasonstring.= ', ' if length($reasonstring);
454         $reasonstring.= $p.' ' if length($p);
455         $reasonstring.= join(' ',map("#$_",sort keys %{$$reasonsref{$p}}));
456     }
457     push(@maintccs,"$maint ($reasonstring)");
458     push(@maintccaddrs,"$maint");
459 }
460 if (@maintccs) {
461     &transcript("MC|@maintccs|\n") if $dl>2;
462     $maintccs= "Cc: ".join(",\n    ",@maintccs)."\n";
463 } else { $maintccs = ""; }
464
465 $reply= <<END;
466 From: $gMaintainerEmail ($gProject $gBug Tracking System)
467 To: $replyto
468 ${maintccs}Subject: Processed: $header{'subject'}
469 In-Reply-To: $header{'message-id'}
470 References: $header{'message-id'}
471 Message-ID: <handler.s.$nn.transcript\@$gEmailDomain>
472
473 ${transcript}Please contact me if you need assistance.
474
475 $gMaintainer
476 (administrator, $gProject $gBugs database)
477 $extras
478 END
479
480 $repliedshow= join(', ',$replyto,@maintccaddrs);
481 &filelock("lock/-1");
482 open(AP,">>db/-1.log") || &quit("open db/-1.log: $!");
483 print(AP
484       "\2\n$repliedshow\n\5\n$reply\n\3\n".
485       "\6\n".
486       "<strong>Request received</strong> from <code>".
487       &sani($header{'from'})."</code>\n".
488       "to <code>".&sani($controlrequestaddr)."</code>\n".
489       "\3\n".
490       "\7\n",@log,"\n\3\n") || &quit("writing db/-1.log: $!");
491 close(AP) || &quit("open db/-1.log: $!");
492 &unfilelock;
493 utime(time,time,"db");
494
495 &sendmailmessage($reply,$replyto,@maintccaddrs);
496
497 unlink("incoming/P$nn") || &quit("unlinking incoming/P$nn: $!");
498
499 sub get_addresses {
500     return
501        map { $_->address() }
502        map { Mail::Address->parse($_) } @_;
503 }
504
505 sub sendmailmessage {
506     local ($message,@recips) = @_;
507     print "mailing to >@recips<\n" if $debug;
508     $c= open(D,"|-");
509     defined($c) || &quit("mailing forking for sendmail: $!");
510     if (!$c) { # ie, we are the child process
511         exec '/usr/lib/sendmail','-f'."$gMaintainerEmail",'-odi','-oem','-oi',get_addresses(@recips);
512         die $!;
513     }
514     print(D $message) || &quit("writing to sendmail process: $!");
515     $!=0; close(D); $? && &quit("sendmail gave exit status $? ($!)");
516     $midix++;
517 }
518
519 sub sendhelp {
520         &sendtxthelpraw("bug-log-mailserver.txt","instructions for request\@$gEmailDomain");
521         &sendtxthelpraw("bug-maint-mailcontrol.txt","instructions for control\@$gEmailDomain")
522             if $control;
523 }
524
525 #sub unimplemented {
526 #    &transcript("Sorry, command $_[0] not yet implemented.\n\n");
527 #}
528
529 sub checkmatch {
530     local ($string,$mvarname,$svarvalue) = @_;
531     local ($mvarvalue);
532     if (@newmergelist) {
533         eval "\$mvarvalue= \$$mvarname";
534         &transcript("D| checkmatch \`$string' /$mvarname/$mvarvalue/$svarvalue/\n")
535             if $dl;
536         $mismatch .=
537             "Values for \`$string' don't match:\n".
538             " #$newmergelist[0] has \`$mvarvalue';\n".
539             " #$ref has \`$svarvalue'\n"
540             if $mvarvalue ne $svarvalue;
541     } else {
542         &transcript("D| setupmatch \`$string' /$mvarname/$svarvalue/\n")
543             if $dl;
544         eval "\$$mvarname= \$svarvalue";
545     }
546 }
547
548 # High-level bug manipulation calls
549 # Do announcements themselves
550 #
551 # Possible calling sequences:
552 #    setbug (returns 0)
553 #    
554 #    setbug (returns 1)
555 #    &transcript(something)
556 #    nochangebug
557 #
558 #    setbug (returns 1)
559 #    $action= (something)
560 #    do {
561 #      (modify s_* variables)
562 #    } while (getnextbug);
563
564 sub nochangebug {
565     &dlen("nochangebug");
566     $state eq 'single' || $state eq 'multiple' || die "$state ?";
567     &cancelbug;
568     &endmerge if $manybugs;
569     $state= 'idle';
570     &dlex("nochangebug");
571 }
572
573 sub setbug {
574     &dlen("setbug $ref");
575     $state eq 'idle' || die "$state ?";
576     if (!&getbug) {
577         &notfoundbug;
578         &dlex("setbug => 0s");
579         return 0;
580     }
581     @thisbugmergelist= split(/ /,$s_mergedwith);
582     if (!@thisbugmergelist) {
583         &foundbug;
584         $manybugs= 0;
585         $state= 'single';
586         $sref=$ref;
587         &dlex("setbug => 1s");
588         return 1;
589     }
590     &cancelbug;
591     &getmerge;
592     $manybugs= 1;
593     if (!&getbug) {
594         &notfoundbug;
595         &endmerge;
596         &dlex("setbug => 0mc");
597         return 0;
598     }
599     &foundbug;
600     $state= 'multiple'; $sref=$ref;
601     &dlex("setbug => 1m");
602     return 1;
603 }
604
605 sub getnextbug {
606     &dlen("getnextbug");
607     $state eq 'single' || $state eq 'multiple' || die "$state ?";
608     &savebug;
609     if (!$manybugs || !@thisbugmergelist) {
610         length($action) || die;
611         &transcript("$action\n$extramessage\n");
612         &endmerge if $manybugs;
613         $state= 'idle';
614         &dlex("getnextbug => 0");
615         return 0;
616     }
617     $ref= shift(@thisbugmergelist);
618     &getbug || die "bug $ref disappeared";
619     &foundbug;
620     &dlex("getnextbug => 1");
621     return 1;
622 }
623
624 # Low-level bug-manipulation calls
625 # Do no announcements
626 #
627 #    getbug (returns 0)
628 #
629 #    getbug (returns 1)
630 #    cancelbug
631 #
632 #    getmerge
633 #    $action= (something)
634 #    getbug (returns 1)
635 #    savebug/cancelbug
636 #    getbug (returns 1)
637 #    savebug/cancelbug
638 #    [getbug (returns 0)]
639 #    &transcript("$action\n\n")
640 #    endmerge
641
642 sub notfoundbug { &transcript("$gBug number $ref not found.\n\n"); }
643 sub foundbug { &transcript("$gBug#$ref: $s_subject\n"); }
644
645 sub getmerge {
646     &dlen("getmerge");
647     $mergelowstate eq 'idle' || die "$mergelowstate ?";
648     &filelock('lock/merge');
649     $mergelowstate='locked';
650     &dlex("getmerge");
651 }
652
653 sub endmerge {
654     &dlen("endmerge");
655     $mergelowstate eq 'locked' || die "$mergelowstate ?";
656     &unfilelock;
657     $mergelowstate='idle';
658     &dlex("endmerge");
659 }
660
661 sub getbug {
662     &dlen("getbug $ref");
663     $lowstate eq 'idle' || die "$state ?";
664     if (&lockreadbug($ref)) {
665         $sref= $ref;
666         $lowstate= "open";
667         &dlex("getbug => 1");
668         $extramessage='';
669         return 1;
670     }
671     $lowstate= 'idle';
672     &dlex("getbug => 0");
673     return 0;
674 }
675
676 sub cancelbug {
677     &dlen("cancelbug");
678     $lowstate eq 'open' || die "$state ?";
679     &unfilelock;
680     $lowstate= 'idle';
681     &dlex("cancelbug");
682 }
683
684 sub savebug {
685     &dlen("savebug $ref");
686     $lowstate eq 'open' || die "$lowstate ?";
687     length($action) || die;
688     $ref == $sref || die "read $sref but saving $ref ?";
689     open(L,">>db/$ref.log") || &quit("opening db/$ref.log: $!");
690     print(L
691           "\6\n".
692           "<strong>".&sani($action)."</strong>\n".
693           "Request was from <code>".&sani($header{'from'})."</code>\n".
694           "to <code>".&sani($controlrequestaddr)."</code>. \n".
695           "\3\n".
696           "\7\n",@log,"\n\3\n") || &quit("writing db/$ref.log: $!");
697     close(L) || &quit("closing db/$ref.log: $!");
698     open(S,">db/$ref.status.new") || &quit("opening db/$ref.status.new: $!");
699     print(S
700           "$s_originator\n".
701           "$s_date\n".
702           "$s_subject\n".
703           "$s_msgid\n".
704           "$s_package\n".
705           "$s_keywords\n".
706           "$s_done\n".
707           "$s_forwarded\n".
708           "$s_mergedwith\n".
709           "$s_severity\n") || &quit("writing db/$ref.status.new: $!");
710     close(S) || &quit("closing db/$ref.status.new: $!");
711     rename("db/$ref.status.new","db/$ref.status") ||
712         &quit("installing new db/$ref.status: $!");
713     &unfilelock;
714     $lowstate= "idle";
715     &dlex("savebug");
716 }
717
718 sub dlen {
719     return if !$dl;
720     &transcript("C> @_ ($state $lowstate $mergelowstate)\n");
721 }
722
723 sub dlex {
724     return if !$dl;
725     &transcript("R> @_ ($state $lowstate $mergelowstate)\n");
726 }
727
728 sub transcript {
729     print $_[0] if $debug;
730     $transcript.= $_[0];
731 }
732
733 sub sendlynxdoc {
734     &sendlynxdocraw;
735     &transcript("\n");
736     $ok++;
737 }
738
739 sub sendtxthelp {
740     &sendtxthelpraw;
741     &transcript("\n");
742     $ok++;
743 }
744
745 sub sendtxthelpraw {
746     local ($relpath,$description) = @_;
747     $doc='';
748     open(D,"$gDocDir/$relpath") || &quit("open doc file $relpath: $!");
749     while(<D>) { $doc.=$_; }
750     close(D);
751     &transcript("Sending $description in separate message.\n");
752     &sendmailmessage(<<END.$doc,$replyto);
753 From: $gMaintainerEmail ($gProject $gBug Tracking System)
754 To: $replyto
755 Subject: $gProject $gBug help: $description
756 References: $header{'message-id'}
757 In-Reply-To: $header{'message-id'}
758 Message-ID: <handler.s.$nn.help.$midix\@$gEmailDomain>
759
760 END
761     $ok++;
762 }
763
764 sub sendlynxdocraw {
765     local ($relpath,$description) = @_;
766     $doc='';
767     open(L,"lynx -nolist -dump http://$gWebDomain/$relpath 2>&1 |") || &quit("fork for lynx: $!");
768     while(<L>) { $doc.=$_; }
769     $!=0; close(L);
770     if ($? == 255 && $doc =~ m/^\n*lynx: Can\'t access start file/) {
771         &transcript("Information ($description) is not available -\n".
772                     "perhaps the $gBug does not exist or is not on the WWW yet.\n");
773          $ok++;
774     } elsif ($?) {
775         &transcript("Error getting $description (code $? $!):\n$doc\n");
776     } else {
777         &transcript("Sending $description.\n");
778         &sendmailmessage(<<END.$doc,$replyto);
779 From: $gMaintainerEmail ($gProject $gBug Tracking System)
780 To: $replyto
781 Subject: $gProject $gBugs information: $description
782 References: $header{'message-id'}
783 In-Reply-To: $header{'message-id'}
784 Message-ID: <handler.s.$nn.info.$midix\@$gEmailDomain>
785
786 END
787          $ok++;
788     }
789 }
790
791 sub addccaddress {
792     my ($cca) = @_;
793     $maintccreasons{$cca}{''}{$ref}= 1;
794 }
795
796 sub addmaintainers 
797 {       # Data structure is:
798     #   maintainer email address &c -> assoc of packages -> assoc of bug#'s
799     my ($p, $addmaint, $pshow);
800     &ensuremaintainersloaded;
801     $anymaintfound=0; $anymaintnotfound=0;
802     for $p (split(m/[ \t?,()]+/,$_[0])) 
803         {       $p =~ y/A-Z/a-z/;
804         $pshow= ($p =~ m/[-+.a-z0-9]+/ ? $& : '');
805         if (defined($maintainerof{$p})) 
806                 {       $addmaint= $maintainerof{$p};
807                         &transcript("MR|$addmaint|$p|$ref|\n") if $dl>2;
808             $maintccreasons{$addmaint}{$p}{$ref}= 1;
809                         print "maintainer add >$p|$addmaint<\n" if $debug;
810         } else { print "maintainer none >$p<\n" if $debug; }
811     }
812 }
813
814 sub ensuremaintainersloaded {
815     my ($a,$b);
816     return if $maintainersloaded++;
817     open(MAINT,"$gMaintainerFile") || die &quit("maintainers open: $!");
818     while (<MAINT>) {
819         m/^\n$/ && next;
820         m/^\s*$/ && next;
821         m/^(\S+)\s+(\S.*\S)\n$/ || &quit("maintainers bogus \`$_'");
822         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
823         $maintainerof{$1}= $2;
824     }
825     close(MAINT);
826 }