chiark / gitweb /
Make noack honoured for approvals too
[modbot-mtm.git] / stump / bin / suspicious.pl
1 #
2 # this script processes "suspicious" messages. It does basically two things:
3 #
4 # 1. Sends an acknowledgment to the author, if necessary (ie if the author did
5 #    not turn ack mode off), and
6 # 2. Prepares, signs, and sends the message to a randomly chosen human 
7 #    moderator for review.
8 #
9
10 $MNG_ROOT = $ENV{'MNG_ROOT'};
11 $Prefix   = $ENV{'BOT_SUBJECT_PREFIX'};
12 $tempFile = "$ENV{'TMP'}/signed.$$";
13
14 # name of moderator to test
15 $TestModerator = "eugenez\@mit.edu";
16
17 $isTesting = 0; # No testing by default
18 $TestFlag="test-test";
19
20 $needAck = $ARGV[0]; shift @ARGV;
21
22 $message = join( " ", @ARGV );
23
24 print STDERR "Needack = $needAck\n";
25
26
27 ######################################################################
28 # Getting data
29 sub readMessage {
30   $IsBody = 0;
31   
32 #open IWJL, ">>/home/webstump/t.log";
33 #print IWJL "=========== SUSPICIOUS READMESSAGE\n";
34
35   while( <STDIN> ) {
36 #print IWJL "SsRm $_\n";
37
38     $Body .= $_;
39
40     $isKOI8 = $isKOI8 || $_ =~ /[\x80-\xFF]/;
41  
42     chop;
43   
44     if( /^$/ ) {
45       $IsBody = 1;
46     }
47   
48     if( !$IsBody ) {
49   
50       if( /^Newsgroups: / ) {
51         $Newsgroups = $_;
52       } elsif( /^Subject: / ) {
53         $Subject = substr( $_, 0, 50 );
54
55         if( $Subject =~ /$TestFlag/i ) { # special subject
56           $isTesting = 'y';
57         }
58
59       } elsif( /^From: / ) {
60         $From = $_;
61         if ($From =~ m/([\w-\.]+)@([\w-\.]*)[^\w-\.]/){
62             $From = "From: $1\@$2";}
63       } elsif( /^Path: / ) {
64         $Path = $_;
65       } elsif( /^Keywords: / ) {
66         $Keywords = $_;
67       } elsif( /^Summary: / ) {
68         $Summary = $_;
69       } elsif( /^Message-ID: / ) {
70         $Message_ID = $_;
71       }
72   
73     }
74   }
75 }
76
77 $MNG_ROOT = "$ENV{'MNG_ROOT'}" || die "Root dir for moderation not specified";
78
79 &readMessage;
80
81 #$Body =~ s/From /_From /g;
82
83
84 if( $isTesting eq 'y' ) {
85   $moderator = $From; # $TestModerator;
86   $moderator =~ s/^From: //i;
87
88   } else { # get random moderator
89   open( MODERATORS, "$MNG_ROOT/etc/moderators" ) 
90     || die "can't open moderators file";
91
92   while( <MODERATORS> ) {
93     next if( /^#/ );
94     next if( /^\s*$/ );
95     ($name, $priority, $flags) = split;
96   
97     if( $priority != 0 && ! ($isKOI8 && $flags =~ /nokoi/i)) { 
98     # priority 0 means "on vacation"
99       push( @moderators, $name );
100     }
101   }
102
103   close( MODERATORS );
104   srand;
105
106   $randNum = rand 100 + time;
107
108   $moderator = $moderators[ $randNum % ($#moderators + 1) ];
109 }
110
111
112 $modNick = $moderator;
113
114 $modNick =~ s/@.*//;
115 $modNick =~ s/-.*//;
116
117 $date = `date`; chop $date;
118
119 print STDERR "Activity: $date Forwarding $Message_ID, $Subject from $From ".
120              "to $moderator for approval\n";
121
122 $MessageNumber = time . $$;
123
124 print STDERR "Opening $MNG_ROOT/tmp/messages/$MessageNumber\n";
125
126 open( MESSAGE, "> $MNG_ROOT/tmp/messages/$MessageNumber" ) or die $!;
127 print MESSAGE $Body or die $!;
128 close( MESSAGE ) or die $!;
129
130 $Subject = "Subject: try again" if( !$Subject );
131
132 open( COMMAND, "| sendmail -odb -oi $moderator > /dev/null"  ) or die $!;
133
134 print COMMAND "From: $ENV{'DECISION_ADDRESS'}
135 $Subject ::$Prefix/$MessageNumber
136 To: $moderator
137 X-Moderate-For: $ENV{'NEWSGROUP'}
138 Organization: Robots Are Us
139
140 $message
141
142 This is an automated message sent to you as the moderator of
143 $ENV{'NEWSGROUP'}. Simply reply to this message,
144 DO NOT quote the message body in your reply, and state your decision ON
145 THE FIRST LINE, choosing LITERALLY from ONE of the following options
146 (you can even cut and paste):
147
148 approve
149 preapprove
150 " or die $!;;
151
152 open( REASONS, "$MNG_ROOT/etc/rejection-reasons.lst" ) or die $!;
153 while( <REASONS> ) {
154   ($reason, $explanation) = split( /::/, $_ );
155   print COMMAND "reject $reason\n" or die $!;
156 }
157 close( REASONS ) or die $!;
158
159 print COMMAND "
160
161 You can also specify a comment, by typing word comment at the
162 beginning of a line _after_ approve, preapprove, or reject, and
163 then your comment on one or several lines. Do NOT put your comment
164 before approve, preapprove, or reject.
165
166 Message follows:
167 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
168 " or die $!;
169
170 print COMMAND $Body or die $!;
171 close( COMMAND ) or die "$? $!";
172
173 if( $needAck eq "yes" ) {
174   open( ACK, "| modack.received" ) or die $!;
175   print ACK $Body or die $!;
176   close( ACK ) or die "$? $!";
177 }
178
179 1;