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