File Coverage

blib/lib/Mail/SpamAssassin/Plugin/ImageInfo.pm
Criterion Covered Total %
statement 28 121 23.1
branch 0 68 0.0
condition 1 50 2.0
subroutine 6 15 40.0
pod 1 9 11.1
total 36 263 13.6


line stmt bran cond sub pod time code
1             # <@LICENSE>
2             # Licensed to the Apache Software Foundation (ASF) under one or more
3             # contributor license agreements. See the NOTICE file distributed with
4             # this work for additional information regarding copyright ownership.
5             # The ASF licenses this file to you under the Apache License, Version 2.0
6             # (the "License"); you may not use this file except in compliance with
7             # the License. You may obtain a copy of the License at:
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing, software
12             # distributed under the License is distributed on an "AS IS" BASIS,
13             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14             # See the License for the specific language governing permissions and
15             # limitations under the License.
16             # </@LICENSE>
17             #
18             # -------------------------------------------------------
19             # ImageInfo Plugin for SpamAssassin
20             # Version: 0.7
21             # Created: 2006-08-02
22             # Modified: 2007-01-17
23             #
24             # Changes:
25             # 0.7 - added image_name_regex to allow pattern matching on the image name
26             # - added support for image/pjpeg content types (progressive jpeg)
27             # - updated imageinfo.cf with a few sample rules for using image_name_regex()
28             # 0.6 - fixed dems_ bug in image_size_range_
29             # 0.5 - added image_named and image_to_text_ratio
30             # 0.4 - added image_size_exact and image_size_range
31             # 0.3 - added jpeg support
32             # 0.2 - optimized by theo
33             # 0.1 - added gif/png support
34             #
35             #
36             # Usage:
37             # image_count()
38             #
39             # body RULENAME eval:image_count(<type>,<min>,[max])
40             # type: 'all','gif','png', or 'jpeg'
41             # min: required, message contains at least this
42             # many images
43             # max: optional, if specified, message must not
44             # contain more than this number of images
45             #
46             # image_count() examples
47             #
48             # body ONE_IMAGE eval:image_count('all',1,1)
49             # body ONE_OR_MORE_IMAGES eval:image_count('all',1)
50             # body ONE_PNG eval:image_count('png',1,1)
51             # body TWO_GIFS eval:image_count('gif',2,2)
52             # body MANY_JPEGS eval:image_count('gif',5)
53             #
54             # pixel_coverage()
55             #
56             # body RULENAME eval:pixel_coverage(<type>,<min>,[max])
57             # type: 'all','gif','png', or 'jpeg'
58             # min: required, message contains at least this
59             # much pixel area
60             # max: optional, if specified, message must not
61             # contain more than this much pixel area
62             #
63             # pixel_coverage() examples
64             #
65             # body LARGE_IMAGE_AREA eval:pixel_coverage('all',150000) # catches any images that are 150k pixel/sq or higher
66             # body SMALL_GIF_AREA eval:pixel_coverage('gif',1,40000) # catches only gifs that 1 to 40k pixel/sql
67             #
68             # image_name_regex()
69             #
70             # body RULENAME eval:image_name_regex(<regex>)
71             # regex: full quoted regexp, see examples below
72             #
73             # image_name_regex() examples
74             #
75             # body CG_DOUBLEDOT_GIF eval:image_name_regex('/^\w{2,9}\.\.gif$/i') # catches double dot gifs abcd..gif
76             #
77             #
78             #
79             # -------------------------------------------------------
80              
81             package Mail::SpamAssassin::Plugin::ImageInfo;
82              
83 22     22   154 use Mail::SpamAssassin::Plugin;
  22         53  
  22         643  
84 22     22   119 use Mail::SpamAssassin::Logger;
  22         51  
  22         1156  
85 22     22   141 use strict;
  22         48  
  22         495  
86 22     22   113 use warnings;
  22         43  
  22         734  
87             # use bytes;
88 22     22   134 use re 'taint';
  22         51  
  22         49935  
