« lib/Mail/Decency/ContentFilter/Core/WeightTranslate.pm
↑ Summary
lib/Mail/Decency/Core/POEForking/Postfix.pm »
| 1 | package Mail::Decency::ContentFilter::Archive; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'Mail::Decency::ContentFilter::Core'; | ||
| 5 | |||
| 6 | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||
| 7 | |||
| 8 | use mro 'c3'; | ||
| 9 | use Data::Dumper; | ||
| 10 | use File::Path qw/ make_path /; | ||
| 11 | use File::Basename qw/ fileparse /; | ||
| 12 | use File::Temp qw/ tempfile /; | ||
| 13 | use File::Copy qw/ copy /; | ||
| 14 | use Mail::Decency::Core::Exception; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Mail::Decency::ContentFilter::Archive | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | Archive module. Write a copy of the passing mail to archive directory on disk. | ||
| 23 | |||
| 24 | =head1 CONFIG | ||
| 25 | |||
| 26 | --- | ||
| 27 | |||
| 28 | disable: 0 | ||
| 29 | |||
| 30 | # possible variables are: | ||
| 31 | # * recipient_domain .. eg recipient.tld | ||
| 32 | # * recipient_prefix .. eg username | ||
| 33 | # * recipient_address .. eg username@recipient.tld | ||
| 34 | # * sender_domain .. eg username@recipient.tld | ||
| 35 | # * sender_prefix .. eg username@domain.tld | ||
| 36 | # * sender_adress .. eg username@domain.tld | ||
| 37 | # * ymd .. eg 2010-05-24 | ||
| 38 | # * hm .. eg 21-26 (= 21:26h) | ||
| 39 | archive_dir: '/var/archive/%recipient_domain%/%recipient_prefix%/%ymd%/%hm%/' | ||
| 40 | #archive_dir: '/var/archive/%ymd%/%recipient_domain%/%recipient_prefix%' | ||
| 41 | |||
| 42 | # wheter to drop the mail after archiving .. means: will not be | ||
| 43 | # reinjected for delivery. | ||
| 44 | drop: 0 | ||
| 45 | |||
| 46 | =head1 CLASS ATTRIBUTES | ||
| 47 | |||
| 48 | |||
| 49 | =head2 archive_dir : Str | ||
| 50 | |||
| 51 | Archive directory where the mails are stored in. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | has archive_dir => ( is => 'rw', isa => 'Str' ); | ||
| 56 | |||
| 57 | =head2 drop : Bool | ||
| 58 | |||
| 59 | If true, drop mails after archiving (do not forward them) | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | has drop => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 64 | |||
| 65 | |||
| 66 | =head1 METHODS | ||
| 67 | |||
| 68 | |||
| 69 | =head2 init | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub init { | ||
| 74 | my ( $self ) = @_; | ||
| 75 | |||
| 76 | # init base, assure we get mime encoded | ||
| 77 | $self->next::method(); | ||
| 78 | |||
| 79 | foreach my $meth( qw/ | ||
| 80 | drop | ||
| 81 | archive_dir | ||
| 82 | / ) { | ||
| 83 | $self->$meth( $self->config->{ $meth } ) | ||
| 84 | if $self->config->{ $meth }; | ||
| 85 | } | ||
| 86 | |||
| 87 | die "Require 'archive_dir' (full path for saving mails)\n" | ||
| 88 | unless $self->archive_dir; | ||
| 89 | |||
| 90 | $self->logger->info( "Mail archive dir: ". $self->archive_dir ); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | =head2 handle | ||
| 95 | |||
| 96 | Archive file into archive folder | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | |||
| 101 | sub handle { | ||
| 102 | my ( $self ) = @_; | ||
| 103 | |||
| 104 | # perform archive | ||
| 105 | $self->archive_mail(); | ||
| 106 | |||
| 107 | # die here with drop exception, if don't want to keep | ||
| 108 | Mail::Decency::Core::Exception::Drop->throw( { message => "Drop after archive" } ) | ||
| 109 | if $self->drop; | ||
| 110 | |||
| 111 | return ; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | =head2 archive_mail | ||
| 116 | |||
| 117 | Write mail to archive directory. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub archive_mail { | ||
| 122 | my ( $self ) = @_; | ||
| 123 | |||
| 124 | # get directory, split into file and dir path | ||
| 125 | my ( $file, $dir ) = fileparse( $self->build_dir ); | ||
| 126 | $file ||= "mail"; | ||
| 127 | |||
| 128 | # try make directory, die on error | ||
| 129 | make_path( $dir, { mode => 0700 } ) unless -d $dir; | ||
| 130 | die "Could not create archive directory '$dir'" unless -d $dir; | ||
| 131 | |||
| 132 | # make a temp file within (assure it is unique) | ||
| 133 | my ( $th, $full_path ) | ||
| 134 | = tempfile( "$dir$file-". time(). "-XXXXXX", UNLINK => 0, SUFFIX => ".eml" ); | ||
| 135 | close $th; | ||
| 136 | |||
| 137 | # copy actual file to archive folder | ||
| 138 | copy( $self->file, $full_path ); | ||
| 139 | $self->logger->debug0( "Stored mail in '$full_path' ". ( -f $full_path ? "OK" : "ERROR" ) ); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | =head2 build_dir | ||
| 144 | |||
| 145 | Builds dir based on variables. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub build_dir { | ||
| 150 | my ( $self ) = @_; | ||
| 151 | |||
| 152 | my $dir = $self->archive_dir; | ||
| 153 | |||
| 154 | # parse recipient_* | ||
| 155 | if ( $dir =~ /\%recipient/ ) { | ||
| 156 | my $recipient_address = $self->normalize_str( $self->to || "unknown\@unknown" ); | ||
| 157 | my ( $recipient_prefix, $recipient_domain ) = split( /@/, $recipient_address, 2 ); | ||
| 158 | $recipient_prefix ||= "unknown"; | ||
| 159 | $recipient_domain ||= "unknown"; | ||
| 160 | $dir =~ s/\%recipient_address\%/$recipient_address/g; | ||
| 161 | $dir =~ s/\%recipient_prefix\%/$recipient_prefix/g; | ||
| 162 | $dir =~ s/\%recipient_domain\%/$recipient_domain/g; | ||
| 163 | } | ||
| 164 | |||
| 165 | # parse sender_* | ||
| 166 | if ( $dir =~ /\%sender/ ) { | ||
| 167 | my $sender_address = $self->normalize_str( $self->from || "unknown\@unknown" ); | ||
| 168 | my ( $sender_prefix, $sender_domain ) = split( /@/, $sender_address, 2 ); | ||
| 169 | $sender_prefix ||= "unknown"; | ||
| 170 | $sender_domain ||= "unknown"; | ||
| 171 | $dir =~ s/\%sender_address\%/$sender_address/g; | ||
| 172 | $dir =~ s/\%sender_prefix\%/$sender_prefix/g; | ||
| 173 | $dir =~ s/\%sender_domain\%/$sender_domain/g; | ||
| 174 | } | ||
| 175 | |||
| 176 | # parse time | ||
| 177 | if ( $dir =~ /\%(ymd|hm)\%/ ) { | ||
| 178 | my @date = localtime(); # 0: sec, 1: min, 2: hour, 3: day, 4: month, 5: year | ||
| 179 | $date[4]++; | ||
| 180 | $date[5] += 1900; | ||
| 181 | |||
| 182 | my $ymd = sprintf( '%04d-%02d-%02d', @date[ 5, 4, 3 ] ); | ||
| 183 | my $hm = sprintf( '%02d-%02d', @date[ 2, 1 ] ); | ||
| 184 | |||
| 185 | $dir =~ s/\%ymd\%/$ymd/g; | ||
| 186 | $dir =~ s/\%hm\%/$hm/g; | ||
| 187 | } | ||
| 188 | |||
| 189 | return $dir; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | =head2 normalize_str | ||
| 194 | |||
| 195 | Replace not allowed characters .. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub normalize_str { | ||
| 200 | my ( $self, $str ) = @_; | ||
| 201 | $str =~ s/[^\p{L}\d\-_\.@\+]/_/gms; | ||
| 202 | $str =~ s/__/_/g; | ||
| 203 | return $str; | ||
| 204 | } | ||
| 205 | |||
| 206 | =head1 AUTHOR | ||
| 207 | |||
| 208 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 209 | |||
| 210 | =head1 COPYRIGHT | ||
| 211 | |||
| 212 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 213 | |||
| 214 | =head1 LICENCSE | ||
| 215 | |||
| 216 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | |||
| 221 | 1; |