« lib/Mail/Decency/Helper/Database/MongoDB.pm
↑ Summary
lib/Mail/Decency/ContentFilter/Core/WeightTranslate.pm »
| 1 | package Mail::Decency::ContentFilter::ClamAV; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends qw/ | ||
| 5 | Mail::Decency::ContentFilter::Core | ||
| 6 | /; | ||
| 7 | with qw/ | ||
| 8 | Mail::Decency::ContentFilter::Core::Virus | ||
| 9 | /; | ||
| 10 | |||
| 11 | use version 0.74; our $VERSION = qv( "v0.1.6" ); | ||
| 12 | |||
| 13 | use mro 'c3'; | ||
| 14 | use Data::Dumper; | ||
| 15 | use ClamAV::Client; | ||
| 16 | use Scalar::Util qw/ blessed /; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Mail::Decency::ContentFilter::ClamAV | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Checks mails against clamav | ||
| 25 | |||
| 26 | =head1 CONFIG | ||
| 27 | |||
| 28 | --- | ||
| 29 | |||
| 30 | disable: 0 | ||
| 31 | #max_size: 0 | ||
| 32 | #timeout: 30 | ||
| 33 | |||
| 34 | # hostname / ip of clamav server | ||
| 35 | #host: '127.0.0.1' | ||
| 36 | #port: 12345 | ||
| 37 | |||
| 38 | # or use unix socket (path) | ||
| 39 | path: /var/run/clamav/clamd.ctl | ||
| 40 | |||
| 41 | |||
| 42 | =head1 CLASS ATTRIBUTES | ||
| 43 | |||
| 44 | =head2 clamav : ClamAV::Client | ||
| 45 | |||
| 46 | Instance of L<ClamAV::Client> | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | has clamav => ( is => 'rw', isa => 'ClamAV::Client' ); | ||
| 51 | |||
| 52 | =head1 METHODS | ||
| 53 | |||
| 54 | =head2 init | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub init { | ||
| 59 | my ( $self ) = @_; | ||
| 60 | |||
| 61 | $self->next::method(); | ||
| 62 | |||
| 63 | if ( $self->config->{ host } ) { | ||
| 64 | die ref( $self ).": Require port in config if using host\n" | ||
| 65 | unless $self->config->{ port }; | ||
| 66 | $self->clamav( ClamAV::Client->new( | ||
| 67 | socket_host => $self->config->{ host }, | ||
| 68 | socket_port => $self->config->{ port }, | ||
| 69 | ) ); | ||
| 70 | } | ||
| 71 | elsif ( $self->config->{ path } ) { | ||
| 72 | $self->clamav( ClamAV::Client->new( | ||
| 73 | socket_name => $self->config->{ path }, | ||
| 74 | ) ); | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | die ref( $self ). ": Require either host and port OR path\n"; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | =head2 handle | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub handle { | ||
| 87 | my ( $self ) = @_; | ||
| 88 | |||
| 89 | my $result; | ||
| 90 | eval { | ||
| 91 | open my $fh, '<', $self->file; | ||
| 92 | $result = $self->clamav->scan_stream( $fh ); | ||
| 93 | close $fh; | ||
| 94 | }; | ||
| 95 | |||
| 96 | if ( $@ ) { | ||
| 97 | if ( blessed( $@ ) && $@->isa( 'ClamAV::Client::Error' ) ) { | ||
| 98 | $self->logger->error( "Error connecting to clamav: $@" ); | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $self->logger->error( "Error occured: $@" ); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( $result ) { | ||
| 106 | $self->logger->info( sprintf( | ||
| 107 | 'Found Virus in mail from %s to %s: %s', | ||
| 108 | $self->from, $self->to, $result | ||
| 109 | ) ); | ||
| 110 | return $self->found_virus( $result ); | ||
| 111 | } | ||
| 112 | |||
| 113 | # return ok | ||
| 114 | return ; | ||
| 115 | } | ||
| 116 | |||
| 117 | =head1 AUTHOR | ||
| 118 | |||
| 119 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 120 | |||
| 121 | =head1 COPYRIGHT | ||
| 122 | |||
| 123 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 124 | |||
| 125 | =head1 LICENCSE | ||
| 126 | |||
| 127 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | 1; |