qpsmtpd/plugins/ident/geoip
Matt Simerson 2dcd34467e geoip: eval loading of Geo::IP, tests, enabled in config
eval Geo::IP and log an error if missing
added 2 tests
enabled in config/plugins
2012-05-22 17:40:02 -04:00

60 lines
1.4 KiB
Perl

#!perl -w
=head1 SYNOPSIS
This plugin uses MaxMind's GeoIP service and the Geo::IP perl module to
do a lookup on incoming connections and record the country of origin.
Thats all it does.
It logs the 2 char country code to connection note I<geoip_country>.
It logs the country name to the connection note I<geoip_country_name>.
Other plugins can use that info to do things to the connection, like
reject or greylist.
=cut
use strict;
use warnings;
use Qpsmtpd::Constants;
#use Geo::IP; # eval'ed in register()
sub register {
my $self = shift;
eval 'use Geo::IP';
if ( $@ ) {
warn "could not load Geo::IP";
$self->log( LOGERROR, "could not load Geo::IP" );
return;
};
$self->register_hook( 'connect', 'connect_handler' );
};
sub connect_handler {
my $self = shift;
my $geoip = Geo::IP->new();
my $remote_ip = $self->qp->connection->remote_ip;
my $c_code = $geoip->country_code_by_addr( $remote_ip ) or do {
$self->log( LOGINFO, "fail: no results" );
return DECLINED;
};
my $c_name = $geoip->country_name_by_addr( $remote_ip );
if ( $c_name ) {
$self->qp->connection->notes('geoip_country_name', $c_name);
};
$self->qp->connection->notes('geoip_country', $c_code);
my $message = $c_code;
$message .= ", $c_name" if $c_name;
$self->log(LOGINFO, $message);
return DECLINED;
}