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