Changeset 42

Show
Ignore:
Timestamp:
20.11.2006 18:54:51 (2 years ago)
Author:
jorge
Message:

Don't need pamfile helper app any more, since we now determine size, height, width directly from the image header.
Fixed bug in FuzzyOcr?.pm where it did not save the file-type in the @hashes array, resulting in erroneous hash generation.
Make shure that the OCR application is executable when generating the scansets array, and skip a configuration if it cannot find an executable.
Updated default scanset configuration to include ocrad pass.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/devel/FuzzyOcr.pm

    r41 r42  
    4949    save_pms($pms); 
    5050 
    51     if ( $pms->get_score() > $conf->{'focr_autodisable_score'} ) { 
    52         debuglog("Scan canceled, message has already more than $conf->{'focr_autodisable_score'} points."); 
     51    if ( $pms->get_score() > $conf->{focr_autodisable_score} ) { 
     52        debuglog("Scan canceled, message has already more than $conf->{focr_autodisable_score} points."); 
    5353        return 0; 
    5454    } 
     
    6363    #debuglog("Starting FuzzyOcr..."); 
    6464    #debuglog("Attempting to load personal wordlist..."); 
    65     if ($conf->{"focr_personal_wordlist"} =~ m/^\//) { 
    66         load_personal_words( $conf->{"focr_personal_wordlist"} ); 
     65    if ($conf->{focr_personal_wordlist} =~ m/^\//) { 
     66        load_personal_words( $conf->{focr_personal_wordlist} ); 
    6767    } else { 
    6868        my $homedir  = (getpwuid($<))[7]; 
    6969        if ($homedir) { 
    70             load_personal_words( $homedir . "/$conf->{'focr_personal_wordlist'}" ); 
     70            load_personal_words( $homedir . "/$conf->{focr_personal_wordlist}" ); 
    7171        } elsif (defined($ENV{HOME})) { 
    72             load_personal_words( $ENV{HOME} . "/$conf->{'focr_personal_wordlist'}" ); 
     72            load_personal_words( $ENV{HOME} . "/$conf->{focr_personal_wordlist}" ); 
    7373        } else { 
    7474            debuglog("Variable \$ENV{HOME} not defined and getpwuid failed, personal wordlist function not available..."); 
     
    146146                if ($b != 0xff) { 
    147147                   debuglog("Invalid JPEG image"); 
     148                   $pos = $pdatalen + 1; 
    148149                   last; 
    149150                } 
     
    161162            } else { 
    162163                ($h,$w) = unpack("nn",substr($pdata,$pos+3,4)); 
    163             } 
    164             debuglog("JPEG: $imgfilename '${h}x${w}'")
    165             $imgfiles{$imgfilename}{ftype} = 2
    166             $imgfiles{$imgfilename}{height} = $h
    167             $imgfiles{$imgfilename}{width}  = $w; 
     164                debuglog("JPEG: $imgfilename '${h}x${w}'"); 
     165                $imgfiles{$imgfilename}{ftype} = 2
     166                $imgfiles{$imgfilename}{height} = $h
     167                $imgfiles{$imgfilename}{width}  = $w
     168            } 
    168169        } elsif ( substr($pdata,0,4) eq "\x89\x50\x4e\x47" ) { 
    169170            # PNG File 
     
    201202            $imgfiles{$imgfilename}{height} = $h ? $h : 1; 
    202203        } 
     204        next unless defined $imgfiles{$imgfilename}{ftype}; 
    203205        $imgfiles{$imgfilename}{fname} = $fname; 
    204206        $imgfiles{$imgfilename}{ctype} = $ctype; 
     
    218220    if ($cnt == 0) { 
    219221        debuglog("Skipping OCR, no image files found..."); 
    220         removedir($imgdir) if (defined($imgdir) and ($conf->{"focr_keep_bad_images"}<2)); 
     222        removedir($imgdir) if (defined($imgdir) and ($conf->{focr_keep_bad_images}<2)); 
    221223        return 0; 
    222224    } 
    223225    debuglog("Found: $cnt images"); $cnt = 0; 
    224     my $t = Mail::SpamAssassin::Timeout->new({ secs => $conf->{"focr_timeout"} }); 
     226    my $t = Mail::SpamAssassin::Timeout->new({ secs => $conf->{focr_timeout} }); 
    225227    my $retcode; 
    226228 
     
    247249                next IMAGE; 
    248250            } 
    249             if (defined($conf->{"focr_max_size_gif"}) and ($$pic{fsize} > $conf->{"focr_max_size_gif"})) { 
    250                 debuglog("GIF file size ($s) exceeds maximum file size for this format, skipping..."); 
     251            if (defined($conf->{focr_max_size_gif}) and ($$pic{fsize} > $conf->{focr_max_size_gif})) { 
     252                debuglog("GIF file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
    251253                next; 
    252254            } 
     
    268270 
    269271            $retcode = $t->run_and_catch(sub { 
    270                 @stdout_data = qx($conf->{"focr_bin_giftext"} $file); 
     272                @stdout_data = qx($conf->{focr_bin_giftext} $file); 
    271273            }); 
    272274            if ($retcode) { 
    273275                chomp $retcode; 
    274                 debuglog("$conf->{'focr_bin_giftext'} Timed out [$retcode], skipping..."); 
    275                 ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     276                debuglog("$conf->{focr_bin_giftext} Timed out [$retcode], skipping..."); 
     277                ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    276278            } 
    277279            foreach (@stdout_data) { 
     
    291293                debuglog("Image is single non-interlaced..."); 
    292294                $tfile .= "-fixed.gif"; 
    293                 printf RAWERR "## $conf->{'focr_bin_giffix'} $file >$tfile 2>>$efile\n" if ($haserr>0); 
     295                printf RAWERR "## $conf->{focr_bin_giffix} $file >$tfile 2>>$efile\n" if ($haserr>0); 
    294296                $retcode = $t->run_and_catch(sub { 
    295                     qx($conf->{"focr_bin_giffix"} $file >$tfile 2>>$efile); 
     297                    qx($conf->{focr_bin_giffix} $file >$tfile 2>>$efile); 
    296298                }); 
    297299                if ($retcode) { 
    298300                    chomp $retcode; 
    299                     debuglog("$conf->{'focr_bin_giffix'}: Timed out [$retcode], skipping..."); 
     301                    debuglog("$conf->{focr_bin_giffix}: Timed out [$retcode], skipping..."); 
    300302                    printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
    301                     ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     303                    ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    302304                } 
    303305                if (open ERR, $efile) { 
     
    316318                if ($interlaced_gif or ($image_count > 1)) { 
    317319                    debuglog("Skipping corrupted interlaced image..."); 
    318                     corrupt_img($conf->{'focr_corrupt_unfixable_score'}, $corrupt); 
     320                    corrupt_img($conf->{focr_corrupt_unfixable_score}, $corrupt); 
    319321                    next; 
    320322                } 
    321323                if (-z $tfile) { 
    322324                    debuglog("Uncorrectable corruption detected, skipping non-interlaced image..."); 
    323                     corrupt_img($conf->{'focr_corrupt_unfixable_score'}, $corrupt); 
     325                    corrupt_img($conf->{focr_corrupt_unfixable_score}, $corrupt); 
    324326                    next; 
    325327                } 
    326328                debuglog("Image is corrupt, but seems fixable, continuing..."); 
    327                 corrupt_img($conf->{'focr_corrupt_score'}, $corrupt); 
     329                corrupt_img($conf->{focr_corrupt_score}, $corrupt); 
    328330            } 
    329331 
     
    341343                    $tfile .= ".gif"; 
    342344                } 
    343                 printf RAWERR qq(## $conf->{'focr_bin_gifinter'} $cfile >$tfile 2>>$efile\n) if ($haserr>0); 
     345                printf RAWERR qq(## $conf->{focr_bin_gifinter} $cfile >$tfile 2>>$efile\n) if ($haserr>0); 
    344346                $retcode = $t->run_and_catch(sub{ 
    345                     qx($conf->{"focr_bin_gifinter"} $cfile >$tfile 2>>$efile); 
     347                    qx($conf->{focr_bin_gifinter} $cfile >$tfile 2>>$efile); 
    346348                }); 
    347349                if ($retcode) { 
    348350                    chomp $retcode; 
    349                     debuglog("$conf->{'focr_bin_gifinter'}: Timed out [$retcode], skipping..."); 
    350351                    printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
    351                     ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
    352                 } 
    353             } 
    354  
    355             printf RAWERR qq(## $conf->{'focr_bin_giftopnm'} $tfile >$pfile 2>>$efile\n) if ($haserr>0); 
     352                    debuglog("$conf->{focr_bin_gifinter}: Timed out [$retcode], skipping..."); 
     353                    ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
     354                } 
     355            } 
     356 
     357            printf RAWERR qq(## $conf->{focr_bin_giftopnm} $tfile >$pfile 2>>$efile\n) if ($haserr>0); 
    356358            $retcode = $t->run_and_catch(sub { 
    357                 qx($conf->{"focr_bin_giftopnm"} $tfile >$pfile 2>>$efile); 
     359                qx($conf->{focr_bin_giftopnm} $tfile >$pfile 2>>$efile); 
    358360            }); 
    359361            if ($retcode) { 
    360362                chomp $retcode; 
    361                 debuglog("$conf->{'focr_bin_giftopnm'}: Timed out [$retcode], skipping..."); 
    362363                printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
    363                 ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     364                debuglog("$conf->{focr_bin_giftopnm}: Timed out [$retcode], skipping..."); 
     365                ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    364366            } 
    365367        } 
     
    371373            } 
    372374 
    373             if (defined($conf->{"focr_max_size_jpeg"}) and ($$pic{fsize} > $conf->{"focr_max_size_jpeg"})) { 
    374                 debuglog("JPEG file size ($s) exceeds maximum file size for this format, skipping..."); 
     375            if (defined($conf->{focr_max_size_jpeg}) and ($$pic{fsize} > $conf->{focr_max_size_jpeg})) { 
     376                debuglog("JPEG file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
    375377                next; 
    376378            } 
     
    384386                } 
    385387            } 
    386             printf RAWERR qq(## $conf->{'focr_bin_jpegtopnm'} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     388            printf RAWERR qq(## $conf->{focr_bin_jpegtopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
    387389            $retcode = $t->run_and_catch(sub { 
    388                 qx($conf->{"focr_bin_jpegtopnm"} $file >$pfile 2>>$efile); 
     390                qx($conf->{focr_bin_jpegtopnm} $file >$pfile 2>>$efile); 
    389391            }); 
    390392            if ($retcode) { 
    391393                chomp $retcode; 
    392                 debuglog("$conf->{'focr_bin_jpegtopnm'}: Timed out [$retcode], skipping..."); 
    393                 ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     394                printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
     395                debuglog("$conf->{focr_bin_jpegtopnm}: Timed out [$retcode], skipping..."); 
     396                ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    394397            } 
    395398        } 
     
    400403                next IMAGE; 
    401404            } 
    402             if (defined($conf->{"focr_max_size_png"}) and ($$pic{fsize} > $conf->{"focr__max_size_png"})) { 
    403                 debuglog("PNG file size ($s) exceeds maximum file size for this format, skipping..."); 
     405            if (defined($conf->{focr_max_size_png}) and ($$pic{fsize} > $conf->{focr__max_size_png})) { 
     406                debuglog("PNG file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
    404407                next; 
    405408            } 
     
    413416                } 
    414417            } 
    415             printf RAWERR qq(## $conf->{'focr_bin_pngtopnm'} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     418            printf RAWERR qq(## $conf->{focr_bin_pngtopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
    416419            $retcode = $t->run_and_catch(sub { 
    417                 qx($conf->{"focr_bin_pngtopnm"} $file >$pfile 2>>$efile); 
     420                qx($conf->{focr_bin_pngtopnm} $file >$pfile 2>>$efile); 
    418421            }); 
    419422            if ($retcode) { 
    420423                chomp $retcode; 
    421                 debuglog("$conf->{'focr_bin_pngtopnm'}: Timed out [$retcode], skipping..."); 
    422424                printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
    423                 ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     425                debuglog("$conf->{focr_bin_pngtopnm}: Timed out [$retcode], skipping..."); 
     426                ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    424427            } 
    425428        } 
     
    430433                next IMAGE; 
    431434            } 
    432             if (defined($conf->{"focr_max_size_bmp"}) and ($$pic{fsize} > $conf->{"focr_max_size_bmp"})) { 
    433                 debuglog("BMP file size ($s) exceeds maximum file size for this format, skipping..."); 
     435            if (defined($conf->{focr_max_size_bmp}) and ($$pic{fsize} > $conf->{focr_max_size_bmp})) { 
     436                debuglog("BMP file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
    434437                next; 
    435438            } 
     
    443446                } 
    444447            } 
    445             printf RAWERR qq(## $conf->{'focr_bin_bmptopnm'} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     448            printf RAWERR qq(## $conf->{focr_bin_bmptopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
    446449            $retcode = $t->run_and_catch(sub { 
    447450                qx($conf->{"focr_bin_bmptopnm"} $file >$pfile 2>>$efile); 
     
    449452            if ($retcode) { 
    450453                chomp $retcode; 
    451                 debuglog("$conf->{'focr_bin_bmptopnm'}: Timed out [$retcode], skipping..."); 
    452454                printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
    453                 ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     455                debuglog("$conf->{focr_bin_bmptopnm}: Timed out [$retcode], skipping..."); 
     456                ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    454457            } 
    455458        } 
     
    460463                next IMAGE; 
    461464            } 
    462             if (defined($conf->{"focr_max_size_tiff"}) and ($$pic{fsize} > $conf->{"focr_max_size_tiff"})) { 
    463                 debuglog("TIFF file size ($s) exceeds maximum file size for this format, skipping..."); 
     465            if (defined($conf->{focr_max_size_tiff}) and ($$pic{fsize} > $conf->{focr_max_size_tiff})) { 
     466                debuglog("TIFF file size ($$pic{fsize}) exceeds maximum file size for this format, skipping..."); 
    464467                next; 
    465468            } 
     
    468471            } 
    469472 
    470             printf RAWERR qq(## $conf->{'focr_bin_tifftopnm'} $file >$pfile 2>>$efile\n) if ($haserr>0); 
     473            foreach my $a (qw/tifftopnm/) { 
     474                unless (defined $conf->{"focr_bin_$a"}) { 
     475                    debuglog("Cannot exec $a, skipping image"); 
     476                    next IMAGE; 
     477                } 
     478            } 
     479            printf RAWERR qq(## $conf->{focr_bin_tifftopnm} $file >$pfile 2>>$efile\n) if ($haserr>0); 
    471480            $retcode = $t->run_and_catch(sub { 
    472481                qx($conf->{'focr_bin_tifftopnm'} $file >$pfile 2>>$efile); 
     
    474483            if ($retcode) { 
    475484                chomp $retcode; 
    476                 debuglog("$conf->{'focr_bin_tifftopnm'}: Timed out [$retcode], skipping..."); 
    477485                printf RAWERR "?? Timed out > $retcode\n" if ($haserr>0); 
    478                 ++$imgerr if $conf->{"focr_keep_bad_images"}>0; next; 
     486                debuglog("$conf->{focr_bin_tifftopnm}: Timed out [$retcode], skipping..."); 
     487                ++$imgerr if $conf->{focr_keep_bad_images}>0; next; 
    479488            } 
    480489        } 
     
    484493        } 
    485494 
    486         if($conf->{"focr_enable_image_hashing"}) { 
     495        if($conf->{focr_enable_image_hashing}) { 
    487496            debuglog("Calculating the image hash: $pfile"); 
    488             ($corrupt, $digest) = calc_image_hash($pfile); 
     497            ($corrupt, $digest) = calc_image_hash($pfile,$pic); 
    489498            if ($corrupt) { 
    490499                debuglog("Error calculating the image hash, skipping hash check..."); 
     
    497506                if ($score > 0) { 
    498507                    known_img_hash($score,$dinfo); 
    499                     debuglog("Message is SPAM. $dinfo")
     508                    debuglog("Message is SPAM. $dinfo") if ($conf->{focr_enable_image_hashing} < 3)
    500509                    removedir($imgdir); 
    501510                    return 0; 
     
    521530        foreach my $scanset (@$scansets) { 
    522531            my $scan = $scanset; 
    523             $scan =~ s/\$gocr/$conf->{"focr_bin_gocr"}/; 
    524             $scan =~ s/\$ocrad/$conf->{"focr_bin_ocrad"}/; 
     532            $scan =~ s/\$gocr/$conf->{focr_bin_gocr}/; 
     533            $scan =~ s/\$ocrad/$conf->{focr_bin_ocrad}/; 
    525534            $scan =~ s/\$pfile/$pfile/; 
    526535            $scan =~ s/\$efile/$efile/g; 
     
    577586            $cnt  += $wcnt; 
    578587            $mcnt += $wcnt; 
    579             if ( ( $conf->{"focr_verbose"} > 0 ) and ($wcnt) ) { 
     588            if ( ( $conf->{focr_verbose} > 0 ) and ($wcnt) ) { 
    580589                push( @found, "\"$w\" in $wcnt lines" ); 
    581590            } 
    582591        } 
    583         if ($conf->{"focr_enable_image_hashing"}) { 
    584             my $info = join('::',$mcnt,$$pic{fname},$$pic{ctype},$digest); 
     592        if ($conf->{focr_enable_image_hashing}) { 
     593            my $info = join('::',$mcnt,$$pic{fname},$$pic{ctype},$$pic{ftype},$digest); 
    585594            push(@hashes, $info); 
    586595        } 
     
    589598     
    590599    if ($cnt == 0) { 
    591         if ($conf->{"focr_enable_image_hashing"} > 1 and @hashes) { 
     600        if ($conf->{focr_enable_image_hashing} > 1 and @hashes) { 
    592601            debuglog("Message is ham, saving..."); 
    593602            foreach my $h (@hashes) { 
     
    606615            . join( "\n", @found ) 
    607616            . "\n($cnt word occurrences found)" ); 
    608         if ($cnt >= $conf->{"focr_counts_required"}) { 
    609             $score = sprintf "%0.3f", $conf->{'focr_base_score'} + (( $cnt - $conf->{"focr_counts_required"} ) * $conf->{'focr_add_score'} ); 
     617        if ($cnt >= $conf->{focr_counts_required}) { 
     618            $score = sprintf "%0.3f", $conf->{focr_base_score} + (( $cnt - $conf->{focr_counts_required} ) * $conf->{focr_add_score} ); 
    610619            debuglog("Message is spam, score = $score"); 
    611620        } else { 
    612             $score = sprintf("%0.3f", $conf->{'focr_add_score'} * $cnt) if $conf->{"focr_score_ham"}; 
     621            $score = sprintf("%0.3f", $conf->{focr_add_score} * $cnt) if $conf->{focr_score_ham}; 
    613622            debuglog("Message is ham, score = $score"); 
    614623        } 
    615         if ($conf->{"focr_enable_image_hashing"} and 
    616             $conf->{"focr_hashing_learn_scanned"} and 
     624        if ($conf->{focr_enable_image_hashing} and 
     625            $conf->{focr_hashing_learn_scanned} and 
    617626            $score > 0) { 
    618627            foreach my $h (@hashes) { 
     
    625634            } 
    626635        } 
    627         if ( $conf->{"focr_verbose"} > 0 ) { 
     636        if ( $conf->{focr_verbose} > 0 and $conf->{focr_verbose} < 3 ) { 
    628637            debuglog($debuginfo); 
    629638        } 
     
    634643            $pms->{conf}->{descriptions}->{FUZZY_OCR} . "\n$debuginfo" ); 
    635644    } 
    636     if ($imgerr == 0 and $conf->{"focr_keep_bad_images"}<2) { 
     645    if ($imgerr == 0 and $conf->{focr_keep_bad_images}<2) { 
    637646        removedir($imgdir); 
    638647    } 
  • trunk/devel/FuzzyOcr/Config.pm

    r41 r42  
    125125                return $Mail::SpamAssassin::Conf::INVALID_VALUE; 
    126126            } 
    127             $self->{'focr_verbose'} = $value+0; 
     127            $self->{focr_verbose} = $value+0; 
    128128        } 
    129129    }); 
     
    151151                return $Mail::SpamAssassin::Conf::INVALID_VALUE; 
    152152            } 
    153             $self->{'focr_enable_image_hashing'} = $value+0; 
     153            $self->{focr_enable_image_hashing} = $value+0; 
    154154        } 
    155155       }); 
     
    208208                return $Mail::SpamAssassin::Conf::INVALID_VALUE; 
    209209            } 
    210             $self->{'focr_keep_bad_images'} = $value+0; 
     210            $self->{focr_keep_bad_images} = $value+0; 
    211211        } 
    212212       }); 
     
    281281    push (@cmds, { 
    282282        setting => 'focr_scansets', 
    283         default => '$gocr -i $pfile, $gocr -l 180 -d 2 -i $pfile, $gocr -l 140 -d 2 -i $pfile', 
     283        default => '$gocr -i $pfile, $gocr -l 180 -d 2 -i $pfile, $ocrad -s5 -T 0.5 $pfile', 
    284284        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    285285        }); 
     
    334334    my ($self, $opts) = @_; 
    335335    $conf = $opts->{conf}; 
    336  
    337     # Use specified scansets 
    338     @scansets = split(/,\s*/, $conf->{'focr_scansets'}); 
    339     debuglog("Using scan: $_") foreach @scansets; 
    340336 
    341337    # find external binaries 
     
    360356        } 
    361357    } 
     358 
     359    # Use specified scansets 
     360    @scansets = split(/,\s*/, $conf->{'focr_scansets'}); 
     361    foreach my $scan (@scansets) { 
     362        if ($scan =~ m/(gocr|ocrad)/) { 
     363            next unless -x $conf->{"focr_bin_$1"}; 
     364        } 
     365        debuglog("Using scan: $scan"); 
     366    } 
     367 
    362368    if ($conf->{focr_enable_image_hashing} == 2 and -r $conf->{focr_digest_db}) { 
    363369        my %DB; my $dbm; my $err = 0; 
    364370        my $now = time - ($conf->{focr_db_max_days}*86400); 
     371        debuglog($conf->{focr_db_hash}); 
    365372        tie %DB, 'MLDBM', $conf->{focr_db_hash} or $err++; 
    366373        if ($err) { 
     
    571578    $logtext =~ s/\n/\n                      /g; 
    572579 
    573     unless ( open LOGFILE, ">>", $conf->{'focr_logfile'} ) { 
    574        warn "Can't open $conf->{'focr_logfile'} for writing, check permissions"; 
     580    unless ( open LOGFILE, ">>", $conf->{focr_logfile} ) { 
     581       warn "Can't open $conf->{focr_logfile} for writing, check permissions"; 
    575582    } 
    576583    flock( LOGFILE, LOCK_EX ); 
     
    586593    my $limit = defined $_[1] ? $_[1] : 1; 
    587594    foreach (@lines) { dbg ("FuzzyOcr: $_"); } 
    588     if ( $conf->{"focr_verbose"} > $limit ) { 
    589         logfile($_[0]) if defined $conf->{"focr_logfile"}; 
     595    if ( $conf->{focr_verbose} > $limit ) { 
     596        logfile($_[0]) if defined $conf->{focr_logfile}; 
    590597    } 
    591598} 
  • trunk/devel/FuzzyOcr/Hashing.pm

    r41 r42  
    7878        my $db   = $conf->{focr_mysql_db}; 
    7979        my $sql  = qq(select * from $db.$dbfile where $dbfile.key='$key'); 
    80         debuglog($sql); 
    8180        my @data = $ddb->selectrow_array($sql); 
    8281        my $next = 0; 
    8382        my $when = 0; 
    8483        if (scalar(@data)>0) { 
    85             debuglog("Found: Score='$data[8]' Info: '$data[9]'"); 
     84            debuglog("Found[$dbfile]: Score='$data[8]' Info: '$data[9]'"); 
    8685            $next  = $data[5]; $next++; 
    87             $when  = $data[6]; $data[8] += 0; 
     86            $when  = $data[7]; $data[8] += 0; 
    8887            $ret   = $data[8] == 0 ? 0.001 : $data[8]; 
    8988            $dinfo = $data[9] || ''; 
     
    101100            } 
    102101        } else { 
    103             my $then = time - ($conf->{"focr_db_max_days"}*86400); 
     102            my $then = time - ($conf->{focr_db_max_days}*86400); 
    104103            $sql = qq(select * from $db.$dbfile); 
    105104            my $sth  = $ddb->prepare($sql); $sth->execute; 
     
    112111                    $next  = $row[5] + 1; 
    113112                    $when  = $row[7] || $now; 
    114                     $ret   = $dbfile eq $conf->{"focr_mysql_hash"} ? $row[8] : $row[5]; 
     113                    $ret   = $dbfile eq $conf->{focr_mysql_hash} ? $row[8] : $row[5]; 
    115114                    $dinfo = $row[9] || ''; 
    116                     debuglog("Found in Table:'$dbfile'"); 
     115                    debuglog("Found[$dbfile]: Score='$row[8]' Info: '$row[9]'"); 
    117116                    last; 
    118117                } 
     
    124123        } 
    125124        if ($ret > 0) { 
    126             if ($dbfile eq $conf->{"focr_mysql_hash"}) { 
     125            if ($dbfile eq $conf->{focr_mysql_hash}) { 
    127126                debuglog("Found Score <$ret> for $txt Image Hash"); 
    128127            } 
    129             debuglog("Matched [$next] time(s). Prev match: ".fmt_time($when)); 
    130             $sql = qq(update $conf->{focr_mysql_db}.$dbfile set $dbfile.match='$next',$dbfile.check='$now' where $dbfile.key='$key'); 
     128            debuglog("Matched [$next] time(s). Prev match: ".fmt_time($now - $when)); 
     129            $sql = qq(update $db.$dbfile set $dbfile.match='$next',$dbfile.check='$now' where $dbfile.key='$key'); 
    131130            debuglog($sql); 
    132131            $ddb->do($sql); 
     
    134133        return ($ret,$dinfo); 
    135134    } 
    136     elsif ($conf->{"focr_enable_image_hashing"} == 2) { 
     135    elsif ($conf->{focr_enable_image_hashing} == 2) { 
    137136        my %DB = (); my $dbm; 
    138137        tie %DB, 'MLDBM', $dbfile, O_RDWR or $ret++; 
     
    153152        } 
    154153        if ($ret == 0) { 
    155             my $then = time - ($conf->{"focr_db_max_days"}*86400); 
     154            my $then = time - ($conf->{focr_db_max_days}*86400); 
    156155            foreach my $k (keys %DB) { 
    157156                $dbm  = $DB{$k}; 
    158157                $hash = $dbm->{basic} ? $dbm->{basic} : "0:0:0:0::$k"; 
    159158                if (within_threshold($digest,$hash)) { 
    160                     $ret  = $dbfile eq $conf->{"focr_db_hash"} ? $dbm->{score} : $dbm->{match}; 
     159                    $ret  = $dbfile eq $conf->{focr_db_hash} ? $dbm->{score} : $dbm->{match}; 
    161160                    $txt  = 'Approx'; $dinfo = $dbm->{dinfo} || ''; 
    162161                    debuglog("Found in: <$dbfile>"); 
     
    166165                $dbm->{check} = $now - 1 unless defined $dbm->{check}; 
    167166                if ($dbm->{check} < $then) { 
    168                     debuglog("Expiring <$k> older than $conf->{'focr_db_max_days'} days"); 
     167                    debuglog("Expiring <$k> older than $conf->{focr_db_max_days} days"); 
    169168                    delete $DB{$k}; 
    170169                } 
     
    173172        if ($ret>0) { 
    174173            $dbm->{match}++; 
    175             if ($dbfile eq $conf->{"focr_db_hash"}) { 
     174            if ($dbfile eq $conf->{focr_db_hash}) { 
    176175                $ret = sprintf("%0.3f",$dbm->{score}); 
    177176                debuglog("Found Score <$ret> for $txt Image Hash"); 
     
    184183        return ($ret,$dinfo); 
    185184    } 
    186     elsif ($conf->{"focr_enable_image_hashing"} == 1) { 
    187         $ret = open HASH, $conf->{"focr_digest_db"}; 
     185    elsif ($conf->{focr_enable_image_hashing} == 1) { 
     186        $ret = open HASH, $conf->{focr_digest_db}; 
    188187        unless($ret) { 
    189             debuglog("No Image Hash database found at \"$conf->{'focr_digest_db'}\", or permissions wrong."); 
     188            debuglog("No Image Hash database found at \"$conf->{focr_digest_db}\", or permissions wrong."); 
    190189            return (0,''); 
    191190        } 
     
    225224            my @data = $ddb->selectrow_array($sql); 
    226225            unless (scalar(@data)) { 
    227                 $sql = "insert into $db.$table values ("; 
    228                 $sql .= "'$key','$img','$_[3]','$_[4]',"; 
    229                 $sql .= sprintf ("'%d','%d','%d','%d','%d','%s')", 
    230                     defined($_[5]) ? $_[5] : 0, 
    231                     $table eq $conf->{focr_mysql_hash} ? 0 : 1, 
    232                     time,time,$score, 
    233                     defined($_[6]) ? $_[6] : '' 
    234                 ); 
     226                $sql = 
     227                    "insert into $db.$table values ('$key','$img','$_[3]','$_[4]','" . 
     228                     defined($_[5]) ? $_[5] : 0 . "','" . 
     229                     $table eq $conf->{focr_mysql_hash} ? 0 : 1 . "','" . 
     230                     time . "','" .  time . "','" .  $score . "'.'" . 
     231                     defined($_[6]) ? $_[6] : '' . "')"; 
    235232                debuglog($sql); 
    236233                $ddb->do($sql); 
    237234            } else { 
    238235                debuglog("Hash already in $db.$table skipping..."); 
    239             } 
    240         } 
    241     } 
    242     elsif ($conf->{"focr_enable_image_hashing"} == 2) { 
    243         my $dbfile = $_[2] || $conf->{"focr_db_hash"}; 
     236                debuglog("Key: $key"); 
     237            } 
     238        } 
     239    } 
     240    elsif ($conf->{focr_enable_image_hashing} == 2) { 
     241        my $dbfile = $_[2] || $conf->{focr_db_hash}; 
    244242        my %DB = (); 
    245243        tie %DB, 'MLDBM', $dbfile or $ret++; 
     
    260258            $dbm->{input} =  
    261259            $dbm->{check} = time; 
    262             $dbm->{match} = $dbfile eq $conf->{"focr_db_hash"} ? 0 : 1; 
     260            $dbm->{match} = $dbfile eq $conf->{focr_db_hash} ? 0 : 1; 
    263261            $DB{$key} = $dbm; 
    264262        } 
    265263        untie %DB; 
    266264    } 
    267     elsif ($conf->{"focr_enable_image_hashing"} == 1) { 
    268         if (-e $conf->{"focr_digest_db"}) { 
    269             $ret = open DB, ">>$conf->{'focr_digest_db'}"; 
     265    elsif ($conf->{focr_enable_image_hashing} == 1) { 
     266        if (-e $conf->{focr_digest_db}) { 
     267            $ret = open DB, ">>$conf->{focr_digest_db}"; 
    270268        } else { 
    271             $ret = open DB,  ">$conf->{'focr_digest_db'}"; 
     269            $ret = open DB,  ">$conf->{focr_digest_db}"; 
    272270        } 
    273271        unless ($ret) { 
    274             debuglog("Unable to open/create Image Hash database at \"$conf->{'focr_digest_db'}\", check permissions."); 
     272            debuglog("Unable to open/create Image Hash database at \"$conf->{focr_digest_db}\", check permissions."); 
    275273            return; 
    276274        } 
    277         debuglog("Adding Hash to \"$conf->{'focr_digest_db'}\""); 
     275        debuglog("Adding Hash to \"$conf->{focr_digest_db}\""); 
    278276        flock( DB, LOCK_EX ); 
    279277        seek( DB, 0, 2 ); 
     
    290288    my %Threshold = %$thresref; 
    291289    my $pfile = $_[0]; 
     290    my $pic   = $_[1]; 
    292291    my ($rcode, $hash); 
    293292 
    294     foreach my $a (qw/pamfile ppmhist/) { 
     293    foreach my $a (qw/ppmhist/) { #pamfile 
    295294        unless (defined $conf->{"focr_bin_$a"}) { 
    296295            info("FuzzyOcr: calc_image_hash cannot exec $a"); 
     
    305304 
    306305    my @stdout_data; 
    307     my ($w,$h,$s,$t) = (0,0,0,0); 
    308     $t = Mail::SpamAssassin::Timeout->new({ secs => $conf->{"focr_timeout"} }); 
     306    my $t = Mail::SpamAssassin::Timeout->new({ secs => $conf->{focr_timeout} }); 
    309307    $rcode = $t->run_and_catch(sub { 
    310         @stdout_data = qx($conf->{"focr_bin_pamfile"} $pfile 2>/dev/null); 
     308        @stdout_data = qx($conf->{focr_bin_ppmhist} -noheader $pfile 2>/dev/null); 
    311309    }); 
    312310    if ($rcode) { 
    313311        chomp $rcode; 
    314         debuglog("$conf->{'focr_bin_pamfile'}: Timed out [$rcode], skipping..."); 
    315         return (1, ''); 
    316     } 
    317     if ($stdout_data[0] =~ m/(\d+) by (\d+)/) { 
    318         $w = $1; $h = $2; 
    319         $s = (stat($pfile))[7]; 
    320     } 
    321  
    322     $rcode = $t->run_and_catch(sub { 
    323         @stdout_data = qx($conf->{'focr_bin_ppmhist'} -noheader $pfile 2>/dev/null); 
    324     }); 
    325     if ($rcode) { 
    326         chomp $rcode; 
    327         debuglog("$conf->{'focr_bin_ppmhist'}: Timed out [$rcode], skipping..."); 
     312        debuglog("$conf->{focr_bin_ppmhist}: Timed out [$rcode], skipping..."); 
    328313        return (1, ''); 
    329314    } 
    330315    my $cnt = 0; 
    331316    my $c = scalar(@stdout_data); 
    332     $hash = sprintf "%d:%d:%d:%d",$s,$h,$w,$c; 
     317    $hash = sprintf "%d:%d:%d:%d", 
     318        defined $pic->{fsize}  ? $pic->{fsize}  : 0, 
     319        defined $pic->{height} ? $pic->{height} : 0, 
     320        defined $pic->{width}  ? $pic->{width}  : 0, 
     321        $c; 
    333322    if ($Threshold{max_hash}) { 
    334323        foreach (@stdout_data) { 
     
    346335 
    347336sub fmt_time { 
    348     my $when = time - $_[0]; 
     337    my $when = $_[0]; 
    349338    my $ret; 
    350339 
     340    debuglog("fmt_time: When =  $when"); 
    351341    if ($when>86400) { 
    352342        my $d = int($when/86400); 
     
    357347        my $h = int($when/3600); 
    358348        $when -= $h*3600; 
    359         $ret .= " $h hours"; 
     349        $ret .= " $h hrs."; 
    360350    } 
    361351    if ($when>60) { 
    362352        my $m = int($when/60); 
    363353        $when -= $m*60; 
    364         $ret .= " $m minutes"; 
     354        $ret .= " $m min."; 
    365355    } 
    366356    if ($when>0) { 
    367         $ret .= " $when seconds"; 
     357        $ret .= " $when sec."; 
    368358    } 
    369359    $ret .= " ago";