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