qpsmtpd/plugins/content_log

79 lines
1.8 KiB
Perl

#!perl -w
=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
=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
=cut
use POSIX qw:strftime:;
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) = @_;
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);
$transaction->body_resetpos;
while (my $line = $transaction->body_getline) {
print $out $line;
}
close $out;
return DECLINED, "successfully saved message.. continuing";
}
sub content_log_file {
my ( $self ) = @_;
# as a decent default, log on a per-day-basis
my $path = $self->qp->config('content_log_file');
$path ||= 'mail/%Y%m%d';
return strftime($path, localtime(time));
}
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;
}