| 1 | package Mail::Decency::ContentFilter::Core::Cmd; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | #extends 'Mail::Decency::ContentFilter::Core'; | ||||||||||||||||||||||||||||
| 5 | #with qw/ Mail::Decency::ContentFilter::Core::User /; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use version 0.74; our $VERSION = qv( "v0.1.6" ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use IO::Pipe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use File::Temp qw/ tempfile /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Mail::Decency::ContentFilter::Core::Cmd | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Base class for all command line filters. Including spam filter such as DSPAM and so on | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 CLASS ATTRIBUTES | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head2 cmd_check : Str | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Check command.. normally the command which is used to filter a single mail. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | has cmd_check => ( is => 'rw', isa => 'Str' ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head2 cmd_learn_ham : Str | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Learn HAM command.. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | has cmd_learn_ham => ( is => 'rw', isa => 'Str', predicate => 'can_learn_ham' ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head2 cmd_learn_ham : Str | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | UNLearn HAM command.. (wrongly trained before) | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | has cmd_unlearn_ham => ( is => 'rw', isa => 'Str', predicate => 'can_unlearn_ham' ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head2 cmd_learn_spam : Str | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Learn new SPAM | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | has cmd_learn_spam => ( is => 'rw', isa => 'Str', predicate => 'can_learn_spam' ); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head2 cmd_unlearn_spam : Str | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Unlearn wrongly trained SPAM | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | has cmd_unlearn_spam => ( is => 'rw', isa => 'Str', predicate => 'can_learn_spam' ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 METHODS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 pre_init | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Add check params: cmd, check, train and untrain to list of check params | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | before init => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 75 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | unshift @{ $self->{ config_params } ||=[] }, qw/ | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"config_params"} ||= []
MC/DC0%0/1
$self->{"config_params"} ||= []
$self->{"config_params"}
Truth table0%0/2
A
A= $self->{"config_params"}
| |||||||||||||||||||||||||||||
| 77 | cmd_check | ||||||||||||||||||||||||||||
| 78 | cmd_learn_ham | ||||||||||||||||||||||||||||
| 79 | cmd_unlearn_ham | ||||||||||||||||||||||||||||
| 80 | cmd_learn_spam | ||||||||||||||||||||||||||||
| 81 | cmd_unlearn_spam | ||||||||||||||||||||||||||||
| 82 | /; | ||||||||||||||||||||||||||||
| 83 | 3 | }; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 handle | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Default handling for any content filter is getting info about the to be filterd file | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub handle { | ||||||||||||||||||||||||||
|
Subroutine
handle: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # pipe file throught command | ||||||||||||||||||||||||||||
| 98 | 0 | my ( $res, $result, $exit_code ) = $self->cmd_filter; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # return if cannot be handled | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return unless $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # chomp lines | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | 1 while chomp $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # handle result by the actual filter module | ||||||||||||||||||||||||||||
| 107 | 0 | return $self->handle_filter_result( $result, $exit_code ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 cmd_filter | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Pipes mail content through command line program and caches result | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub cmd_filter { | ||||||||||||||||||||||||||
|
Subroutine
cmd_filter: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ( $self, $cmd_type ) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | $cmd_type ||= 'check'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$cmd_type ||= "check"
MC/DC0%0/1
$cmd_type ||= "check"
$cmd_type
Truth table0%0/2
A
A= $cmd_type
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # retreive user | ||||||||||||||||||||||||||||
| 122 | 0 | my $user = $self->get_user(); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # build command | ||||||||||||||||||||||||||||
| 125 | 0 | my $cmd = $self->build_cmd( $cmd_type ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # if command required user and no user could be determined -> abort | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ( $cmd =~ /%user%/ && ! $user ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cmd =~ /%user%/ and not $user
MC/DC0%0/2
$cmd =~ /%user%/ and not $user
$cmd =~ /%user%/
not $user
Truth table0%0/3
A and not(B)
A= $cmd =~ /%user%/B= not $user
| |||||||||||||||||||||||||||||
| 129 | 0 | $self->logger->error( "Could not determine user for recipient ". $self->to. ", abort" ); | |||||||||||||||||||||||||||
| 130 | 0 | return ( 0 ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # replace user in command | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $cmd =~ s/%user%/$user/g if $user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | $self->logger->debug3( "Run cmd '$cmd'" ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # we cannot redirect STDOUT and STDERR in a multi-process environment! | ||||||||||||||||||||||||||||
| 139 | # instead we'll use a tem file | ||||||||||||||||||||||||||||
| 140 | 0 | my ( $th, $tn ) = tempfile( $self->server->temp_dir. "/file-XXXXXX", UNLINK => 1 ); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # wheter uses stdin or use file name | ||||||||||||||||||||||||||||
| 143 | 0 | my $stdin = 1; | |||||||||||||||||||||||||||
| 144 | 0 | my ( $input_handle, $input_file ); # in handle, in file | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # pritn to file and give this file to command line | ||||||||||||||||||||||||||||
| 147 | 0 | my $file_mode = 0; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ( $cmd =~ /%file%/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | $file_mode++; | |||||||||||||||||||||||||||
| 150 | 0 | ( $input_handle, $input_file ) = tempfile( $self->server->temp_dir. "/file-XXXXXX", UNLINK => 0 ); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # open command line and print to it | ||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||
| 155 | 0 | open $input_handle, '|-', "$cmd 1>\"$tn\" 2>\"$tn\""; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # open now the mail file and pipe | ||||||||||||||||||||||||||||
| 159 | 0 | open my $fh, '<', $self->file; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # print whole mime data to pipe | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | while ( my $l = <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | print $input_handle $l; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # close input and command | ||||||||||||||||||||||||||||
| 167 | 0 | close $fh; | |||||||||||||||||||||||||||
| 168 | 0 | close $input_handle; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # in file mode: provide file name as input | ||||||||||||||||||||||||||||
| 171 | 0 | my $system_result = 0; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | if ( $file_mode ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | ( my $cmd_file = $cmd ) =~ s/%file%/$input_file/; | |||||||||||||||||||||||||||
| 174 | 0 | $self->logger->debug3( "Run command '$cmd_file'" ); | |||||||||||||||||||||||||||
| 175 | 0 | `$cmd_file 1>"$tn" 2>"$tn"`; | |||||||||||||||||||||||||||
| 176 | 0 | $system_result = $?; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # read now output from tempfile and remove it.. break after first empty line | ||||||||||||||||||||||||||||
| 180 | # to assure we'll get only headers!! | ||||||||||||||||||||||||||||
| 181 | 0 | reset $th; | |||||||||||||||||||||||||||
| 182 | 0 | my $in = ""; | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | while ( my $l = <$th> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | last if $l =~ /^$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | $in .= $l; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | close $th; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | unlink( $tn ) if -f $tn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | unlink( $input_file ) if $input_file && -f $input_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$input_file and -f $input_file
MC/DC0%0/2
$input_file and -f $input_file
$input_file
-f $input_file
Truth table0%0/3
A and B
A= $input_fileB= -f $input_file
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | return ( 1, $in, $system_result ); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head2 build_cmd | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Can be overwritte by descendant module | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Build cmd | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub build_cmd { | ||||||||||||||||||||||||||
|
Subroutine
build_cmd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 0 | my ( $self, $type ) = @_; | |||||||||||||||||||||||||||
| 204 | 0 | my $meth = "cmd_$type"; | |||||||||||||||||||||||||||
| 205 | 0 | return $self->$meth; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Ulrich Kautz <uk@fortrabbit.de> | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 LICENCSE | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | This library is free software and may be distributed under the same terms as perl itself. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 3 | 1; | |||||||||||||||||||||||||||