Merge pull request #161 from jaredj/configurable-content-log

content_log configurability and testing
This commit is contained in:
Matt Simerson 2014-12-03 09:39:34 -08:00
commit 5375c9818d
4 changed files with 145 additions and 6 deletions

View File

@ -1,16 +1,38 @@
#!perl -w
# A simple example of a plugin that logs all incoming mail to a file.
# Useful for debugging other plugins or keeping an archive of things.
=head1 NAME
content_log - record incoming mail to a file
=head1 DESCRIPTION
A simple example of a plugin that logs all incoming mail to a file.
Useful for debugging other plugins or keeping an archive of things.
=head1 CONFIG
=head2 content_log_enabled <true|false>
Used to enable and disable content logging
Default: true
=cut
use POSIX qw:strftime:;
sub hook_data_post {
sub register {
my ( $self ) = @_;
return if ! $self->content_log_enabled;
$self->register_hook('data_post', 'data_post_handler');
}
sub data_post_handler {
my ($self, $transaction) = @_;
# as a decent default, log on a per-day-basis
my $date = strftime("%Y%m%d", localtime(time));
open(my $out, ">>mail/$date")
return DECLINED, 'excluded from content logging'
if $self->exclude();
open my $out, '>>', $self->content_log_file
or return DECLINED, "Could not open log file.. continuing anyway";
$transaction->header->print($out);
@ -23,3 +45,22 @@ sub hook_data_post {
return DECLINED, "successfully saved message.. continuing";
}
sub content_log_file {
# as a decent default, log on a per-day-basis
my $date = strftime("%Y%m%d", localtime(time));
return "mail/$date";
}
sub content_log_enabled {
my ( $self ) = @_;
my $enabled = $self->qp->config('content_log_enabled');
$enabled = 'true' if ! defined $enabled;
return 1 if $enabled =~ /^(?:true|1|yes)\s*$/i;
return 0;
}
sub exclude {
# override this method to exclude some messages from content logging
return 0;
}

View File

@ -0,0 +1 @@
false

View File

@ -13,6 +13,7 @@
# my $MAXCONNIP = 5; # max simultaneous connections from one IP
# settings... without this it will NOT refuse more than $MAXCONNIP connections
# from one IP!
content_log
hosts_allow
# information plugins

View File

@ -0,0 +1,96 @@
#!perl -w
use strict;
use warnings;
sub register_tests {
my ( $self ) = @_;
$self->register_test('test_content_log_file');
$self->register_test('test_content_log_enabled');
$self->register_test('test_exclude');
}
sub test_content_log_file {
my ( $self ) = @_;
ok( $self->content_log_file, 'content_log_file() returns something' );
}
sub test_content_log_enabled {
my ( $self ) = @_;
ok( ! $self->content_log_enabled, 'content_log_enabled() returns false' );
my @test_data = (
{
config => 'true',
expected => 1,
},
{
config => undef,
expected => 1,
},
{
config => '1',
expected => 1,
},
{
config => 'yes',
expected => 1,
},
{
config => 'yes ',
expected => 1,
},
{
config => '0',
expected => 0,
},
{
config => 'false',
expected => 0,
},
{
config => 'no',
expected => 0,
},
{
config => 'asdf ',
expected => 0,
},
);
$self->save_hook;
for ( @test_data ) {
my $descr = "content_log_enabled="
. ( defined $_->{config } ? "'$_->{config }'" : 'undef' )
. ( $_->{expected} ? ' enables' : ' disables' )
. ' content logging';
$self->fake_config( $_->{config } );
is( $self->content_log_enabled, $_->{expected}, $descr );
}
$self->restore_hook;
}
our $oldhook;
sub save_hook {
my ( $self ) = @_;
$oldhook = $self->qp->hooks->{config};
}
sub restore_hook {
my ( $self ) = @_;
$self->qp->hooks->{config} = $oldhook;
}
sub fake_config {
my ( $self, $value ) = @_;
$self->qp->hooks->{config} = [
{
name => 'test hook',
code => sub { return OK, $value },
},
];
}
sub test_exclude {
my ( $self ) = @_;
ok( ! $self->exclude, 'exclude() default method returns false' );
}