chiark / gitweb /
Old version
[bofhbot] / autorep.pl
1 #!/usr/bin/perl
2
3 my $rcsid='$Id: testrep.channel,v 1.2 2000/08/22 23:52:51 sec Exp sec $';
4 my $len=5; # Max Size in KB of included article.
5
6 use Date::Manip;
7 use strict;
8
9 $|=1;
10 while(<>){
11         chomp;
12         if(!open(ARTICLE,"/usr/lib/news/bin/sm '$_'|")){
13                 print "$0 failed to get article $_\n";
14         };
15         print "$_\n";
16         &reply();
17         close(ARTICLE);
18 };
19 print "$0 Exiting on eof.\n";
20 sleep(10);
21 exit(0);
22
23 sub reply(){
24 my @hdrs;
25 my ($nor,$date,$from,$reply,$mid,$path,$subject,$ng);
26 my ($pathe);
27 my ($nodes,@paths,$paths,$s);
28
29 while (<ARTICLE>){
30         push @hdrs,$_;
31         chomp;
32         last if /^$/;
33         $nor=1 if (/(ignore|no\s?reply)/i);
34         $date=$_ if (s/^date:\s+//i);
35         $from=$_ if (s/^from:\s+//i);
36         $reply=$_ if (s/^reply-to:\s+//i);
37         $mid=$_ if (s/^message-id:\s+//i);
38         $path=$_ if (s/^path:\s+//i);
39         $subject=$_ if (s/^subject:\s+//i);
40         $ng=$_ if (s/^newsgroups:\s+//i);
41 }
42
43 for my $k (1..10){
44         $_ = scalar <ARTICLE>;
45         push @hdrs,$_;
46         $nor=1 if (/(ignore|no\s?reply)/i);
47 }
48
49 return if ($ng =~ /,/); # Ignore cross-postings.
50 return if ($nor);       # User wanted us to ignore it.
51 return if ($from =~ /NNTP-Monitor\@hero\.vmc\.private\.ntl\.com/) ;
52                         # spammy and unmailable
53
54 my $diff=&timediff(time-
55 UnixDate(ParseDate($date),"%s")
56 ),"\n";
57
58 sub timediff(){
59         my @n=qw(second minute hour day);
60         my @t=(60,60,24,99999);
61         my $r=shift;
62         my $f=1;
63         my $p=0;
64         my $s="";
65         for my $i (0..$#t){
66                 $p=$r%$t[$i];$r=int($r/$t[$i]);
67                 #print "p: $p, r: $r\n";
68                 if($p){$s=(($f-->0 && $r>0)?"and ":"").$p." ".$n[$i].($p>1?"s":"").($f<-1?",":"").($s?" ".$s:"")}
69         }
70         return $s;
71 }
72
73 $from=$reply if ($reply);
74
75 $path =~ /([^!]*)!/; $pathe=$1;
76 $nodes = (($paths = $path)=~ s/!/ /g);
77 $s=$nodes>1?"s":"";
78
79 @paths=split(/!/,$path);
80 pop(@paths);
81 $paths=join(" ->_",reverse @paths).". ";
82 $paths=~s/([^\n]{40,75})[ \n]/$1\n/g;
83 $paths=~s/_/ /g;
84
85 my $rdate=localtime;
86
87 open(FOO,"|/usr/sbin/sendmail -t -f usenet-autoresponder\@ysolde.ucam.org");
88 print FOO <<EOF;
89 Subject: Response to a test message. (was: $subject)
90 From: $ng autoresponder <usenet\@ysolde.ucam.org>
91 To: $from
92 X-Rcs-Id: $rcsid
93 X-Local-Version: 0.1.0
94 Precedence: junk
95
96 This message has been generated automatically in order to help you track
97 the circulation in the $ng newsgroup. If you do not wish any reply
98 from this program, just include the words "ignore" or "no reply" in the
99 header or in the 10 first lines of the body of your message.
100
101 Your Message $mid was received by $pathe
102 at $rdate.
103
104 Assuming your clock was set correctly, this means that your article
105 travelled the network for $diff
106 before arriving here.
107
108 It had to pass through the following $nodes Usenet node$s starting with
109 your system:
110 $paths
111
112 Full headers plus the first 10 lines of your original message were:
113
114 EOF
115
116 my $str=join ("",@hdrs);
117 print FOO substr($str,0,$len*1024);
118 if (substr($str,$len*1024,1)){
119         print "\n- The above message has been trunctated, because it is longer than  $len KB.\n";
120 }
121 close(FOO);
122
123 };