| 1 | use 5.014; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Mail::Qmail::Filter::ClamAV; | ||
| 5 | |||
| 6 | our $VERSION = '1.0'; | ||
| 7 | |||
| 8 | use Mo qw(coerce default); | ||
| 9 | extends 'Mail::Qmail::Filter'; | ||
| 10 | |||
| 11 | has 'clamav_options' => sub { [] }; | ||
| 12 | has 'dump_malware_to'; | ||
| 13 | has 'error_text' => 'An error occured when scanning the message for viruses.'; | ||
| 14 | has 'reject_text' => sub { | ||
| 15 | sub { "Virus found: $_[0]" } | ||
| 16 | }; | ||
| 17 | |||
| 18 | sub filter { | ||
| 19 | my $self = shift; | ||
| 20 | my $message = $self->message; | ||
| 21 | my $body_ref = $message->body_ref; | ||
| 22 | |||
| 23 | require File::Scan::ClamAV; # lazy load because filter might be skipped | ||
| 24 | my $av = File::Scan::ClamAV->new( @{ $self->clamav_options } ); | ||
| 25 | my ( $response, $virus ) = $av->streamscan($$body_ref); | ||
| 26 | if ( my $errstr = $av->errstr ) { | ||
| 27 | $self->defer( $self->error_text, $errstr ); | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | $self->debug( result => $response ); | ||
| 31 | if ($virus) { | ||
| 32 | $self->debug( virus => $virus ); | ||
| 33 | if ( defined( my $dir = $self->dump_malware_to ) ) { | ||
| 34 | require Path::Tiny and Path::Tiny->import('path') | ||
| 35 | unless defined &path; | ||
| 36 | path( $dir, my $file = join '_', $^T, $$, $virus =~ y!/\n!_!r ) | ||
| 37 | ->spew($$body_ref); | ||
| 38 | $self->debug( 'dumped message to' => $file ); | ||
| 39 | } | ||
| 40 | $self->reject( $self->reject_text, $virus ); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |