home *** CD-ROM | disk | FTP | other *** search
/ ftp.science.tver.ru / ftp.science.tver.ru.tar / ftp.science.tver.ru / pub / filter_nnrpd.pl < prev    next >
Perl Script  |  2006-11-06  |  7KB  |  174 lines

  1. #! /usr/bin/perl
  2.  
  3. # ⁿ╘╧╘ ╞╔╠╪╘╥ ╘╥┼┬╒┼╘ ╒╙╘┴╬╧╫╠┼╬╬╧╟╧ ═╧─╒╠╤ CRC32 ─╠╤ Perl.
  4. # ⌡ ╦╧╟╧ FreeBSD ╔▌╔╘┼ ╫ ╨╧╥╘┴╚. Σ╠╤ linux ╬┼ ┌╬┴└, ╔▌╔╘┼.
  5.  
  6. use String::CRC32;
  7.  
  8. # Σ┴╠┼┼ ╦╒╙╧╦ ╔┌ ─╔╙╘╥╔┬╒╘╔╫┴ inn. ∩╙╘┴╫╪╘┼ ╦┴╦ ┼╙╘╪.
  9.  
  10. # $Id: filter_nnrpd.pl,v 1.2 2002/12/12 05:01:42 vinocur Exp $
  11. #
  12. # Sample perl filtering code for nnrpd hook.
  13. #
  14.  
  15. #
  16. # This file is loaded when nnrpd starts up. If it defines a sub named
  17. # `filter_post', then that function will be called during processing of a
  18. # posting. It has access to the headers of the article via the associative
  19. # array `%hdr'. If it returns a null string then the article is accepted
  20. # for posting. A non-null string rejects it, and the value returned is used
  21. # in the rejection message.
  22. #
  23.  
  24. #
  25. # Do any initialization steps.
  26. #
  27. my %config = (checkincludedtext => 0,
  28.               includedcutoff => 40,
  29.               includedratio => 0.6,
  30.               quotere => '^[>:]',
  31.               antiquotere => '^[<]',  # so as not to reject dict(1) output
  32.              );
  33.  
  34.  
  35. #
  36. # Sample filter
  37. #
  38. sub filter_post {
  39.     my $rval = "" ;             # assume we'll accept.
  40.     
  41. # µ╔╠╪╘╥ ─╠╤ ╞╔─╧, ╨╧─╙╘┴╬╧╫╦┴ X_COMMENT-TO, ┌┴═┼╬┴ msgid ╬┴ ╞╔─╧█╬┘╩.
  42. # H┴╨╔╙┴╬ ┬╠┴╟╧─┴╥╤ ß╠┼╦╙┴╬─╥╒ τ╧╘╠╔┬╒, 2:5080/1003;
  43. # ≈╠┴─╒ ≡╠╒╓╬╔╦╧╫╒ 2:5021/19 ╔ ≤┼╥╟┼└ ≥┼╫╘╧╫╒ 2:5021/49. 
  44. # ≤╨┴╙╔┬╧ ╔═ ╧╟╥╧═╬╧┼ ╧╘ ╫╙┼╚ ╨╧╠╪┌╧╫┴╘┼╠┼╩ ╟┼╩╘┴.
  45. # ≥┼┌╒╠╪╘┴╘ ╔╚ ╥┴┬╧╘┘ ╧┬╧┬▌╔╠ Yuri Kosivtsov, 2:5021/29,
  46. # ╥┼┴╠╔┌╧╫┴╬ ╬┴ news://fido.tver.ru ╔ http://fido.tver.ru
  47.     
  48.     my $grephistory = '/usr/local/news/bin/grephistory';
  49.     my $sm = '/usr/local/news/bin/sm';
  50.  
  51. # Σ┴╠┼┼ ╧╨╤╘╪ ╦┴╦┴╤-╘╧ ╞╔╟╬╤ ╔┌ ─╔╙╘╥╔┬╒╘╔╫┴
  52.  
  53. ### Uncomment this next block to reject articles that have 'make money'
  54. ### in their subject, or which have a "Re: " subject, but no References:
  55. ### header, or which have an invalid From.
  56.  
  57. ##    if ($hdr{"Subject"} =~ /make.*money/i) {
  58. ##        $rval = "Spam is not acceptable here..." ;
  59. ##    } elsif ($hdr{'Subject'} =~ /^Re: /o and $hdr{'References'} eq "") {
  60. ##        $rval = "Followup without References:";
  61. ##    } elsif ($hdr{'From'} =~ /^\w*$/o or
  62. ##             $hdr{'From'} !~ /^(.+?)\@([-\w\d]+\.)*([-\w\d]+)\.([-\w\d]{2,})$/o) {
  63. ##        $rval = "From: is invalid, must be user\@[host.]domain.tld";
  64. ##    }
  65.  
  66.  
  67. ### The next block rejects articles with too much quoted text, if the
  68. ### config hash directs it to.
  69.  
  70.     if ($config{checkincludedtext}) {
  71.         my ($lines, $quoted, $antiquoted) = analyze($body);
  72.         if ($lines > $config{includedcutoff}
  73.                 && $quoted - $antiquoted > $lines * $config{includedratio}) {
  74.             $rval = "Article contains too much quoted text";
  75.         }
  76.     }
  77.  
  78. # ╦╒╙╧╦ ≡╠╒╓╬╔╦╧╫┴ ╔ ≥┼╫╘╧╫┴
  79.  
  80. # ≡╧╨┘╘╦┴ ╙╟┼╬┼╥╔╥╧╫┴╘╪ ╞╔─╧█╬┘╩ MSGID.
  81. # ≡╥╧┬╠┼═┴ ╫ ╙╠┼─╒└▌┼═. ≡╥╔ ╟┼╩╘╧╫┴╬╔╔ ╬╪└╙╧╫ ╟┼╬┼╥╔╥╒┼╘╙╤ ═╙╟╔─
  82. # "<┼═┼╩╠> ╬╧═┼╥".  ⁿ╘╧ ╨╠╧╚╧, ╘.╦. ╨╥╔ ─╫╧╩╬╧═ (╘╥╧╩╬╧═ ╔ ╘.╨., ╫┼─╪ ╘╫╧╩
  83. # ╟┼╩╘ ╬┼ ╧─╔╬ ╙ ╫ ╙┼╘╔ :-)) ╟┼╩╘╧╫┴╬╔╔ ═╧╟╒╘ ┬┘╘╪ ╙┬╧╔. δ╥╧═┼ ╘╧╟╧,
  84. # ╬┼╦╧╘╧╥┘┼ ▐╔╘┴╠╦╔ ┬┼╥╒╘ ╔┌ MSGID ┴─╥┼╙ ╒┌╠┴ ╔ █╔┌┼└╘ ╧╘ ╬┼╞╔─╧█╬╧╟╧
  85. # ╫╔─┴ ╔─┼╬╘╔╞╔╦┴╘╧╥┴. ≡╧▄╘╧═╒, ╘┼ ╨╧╠╪┌╧╫┴╘┼╠╔ ╟┼╩╘┴, ╦╧╘╧╥┘┼ ╔═┼└╘
  86. # ╞╔─╧█╬┘┼ ┴─╥┼╙┴ (╨╧╔╬╘┘ ╔ ╬╧─┘, ╨╧ ╦┴╦╔═-╠╔┬╧ ╨╥╔▐╔╬┴═ ╨╧╠╪┌╒└▌╔═╔╙╤
  87. # ╟┼╩╘╧═), ╨╥┼─▀╤╫╠╤└╘ ┼═┼╩╠ ╫╔─┴ Vasya_Pupkin@pX.fY.nZ.z2.fido.tver.ru.
  88. # z2.fido.tver.ru - ▄╘╧ ═╧╩ ─╧═┼╬, ╬┼ ┌┴┬╒─╪╘┼ ┌┴═┼╬╔╘╪ ╬┴ ╙╫╧╩ :-)
  89. # ⌠┴╦╧╩ ┼═┼╩╠ ┌┴═┼╬╤┼╘╙╤ ╬┴ MSGID ╫╔─┴ "2:Z/Y.X ╬╧═┼╥".
  90. # σ╙╠╔ ┴─╥┼╙ ╬┼ ╙╧╧╘╫┼╘╙╘╫╒┼╘ ═┴╙╦┼, ╘╧ ╟┼╬┼╥╔╥╒┼╘╙╤ MSGID
  91. # ╫╔─┴ "2:5021/29.100 ╬╧═┼╥", ╟─┼ 2:5021/29.100 - ┴─╥┼╙, ╬┴ ╦╧╘╧╥╧═
  92. # ╒╙╘┴╬╧╫╠┼╬ ╟┼╩╘
  93.  
  94. # ╦╒╙╧╦ ≥┼╫╘╧╫┴
  95.  
  96.     $ftn_suff = "z2.fido.tver.ru";                                      
  97.                                                                             
  98.     $tmp = $hdr{"Message-ID"};                                          
  99.     $crc = crc32($tmp);                                                 
  100.     $modify_headers = 1;                                                
  101.                                                                                 
  102.     $id = lc ($hdr{"From"});  # Lower case                              
  103.                                                                                     
  104.     if ($id =~ /^.+\@p(\d+)\.f(\d+)\.n(\d+)\.z2\.fido\.tver\.ru/) {    
  105.     $p = $1;                                                          
  106.     $f = $2;                                                          
  107.     $n = $3;                                                          
  108.     $hdr{"Message-ID"} = sprintf("<%u\@p$p.f$f.n$n.$ftn_suff>", $crc);
  109.     $hdr{"X-FTN-MSGID"} = sprintf("2:$n/$f.$p %x",$crc);              
  110.       } elsif ( $id =~ /^.+\@f(\d+)\.n(\d+)\.z2\.fido\.tver\.ru/) {      
  111.         $f = $1;                                                          
  112.         $n = $2;                                                          
  113.     $hdr{"Message-ID"} = sprintf("<%u\@f$f.n$n.$ftn_suff>", $crc);    
  114.     $hdr{"X-FTN-MSGID"} = sprintf("2:$n/$f %x",$crc);                 
  115.       } else {                                                            
  116.         $hdr{"Message-ID"} = sprintf("<%u\@p100.f29.n5021.$ftn_suff>",$crc);   
  117.         $hdr{"X-FTN-MSGID"} = sprintf("2:5021/29.100 %x",$crc);               
  118.     }
  119.  
  120. # δ╒╙╧╦ ß.τ╧╘╠╔┬┴. Σ┼╠┴┼╘ X-COMMENT-To. ∩╙╬╧╫┴╬ ╬┴ ╒╘╔╠╔╘┼
  121. # grephistory ╔┌ inn
  122. # σ╙╠╔ ╫ ╙╧╧┬▌┼╬╔╔ ╒╓┼ ┼╙╘╪ X-COMMENT-To, ╘╧ ╬╔▐┼╟╧ ╬┼ ╨╥╧╔╙╚╧─╔╘.
  123. # σ╙╠╔ X-COMMENT-To ╬┼╘, ╘╧ ╨╥╧╔┌╫╧─╔╘╙╤ ╨╧╔╙╦ ╨╧ history ╔ ╧╘╘╒─┴
  124. # ┬┼╥┼╘╙╤ ╔═╤ ╧╘╨╥┴╫╔╘┼╠╤. σ╙╠╔ ╬╔▐┼╟╧ ╬┼ ╬┴╩─┼╬╧, ╘╧ X-COMMENT-To
  125. # ╬┼ ╫╙╘┴╫╠╤┼╘╙╤.
  126.  
  127.     if ( !(($hdr{"Comment-To"} ne "") ||
  128.            ($hdr{"X-Comment-To"} ne "") ||
  129.            ($hdr{"X-FTN-To"} ne "") ||
  130.            ($hdr{"X-Fidonet-Comment-To"} ne "") ||
  131.            ($hdr{"X-Apparently-To"} ne "")))  {
  132.            my $refs = $hdr{"References"};
  133.        if( $refs ne "" ) {
  134.                 my @refs = split(/ /, $refs);
  135.             my $msgid = pop @refs;
  136.             $msgid =~ s/[<>|;\s'"]//g;
  137.             my $fn=`$grephistory \'$msgid\' 2>&1`;
  138.         if( (!($fn =~ /\/dev\/null/)) && 
  139.             (!($fn =~ /Not found/)) ) {
  140.             my $orig_from = `$sm $fn`;
  141.             my @orig_from = grep(/^From:/, split(/\n/, $orig_from));
  142.             $orig_from = shift @orig_from;
  143.             $orig_from =~ s/^From:\s*//;
  144.             $orig_from =~ s/^\s*(.+[^\s])\s*<[^\s><"\(\)\@]+\@[^\s><"\(\)\@]+>\s*$/$1/;
  145.             $orig_from =~ s/^\s*<{0,1}[^\s><"\(\)\@]+\@[^\s><"\(\)\@]+>{0,1}\s*\((.+)\)\s*$/$1/;
  146.             $orig_from =~ s/^"//;
  147.             $orig_from =~ s/"$//;
  148.             $hdr{"X-Comment-To"} = $orig_from;
  149.             $modify_headers = 1;
  150.             }
  151.         }
  152.         }
  153.     return $rval;
  154. }
  155.  
  156. # Σ┴╠┼┼ ╦╒╙╧╦ ╔┌ ─╔╙╘╥╔┬╒╘╔╫┴ inn
  157.  
  158. sub analyze {
  159.     my ($lines, $quoted, $antiquoted) = (0, 0, 0);
  160.     local $_ = shift;
  161.  
  162.     do {
  163.         if ( /\G$config{quotere}/mgc ) {
  164.             $quoted++;
  165.         } elsif ( /\G$config{antiquotere}/mgc ) {
  166.             $antiquoted++;
  167.         }
  168.     } while ( /\G(.*)\n/gc && ++$lines );
  169.  
  170.     return ($lines, $quoted, $antiquoted);
  171. }
  172.