| 1 | package Mail::Decency::ContentFilter::Cookbook; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Mail::Decency::ContentFilter::Cookbook - How to write a content filter module | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module contains a description on howto write a content filter module. | ||
| 15 | |||
| 16 | =head1 EXAMPLES | ||
| 17 | |||
| 18 | Hope this helps to understand what you can do. Have a look at the existing modules for more examples. Also look at L<Mail::Decency::ContentFilter::Core> for available methods. | ||
| 19 | |||
| 20 | =head2 SIMPLE EXAMPLE | ||
| 21 | |||
| 22 | |||
| 23 | package Mail::Decency::ContentFilter::MyModule; | ||
| 24 | |||
| 25 | use Moose; | ||
| 26 | extends 'Mail::Decency::ContentFilter::Core'; | ||
| 27 | |||
| 28 | has some_key => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 29 | |||
| 30 | # | ||
| 31 | # The init method is kind of a new or BUILD method, which should | ||
| 32 | # init all configurations from the YAML file | ||
| 33 | # | ||
| 34 | sub init { | ||
| 35 | my ( $self ) = @_; | ||
| 36 | |||
| 37 | # in YAML: | ||
| 38 | # --- | ||
| 39 | # some_key: 1 | ||
| 40 | $self->some_key( 1 ) | ||
| 41 | if $self->config->{ some_key }; | ||
| 42 | } | ||
| 43 | |||
| 44 | # | ||
| 45 | # The handle method will be called by the ContentFilter server each time a new | ||
| 46 | # mail is filtered | ||
| 47 | # | ||
| 48 | |||
| 49 | sub handle { | ||
| 50 | my ( $self ) = @_; | ||
| 51 | |||
| 52 | # get the temporary queue file | ||
| 53 | my $file = $self->file; | ||
| 54 | |||
| 55 | # read the size | ||
| 56 | my $size = $self->file_size; | ||
| 57 | |||
| 58 | # manipulate the MIME::Entity object of the current | ||
| 59 | $self->mime->head->add( 'X-MyModule' => 'passed' ); | ||
| 60 | $self->write_mime; | ||
| 61 | |||
| 62 | # get sender and recipient | ||
| 63 | my $sender = $self->from; | ||
| 64 | my $recipient = $self->to; | ||
| 65 | |||
| 66 | # access the datbaase | ||
| 67 | my $data_ref = $self->database->get( schema => table => $search_ref ); | ||
| 68 | $data_ref->{ some_attrib } = time(); | ||
| 69 | $self->database->get( schema => table => $search_ref, $data_ref ); | ||
| 70 | |||
| 71 | # access the cache | ||
| 72 | my $cached_ref = $self->cache->get( "cache-name" ) || { something => 1 }; | ||
| 73 | $cached_ref->{ something } ++; | ||
| 74 | $self->cache->set( "cache-name" => $cached_ref ); | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | =head2 SPAM FILTER EXAMPLE | ||
| 79 | |||
| 80 | package Mail::Decency::ContentFilter::MySpamFilter; | ||
| 81 | |||
| 82 | use Moose; | ||
| 83 | extends qw/ | ||
| 84 | Mail::Decency::ContentFilter::Core::Spam | ||
| 85 | /; | ||
| 86 | |||
| 87 | |||
| 88 | sub handle { | ||
| 89 | my ( $self ) = @_; | ||
| 90 | |||
| 91 | # throws exception if spam is recognized | ||
| 92 | $self->add_spam_score( -100, "You shall not send me mail" ) | ||
| 93 | if $self->from eq 'evil@sender.tld'; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | =head2 VIRUS FILTER EXAMPLE | ||
| 98 | |||
| 99 | package Mail::Decency::ContentFilter::MyVirusFilter; | ||
| 100 | |||
| 101 | use Moose; | ||
| 102 | extends qw/ | ||
| 103 | Mail::Decency::ContentFilter::Core::Virus | ||
| 104 | /; | ||
| 105 | |||
| 106 | sub handle { | ||
| 107 | my ( $self ) = @_; | ||
| 108 | |||
| 109 | # throws exception | ||
| 110 | if ( time() % 86400 == 0 ) { | ||
| 111 | $self->found_virus( "Your daily virus" ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | =head2 HOOKS | ||
| 116 | |||
| 117 | There are two kinds of hooks which can be implemented by any modules. They exist, because not necessary all modules will be run in every session (eg if the first recognizes the mail as spam and throws an exception). | ||
| 118 | |||
| 119 | =head3 PRE FINISH HOOK | ||
| 120 | |||
| 121 | Called after the modules are processed. Has to return the status ("virus", "spam", "drop" or "ok") and the final code (CF_FINAL_* from L<Mail::Decency::ContentFilter::Core::Constants>). | ||
| 122 | |||
| 123 | package Mail::Decency::ContentFilter::MyPreHook; | ||
| 124 | |||
| 125 | use Moose; | ||
| 126 | extends 'Mail::Decency::ContentFilter::Core'; | ||
| 127 | use Mail::Decency::ContentFilter::Core::Constants; | ||
| 128 | |||
| 129 | # example from the HoneyCollector modules, which | ||
| 130 | # assures marked mails to be collected | ||
| 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 | =head2 POST FINISH HOOK | ||
| 148 | |||
| 149 | Called after the finish_(ok|spam|virus) methods. Takes the status as arguments and has to return the status and the final code. | ||
| 150 | |||
| 151 | package Mail::Decency::ContentFilter::MyPreHook; | ||
| 152 | |||
| 153 | use Moose; | ||
| 154 | extends 'Mail::Decency::ContentFilter::Core'; | ||
| 155 | use Mail::Decency::ContentFilter::Core::Constants; | ||
| 156 | |||
| 157 | sub hook_post_finish { | ||
| 158 | my ( $self, $status ) = @_; | ||
| 159 | |||
| 160 | # force to pass all recognized virus and spams.. | ||
| 161 | if ( $status eq 'virus' || $status eq 'spam' ) { | ||
| 162 | return ( ok => CF_FINAL_OK ); | ||
| 163 | } | ||
| 164 | |||
| 165 | # delete all mails recognized as OK | ||
| 166 | elsif ( $status eq 'ok' ) { | ||
| 167 | return ( drop => CF_FINAL_OK ); | ||
| 168 | } | ||
| 169 | |||
| 170 | # bounce mails supposed to be dropped | ||
| 171 | elsif ( $status eq 'drop' ) { | ||
| 172 | return ( ok => CF_FINAL_ERROR ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | =head1 INCLUDE MODULE | ||
| 177 | |||
| 178 | To include the module, simple add it in your contnet filter | ||
| 179 | |||
| 180 | =head2 YAML | ||
| 181 | |||
| 182 | In content-filter.yml ... | ||
| 183 | |||
| 184 | --- | ||
| 185 | |||
| 186 | # .. | ||
| 187 | |||
| 188 | filters: | ||
| 189 | - MyModule: | ||
| 190 | some_key: 1 | ||
| 191 | - MyModule: /path/to/my-module.yml | ||
| 192 | |||
| 193 | |||
| 194 | =head2 PERL | ||
| 195 | |||
| 196 | my $content_filter = Mail::Decency::ContentFilter->new( | ||
| 197 | # .. | ||
| 198 | filters => [ | ||
| 199 | { MyModule => { some_key => 1 } } | ||
| 200 | ] | ||
| 201 | ); | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 206 | |||
| 207 | =head1 COPYRIGHT | ||
| 208 | |||
| 209 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 210 | |||
| 211 | =head1 LICENCSE | ||
| 212 | |||
| 213 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | |||
| 218 | 1; |