Mail-MtPolicyd

 view release on metacpan or  search on metacpan

lib/Mail/MtPolicyd/Plugin/Honeypot.pm  view on Meta::CPAN

package Mail::MtPolicyd::Plugin::Honeypot;

use Moose;
use namespace::autoclean;

our $VERSION = '2.05'; # VERSION
# ABSTRACT: mtpolicyd plugin for creating an honeypot

extends 'Mail::MtPolicyd::Plugin';
with 'Mail::MtPolicyd::Plugin::Role::Scoring';
with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
	'uc_attributes' => [ 'enabled' ],
};
with 'Mail::MtPolicyd::Plugin::Role::PluginChain';

use Mail::MtPolicyd::Plugin::Result;


has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );

has 'score' => ( is => 'rw', isa => 'Maybe[Num]' );
has 'mode' => ( is => 'rw', isa => 'Str', default => 'reject');

has 'recipients' => ( is => 'rw', isa => 'Str', default => '' );
has 'recipients_re' => ( is => 'rw', isa => 'Str', default => '' );

has _recipients => ( is => 'ro', isa => 'ArrayRef', lazy => 1,
	default => sub {
		my $self = shift;
		return  [ split(/\s*,\s*/, $self->recipients) ];
	},
);

has _recipients_re => ( is => 'ro', isa => 'ArrayRef', lazy => 1,
	default => sub {
		my $self = shift;
		return  [ split(/\s*,\s*/, $self->recipients_re) ];
	},
);

has 'reject_message' => ( is => 'rw', isa => 'Str', default => 'trapped by honeypod' );

has 'expire' => ( is => 'rw', isa => 'Int', default => 60*60*2 );

sub run {
	my ( $self, $r ) = @_;
	my $ip = $r->attr('client_address');
	my $recipient = $r->attr('recipient');
	my $session = $r->session;

	my $enabled = $self->get_uc( $session, 'enabled' );
	if( $enabled eq 'off' ) {
		return;
	}

	if( $self->is_in_honeypot( $r, $ip ) ) {
		return $self->trapped_action;
	}
	if( $self->is_honeypot_recipient( $recipient ) ) {
		$self->add_to_honeypot( $r, $ip );
		return $self->trapped_action;
	}

	return;
}

sub trapped_action {
	my ( $self, $r ) = @_;

	if( $self->mode eq 'reject' ) {
		return( Mail::MtPolicyd::Plugin::Result->new(
			action => 'reject '.$self->reject_message,
			abort => 1,
		) );
	}
	if( defined $self->score && ! $r->is_already_done($self->name.'-score') ) {
		$self->add_score($r, $self->name => $self->score);
	}
	if( defined $self->chain ) {
		my $chain_result = $self->chain->run( $r );
		return( @{$chain_result->plugin_results} );
	}
	return;
}

sub is_honeypot_recipient {
	my ( $self, $recipient ) = @_;

	if( $self->is_in_recipients( $recipient )
			|| $self->is_in_recipients_re( $recipient ) ) {
		return(1);
	}

	return(0);



( run in 2.902 seconds using v1.01-cache-2.11-cpan-524268b4103 )