« lib/Mail/Decency/ContentFilter/CRM114.pm
↑ Summary
lib/Mail/Decency/ContentFilter/SpamAssassin.pm »
| 1 | package Mail::Decency::ContentFilter::HoneyCollector; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends qw/ | ||
| 5 | Mail::Decency::ContentFilter::Archive | ||
| 6 | /; | ||
| 7 | |||
| 8 | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||
| 9 | |||
| 10 | use mro 'c3'; | ||
| 11 | use Data::Dumper; | ||
| 12 | use File::Path qw/ make_path /; | ||
| 13 | use File::Copy qw/ copy /; | ||
| 14 | use Mail::Decency::Core::Exception; | ||
| 15 | use Mail::Decency::ContentFilter::Core::Constants; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Mail::Decency::ContentFilter::HoneyCollector | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Counter part of the policy server's L<Mail::Decency::Policy::HoneyPot>. It receives honey-flagged mails and either trains them directly into activated spam filters or stores them in a quarantine folder for later manual training. | ||
| 24 | |||
| 25 | All mails marked with the honey flag will be dropped and NOT delivered ! | ||
| 26 | |||
| 27 | =head1 CONFIG | ||
| 28 | |||
| 29 | --- | ||
| 30 | |||
| 31 | disable: 0 | ||
| 32 | #max_size: 0 | ||
| 33 | #timeout: 30 | ||
| 34 | |||
| 35 | # train into activated spam filters | ||
| 36 | #train: 0 | ||
| 37 | |||
| 38 | # directory for collecting spams mails | ||
| 39 | archive_dir: /var/spool/decency/honey | ||
| 40 | |||
| 41 | |||
| 42 | =head1 CLASS ATTRIBUTES | ||
| 43 | |||
| 44 | |||
| 45 | =head2 archive_dir : Str | ||
| 46 | |||
| 47 | Output directory of honey mails. The format is the same as for the L<Mail::Decency::ContentFilter::Archive> module. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | has archive_dir => ( is => 'rw', isa => 'Str', predicate => 'do_collect' ); | ||
| 52 | |||
| 53 | =head2 train : Bool | ||
| 54 | |||
| 55 | Wheter train tagged mails into activated spam filters | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | has train => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 60 | |||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | |||
| 65 | =head2 init | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub init { | ||
| 70 | my ( $self ) = @_; | ||
| 71 | |||
| 72 | # init base, assure we get mime encoded | ||
| 73 | $self->next::method(); | ||
| 74 | |||
| 75 | # enable collection | ||
| 76 | if ( $self->config->{ archive_dir } ) { | ||
| 77 | make_path( $self->config->{ archive_dir }, { mode => 0700 } ) | ||
| 78 | unless -d $self->config->{ archive_dir }; | ||
| 79 | die "HoneyCollector: Could not create archive_dir '". $self->config->{ archive_dir }. "'\n" | ||
| 80 | unless -d $self->config->{ archive_dir }; | ||
| 81 | $self->archive_dir( $self->config->{ archive_dir } ); | ||
| 82 | } | ||
| 83 | |||
| 84 | # enable train | ||
| 85 | $self->train( 1 ) | ||
| 86 | if $self->config->{ train }; | ||
| 87 | |||
| 88 | # min required | ||
| 89 | die "HoneyCollector: Activate at least one of train or archive_dir\n" | ||
| 90 | unless $self->train || $self->do_collect; | ||
| 91 | |||
| 92 | $self->logger->debug0( "Train spam mails: ". ( $self->train ? "enabled" : "disabled" ) ); | ||
| 93 | $self->logger->debug0( "Collect spam mails: ". ( $self->do_collect ? "enabled" : "disabled" ) ); | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | =head2 handle | ||
| 99 | |||
| 100 | Archive file into archive folder | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | |||
| 105 | sub handle { | ||
| 106 | my ( $self ) = @_; | ||
| 107 | |||
| 108 | # only act on marked mails | ||
| 109 | return | ||
| 110 | if ! $self->session_data->has_flag( 'honey' ) | ||
| 111 | || $self->session_data->has_flag( 'honey_collected' ) | ||
| 112 | ; | ||
| 113 | |||
| 114 | # set honey collected | ||
| 115 | $self->session_data->set_flag( 'honey_collected' ); | ||
| 116 | |||
| 117 | # collect the honey | ||
| 118 | $self->_collect_honey(); | ||
| 119 | |||
| 120 | # drop mail in any case | ||
| 121 | Mail::Decency::Core::Exception::Drop->throw( { message => "Drop after collection" } ); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | =head2 hook_pre_finish | ||
| 126 | |||
| 127 | If no honey has been collected (cause some spam filters ran before) -> get it now | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub hook_pre_finish { | ||
| 132 | my ( $self, $status ) = @_; | ||
| 133 | |||
| 134 | # has been flagged.. | ||
| 135 | return ( $status, CF_FINAL_OK ) | ||
| 136 | if ! $self->session_data->has_flag( 'honey' ) | ||
| 137 | || $self->session_data->has_flag( 'honey_collected' ) | ||
| 138 | ; | ||
| 139 | |||
| 140 | # collect the honey | ||
| 141 | $self->_collect_honey(); | ||
| 142 | |||
| 143 | # drop the mail | ||
| 144 | return ( 'drop', CF_FINAL_OK ); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | =head2 _collect_honey | ||
| 150 | |||
| 151 | Train to spam filters and / or write to archive directory | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub _collect_honey { | ||
| 156 | my ( $self ) = @_; | ||
| 157 | |||
| 158 | # train mail | ||
| 159 | if ( $self->train ) { | ||
| 160 | $self->logger->debug2( "Train mail into spam" ); | ||
| 161 | my @spam_modules = | ||
| 162 | grep { | ||
| 163 | $_->isa( 'Mail::Decency::ContentFilter::Core::Spam' ) | ||
| 164 | && $_->isa( 'Mail::Decency::ContentFilter::Core::Cmd' ) | ||
| 165 | && $_->can_learn_spam | ||
| 166 | && ! $_->config->{ disable_train } | ||
| 167 | } | ||
| 168 | @{ $self->server->childs } | ||
| 169 | ; | ||
| 170 | foreach my $module( @spam_modules ) { | ||
| 171 | # train into filter | ||
| 172 | $module->cmd_filter( 'learn_spam' ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | # collect mail.. | ||
| 177 | if ( $self->do_collect ) { | ||
| 178 | $self->logger->debug2( "Do collect mail" ); | ||
| 179 | |||
| 180 | # disable dropping (we'll do ourself) | ||
| 181 | $self->drop( 0 ); | ||
| 182 | |||
| 183 | # perform archive (Mail::Decency::ContentFilter::Archive) | ||
| 184 | $self->archive_mail(); | ||
| 185 | } | ||
| 186 | |||
| 187 | return ; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | =head1 AUTHOR | ||
| 192 | |||
| 193 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 194 | |||
| 195 | =head1 COPYRIGHT | ||
| 196 | |||
| 197 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 198 | |||
| 199 | =head1 LICENCSE | ||
| 200 | |||
| 201 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | |||
| 206 | 1; |