]> code.citadel.org Git - infanticide.git/blob - infanticide.pm
Initial commit
[infanticide.git] / infanticide.pm
1 #!/usr/bin/perl
2
3 # This is a SpamAssassin module that kills mail from newly-born domains.
4
5
6 package infanticide;
7 use Mail::SpamAssassin::Plugin;
8 use Sys::Syslog;
9 use Sys::Syslog qw(:standard :macros);
10 #use Time::Local;
11 use HTTP::Date;
12
13 our @ISA = qw(Mail::SpamAssassin::Plugin);
14 my $counter = 100;
15 sub new {
16         my ($class, $mailsa) = @_;
17
18         # the usual perlobj boilerplate to create a subclass object
19         $class = ref($class) || $class;
20         my $self = $class->SUPER::new($mailsa);
21         bless ($self, $class);
22  
23         # syslog is fun
24         syslog('info', 'Initializing INFANTICIDE for SpamAssassin (c) 2022-2023 by Art Cancro');
25
26         # Register evaluation rules
27         $self->register_eval_rule ("check_for_disposable_domain");
28
29         # and return the new plugin object
30         return $self;
31 }
32
33
34 sub trim {
35         my $s = shift; $s =~ s/^\s+|\s+$//g; return $s
36 }
37
38
39 sub infanticide_get_creation_date {
40         my $domain_to_test = $_[0];
41         my $cDate = "";
42
43         open my $fh, "-|", "whois", $domain_to_test     or return("");
44         my @lines = <$fh>                               or return("");
45         close $fh                                       or return("");
46         foreach(@lines) {
47                 if (index($_, "Creation Date:") != -1) {
48                         my @arr = split("Creation Date:", $_);
49                         $cDate = trim($arr[1]);
50                         # 2017-06-02T13:15:42Z
51                 }
52         }
53         return($cDate);
54 }
55
56
57 sub check_for_disposable_domain() {
58         $counter = $counter + 1;
59
60         my ($self, $pms) = @_;
61         my $fromaddr = $pms->get("From:addr", undef);
62         my @fromarr = split('@', $fromaddr);
63         my $domain = $fromarr[1];
64         my $creationDate = infanticide_get_creation_date($domain);
65         if ($creationDate eq "") {
66                 syslog('debug', "infanticide: " . $counter . " no creation date for " . $domain);
67                 return(0);
68         }
69         my $dt = str2time($creationDate);
70         my $daysAgo = (time() - $dt) / 86400;
71         syslog("debug", "infanticide: " . $domain . " was created " . $daysAgo . " days ago");
72         if ($daysAgo < 30) {
73                 return(1);
74         }
75         return(0);
76 }