| 1 | package Mail::Decency::Policy::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::Policy::Cookbook - How to write a policy 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::Policy::Core> for available methods. | ||
| 19 | |||
| 20 | =head2 SIMPLE EXAMPLE | ||
| 21 | |||
| 22 | |||
| 23 | package Mail::Decency::Policy::MyModule; | ||
| 24 | |||
| 25 | use Moose; | ||
| 26 | extends 'Mail::Decency::Policy::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, $server, $attrs_ref ) = @_; | ||
| 51 | |||
| 52 | # $attrs_ref is: | ||
| 53 | # { | ||
| 54 | # client_address => '123.123.123.213', | ||
| 55 | # recipient_address => 'recipient@domain.tld', | ||
| 56 | # recipient_domain => 'domain.tld', | ||
| 57 | # sender_address => 'sender@domain.tld', | ||
| 58 | # sender_domain => 'domain.tld', | ||
| 59 | # ... | ||
| 60 | # } | ||
| 61 | # see http://www.postfix.org/SMTPD_POLICY_README.html | ||
| 62 | |||
| 63 | # add spam score (throws exception) | ||
| 64 | $self->add_spam_score( -300, | ||
| 65 | "Message for X-Decency-Detail header", | ||
| 66 | "Reject message for SMTP REJECT" | ||
| 67 | ) if $attrs_ref->{ client_address } eq '123.123.123.0'; | ||
| 68 | |||
| 69 | # go to a final state (throws exception) | ||
| 70 | $self->go_final_state( OK => "Mail is accepted" ) | ||
| 71 | if $attrs_ref->{ recipient_domain } eq 'something.tld'; | ||
| 72 | $self->go_final_state( REJECT => "No, i dont want this" ) | ||
| 73 | if $attrs_ref->{ recipient_domain } eq 'lalala.tld'; | ||
| 74 | $self->go_final_state( 454 => "Please, try later" ) | ||
| 75 | if $attrs_ref->{ recipient_domain } eq 'yadda.tld'; | ||
| 76 | |||
| 77 | # access the datbaase | ||
| 78 | my $data_ref = $self->database->get( schema => table => $search_ref ); | ||
| 79 | $data_ref->{ some_attrib } = time(); | ||
| 80 | $self->database->get( schema => table => $search_ref, $data_ref ); | ||
| 81 | |||
| 82 | # access the cache | ||
| 83 | my $cached_ref = $self->cache->get( "cache-name" ) || { something => 1 }; | ||
| 84 | $cached_ref->{ something } ++; | ||
| 85 | $self->cache->set( "cache-name" => $cached_ref ); | ||
| 86 | |||
| 87 | # set a flag for later evaluation (also in ContentFilter) | ||
| 88 | $self->set_flag( 'bla' ); | ||
| 89 | $self->logger->info( "What can i say?" ) if $self->has_flag( "blub" ); | ||
| 90 | $self->del_flag( 'nada' ) if time() % 9999 = 33; | ||
| 91 | |||
| 92 | # access session data | ||
| 93 | warn "> CURRENT SPAM SCORE ". $self->session_data->spam_score. "\n"; | ||
| 94 | } | ||
| 95 | |||
| 96 | =head1 INCLUDE MODULE | ||
| 97 | |||
| 98 | To include the module, simple add it in your contnet filter | ||
| 99 | |||
| 100 | =head2 YAML | ||
| 101 | |||
| 102 | In policy.yml ... | ||
| 103 | |||
| 104 | --- | ||
| 105 | |||
| 106 | # .. | ||
| 107 | |||
| 108 | policy: | ||
| 109 | - MyModule: | ||
| 110 | some_key: 1 | ||
| 111 | - MyModule: /path/to/my-module.yml | ||
| 112 | |||
| 113 | |||
| 114 | =head2 PERL | ||
| 115 | |||
| 116 | my $policy = Mail::Decency::Policy->new( | ||
| 117 | # .. | ||
| 118 | policy => [ | ||
| 119 | { MyModule => { some_key => 1 } } | ||
| 120 | ] | ||
| 121 | ); | ||
| 122 | |||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 127 | |||
| 128 | =head1 COPYRIGHT | ||
| 129 | |||
| 130 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 131 | |||
| 132 | =head1 LICENCSE | ||
| 133 | |||
| 134 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | |||
| 139 | 1; |