| 1 | package Mail::Decency::ContentFilter::MimeAttribs; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | extends 'Mail::Decency::ContentFilter::Core'; | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use mro 'c3'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Mail::Decency::ContentFilter::MimeAttribs | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | Simple mime manipulation. Be careful, can break DKIM or alike. | ||||||||||
| 18 | |||||||||||
| 19 | =head1 CONFIG | ||||||||||
| 20 | |||||||||||
| 21 | --- | ||||||||||
| 22 | |||||||||||
| 23 | disable: 0 | ||||||||||
| 24 | #max_size: 0 | ||||||||||
| 25 | #timeout: 30 | ||||||||||
| 26 | |||||||||||
| 27 | add_header: | ||||||||||
| 28 | 'X-SomeHeader': "Some Value" | ||||||||||
| 29 | 'X-OtherHeader': "Other Value" | ||||||||||
| 30 | |||||||||||
| 31 | set_header: | ||||||||||
| 32 | 'X-Mailer': "My Company v0.1" | ||||||||||
| 33 | |||||||||||
| 34 | replace_header: | ||||||||||
| 35 | 'X-DSPAM': "replaced" | ||||||||||
| 36 | |||||||||||
| 37 | remove_header: | ||||||||||
| 38 | - 'Subject' | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | =head1 CLASS ATTRIBUTES | ||||||||||
| 43 | |||||||||||
| 44 | =head2 add_header : HashRef | ||||||||||
| 45 | |||||||||||
| 46 | Add headers to mail .. if header already existing, it will be added also | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | 1 | has add_header => ( is => 'rw', isa => 'HashRef', predicate => 'has_add_header' ); | |||||||||
| 51 | |||||||||||
| 52 | =head2 set_header : HashRef | ||||||||||
| 53 | |||||||||||
| 54 | Set's a header. If exists, it will be overwritten. | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 1 | has set_header => ( is => 'rw', isa => 'HashRef', predicate => 'has_set_header' ); | |||||||||
| 59 | |||||||||||
| 60 | =head2 replace_header : HashRef | ||||||||||
| 61 | |||||||||||
| 62 | Replaces a header. If not existing, nothing will be written | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 1 | has replace_header => ( is => 'rw', isa => 'HashRef', predicate => 'has_replace_header' ); | |||||||||
| 67 | |||||||||||
| 68 | =head2 remove_header : ArrayRef[Str] | ||||||||||
| 69 | |||||||||||
| 70 | Removes a header, if it exists. | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 1 | has remove_header => ( is => 'rw', isa => 'ArrayRef[Str]', predicate => 'has_remove_header' ); | |||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | =head1 METHODS | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | =head2 init | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 85 | 0 | my ( $self ) = @_; | |||||||||
| 86 | |||||||||||
| 87 | # init base, assure we get mime encoded | ||||||||||
| 88 | 0 | $self->next::method(); | |||||||||
| 89 | |||||||||||
| 90 | 0 | foreach my $meth( qw/ | |||||||||
| 91 | add_header | ||||||||||
| 92 | set_header | ||||||||||
| 93 | replace_header | ||||||||||
| 94 | remove_header | ||||||||||
| 95 | / ) { | ||||||||||
| 96 | $self->$meth( $self->config->{ $meth } ) | ||||||||||
| 97 | 0 | ▶ | if $self->config->{ $meth }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | =head2 handle | ||||||||||
| 103 | |||||||||||
| 104 | Default handling for any content filter is getting info about the to be filterd file | ||||||||||
| 105 | |||||||||||
| 106 | =cut | ||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | sub handle { | ||||||||
|
Subroutine
handle: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||
| 110 | 0 | my ( $self ) = @_; | |||||||||
| 111 | |||||||||||
| 112 | # get mime object | ||||||||||
| 113 | 0 | my $mime = $self->mime; | |||||||||
| 114 | |||||||||||
| 115 | # get header object | ||||||||||
| 116 | 0 | my $header = $mime->head; | |||||||||
| 117 | |||||||||||
| 118 | # add new headers | ||||||||||
| 119 | 0 | ▶ | if ( $self->has_add_header ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | ▶ | while ( my ( $name, $value ) = each %{ $self->add_header } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | $header->add( $name => $value ); | |||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | # remove headers | ||||||||||
| 126 | 0 | ▶ | if ( $self->has_remove_header ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | foreach my $name( @{ $self->remove_header } ) { | |||||||||
| 128 | 0 | $header->delete( $name ); | |||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | # set headers (add or replace) | ||||||||||
| 133 | 0 | ▶ | if ( $self->has_set_header ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | while ( my ( $name, $value ) = each %{ $self->set_header } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | my $val = $header->get( $name ); | |||||||||
| 136 | 0 | ▶ | unless ( $val ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | $header->add( $name => $value ); | |||||||||
| 138 | } | ||||||||||
| 139 | else { | ||||||||||
| 140 | 0 | $value =~ s/%orig%/$val/g; | |||||||||
| 141 | 0 | $header->replace( $name => $value ); | |||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | # replace headers | ||||||||||
| 147 | 0 | ▶ | if ( $self->has_replace_header ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | ▶ | while ( my ( $name, $value ) = each %{ $self->replace_header } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 0 | my $val = $header->get( $name ); | |||||||||
| 150 | 0 | ▶ | next unless $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 0 | $value =~ s/%orig%/$val/g; | |||||||||
| 152 | 0 | $header->replace( $name => $value ); | |||||||||
| 153 | } | ||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | # update file for mime | ||||||||||
| 157 | 0 | $self->write_mime; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | |||||||||||
| 161 | =head1 AUTHOR | ||||||||||
| 162 | |||||||||||
| 163 | Ulrich Kautz <uk@fortrabbit.de> | ||||||||||
| 164 | |||||||||||
| 165 | =head1 COPYRIGHT | ||||||||||
| 166 | |||||||||||
| 167 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||||||||||
| 168 | |||||||||||
| 169 | =head1 LICENCSE | ||||||||||
| 170 | |||||||||||
| 171 | This library is free software and may be distributed under the same terms as perl itself. | ||||||||||
| 172 | |||||||||||
| 173 | =cut | ||||||||||
| 174 | |||||||||||
| 175 | |||||||||||
| 176 | 1 | 1; | |||||||||