89              
90             our @ISA = qw(Mail::SpamAssassin::Plugin);
91              
92             # constructor: register the eval rule
93             sub new {
94 63     63 1 238 my $class = shift;
95 63         166 my $mailsaobject = shift;
96              
97             # some boilerplate...
98 63   33     680 $class = ref($class) || $class;
99 63         366 my $self = $class->SUPER::new($mailsaobject);
100 63         170 bless ($self, $class);
101              
102 63         300 $self->register_eval_rule ("image_count");
103 63         281 $self->register_eval_rule ("pixel_coverage");
104 63         225 $self->register_eval_rule ("image_size_exact");
105 63         217 $self->register_eval_rule ("image_size_range");
106 63         206 $self->register_eval_rule ("image_named");
107 63         214 $self->register_eval_rule ("image_name_regex");
108 63         229 $self->register_eval_rule ("image_to_text_ratio");
109              
110 63         576 return $self;
111             }
112              
113             # -----------------------------------------
114              
115             my %get_details = (
116             'gif' => sub {
117             my ($pms, $part) = @_;
118             my $header = $part->decode(13);
119              
120             # make sure this is actually a valid gif..
121             return unless $header =~ s/^GIF(8[79]a)//;
122             my $version = $1;
123              
124             my ($width, $height, $packed, $bgcolor, $aspect) = unpack("vvCCC", $header);
125             my $color_table_size = 1 << (($packed & 0x07) + 1);
126              
127             # for future enhancements
128             #my $global_color_table = $packed & 0x80;
129             #my $has_global_color_table = $global_color_table ? 1 : 0;
130             #my $sorted_colors = ($packed & 0x08)?1:0;
131             #my $resolution = ((($packed & 0x70) >> 4) + 1);
132              
133             if ($height && $width) {
134             my $area = $width * $height;
135             $pms->{imageinfo}->{pc_gif} += $area;
136             $pms->{imageinfo}->{dems_gif}->{"${height}x${width}"} = 1;
137             $pms->{imageinfo}->{names_all}->{$part->{'name'}} = 1 if $part->{'name'};
138             dbg("imageinfo: gif image ".($part->{'name'} ? $part->{'name'} : '')." is $height x $width pixels ($area pixels sq.), with $color_table_size color table");
139             }
140             },
141              
142             'png' => sub {
143             my ($pms, $part) = @_;
144             my $data = $part->decode();
145              
146             return unless (substr($data, 0, 8) eq "\x89PNG\x0d\x0a\x1a\x0a");
147              
148             my $datalen = length $data;
149             my $pos = 8;
150             my $chunksize = 8;
151             my ($width, $height) = ( 0, 0 );
152             my ($depth, $ctype, $compression, $filter, $interlace);
153              
154             while ($pos < $datalen) {
155             my ($len, $type) = unpack("Na4", substr($data, $pos, $chunksize));
156             $pos += $chunksize;
157              
158             last if $type eq "IEND"; # end of png image.
159              
160             next unless ( $type eq "IHDR" && $len == 13 );
161              
162             my $bytes = substr($data, $pos, $len + 4);
163             my $crc = unpack("N", substr($bytes, -4, 4, ""));
164              
165             if ($type eq "IHDR" && $len == 13) {
166             ($width, $height, $depth, $ctype, $compression, $filter, $interlace) = unpack("NNCCCCC", $bytes);
167             last;
168             }
169             }
170              
171             if ($height && $width) {
172             my $area = $width * $height;
173             $pms->{imageinfo}->{pc_png} += $area;
174             $pms->{imageinfo}->{dems_png}->{"${height}x${width}"} = 1;
175             $pms->{imageinfo}->{names_all}->{$part->{'name'}} = 1 if $part->{'name'};
176             dbg("imageinfo: png image ".($part->{'name'} ? $part->{'name'} : '')." is $height x $width pixels ($area pixels sq.)");
177             }
178             },
179              
180             'jpeg' => sub {
181             my ($pms, $part) = @_;
182              
183             my $data = $part->decode();
184              
185             my $index = substr($data, 0, 2);
186             return unless $index eq "\xFF\xD8";
187              
188             my $pos = 2;
189             my $chunksize = 4;
190             my ($prec, $height, $width, $comps) = (undef,0,0,undef);
191             while (1) {
192             my ($xx, $mark, $len) = unpack("CCn", substr($data, $pos, $chunksize));
193             last if (!defined $xx || $xx != 0xFF);
194             last if (!defined $mark || $mark == 0xDA || $mark == 0xD9);
195             last if (!defined $len || $len < 2);
196             $pos += $chunksize;
197             my $block = substr($data, $pos, $len - 2);
198             my $blocklen = length($block);
199             if ( ($mark >= 0xC0 && $mark <= 0xC3) || ($mark >= 0xC5 && $mark <= 0xC7) ||
200             ($mark >= 0xC9 && $mark <= 0xCB) || ($mark >= 0xCD && $mark <= 0xCF) ) {
201             ($prec, $height, $width, $comps) = unpack("CnnC", substr($block, 0, 6, ""));
202             last;
203             }
204             $pos += $blocklen;
205             }
206              
207             if ($height && $width) {
208             my $area = $height * $width;
209             $pms->{imageinfo}->{pc_jpeg} += $area;
210             $pms->{imageinfo}->{dems_jpeg}->{"${height}x${width}"} = 1;
211             $pms->{imageinfo}->{names_all}->{$part->{'name'}} = 1 if $part->{'name'};
212             dbg("imageinfo: jpeg image ".($part->{'name'} ? $part->{'name'} : '')." is $height x $width pixels ($area pixels sq.)");
213             }
214              
215             },
216              
217             );
218              
219             sub _get_images {
220 0     0     my ($self,$pms) = @_;
221 0           my $result = 0;
222              
223 0           foreach my $type ( 'all', keys %get_details ) {
224 0           $pms->{'imageinfo'}->{"pc_$type"} = 0;
225 0           $pms->{'imageinfo'}->{"count_$type"} = 0;
226             }
227              
228 0           foreach my $p ($pms->{msg}->find_parts(qr@^image/(?:gif|png|jpeg)$@, 1)) {
229             # make sure its base64 encoded
230 0   0       my $cte = lc($p->get_header('content-transfer-encoding') || '');
231 0 0         next if ($cte !~ /^base64$/);
232              
233 0           my ($type) = $p->{'type'} =~ m@/(\w+)$@;
234 0 0 0       if ($type && exists $get_details{$type}) {
235 0           $get_details{$type}->($pms,$p);
236 0           $pms->{'imageinfo'}->{"count_$type"} ++;
237             }
238             }
239              
240 0           foreach my $name ( keys %{$pms->{'imageinfo'}->{"names_all"}} ) {
  0            
241 0           dbg("imageinfo: image name $name found");
242             }
243              
244 0           foreach my $type ( keys %get_details ) {
245 0           $pms->{'imageinfo'}->{'pc_all'} += $pms->{'imageinfo'}->{"pc_$type"};
246 0           $pms->{'imageinfo'}->{'count_all'} += $pms->{'imageinfo'}->{"count_$type"};
247 0           foreach my $dem ( keys %{$pms->{'imageinfo'}->{"dems_$type"}} ) {
  0            
248 0           dbg("imageinfo: adding $dem to dems_all");
249 0           $pms->{'imageinfo'}->{'dems_all'}->{$dem} = 1;
250             }
251             }
252             }
253              
254             # -----------------------------------------
255              
256             sub image_named {
257 0     0 0   my ($self,$pms,$body,$name) = @_;
258 0 0         return unless (defined $name);
259              
260             # make sure we have image data read in.
261 0 0         if (!exists $pms->{'imageinfo'}) {
262 0           $self->_get_images($pms);
263             }
264              
265 0 0         return 0 unless (exists $pms->{'imageinfo'}->{"names_all"});
266 0 0         return 1 if (exists $pms->{'imageinfo'}->{"names_all"}->{$name});
267 0           return 0;
268             }
269              
270             # -----------------------------------------
271              
272             sub image_name_regex {
273 0     0 0   my ($self,$pms,$body,$re) = @_;
274 0 0         return unless (defined $re);
275              
276             # make sure we have image data read in.
277 0 0         if (!exists $pms->{'imageinfo'}) {
278 0           $self->_get_images($pms);
279             }
280              
281 0 0         return 0 unless (exists $pms->{'imageinfo'}->{"names_all"});
282              
283 0           my $hit = 0;
284 0           foreach my $name (keys %{$pms->{'imageinfo'}->{"names_all"}}) {
  0            
285 0           dbg("imageinfo: checking image named $name against regex $re");
286 0 0         if (eval { $name =~ /$re/ }) { $hit = 1 }
  0            
  0            
287 0 0         dbg("imageinfo: error in regex /$re/ - $@") if $@;
288 0 0         if ($hit) {
289 0           dbg("imageinfo: image_name_regex hit on $name");
290 0           return 1;
291             }
292             }
293 0           return 0;
294              
295             }
296              
297             # -----------------------------------------
298              
299             sub image_count {
300 0     0 0   my ($self,$pms,$body,$type,$min,$max) = @_;
301              
302 0 0         return unless defined $min;
303              
304             # make sure we have image data read in.
305 0 0         if (!exists $pms->{'imageinfo'}) {
306 0           $self->_get_images($pms);
307             }
308              
309             # dbg("imageinfo: count: $min, ".($max ? $max:'').", $type, ".$pms->{'imageinfo'}->{"count_$type"});
310 0           return result_check($min, $max, $pms->{'imageinfo'}->{"count_$type"});
311             }
312              
313             # -----------------------------------------
314              
315             sub pixel_coverage {
316 0     0 0   my ($self,$pms,$body,$type,$min,$max) = @_;
317              
318 0 0 0       return unless (defined $type && defined $min);
319              
320             # make sure we have image data read in.
321 0 0         if (!exists $pms->{'imageinfo'}) {
322 0           $self->_get_images($pms);
323             }
324              
325             # dbg("imageinfo: pc_$type: $min, ".($max ? $max:'').", $type, ".$pms->{'imageinfo'}->{"pc_$type"});
326 0           return result_check($min, $max, $pms->{'imageinfo'}->{"pc_$type"});
327             }
328              
329             # -----------------------------------------
330              
331             sub image_to_text_ratio {
332 0     0 0   my ($self,$pms,$body,$type,$min,$max) = @_;
333 0 0 0       return unless (defined $type && defined $min && defined $max);
      0        
334              
335             # make sure we have image data read in.
336 0 0         if (!exists $pms->{'imageinfo'}) {
337 0           $self->_get_images($pms);
338             }
339              
340             # depending on how you call this eval (body vs rawbody),
341             # the $textlen will differ.
342 0           my $textlen = length(join('',@$body));
343              
344 0 0 0       return 0 unless ( $textlen > 0 && exists $pms->{'imageinfo'}->{"pc_$type"} && $pms->{'imageinfo'}->{"pc_$type"} > 0);
      0        
345              
346 0           my $ratio = $textlen / $pms->{'imageinfo'}->{"pc_$type"};
347 0           dbg("imageinfo: image ratio=$ratio, min=$min max=$max");
348 0           return result_check($min, $max, $ratio, 1);
349             }
350              
351             # -----------------------------------------
352              
353             sub image_size_exact {
354 0     0 0   my ($self,$pms,$body,$type,$height,$width) = @_;
355 0 0 0       return unless (defined $type && defined $height && defined $width);
      0        
356              
357             # make sure we have image data read in.
358 0 0         if (!exists $pms->{'imageinfo'}) {
359 0           $self->_get_images($pms);
360             }
361              
362 0 0         return 0 unless (exists $pms->{'imageinfo'}->{"dems_$type"});
363 0 0         return 1 if (exists $pms->{'imageinfo'}->{"dems_$type"}->{"${height}x${width}"});
364 0           return 0;
365             }
366              
367             # -----------------------------------------
368              
369             sub image_size_range {
370 0     0 0   my ($self,$pms,$body,$type,$minh,$minw,$maxh,$maxw) = @_;
371 0 0 0       return unless (defined $type && defined $minh && defined $minw);
      0        
372              
373             # make sure we have image data read in.
374 0 0         if (!exists $pms->{'imageinfo'}) {
375 0           $self->_get_images($pms);
376             }
377              
378 0           my $name = 'dems_'.$type;
379 0 0         return unless (exists $pms->{'imageinfo'}->{$name});
380              
381 0           foreach my $dem ( keys %{$pms->{'imageinfo'}->{"dems_$type"}}) {
  0            
382 0           my ($h,$w) = split(/x/,$dem);
383 0 0         next if ($h < $minh); # height less than min height
384 0 0         next if ($w < $minw); # width less than min width
385 0 0 0       next if (defined $maxh && $h > $maxh); # height more than max height
386 0 0 0       next if (defined $maxw && $w > $maxw); # width more than max width
387              
388             # if we make it here, we have a match
389 0           return 1;
390             }
391              
392 0           return 0;
393             }
394              
395             # -----------------------------------------
396              
397             sub result_check {
398 0     0 0   my ($min, $max, $value, $nomaxequal) = @_;
399 0 0         return 0 unless defined $value;
400 0 0         return 0 if ($value < $min);
401 0 0 0       return 0 if (defined $max && $value > $max);
402 0 0 0       return 0 if (defined $nomaxequal && $nomaxequal && $value == $max);
      0        
403 0           return 1;
404             }
405              
406             # -----------------------------------------
407              
408             1;