root/trunk/devel/patchset2.patch

Revision 116, 17.8 kB (checked in by decoder, 2 years ago)

Remove redundant use() calls, fixing ticket #21
Added O_CREAT to another tie call
Fixed "Prev Match" time output
Added patchset2 to SVN

  • FuzzyOcr.cf

    old new  
    3333# Default value: 1 
    3434#focr_verbose 3 
    3535 
     36# Log Message-Id, From, To 
     37# Default: 1 
     38#focr_log_pmsinfo 0 
     39 
    3640# Send logging output to stderr. 
    3741# Default value: 1 
    3842#focr_log_stderr 0 
     
    163167# Default value: 0 
    164168#focr_global_timeout 1 
    165169 
     170# Minimum image size to scan. Images with dimensions smaller than the 
     171# ones specified here will be skipped: 
     172# Default: Height:4 Width:4 
     173#focr_min_height 4 
     174#focr_min_width 4 
     175 
    166176# Maximum file size for different formats in byte, bigger pictures  
    167177# will not be scanned  
    168178# Default values: Unlimited) 
  • FuzzyOcr.pm

    old new  
    124124    my $imgerr   = 0; 
    125125    my $main     = $self->{main}; 
    126126 
    127     my $from = $pms->get('From') ? $pms->get('From') : "<no sender>"; 
    128     my $to = $pms->get('To') ? $pms->get('To') : "<no receipients>"; 
    129     my $msgid = $pms->get('Message-Id') ? $pms->get('Message-Id') : "<no messageid>"; 
     127    debuglog("Starting FuzzyOcr..."); 
     128     
     129    #Show PMS info if asked to 
     130    if ($conf->{focr_log_pmsinfo}) { 
     131        my $msgid = $pms->get('Message-Id') ? $pms->get('Message-Id') : "<no messageid>"; 
     132        my $from = $pms->get('From') ? $pms->get('From') : "<no sender>"; 
     133        my $to = $pms->get('To') ? $pms->get('To') : "<no receipients>"; 
     134        chomp($from, $to, $msgid); 
     135        infolog("Processing Message with ID \"$msgid\" ($from -> $to)"); 
     136    } 
    130137 
    131     chomp($from, $to, $msgid); 
    132  
    133     debuglog("Starting FuzzyOcr..."); 
    134     infolog("Processing Message with ID \"$msgid\" ($from -> $to)"); 
    135138    foreach my $p ( 
    136139        $pms->{msg}->find_parts(qr(^image\b)i), 
    137140        $pms->{msg}->find_parts(qr(Application/Octet-Stream)i) 
     
    146149            $fname =~ tr/\@\$\%\&/_/s; 
    147150        } 
    148151 
    149         my $filename = $fname; $filename =~ tr{a-zA-Z0-9\.}{_}cs; 
     152        my $filename = $fname; $filename =~ tr{a-zA-Z0-9\-.}{_}cs; 
     153        debuglog("fname: \"$fname\" => \"$filename\""); 
    150154        my $pdata = $p->decode(); 
    151155        my $pdatalen = length($pdata); 
    152156        my $w = 0; my $h = 0; 
    153157 
    154         my $blah = substr($pdata,0,3); 
    155  
    156158        if ( substr($pdata,0,3) eq "\x47\x49\x46" ) { 
    157159            ## GIF File 
    158160            $imgfiles{$filename}{ftype} = 1;  
    159161            ($w,$h) = unpack("vv",substr($pdata,6,4)); 
    160             infolog("GIF: [${h}x${w}] $filename"); 
     162            infolog("GIF: [${h}x${w}] $filename ($pdatalen)"); 
    161163            $imgfiles{$filename}{width}  = $w; 
    162164            $imgfiles{$filename}{height} = $h; 
    163165        } elsif ( substr($pdata,0,2) eq "\xff\xd8" ) { 
     
    184186                errorlog("Cannot find image dimensions"); 
    185187            } else { 
    186188                ($h,$w) = unpack("nn",substr($pdata,$pos+3,4)); 
    187                 infolog("JPEG: [${h}x${w}] $filename"); 
     189                infolog("JPEG: [${h}x${w}] $filename ($pdatalen)"); 
    188190                $imgfiles{$filename}{ftype} = 2; 
    189191                $imgfiles{$filename}{height} = $h; 
    190192                $imgfiles{$filename}{width}  = $w; 
     
    195197            $imgfiles{$filename}{ftype}  = 3; 
    196198            $imgfiles{$filename}{width}  = $w; 
    197199            $imgfiles{$filename}{height} = $h; 
    198             infolog("PNG: [${h}x${w}] $filename"); 
     200            infolog("PNG: [${h}x${w}] $filename ($pdatalen)"); 
    199201        } elsif ( substr($pdata,0,2) eq "BM" ) { 
    200202            ## BMP File 
    201             ($w,$h) = unpack("NN",substr($pdata,18,8)); 
     203            ($w,$h) = unpack("VV",substr($pdata,18,8)); 
    202204            $imgfiles{$filename}{ftype}  = 4; 
    203205            $imgfiles{$filename}{width}  = $w; 
    204206            $imgfiles{$filename}{height} = $h; 
    205             infolog("BMP: [${h}x${w}] $filename"); 
     207            infolog("BMP: [${h}x${w}] $filename ($pdatalen)"); 
    206208        } elsif ( 
    207209            ## TIFF File 
    208210            (substr($pdata,0,4) eq "\x4d\x4d\x00\x2a") or 
     
    218220                $w = $val if ($id == 257); 
    219221                last if ($h != 0 and $w != 0); 
    220222            } 
    221             infolog("TIFF: [${h}x${w}] $filename ($worder)"); 
     223            infolog("TIFF: [${h}x${w}] $filename ($pdatalen) ($worder)"); 
    222224            infolog("Cannot determine size of TIFF image, setting to '1x1'") if ($h == 0 and $w == 0); 
    223225            $imgfiles{$filename}{ftype}  = 5; 
    224226            $imgfiles{$filename}{width}  = $w ? $w : 1; 
     
    230232            infolog("Skipping file with content-type=\"$ctype\" name=\"$fname\""); 
    231233            delete $imgfiles{$filename}; 
    232234            next; 
     235        } 
    233236 
     237        #Skip images that cannot contain text 
     238        if ($imgfiles{$filename}{height} < $conf->{focr_min_height}) { 
     239            infolog("Skipping image: height < $conf->{focr_min_height}"); 
     240            delete $imgfiles{$filename}; 
     241            next; 
    234242        } 
    235243 
     244        #Skip images that cannot contain text 
     245        if ($imgfiles{$filename}{width} < $conf->{focr_min_width}) { 
     246            infolog("Skipping image: width < $conf->{focr_min_width}"); 
     247            delete $imgfiles{$filename}; 
     248            next; 
     249        } 
     250 
    236251        #Found Image!! Get a temporary dir to save image 
    237252        $imgdir = Mail::SpamAssassin::Util::secure_tmpdir(); 
    238253        unless ($imgdir) { 
     
    243258 
    244259        #Generete unique filename to store image 
    245260        my $imgfilename = Mail::SpamAssassin::Util::untaint_file_path( 
    246             $imgdir . "/" . $fname 
     261            $imgdir . "/" . $filename 
    247262        ); 
    248263        my $unique = 0; 
    249264        while (-e $imgfilename) { 
    250265            $imgfilename = Mail::SpamAssassin::Util::untaint_file_path( 
    251                 $imgdir . "/" . chr(65+$unique) . "." . $fname 
     266                $imgdir . "/" . chr(65+$unique) . "." . $filename 
    252267            ); 
    253268            $unique++; 
    254269        } 
     
    308323        } 
    309324    } 
    310325 
    311     IMAGE: 
    312326    my $haserr; 
    313327    foreach my $filename (keys %imgfiles) { 
    314328        my $pic = $imgfiles{$filename}; 
     
    351365            infolog("Found GIF header name=\"$$pic{fname}\""); 
    352366            if ($conf->{focr_skip_gif}) { 
    353367                infolog("Skipping image check"); 
    354                 next IMAGE
     368                next
    355369            } 
    356370            if (defined($conf->{focr_max_size_gif}) and ($$pic{fsize} > $conf->{focr_max_size_gif})) { 
    357371                infolog("GIF file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
     
    374388            foreach my $a (qw/gifsicle giftext giffix gifinter giftopnm/) { 
    375389                unless (defined $conf->{"focr_bin_$a"}) { 
    376390                    errorlog("Cannot exec $a, skipping image"); 
    377                     next IMAGE
     391                    next
    378392                } 
    379393            } 
     394 
    380395            my @stderr_data; 
    381  
    382396            my ($retcode, @stdout_data) = save_execute( 
    383397                "$conf->{focr_bin_giftext} $file", 
    384398                undef, 
     
    429443                    } 
    430444                } 
    431445            } 
    432  
    433             if (defined($conf->{focr_max_size_gif}) and (((stat($tfile))[7]) > $conf->{focr_max_size_gif})) { 
    434                 infolog("Fixed GIF file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
     446            my $fixedsize = (stat($tfile))[7]; 
     447            if (defined($conf->{focr_max_size_gif}) and ($fixedsize > $conf->{focr_max_size_gif})) { 
     448                infolog("Fixed GIF file size ($fixedsize) exceeds maximum file size for this format, skipping..."); 
    435449                next; 
    436450            } 
    437451 
     
    503517            infolog("Found JPEG header name=\"$$pic{fname}\""); 
    504518            if ($conf->{focr_skip_jpeg}) { 
    505519                infolog("Skipping image check"); 
    506                 next IMAGE
     520                next
    507521            } 
    508522 
    509523            if (defined($conf->{focr_max_size_jpeg}) and ($$pic{fsize} > $conf->{focr_max_size_jpeg})) { 
     
    523537            foreach my $a (qw/jpegtopnm/) { 
    524538                unless (defined $conf->{"focr_bin_$a"}) { 
    525539                    errorlog("Cannot exec $a, skipping image"); 
    526                     next IMAGE
     540                    next
    527541                } 
    528542            } 
    529543            printf RAWERR qq(## $conf->{focr_bin_jpegtopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     
    545559            infolog("Found PNG header name=\"$$pic{fname}\""); 
    546560            if ($conf->{focr_skip_png}) { 
    547561                infolog("Skipping image check"); 
    548                 next IMAGE
     562                next
    549563            } 
    550564            if (defined($conf->{focr_max_size_png}) and ($$pic{fsize} > $conf->{focr__max_size_png})) { 
    551565                infolog("PNG file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
     
    562576            foreach my $a (qw/pngtopnm/) { 
    563577                unless (defined $conf->{"focr_bin_$a"}) { 
    564578                    errorlog("Cannot exec $a, skipping image"); 
    565                     next IMAGE
     579                    next
    566580                } 
    567581            } 
    568582 
     
    585599            infolog("Found BMP header name=\"$$pic{fname}\""); 
    586600            if ($conf->{focr_skip_bmp}) { 
    587601                infolog("Skipping image check"); 
    588                 next IMAGE
     602                next
    589603            } 
    590604            if (defined($conf->{focr_max_size_bmp}) and ($$pic{fsize} > $conf->{focr_max_size_bmp})) { 
    591605                infolog("BMP file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
     
    602616            foreach my $a (qw/bmptopnm/) { 
    603617                unless (defined $conf->{"focr_bin_$a"}) { 
    604618                    errorlog("Cannot exec $a, skipping image"); 
    605                     next IMAGE
     619                    next
    606620                } 
    607621            } 
    608622            printf RAWERR qq(## $conf->{focr_bin_bmptopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     
    624638            infolog("Found TIFF header name=\"$$pic{fname}\""); 
    625639            if ($conf->{focr_skip_tiff}) { 
    626640                infolog("Skipping image check"); 
    627                 next IMAGE
     641                next
    628642            } 
    629643            if (defined($conf->{focr_max_size_tiff}) and ($$pic{fsize} > $conf->{focr_max_size_tiff})) { 
    630644                infolog("TIFF file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
     
    642656            foreach my $a (qw/tifftopnm/) { 
    643657                unless (defined $conf->{"focr_bin_$a"}) { 
    644658                    errorlog("Cannot exec $a, skipping image"); 
    645                     next IMAGE
     659                    next
    646660                } 
    647661            } 
    648662            printf RAWERR qq(## $conf->{focr_bin_tifftopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     
    688702                ($score,$dinfo) = check_image_hash_db($digest, $whash, $$pic{fname}, $$pic{ctype}, $$pic{ftype}); 
    689703                if ($score > 0) { 
    690704                    infolog("Image in KNOWN_GOOD. Skipping OCR checks..."); 
    691                     next IMAGE
     705                    next
    692706                } 
    693707            } 
    694708            if ($digest eq '') { 
    695709                infolog("Empty Hash, skipping..."); 
    696                 next IMAGE
     710                next
    697711            } 
    698712        } else { 
    699713            infolog("Image hashing disabled in configuration, skipping..."); 
     
    711725 
    712726        my @ocr_results = (); 
    713727        my $scansets = get_scansets(); 
     728        my $newlist = ''; 
     729        foreach my $s (@$scansets) { 
     730            $newlist .= ' ' . $s->{label} . '(' . $s->{hit_counter} . ')'; 
     731        } 
     732        infolog("Scanset Order:$newlist"); 
    714733        my $mcnt = 0; 
    715734        my $modus = 0; 
    716735        my $modus_match = 0; 
     
    832851                            } 
    833852                        } 
    834853                    } 
    835                     infolog("Resorting scanset list..."); 
    836                     @$scansets = sort { $b->{hit_counter} <=> $a->{hit_counter} } @$scansets; 
     854 
    837855                } 
    838856                last; 
    839857            } 
  • FuzzyOcr/Config.pm

    old new  
    130130} 
    131131 
    132132sub get_scansets { 
     133    if ($conf->{focr_autosort_scanset}) { 
     134        @scansets = sort { $b->{hit_counter} <=> $a->{hit_counter} } @scansets; 
     135    } 
    133136    return \@scansets; 
    134137} 
    135138 
     
    194197            type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    195198        }); 
    196199    } 
     200    foreach my $t (qw/height width/) { 
     201        push (@cmds, { 
     202            setting => 'focr_min_'.$t, 
     203            default => 4, 
     204            type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
     205        }); 
     206    } 
    197207    push (@cmds, { 
    198208        setting => 'focr_threshold', 
    199209        default => 0.25, 
     
    245255    }); 
    246256 
    247257    push (@cmds, { 
     258        setting => 'focr_log_pmsinfo', 
     259        default => 1, 
     260        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
     261    }); 
     262 
     263    push (@cmds, { 
    248264        setting => 'focr_enable_image_hashing', 
    249265        default => 0, 
    250266        code => sub { 
  • FuzzyOcr/Hashing.pm

    old new  
    9494            $dinfo = $data[9] || ''; 
    9595            if ($data[2] eq '') { 
    9696                infolog("Updating $txt info File-Name:'$fname'"); 
    97                 $ddb->do(qq(update $db.$dbfile set $dbfile.fname='$fname' where $dbfile.key='$key')); 
     97                $ddb->do(qq(update $db.$dbfile set $dbfile.fname=? where $dbfile.key='$key'),undef,$fname); 
    9898            } 
    9999            if ($data[3] eq '') { 
    100100                infolog("Updating $txt info Content-Type:'$ctype'"); 
    101                 $ddb->do(qq(update $db.$dbfile set $dbfile.ctype='$ctype' where $dbfile.key='$key')); 
     101                $ddb->do(qq(update $db.$dbfile set $dbfile.ctype=? where $dbfile.key='$key'),undef,$ctype); 
    102102            } 
    103103            if ($data[4] != $ftype) { 
    104104                infolog("Updating $txt info File-Type:'$ftype'"); 
    105                 $ddb->do(qq(update $db.$dbfile set $dbfile.ftype='$ftype' where $dbfile.key='$key')); 
     105                $ddb->do(qq(update $db.$dbfile set $dbfile.ftype=? where $dbfile.key='$key'),undef,$ftype); 
    106106            } 
    107107        } 
    108108        unless ($match) { 
     
    134134            } 
    135135            infolog("Matched [$next] time(s). Prev match: ".fmt_time($now - $when)); 
    136136            $sql = qq(update $db.$dbfile set $dbfile.match='$next',$dbfile.check='$now' where $dbfile.key='$key'); 
    137             debuglog($sql,2); 
    138137            $ddb->do($sql); 
     138            debuglog($sql); 
    139139        } 
    140140        return ($ret,$dinfo); 
    141141    } 
     
    143143        use MLDBM qw(DB_File Storable); 
    144144        use MLDBM::Sync; 
    145145        my %DB = (); my $dbm; my $sdbm; 
    146         $sdbm = tie %DB, 'MLDBM::Sync', $dbfile, O_RDWR or $ret++; 
     146        $sdbm = tie %DB, 'MLDBM::Sync', $dbfile, O_CREAT|O_RDWR or $ret++; 
    147147        if ($ret>0) { 
    148148            warnlog("No Image Hash database found at \"$dbfile\", or permissions wrong."); 
    149149            return (0,''); 
     
    242242                if ($conf->{focr_mysql_update_hash}) { 
    243243                    infolog("Hash already in $db.$table updating..."); 
    244244                    $sql  = "update $db.$table set "; 
    245                     $sql .= "basic='$img',"   unless ($data[1] eq $img); 
    246                     $sql .= "fname='$fname'," unless ($data[2] eq $fname);  
    247                     $sql .= "ctype='$ctype'," unless ($data[3] eq $ctype);  
    248                     $sql .= "ftype='$ftype'," unless ($data[4] == $ftype); 
    249                     $sql .= "score='$score'," unless ($data[8] == $score); 
    250                     $sql .= "dinfo='$dinfo'," unless ($data[9] eq $dinfo); 
     245                    my @params; 
     246                    unless ($data[1] eq $img) { 
     247                        $sql .= "basic=?,"; push @params,$img; 
     248                    } 
     249                    unless ($data[2] eq $fname) { 
     250                        $sql .= "fname=?,"; push @params,$fname; 
     251                    } 
     252                    unless ($data[3] eq $ctype) { 
     253                        $sql .= "ctype=?,"; push @params,$ctype; 
     254                    } 
     255                    unless ($data[4] == $ftype) { 
     256                        $sql .= "ftype=?,"; push @params,$ftype; 
     257                    } 
     258                    unless ($data[8] == $score) { 
     259                        $sql .= "score=?,"; push @params,$score; 
     260                    } 
     261                    unless ($data[9] == $dinfo) { 
     262                        $sql .= "dinfo=?,"; push @params,$dinfo; 
     263                    } 
    251264                    $sql  =~ s/,$//; 
    252265                    $sql .= " where $table.key='$key'"; 
     266                    $ddb->do($sql,undef,@params); 
     267                    foreach my $p (@params) { $sql =~ s/\?/$p/; } 
    253268                    debuglog($sql); 
    254                     $ddb->do($sql); 
    255269                } else { 
    256270                    infolog("Hash already in $db.$table skipping..."); 
    257271                } 
    258272            } else { 
    259                 $sql = 
    260                     "insert into $db.$table values ('". $key 
    261                      . "','" . $img 
    262                      . "','" . $fname 
    263                      . "','" . $ctype 
    264                      . "','" . $ftype 
    265                      . "','" . ($table eq $conf->{focr_mysql_hash} ? 0 : 1) 
    266                      . "','" . time 
    267                      . "','" . time 
    268                      . "','" . $score 
    269                      . "','" . $dinfo 
    270                      . "')"; 
     273                my @params = ( 
     274                    $key, $img, $fname, $ctype, $ftype, 
     275                    ($table eq $conf->{focr_mysql_hash} ? 0 : 1), 
     276                    time, time, $score, $dinfo); 
     277                $sql = "insert into $db.$table values (?,?,?,?,?,?,?,?,?,?)"; 
     278                $ddb->do($sql,undef,@params); 
     279                foreach my $p (@params) { $sql =~ s/\?/$p/; } 
    271280                debuglog($sql); 
    272                 $ddb->do($sql); 
    273281            } 
    274282        } 
    275283    } 
Note: See TracBrowser for help on using the browser.