From 50badecc48c7241ca44bdc3b35c1b03cf5c08f6a Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 2 Dec 2014 16:28:51 -0600 Subject: [PATCH 1/9] Split out content log file name logic --- plugins/content_log | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plugins/content_log b/plugins/content_log index b4f04c4..48f1567 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -8,9 +8,7 @@ use POSIX qw:strftime:; sub hook_data_post { 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") + open my $out, '>>', $self->content_log_file; or return DECLINED, "Could not open log file.. continuing anyway"; $transaction->header->print($out); @@ -23,3 +21,9 @@ 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"; +} From 59ae9643e0beb408b779f615c9637257dd396ccb Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 2 Dec 2014 16:51:51 -0600 Subject: [PATCH 2/9] Add config to enable content log, on by default --- plugins/content_log | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/content_log b/plugins/content_log index 48f1567..2c9ceca 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -5,7 +5,15 @@ use POSIX qw:strftime:; -sub hook_data_post { +sub register { + my ( $self, $qp ) = @_; + my $enabled = $qp->config('enable_content_log'); + $enabled = 'true' if ! defined $enabled; + return if lc($enabled) ne 'true'; + $self->register_hook('data_post', 'data_post_handler'); +} + +sub data_post_handler { my ($self, $transaction) = @_; open my $out, '>>', $self->content_log_file; From deb0fbba695c546de38c842e3c118bf1c318ef75 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 2 Dec 2014 16:54:03 -0600 Subject: [PATCH 3/9] Fix broken semicolon --- plugins/content_log | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/content_log b/plugins/content_log index 2c9ceca..992c1f5 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -16,7 +16,7 @@ sub register { sub data_post_handler { my ($self, $transaction) = @_; - open my $out, '>>', $self->content_log_file; + open my $out, '>>', $self->content_log_file or return DECLINED, "Could not open log file.. continuing anyway"; $transaction->header->print($out); From ae887c156b1c1316b0a31bd4c1179885cad5beb7 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 2 Dec 2014 17:02:45 -0600 Subject: [PATCH 4/9] Add some basic testing for content_log --- t/config/enable_content_log | 1 + t/config/plugins | 1 + t/plugin_tests/content_log | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 t/config/enable_content_log create mode 100644 t/plugin_tests/content_log diff --git a/t/config/enable_content_log b/t/config/enable_content_log new file mode 100644 index 0000000..c508d53 --- /dev/null +++ b/t/config/enable_content_log @@ -0,0 +1 @@ +false diff --git a/t/config/plugins b/t/config/plugins index 48903ea..d2f9b25 100644 --- a/t/config/plugins +++ b/t/config/plugins @@ -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 diff --git a/t/plugin_tests/content_log b/t/plugin_tests/content_log new file mode 100644 index 0000000..7355815 --- /dev/null +++ b/t/plugin_tests/content_log @@ -0,0 +1,16 @@ +#!perl -w + +use strict; +use warnings; + +sub register_tests { + my ( $self ) = @_; + + $self->register_test('test_content_log_file'); +} + +sub test_content_log_file { + my ( $self ) = @_; + ok( $self->content_log_file, 'content_log_file() returns something' ); +} + From edc8f7600dbfdb8125ed06a7db957d88609ac924 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 2 Dec 2014 17:08:07 -0600 Subject: [PATCH 5/9] Test content_log enable/disable logic --- plugins/content_log | 14 ++++++++++---- t/plugin_tests/content_log | 7 ++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/plugins/content_log b/plugins/content_log index 992c1f5..6b884ea 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -6,10 +6,8 @@ use POSIX qw:strftime:; sub register { - my ( $self, $qp ) = @_; - my $enabled = $qp->config('enable_content_log'); - $enabled = 'true' if ! defined $enabled; - return if lc($enabled) ne 'true'; + my ( $self ) = @_; + return if ! $self->content_log_enabled; $self->register_hook('data_post', 'data_post_handler'); } @@ -35,3 +33,11 @@ sub content_log_file { my $date = strftime("%Y%m%d", localtime(time)); return "mail/$date"; } + +sub content_log_enabled { + my ( $self ) = @_; + my $enabled = $self->qp->config('enable_content_log'); + $enabled = 'true' if ! defined $enabled; + return 1 if lc($enabled) eq 'true'; + return 0; +} diff --git a/t/plugin_tests/content_log b/t/plugin_tests/content_log index 7355815..a4ea0d9 100644 --- a/t/plugin_tests/content_log +++ b/t/plugin_tests/content_log @@ -5,8 +5,8 @@ use warnings; sub register_tests { my ( $self ) = @_; - $self->register_test('test_content_log_file'); + $self->register_test('test_content_log_enabled'); } sub test_content_log_file { @@ -14,3 +14,8 @@ sub test_content_log_file { 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' ); +} + From e95f1e294e4f0c6b3b14681f16107d48e696aa2e Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 2 Dec 2014 17:13:00 -0600 Subject: [PATCH 6/9] Add method to exclude messages from content log --- plugins/content_log | 8 ++++++++ t/plugin_tests/content_log | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/plugins/content_log b/plugins/content_log index 6b884ea..c6c43ae 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -14,6 +14,9 @@ sub register { sub data_post_handler { my ($self, $transaction) = @_; + 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"; @@ -41,3 +44,8 @@ sub content_log_enabled { return 1 if lc($enabled) eq 'true'; return 0; } + +sub exclude { + # override this method to exclude some messages from content logging + return 0; +} diff --git a/t/plugin_tests/content_log b/t/plugin_tests/content_log index a4ea0d9..cac14fa 100644 --- a/t/plugin_tests/content_log +++ b/t/plugin_tests/content_log @@ -7,6 +7,7 @@ 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 { @@ -19,3 +20,8 @@ sub test_content_log_enabled { ok( ! $self->content_log_enabled, 'content_log_enabled() returns false' ); } +sub test_exclude { + my ( $self ) = @_; + ok( ! $self->exclude, 'exclude() default method returns false' ); +} + From c77a3344d73b94dc1cbc78b435cfebe5aadc36c7 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 3 Dec 2014 08:57:05 -0600 Subject: [PATCH 7/9] Rename enable_content_log to content_log_enabled --- plugins/content_log | 2 +- t/config/{enable_content_log => content_log_enabled} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename t/config/{enable_content_log => content_log_enabled} (100%) diff --git a/plugins/content_log b/plugins/content_log index c6c43ae..419d77a 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -39,7 +39,7 @@ sub content_log_file { sub content_log_enabled { my ( $self ) = @_; - my $enabled = $self->qp->config('enable_content_log'); + my $enabled = $self->qp->config('content_log_enabled'); $enabled = 'true' if ! defined $enabled; return 1 if lc($enabled) eq 'true'; return 0; diff --git a/t/config/enable_content_log b/t/config/content_log_enabled similarity index 100% rename from t/config/enable_content_log rename to t/config/content_log_enabled From 64c7519d7cdf45e0b99387b904716a93d30f1bdf Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 3 Dec 2014 09:57:27 -0600 Subject: [PATCH 8/9] content_log_enabled: Accept multiple 'true' values --- plugins/content_log | 2 +- t/plugin_tests/content_log | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/plugins/content_log b/plugins/content_log index 419d77a..1f8b699 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -41,7 +41,7 @@ sub content_log_enabled { my ( $self ) = @_; my $enabled = $self->qp->config('content_log_enabled'); $enabled = 'true' if ! defined $enabled; - return 1 if lc($enabled) eq 'true'; + return 1 if $enabled =~ /^(?:true|1|yes)\s*$/i; return 0; } diff --git a/t/plugin_tests/content_log b/t/plugin_tests/content_log index cac14fa..61a5e2a 100644 --- a/t/plugin_tests/content_log +++ b/t/plugin_tests/content_log @@ -18,6 +18,75 @@ sub test_content_log_file { 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 { From 6820a1f5fadbfe148a043c724386d2429ae667a3 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 3 Dec 2014 10:01:31 -0600 Subject: [PATCH 9/9] Add POD for content_log plugin --- plugins/content_log | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/plugins/content_log b/plugins/content_log index 1f8b699..9392f69 100644 --- a/plugins/content_log +++ b/plugins/content_log @@ -1,7 +1,22 @@ #!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 + +Used to enable and disable content logging + +Default: true +=cut use POSIX qw:strftime:;