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