2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2012-04-08 02:11:16 +02:00
|
|
|
|
2014-12-03 17:01:31 +01:00
|
|
|
=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
|
2014-12-03 22:50:45 +01:00
|
|
|
|
|
|
|
=head2 content_log_file <pattern>
|
|
|
|
|
|
|
|
Path to the content log file.
|
|
|
|
|
|
|
|
This option is passed through strftime(); see
|
|
|
|
http://man7.org/linux/man-pages/man3/strftime.3.html
|
|
|
|
or the appropriate documentation for your operating system)
|
|
|
|
|
|
|
|
Default: mail/%Y%m%d
|
2014-12-03 17:01:31 +01:00
|
|
|
=cut
|
2002-11-01 03:08:38 +01:00
|
|
|
|
|
|
|
use POSIX qw:strftime:;
|
|
|
|
|
2014-12-02 23:51:51 +01:00
|
|
|
sub register {
|
2014-12-03 00:08:07 +01:00
|
|
|
my ( $self ) = @_;
|
|
|
|
return if ! $self->content_log_enabled;
|
2014-12-02 23:51:51 +01:00
|
|
|
$self->register_hook('data_post', 'data_post_handler');
|
|
|
|
}
|
|
|
|
|
|
|
|
sub data_post_handler {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $transaction) = @_;
|
2002-11-01 03:08:38 +01:00
|
|
|
|
2014-12-03 00:13:00 +01:00
|
|
|
return DECLINED, 'excluded from content logging'
|
|
|
|
if $self->exclude();
|
|
|
|
|
2014-12-02 23:54:03 +01:00
|
|
|
open my $out, '>>', $self->content_log_file
|
2014-09-18 03:28:51 +02:00
|
|
|
or return DECLINED, "Could not open log file.. continuing anyway";
|
2002-11-01 03:08:38 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$transaction->header->print($out);
|
|
|
|
$transaction->body_resetpos;
|
|
|
|
while (my $line = $transaction->body_getline) {
|
|
|
|
print $out $line;
|
|
|
|
}
|
2002-11-01 03:08:38 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
close $out;
|
2002-11-01 03:08:38 +01:00
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
return DECLINED, "successfully saved message.. continuing";
|
2002-11-01 03:08:38 +01:00
|
|
|
}
|
2014-12-02 23:28:51 +01:00
|
|
|
|
|
|
|
sub content_log_file {
|
2014-12-03 22:50:45 +01:00
|
|
|
my ( $self ) = @_;
|
2014-12-02 23:28:51 +01:00
|
|
|
# as a decent default, log on a per-day-basis
|
2014-12-03 22:50:45 +01:00
|
|
|
my $path = $self->qp->config('content_log_file');
|
|
|
|
$path ||= 'mail/%Y%m%d';
|
|
|
|
return strftime($path, localtime(time));
|
2014-12-02 23:28:51 +01:00
|
|
|
}
|
2014-12-03 00:08:07 +01:00
|
|
|
|
|
|
|
sub content_log_enabled {
|
|
|
|
my ( $self ) = @_;
|
2014-12-03 15:57:05 +01:00
|
|
|
my $enabled = $self->qp->config('content_log_enabled');
|
2014-12-03 00:08:07 +01:00
|
|
|
$enabled = 'true' if ! defined $enabled;
|
2014-12-03 16:57:27 +01:00
|
|
|
return 1 if $enabled =~ /^(?:true|1|yes)\s*$/i;
|
2014-12-03 00:08:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-03 00:13:00 +01:00
|
|
|
|
|
|
|
sub exclude {
|
|
|
|
# override this method to exclude some messages from content logging
|
|
|
|
return 0;
|
|
|
|
}
|