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