-
Notifications
You must be signed in to change notification settings - Fork 1
/
FromDomainCheck.pm
70 lines (54 loc) · 1.46 KB
/
FromDomainCheck.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
use strict;
use warnings;
package FromDomainCheck;
use Mail::SpamAssassin::Plugin;
use Net::DNS::Resolver;
my $MAIL_OK = 0;
my $MAIL_SPAM = 1;
my $ACTION_UNRESOLVABLE_HOSTNAME = $MAIL_OK; # if you want to drop these emails, set it to $MAIL_SPAM
my $DOMAIN_PATTERN = q"\@([\d\w\-\.]+)";
my $RECORD_TYPE_A = "A";
my %IP_BLACKLIST = (
"185.140.110.3" => 1,
"185.207.8.14" => 1,
"185.207.11.245" => 1,
"185.207.8.246" => 1,
"174.129.25.170" => 1,
"89.144.57.114" => 1
);
our @ISA = qw(Mail::SpamAssassin::Plugin);
sub new {
my ($class, $mailsa) = @_;
$class = ref($class) || $class;
my $self = $class->SUPER::new($mailsa);
bless ($self, $class);
$self->register_eval_rule ("check_from_domain_ip");
return $self;
}
sub get_from_domain {
my ($message) = @_;
my $from_address = lc($message->get('From:addr'));
$from_address =~ /$DOMAIN_PATTERN/;
my $from_domain = $1;
}
sub is_ip_blacklisted {
my ($ip_address) = @_;
return 1 if exists $IP_BLACKLIST{$ip_address};
}
sub check_from_domain_ip {
my ($self, $message) = @_;
my $resolver = Net::DNS::Resolver->new;
my $message_from_domain = get_from_domain($message);
my $result = $resolver->query($message_from_domain, $RECORD_TYPE_A);
if (!$result) {
return $ACTION_UNRESOLVABLE_HOSTNAME;
}
foreach my $record ($result->answer) {
if (is_ip_blacklisted($record->address)) {
return $MAIL_SPAM;
}
}
return $MAIL_OK;
}
1; # required by perl