added writesize
[tridge/junkcode.git] / received_from.pl
1 #!/usr/bin/perl -w
2
3 my $ip_file = "$ENV{HOME}/.spam_ips";
4
5 #####################################################################
6 # load a data structure from a file (as saved with SaveStructure)
7 sub LoadStructure($)
8 {
9         my $f = shift;
10         my $contents = FileLoad($f);
11         return eval "$contents";
12 }
13
14
15 use strict;
16 use Data::Dumper;
17
18 #####################################################################
19 # read a file into a string
20 sub FileLoad($)
21 {
22     my($filename) = shift;
23     local(*INPUTFILE);
24     open(INPUTFILE, $filename) || return undef;
25     my($saved_delim) = $/;
26     undef $/;
27     my($data) = <INPUTFILE>;
28     close(INPUTFILE);
29     $/ = $saved_delim;
30     return $data;
31 }
32
33 #####################################################################
34 # write a string into a file
35 sub FileSave($$)
36 {
37     my($filename) = shift;
38     my($v) = shift;
39     local(*FILE);
40     open(FILE, ">$filename") || die "can't open $filename";    
41     print FILE $v;
42     close(FILE);
43 }
44
45 #####################################################################
46 # save a data structure into a file
47 sub SaveStructure($$)
48 {
49     my($filename) = shift;
50     my($v) = shift;
51     FileSave($filename, Dumper($v));
52 }
53
54
55 ##############
56 # main program
57
58 my $ip = 0;
59
60 while (my $line = <>) {
61         if ($line =~ /^Received: from .*?\[([\d\.]+)\]/) {
62                 $ip = $1;
63                 last;
64         }
65 }
66
67 if ($ip eq "127.0.0.1") {
68         exit 0;
69 }
70
71 if ($ip) {
72         my $ips = LoadStructure($ip_file);
73         $ips->{$ip}++;
74         SaveStructure($ip_file, $ips);
75 }