chiark / gitweb /
xlog: Optionally show more approval/rejection reasons
[modbot-mtm.git] / webstump / scripts / filter.lib.pl
1 #
2 #
3 # This library of functions is used for filtering messages.
4 #
5
6
7 # processes approval decision.
8 #
9 # Arguments: 
10 #
11 # Subject, newsgroup, ShortDirectoryName, decision, comment
12
13 sub process_approval_decision {
14   my $cathow = @_>=6 ? pop(@_) : "UNKNOWN";
15   my $comment = pop( @_ );
16   my $decision = pop( @_ );
17   my $ShortDirectoryName = pop( @_ );
18   my $newsgroup = pop( @_ );
19   my $Subject = pop( @_ );
20   my $now = time;
21
22   my $address = $newsgroups_index{$newsgroup};
23
24   my $message = "To: $newsgroups_index{$newsgroup}\n" .
25                 "Subject: $Subject\n" .
26                 "Organization: http://www.algebra.com/~ichudov/stump\n";
27
28   $message .= "\n# $cathow\n";
29   $message .= "\n$decision\n";
30   $message .= "comment $comment\n" if $comment;
31   &email_message( $message, $address );
32
33   my $sanisubj= $Subject;
34   $sanisubj =~ s/.*\:\://;
35
36 print STDERR "DECISION: $newsgroup | $ShortDirectoryName | $decision | $cathow | $now | $sanisubj\n";
37
38   &rmdir_rf( &article_file_name( $ShortDirectoryName ) );
39
40 }
41
42
43 ###################################################################### checkAck
44 # checks the string matches one of the substrings. A name is matched
45 # against the substrings as regexps and substrings as literal substrings.
46 #
47 # Arguments: address, listname
48
49 sub name_is_in_list { # address, listname
50   my $listName = pop( @_ );
51   my $address = pop( @_ );
52
53   my $item = "";
54   my $Result = "";
55
56   open( LIST, &full_config_file_name( $listName ) ) || return "";
57
58   while( $item = <LIST> ) {
59
60     chomp $item;
61
62     next unless $item =~ /\S/;
63     next if $item =~ /^\s*\#/;
64
65     if ($listName eq 'good.posters.list') {
66         if( lc $address eq lc $item ) {
67             $Result = $item;
68         }
69     } else {
70         if( eval { $address =~ /$item/i; } ) {
71             $Result = $item;
72         }
73     }
74   }
75
76   close( LIST );
77
78   return $Result;
79 }
80
81 ######################################################################
82 # reviews incoming message and decides: approve, reject, keep
83 # in queue for human review
84 #
85 # Arguments: Newsgroup, From, Subject, Message, Dir
86 #
87 # RealSubject is the shorter subject from original posting
88 sub review_incoming_message { # Newsgroup, From, Subject, RealSubject, Message, Dir
89   my $dir = pop( @_ );
90   my $message = pop( @_ );
91   my $real_subject = pop( @_ );
92   my $subject = pop( @_ );
93   my $from = pop( @_ );
94   my $newsgroup = pop( @_ );
95
96   if( &name_is_in_list( $from, "bad.posters.list" ) ) {
97     &process_approval_decision( $subject, $newsgroup, $dir, "reject blocklist", "", "auto bad poster" );
98     return;
99   }
100
101   if( &name_is_in_list( $real_subject, "bad.subjects.list" ) ) {
102     &process_approval_decision( $subject, $newsgroup, $dir, "reject thread", "", "auto bad subject" );
103     return;
104   }
105
106   if( &name_is_in_list( $message, "bad.words.list" ) ) {
107     &process_approval_decision( $subject, $newsgroup, $dir, "reject charter", 
108     "Your message has been autorejected because it appears to be off topic
109     based on our filtering criteria. Like everything, filters do not
110     always work perfectly and you can always appeal this decision.",
111                                 "auto bad word" );
112     return;
113   }
114
115   my $warning_file = &article_file_name( $dir ) . "/stump-warning.txt";
116   my $match;
117
118   $ignore_demo_mode = 1;
119
120   if( $match = &name_is_in_list( $from, "watch.posters.list" ) ) {
121     &append_to_file( $warning_file, "Warning: poster '$from' matches '$match' from the list of suspicious posters\n" );
122 print STDERR "Filing Article for review because poster '$from' matches '$match'\n";
123     return; # file message
124   }
125
126   if( $match = &name_is_in_list( $real_subject, "watch.subjects.list" ) ) {
127     &append_to_file( $warning_file, "Warning: subject '$real_subject' matches '$match' from the list of suspicious subjects\n" );
128 print STDERR "Filing Article for review because subject '$subject' matches '$match'\n";
129     return; # file message
130   }
131
132   if( $match = &name_is_in_list( $message, "watch.words.list" ) ) {
133     &append_to_file( $warning_file, "Warning: article matches '$match' from the list of suspicious words\n" );
134 print STDERR "Filing Article for review because article matches '$match'\n";
135     return; # file message
136   }
137
138   if( &name_is_in_list( $from, "good.posters.list" ) ) {
139     &process_approval_decision( $subject, $newsgroup, $dir, "approve", "",
140                                 "auto good poster" );
141     return;
142   }
143
144   if( &name_is_in_list( $real_subject, "good.subjects.list" ) ) {
145     &process_approval_decision( $subject, $newsgroup, $dir, "approve", "",
146                                 "auto good subject" );
147     return;
148   }
149
150   # if the message remains here, it is stored for human review.
151
152 }
153
154 1;