Mail-Decency

 view release on metacpan or  search on metacpan

lib/Mail/Decency/LogParser/GeoSource.pm  view on Meta::CPAN

/;

use version 0.74; our $VERSION = qv( "v0.1.4" );

use Geo::IP;
use Data::Dumper;

=head1 NAME

Mail::Decency::LogParser::GeoSource


=head1 DESCRIPTION

Log statistics about geographical sender sources.

=head1 CLASS ATTRIBUTES

=head2 interval_formats : ArrayRef[Str]

Intervals in strftime format for L<DateTime>

=cut

has interval_formats => ( is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] } );

=head2 geo_ip : Geo::IP

Instance of Geo::IP

=cut

has geo_ip => ( is => 'ro', isa => 'Geo::IP', default => sub { Geo::IP->new( GEOIP_STANDARD ) } );

=head2 enable_per_sender : Bool

Wheter source stats should be enabled per sender domain.. CAN BECOME VERY HUGE!!

Default: 0

=cut

has enable_per_sender => ( is => 'rw', isa => 'Bool', default => 0 );

=head2 enable_per_recipient : Bool

Wheter source stats should be enabled per recipient domain..

Default: 0

=cut

has enable_per_recipient => ( is => 'rw', isa => 'Bool', default => 0 );

=head2 schema_definition : HashRef[HashRef]

Schema for database

=cut

has schema_definition   => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub {
    {
        geo => {
            source => {
                from_domain => [ varchar => 255 ],
                to_domain   => [ varchar => 255 ],
                country     => [ varchar => 2 ],
                type        => [ varchar => 25 ],
                interval    => [ varchar => 25 ],
                counter     => 'integer',
                last_update => 'integer',
                -unique     => [ 'from_domain', 'to_domain', 'country', 'type', 'interval' ]
            },
        }
    };
} );

=head1 METHODS

=head2 init

=cut

sub setup {
    my ( $self ) = @_;
    
    $self->config->{ interval_formats } ||= [ '%Y-%m-%d', '%Y-%m', '%Y' ];
    $self->interval_formats( $self->config->{ interval_formats } );
    
    $self->enable_per_recipient( 1 )
        if $self->config->{ enable_per_recipient };
    
    $self->enable_per_sender( 1 )
        if $self->config->{ enable_per_sender };
    
    
}


=head2 handle

Checks wheter incoming mail is whilist for final recipient

=cut

sub handle_data {
    my ( $self, $parsed_ref ) = @_;
    
    # no relevant
    return 
        if ! $parsed_ref->{ ip } || ! $parsed_ref->{ final } || ! ( $parsed_ref->{ reject } || $parsed_ref->{ bounced } || $parsed_ref->{ sent } );
    
    # determine country
    my $country = $self->geo_ip->country_code_by_addr( $parsed_ref->{ ip } ) || "";
    #return unless $country;
    
    # setup save pairs
    my @pairs = ( [ qw/ total total / ] );
    push @pairs, [ $parsed_ref->{ from_domain }, 'total' ]
        if $self->enable_per_sender && $parsed_ref->{ from_domain };
    push @pairs, [ 'total', $parsed_ref->{ to_domain } ]



( run in 0.657 second using v1.01-cache-2.11-cpan-39bf76dae61 )