chiark / gitweb /
3e408f103d2b297b9f6571a89c6d94b9b4ed57d6
[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
54       $MessageFile = "$MNG_ROOT/tmp/messages/$MessageNumber";
55  
56       if( !($MessageNumber =~ /[0-9]+/) || !(-r $MessageFile) )
57         {
58           $Error = "Message number in subject incorrect";
59         }
60  
61     } else {
62       $Error = "no message number";
63     }
64  
65   } elsif ( /^From: /i ) {
66     $From = $_;
67     $From =~ s/^From: //i;
68   }
69 }
70
71
72 after_header:
73  
74 &processError($Error) if( $Error );
75  
76 #
77 # Now we are looking at the body
78 #
79  
80 $done = "";
81 $command = "";
82 $comment = "";
83  
84 while( <STDIN> ) {
85  
86   chop;
87  
88   if( /preapprove/i ) {
89     $command = "processPreapproved xxx";
90     $done = "yes";
91     goto after_body;
92   }
93  
94   if( /approve/i ) {
95       $command = "processApproved xxx ";
96     $done = "yes";
97     goto after_body;
98   }
99  
100   if( /reject/i ) {
101  
102     $reason = $_;
103     $reason =~ s/^.*reject //i;
104     $reason =~ s/( |`|'|"|;|\.|\/|\\)//g;
105     &processError( "wrong rejection reason" ) if( !$reason );
106
107     &processError( "Wrong rejection reason" )
108        if( !( -r "$MNG_ROOT/etc/messages/$reason" ) 
109            && ($reason ne "custom")
110          );
111  
112     $command = "processRejected xxx $reason";
113  
114     $done = "yes";
115     goto after_body;
116   }
117 }
118
119 after_body:
120 print STDERR "After body\n";
121
122 &processError( "No Command Specified" ) if( !$command );
123
124 while( <> ) {
125   if( /^comment/ ) {
126     s/^comment//;
127     $comment = $_;  
128     $comment .= $_ while( <> );
129   }
130 }
131
132 print STDERR "Comment is: $comment\n" if( $comment );
133
134 $ENV{'EXPLANATION'} = $comment;
135
136 open( COMMAND, "| $command" ) ||  &processError( "$command failed" );
137
138   open( MESSAGE, "$MessageFile" ) || &processError( "Can't open $MessageFile" );
139   while( <MESSAGE> ) { print COMMAND or die $!; }
140   close( MESSAGE ) or die "$? $!";
141
142 #  if( $comment && !($command =~ '^processRejected') ) {
143 #    print COMMAND 
144 #       "\n======================================= MODERATOR'S COMMENT: \n" .
145 #       $comment;
146 #  }
147 close( COMMAND ) or die "$? $!";
148
149 &processError( "No action specified" ) 
150   if( $done ne "yes" );
151
152 unlink( $MessageFile );
153
154
155 1;