1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
package Mail::SpamAssassin::Plugin::BodyEval;
use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::Logger;
use Mail::SpamAssassin::Constants qw(:sa);
use strict;
use warnings;
use bytes;
use re 'taint';
use vars qw(@ISA);
@ISA = qw(Mail::SpamAssassin::Plugin);
# constructor: register the eval rule
sub new {
my $class = shift;
my $mailsaobject = shift;
# some boilerplate...
$class = ref($class) || $class;
my $self = $class->SUPER::new($mailsaobject);
bless ($self, $class);
# the important bit!
$self->register_eval_rule("multipart_alternative_difference");
$self->register_eval_rule("multipart_alternative_difference_count");
$self->register_eval_rule("check_blank_line_ratio");
$self->register_eval_rule("tvd_vertical_words");
$self->register_eval_rule("check_stock_info");
return $self;
}
sub multipart_alternative_difference {
my ($self, $pms, $fulltext, $min, $max) = @_;
$self->_multipart_alternative_difference($pms) unless (exists $pms->{madiff});
if (($min == 0 || $pms->{madiff} > $min) &&
($max eq "undef" || $pms->{madiff} <= $max)) {
return 1;
}
return 0;
}
sub multipart_alternative_difference_count {
my ($self, $pms, $fulltext, $ratio, $minhtml) = @_;
$self->_multipart_alternative_difference($pms) unless (exists $pms->{madiff});
return 0 unless $pms->{madiff_html} > $minhtml;
return(($pms->{madiff_text} / $pms->{madiff_html}) > $ratio);
}
sub _multipart_alternative_difference {
my ($self, $pms) = @_;
$pms->{madiff} = 0;
$pms->{madiff_html} = 0;
$pms->{madiff_text} = 0;
my $msg = $pms->{msg};
# Find all multipart/alternative parts in the message
my @ma = $msg->find_parts(qr@^multipart/alternative\b@i);
# If there are no multipart/alternative sections, skip this test.
return if (!@ma);
# Figure out what the MIME content of the message looks like
my @content = $msg->content_summary();
# Exchange meeting requests come in as m/a text/html text/calendar,
# which we want to ignore because of the high FP rate it would cause.
#
if (@content == 3 && $content[2] eq 'text/calendar' &&
$content[1] eq 'text/html' &&
$content[0] eq 'multipart/alternative') {
return;
}
# Go through each of the multipart parts
foreach my $part (@ma) {
my %html;
my %text;
# limit our search to text-based parts
my @txt = $part->find_parts(qr@^text\b@i);
foreach my $text (@txt) {
# we only care about the rendered version of the part
my ($type, $rnd) = $text->rendered();
next unless defined $type;
# parse the rendered text into tokens. assume they are whitespace
# separated, and ignore anything that doesn't have a word-character
# in it (0-9a-zA-Z_) since those are probably things like bullet
# points, horizontal lines, etc. this assumes that punctuation
# in one part will be the same in other parts.
#
if ($type eq 'text/html') {
foreach my $w (grep(/\w/, split(/\s+/, $rnd))) {
#dbg("eval: HTML: $w");
$html{$w}++;
}
# If there are no words, mark if there's at least 1 image ...
if (!%html && exists $pms->{html}{inside}{img}) {
# Use "\n" as the mark since it can't ever occur normally
$html{"\n"}=1;
}
}
else {
foreach my $w (grep(/\w/, split(/\s+/, $rnd))) {
#dbg("eval: TEXT: $w");
$text{$w}++;
}
}
}
# How many HTML tokens do we have at the start?
my $orig = keys %html;
next if ($orig == 0);
$pms->{madiff_html} = $orig;
$pms->{madiff_text} = keys %text;
dbg('eval: text words: ' . $pms->{madiff_text} . ', html words: ' . $pms->{madiff_html});
# If the token appears at least as many times in the text part as
# in the html part, remove it from the list of html tokens.
while(my ($k,$v) = each %text) {
delete $html{$k} if (exists $html{$k} && $html{$k}-$text{$k} < 1);
}
#map { dbg("eval: LEFT: $_") } keys %html;
# In theory, the tokens should be the same in both text and html
# parts, so there would be 0 tokens left in the html token list, for
# a 0% difference rate. Calculate it here, and record the difference
# if it's been the highest so far in this message.
my $diff = scalar(keys %html)/$orig*100;
$pms->{madiff} = $diff if ($diff > $pms->{madiff});
dbg("eval: " . sprintf "madiff: left: %d, orig: %d, max-difference: %0.2f%%", scalar(keys %html), $orig, $pms->{madiff});
}
return;
}
sub check_blank_line_ratio {
my ($self, $pms, $fulltext, $min, $max, $minlines) = @_;
if (!defined $minlines || $minlines < 1) {
$minlines = 1;
}
my $blank_line_ratio_ref = $pms->{blank_line_ratio};
if (! exists $blank_line_ratio_ref->{$minlines}) {
$fulltext = $pms->get_decoded_body_text_array();
my $blank = 0;
my $nlines = 0;
foreach my $chunk (@$fulltext) {
foreach (split(/^/m, $chunk, -1)) {
$nlines++;
$blank++ if !/\S/;
}
}
# report -1 if it's a blank message ...
$blank_line_ratio_ref->{$minlines} =
$nlines < $minlines ? -1 : 100 * $blank / $nlines;
}
return (($min == 0 && $blank_line_ratio_ref->{$minlines} <= $max) ||
($blank_line_ratio_ref->{$minlines} > $min &&
$blank_line_ratio_ref->{$minlines} <= $max));
}
sub tvd_vertical_words {
my ($self, $pms, $text, $min, $max) = @_;
# klugy
$max = 101 if ($max >= 100);
if (!defined $pms->{tvd_vertical_words}) {
$pms->{tvd_vertical_words} = -1;
foreach (@{$text}) {
my $l = length $_;
next unless ($l > 5);
my $spaces = tr/ / /;
my $nonspaces = $l - $spaces;
my $pct;
if ($spaces > $nonspaces || $nonspaces == 0) {
$pct = 100;
}
else {
$pct = int(100*$spaces/$nonspaces);
}
$pms->{tvd_vertical_words} = $pct if ($pct > $pms->{tvd_vertical_words});
}
}
return 1 if ($pms->{tvd_vertical_words} >= $min && $pms->{tvd_vertical_words} < $max);
}
sub check_stock_info {
my ($self, $pms, $fulltext, $min) = @_;
$self->_check_stock_info($pms) unless (exists $pms->{stock_info});
if ($min == 0 || $pms->{stock_info} >= $min) {
return 1;
}
return 0;
}
sub _check_stock_info {
my ($self, $pms) = @_;
$pms->{stock_info} = 0;
# Find all multipart/alternative parts in the message
my @parts = $pms->{msg}->find_parts(qr@^text/plain$@i);
return if (!@parts);
# Go through each of the multipart parts
my %hits;
my $part = $parts[0];
my ($type, $rnd) = $part->rendered();
return unless $type;
# bug 5644,5717: avoid pathological cases where a regexp takes massive amount
# of time by applying the regexp to limited-size text chunks, one at a time
foreach my $rnd_chunk (
Mail::SpamAssassin::Message::split_into_array_of_short_paragraphs($rnd))
{
foreach ( $rnd_chunk =~ /^\s*([^:\s][^:\n]{2,29})\s*:\s*\S/mg ) {
my $str = lc $_;
$str =~ tr/a-z//cd;
#$str =~ s/([a-z])0([a-z])/$1o$2/g;
if ($str =~ /(
^trad(?:e|ing)date|
company(?:name)?|
s\w?(?:t\w?o\w?c\w?k|y\w?m(?:\w?b\w?o\w?l)?)|
t(?:arget|icker)|
(?:opening|current)p(?:rice)?|
p(?:rojected|osition)|
expectations|
weeks?high|
marketperformance|
(?:year|week|month|day|price)(?:target|estimates?)|
sector|
r(?:ecommendation|ating)
)$/x) {
$hits{$1}++;
dbg("eval: stock info hit: $1");
}
}
}
$pms->{stock_info} = scalar keys %hits;
dbg("eval: stock info total: ".$pms->{stock_info});
return;
}
1;
|