chiark / gitweb /
Plumb messagenumber through in an env var to our sub-scripts
[modbot-mtm.git] / stump / bin / acceptFromMod.pl
1 #
2 # This script accepts an email from moderators and processes
3 # an approval or a rejection.
4 #
5  
6 $MNG_ROOT = $ENV{'MNG_ROOT'} || die "\$MNG_ROOT is not defined";
7  
8 $Prefix = $ENV{'BOT_SUBJECT_PREFIX'} 
9   || die "\$BOT_SUBJECT_PREFIX is not defined";
10  
11 $MessageNumber = "";
12 $Error = "";
13  
14 #######################################################################
15 # notifies the bot supporter
16 #
17 sub processError {
18   $msg = pop( @_ );
19
20 print STDERR "Approval error: " . $msg . "\n"; 
21
22   if( $MessageFile && -r $MessageFile ) {
23     system( "suspicious no NOTICE: This message is being RE-SENT to you because of the following error in your approval: $msg < $MessageFile" );
24   } else {
25     print STDERR "ERROR: Completely bogus approval from $From, $msg\n";
26   }
27
28   exit 1;
29 }
30  
31 ####################################################################### main
32 # handling headers
33 #
34 while( <STDIN> ) {
35  
36   chop;
37
38   if( /^$/ ) {
39     goto after_header;
40   }
41  
42   if( /^Subject: /i  && !$Subject ) {
43  
44     $Subject = $_;
45     $Subject =~ s/^Subject: //i;
46  
47     if( /::$Prefix\// ) {
48  
49       $MessageNumber = $Subject;
50       $MessageNumber =~ s/^.*::$Prefix\///;
51       $MessageNumber =~ /(\d+)/;
52       $MessageNumber = $1;
53       $ENV{'WEBSTUMP_MESSAGENUM'}= $MessageNumber;
54
55       $MessageFile = "$MNG_ROOT/tmp/messages/$MessageNumber";
56  
57       if( !($MessageNumber =~ /[0-9]+/) || !(-r $MessageFile) )
58         {
59           $Error = "Message number in subject incorrect";
60         }
61  
62     } else {
63       $Error = "no message number";
64     }
65  
66   } elsif ( /^From: /i ) {
67     $From = $_;
68     $From =~ s/^From: //i;
69   }
70 }
71
72
73 after_header:
74  
75 &processError($Error) if( $Error );
76  
77 #
78 # Now we are looking at the body
79 #
80  
81 $done = "";
82 $command = "";
83 $comment = "";
84  
85 while( <STDIN> ) {
86  
87   chop;
88  
89   if( /preapprove/i ) {
90     $command = "processPreapproved xxx";
91     $done = "yes";
92     goto after_body;
93   }
94  
95   if( /approve/i ) {
96       $command = "processApproved xxx ";
97     $done = "yes";
98     goto after_body;
99   }
100  
101   if( /reject/i ) {
102  
103     $reason = $_;
104     $reason =~ s/^.*reject //i;
105     $reason =~ s/( |`|'|"|;|\.|\/|\\)//g;
106     &processError( "wrong rejection reason" ) if( !$reason );
107
108     &processError( "Wrong rejection reason" )
109        if( !( -r "$MNG_ROOT/etc/messages/$reason" ) 
110            && ($reason ne "custom")
111          );
112  
113     $command = "processRejected xxx $reason";
114  
115     $done = "yes";
116     goto after_body;
117   }
118 }
119
120 after_body:
121 print STDERR "After body\n";
122
123 &processError( "No Command Specified" ) if( !$command );
124
125 while( <> ) {
126   if( /^comment/ ) {
127     s/^comment//;
128     $comment = $_;  
129     $comment .= $_ while( <> );
130   }
131 }
132
133 print STDERR "Comment is: $comment\n" if( $comment );
134 print STDERR "Signal handling for SIGPIPE: $SIG{PIPE}.\n";
135
136 $ENV{'EXPLANATION'} = $comment;
137
138 open( COMMAND, "| $command" ) ||  &processError( "$command failed" );
139
140   open( MESSAGE, "$MessageFile" ) || &processError( "Can't open $MessageFile" );
141   while( <MESSAGE> ) { print COMMAND or die $!; }
142   close( MESSAGE ) or die "$? $!";
143
144 #  if( $comment && !($command =~ '^processRejected') ) {
145 #    print COMMAND 
146 #       "\n======================================= MODERATOR'S COMMENT: \n" .
147 #       $comment;
148 #  }
149 close( COMMAND ) or die "$command $? $!";
150
151 &processError( "No action specified" ) 
152   if( $done ne "yes" );
153
154 unlink( $MessageFile );
155
156
157 1;