chiark / gitweb /
Merge branch 'running' of login.chiark.greenend.org.uk:public-git/inn-innduct
[innduct.git] / samples / filter_nnrpd.pl
1 #
2 # $Id: filter_nnrpd.pl 5981 2002-12-12 05:01:42Z vinocur $
3 #
4 # Sample perl filtering code for nnrpd hook.
5 #
6
7 #
8 # This file is loaded when nnrpd starts up. If it defines a sub named
9 # `filter_post', then that function will be called during processing of a
10 # posting. It has access to the headers of the article via the associative
11 # array `%hdr'. If it returns a null string then the article is accepted
12 # for posting. A non-null string rejects it, and the value returned is used
13 # in the rejection message.
14 #
15
16 #
17 # Do any initialization steps.
18 #
19 my %config = (checkincludedtext => 0,
20               includedcutoff => 40,
21               includedratio => 0.6,
22               quotere => '^[>:]',
23               antiquotere => '^[<]',  # so as not to reject dict(1) output
24              );
25
26
27 #
28 # Sample filter
29 #
30 sub filter_post {
31     my $rval = "" ;             # assume we'll accept.
32
33 ### Uncomment this next block to reject articles that have 'make money'
34 ### in their subject, or which have a "Re: " subject, but no References:
35 ### header, or which have an invalid From.
36
37 ##    if ($hdr{"Subject"} =~ /make.*money/i) {
38 ##        $rval = "Spam is not acceptable here..." ;
39 ##    } elsif ($hdr{'Subject'} =~ /^Re: /o and $hdr{'References'} eq "") {
40 ##        $rval = "Followup without References:";
41 ##    } elsif ($hdr{'From'} =~ /^\w*$/o or
42 ##             $hdr{'From'} !~ /^(.+?)\@([-\w\d]+\.)*([-\w\d]+)\.([-\w\d]{2,})$/o) {
43 ##        $rval = "From: is invalid, must be user\@[host.]domain.tld";
44 ##    }
45
46
47 ### The next block rejects articles with too much quoted text, if the
48 ### config hash directs it to.
49
50     if ($config{checkincludedtext}) {
51         my ($lines, $quoted, $antiquoted) = analyze($body);
52         if ($lines > $config{includedcutoff}
53                 && $quoted - $antiquoted > $lines * $config{includedratio}) {
54             $rval = "Article contains too much quoted text";
55         }
56     }
57
58     return $rval;
59 }
60
61 sub analyze {
62     my ($lines, $quoted, $antiquoted) = (0, 0, 0);
63     local $_ = shift;
64
65     do {
66         if ( /\G$config{quotere}/mgc ) {
67             $quoted++;
68         } elsif ( /\G$config{antiquotere}/mgc ) {
69             $antiquoted++;
70         }
71     } while ( /\G(.*)\n/gc && ++$lines );
72
73     return ($lines, $quoted, $antiquoted);
74 }