Changeset 68

Show
Ignore:
Timestamp:
30.11.2006 00:36:22 (2 years ago)
Author:
jorge
Message:

SVN fixed.
load_global_words and load_personal_words replaced with read_words

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk

    • Property svn:ignore set to
      devel2
  • trunk/devel/FuzzyOcr.pm

    r67 r68  
    1414use Mail::SpamAssassin::Plugin; 
    1515 
    16 use Time::HiRes qw( time usleep ualarm gettimeofday tv_interval ); 
     16use Time::HiRes qw( gettimeofday tv_interval ); 
    1717use String::Approx 'adistr'; 
    1818use FileHandle; 
    1919use Fcntl ':flock'; 
     20use POSIX; 
    2021 
    2122use lib qw(. /etc/mail/spamassassin); # Allow placing of FuzzyOcr in siteconfigdir 
    2223 
    23 use FuzzyOcr::Config qw(kill_pid get_tmpdir set_tmpdir get_pms save_pms get_timeout get_ddb get_thresholds get_scansets get_config get_wordlist set_config finish_parsing_end parse_config load_global_words load_personal_words debuglog logfile); 
    24 use FuzzyOcr::Hashing qw(check_image_hash_db check_image_hash_db add_image_hash_db calc_image_hash); 
     24use FuzzyOcr::Logging qw(debuglog); 
     25use FuzzyOcr::Config qw(kill_pid 
     26    get_tmpdir 
     27    set_tmpdir 
     28    get_pms 
     29    save_pms 
     30    get_timeout 
     31    get_mysql_ddb 
     32    get_scansets 
     33    get_wordlist 
     34    set_config 
     35    get_config 
     36    finish_parsing_end 
     37    read_words); 
     38use FuzzyOcr::Hashing qw(check_image_hash_db add_image_hash_db calc_image_hash); 
    2539use FuzzyOcr::Deanimate qw(deanimate); 
    2640use FuzzyOcr::Scoring qw(wrong_ctype corrupt_img known_img_hash); 
     
    4660 
    4761sub fuzzyocr_check { 
     62    my ( $self, $pms ) = @_; 
    4863    my $conf = get_config(); 
    49     my ( $self, $pms ) = @_; 
    5064 
    5165    save_pms($pms); 
     
    8498    my ( $self, $conf, $pms ) = @_; 
    8599 
    86     if ( $pms->get_score() > $conf->{focr_autodisable_score} ) { 
    87         debuglog("Scan canceled, message has already more than $conf->{focr_autodisable_score} points."); 
     100    my $current_score = $pms->get_score(); 
     101    my $score = $conf->{focr_autodisable_score} || 100; 
     102 
     103    if ( $current_score > $score ) { 
     104        debuglog("Scan canceled, message has already more than $score points ($current_score)."); 
    88105        return 0; 
    89106    } 
    90107 
    91     if ( $pms->get_score() < $conf->{focr_autodisable_negative_score} ) { 
    92         debuglog("Scan canceled, message has less than $conf->{focr_autodisable_negative_score} points."); 
     108    $score = $conf->{focr_autodiable_negative_score} || -100; 
     109    if ( $current_score < $score ) { 
     110        debuglog("Scan canceled, message has less than $score points ($current_score)."); 
    93111        return 0; 
    94112    } 
     
    101119    my $cnt      = 0; 
    102120    my $imgerr   = 0; 
    103     my $main = $self->{main}; 
     121    my $main     = $self->{main}; 
    104122 
    105123    debuglog("Starting FuzzyOcr..."); 
    106124    debuglog("Attempting to load personal wordlist..."); 
    107125    if ($conf->{focr_personal_wordlist} =~ m/^\//) { 
    108         load_personal_words( $conf->{focr_personal_wordlist} ); 
     126        read_words( $conf->{focr_personal_wordlist} ); 
    109127    } else { 
    110         my $peruserlist  = $main->sed_path($conf->{focr_personal_wordlist}); 
    111         unless ($peruserlist) { 
     128        my $peruserlist = $main->sed_path($conf->{focr_personal_wordlist}); 
     129        if (-r $peruserlist) { 
     130            read_words($peruserlist); 
     131        } else { 
    112132            debuglog("Error getting personal wordlist, skipping..."); 
    113133        } 
    114         load_personal_words($peruserlist); 
    115134    } 
    116135 
     
    264283    debuglog("Found: $cnt images"); $cnt = 0; 
    265284    if ($conf->{focr_enable_image_hashing} == 3) { 
    266         $ddb = $conf->{focr_ddb} = get_ddb(); 
     285        $ddb = $conf->{focr_ddb} = get_mysql_ddb(); 
    267286    } 
    268287 
     
    310329            my ($retcode, @stdout_data) = save_execute( 
    311330                "$conf->{focr_bin_giftext} $file", 
    312                undef, 
     331                undef, 
    313332                ">$imgdir/giftext.info", 
    314333                ">>$imgdir/giftext.err", 1); 
     
    389408                printf RAWERR qq(## $conf->{focr_bin_gifinter} $cfile >$tfile 2>>$efile\n) if ($haserr>0); 
    390409         
    391                $retcode = save_execute("$conf->{focr_bin_gifinter} $cfile", undef, ">$tfile", ">>$efile"); 
     410                $retcode = save_execute("$conf->{focr_bin_gifinter} $cfile", undef, ">$tfile", ">>$efile"); 
    392411 
    393412                if ($retcode<0) { 
     
    406425            printf RAWERR qq(## $conf->{focr_bin_giftopnm} $tfile >$pfile 2>>$efile\n) if ($haserr>0); 
    407426 
    408            $retcode = save_execute("$conf->{focr_bin_giftopnm} $tfile", undef, ">$pfile", ">>$efile"); 
     427            $retcode = save_execute("$conf->{focr_bin_giftopnm} $tfile", undef, ">$pfile", ">>$efile"); 
    409428 
    410429            if ($retcode<0) { 
  • trunk/devel/FuzzyOcr.preps

    r60 r68  
    1010} 
    1111 
     12# requires ImageMagic convert 
    1213preprocessor maketiff { 
    1314    command = convert $input tiff:$output 
  • trunk/devel/FuzzyOcr.scansets

    r62 r68  
    88scanset ocrad-invert { 
    99ocr_command = $ocrad -s5 -i $input 
     10} 
     11 
     12scanset gocr { 
     13ocr_command = $gocr -i $input 
     14} 
     15 
     16scanset gocr-180 { 
     17ocr_command = $gocr -l 180 -d 2 -i $input 
    1018} 
    1119 
  • trunk/devel/FuzzyOcr/Config.pm

    r67 r68  
    22package FuzzyOcr::Config; 
    33 
    4 use FuzzyOcr::Logging qw(debuglog logfile); 
    5  
     4use lib qw(..); 
     5use FuzzyOcr::Logging qw(debuglog); 
    66use FuzzyOcr::Scanset; 
    77use FuzzyOcr::Preprocessor; 
    8  
     8use Mail::SpamAssassin::Logger; 
    99 
    1010use base 'Exporter'; 
    11 our @EXPORT_OK = qw( 
     11our @EXPORT_OK = qw/ 
     12    parse_config 
     13    finish_parsing_end 
     14    get_config  
     15    set_config  
    1216    set_pid 
    1317    unset_pid 
     
    2125    get_preprocessor  
    2226    get_thresholds  
    23     get_config  
    2427    get_wordlist  
    25     set_config  
    26     get_ddb 
    27     finish_parsing_end  
    28     parse_config  
    29     load_global_words  
    30     load_personal_words  
    31     debuglog  
    32     logfile); 
    33  
    34 use Fcntl ':flock'; 
    35 use POSIX; 
     28    get_mysql_ddb 
     29    read_words  
     30    /; 
    3631 
    3732use constant HAS_DBI => eval { require DBI; }; 
     
    150145} 
    151146 
    152 sub get_ddb { 
     147sub get_mysql_ddb { 
    153148    return undef unless (HAS_DBI and HAS_DBD_MYSQL); 
    154149    use DBI; 
     
    205200        default => 10, 
    206201        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    207        }); 
     202    }); 
    208203 
    209204    push (@cmds, { 
     
    211206        default => 0, 
    212207        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    213        }); 
     208    }); 
    214209 
    215210    push (@cmds, { 
    216211        setting => 'focr_logfile', 
    217212        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    218        }); 
     213    }); 
    219214 
    220215    push (@cmds, { 
     
    222217        default => 0, 
    223218        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    224        }); 
     219    }); 
    225220 
    226221    push (@cmds, { 
     
    237232            $self->{focr_enable_image_hashing} = $value+0; 
    238233        } 
    239        }); 
     234    }); 
    240235 
    241236    push (@cmds, { 
     
    243238        default => 1, 
    244239        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    245        }); 
     240    }); 
     241 
     242    push (@cmds, { 
     243        setting => 'focr_skip_updates', 
     244        default => 0, 
     245        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
     246    }); 
    246247 
    247248    push (@cmds, { 
    248249        setting => 'focr_digest_db', 
    249         default => "/etc/mail/spamassassin/FuzzyOcr.hashdb", 
    250         type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    251        }); 
     250        default => "__local_rules_dir__/FuzzyOcr.hashdb", 
     251        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
     252    }); 
    252253 
    253254    push (@cmds, { 
    254255        setting => 'focr_global_wordlist', 
    255         default => "/etc/mail/spamassassin/FuzzyOcr.words", 
    256         type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    257        }); 
     256        default => "__local_rules_dir__/FuzzyOcr.words", 
     257        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
     258    }); 
    258259 
    259260    push (@cmds, { 
     
    261262        default => "__userstate__/FuzzyOcr.words", 
    262263        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    263        }); 
     264    }); 
    264265 
    265266    push (@cmds, { 
    266267        setting => 'focr_db_hash', 
    267         default => "/etc/mail/spamassassin/FuzzyOcr.db", 
    268         type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    269        }); 
     268        default => "__local_rules_dir__/FuzzyOcr.db", 
     269        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
     270    }); 
    270271 
    271272    push (@cmds, { 
    272273        setting => 'focr_db_safe', 
    273         default => "/etc/mail/spamassassin/FuzzyOcr.safe.db", 
    274         type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    275        }); 
     274        default => "__local_rules_dir__/FuzzyOcr.safe.db", 
     275        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
     276    }); 
    276277 
    277278    push (@cmds, { 
     
    279280        default => 35, 
    280281        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    281        }); 
     282    }); 
    282283 
    283284    push (@cmds, { 
     
    294295            $self->{focr_keep_bad_images} = $value+0; 
    295296        } 
    296        }); 
     297    }); 
    297298 
    298299    push (@cmds, { 
     
    300301        default => 0, 
    301302        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    302        }); 
     303    }); 
    303304 
    304305    push (@cmds, { 
     
    306307        default => 5, 
    307308        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    308        }); 
     309    }); 
    309310 
    310311    push (@cmds, { 
     
    312313        default => 1, 
    313314        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    314        }); 
     315    }); 
    315316 
    316317    push (@cmds, { 
     
    318319        default => 2.5, 
    319320        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    320        }); 
     321    }); 
    321322 
    322323    push (@cmds, { 
     
    324325        default => 5, 
    325326        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    326        }); 
     327    }); 
    327328 
    328329    push (@cmds, { 
     
    330331        default => 1.5, 
    331332        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    332        }); 
     333    }); 
    333334 
    334335    push (@cmds, { 
     
    336337        default => 10, 
    337338        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    338        }); 
     339    }); 
    339340 
    340341    push (@cmds, { 
     
    342343        default => -5, 
    343344        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    344        }); 
     345    }); 
    345346 
    346347    push (@cmds, { 
     
    348349        default => '/usr/local/netpbm/bin:/usr/local/bin:/usr/bin', 
    349350        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    350        }); 
     351    }); 
    351352 
    352353    foreach (@bin_utils) { 
     
    354355            setting => 'focr_bin_'.$_, 
    355356            type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    356            }); 
     357        }); 
    357358    } 
    358359 
     
    362363            default => 0, 
    363364            type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    364             }); 
     365        }); 
    365366        push (@cmds, { 
    366367            setting => 'focr_max_size_'.$_, 
    367368            type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    368             }); 
     369        }); 
    369370    } 
    370371 
    371372    push (@cmds, { 
    372373        setting => 'focr_scanset_file', 
    373         default => "/etc/mail/spamassassin/FuzzyOcr.scansets"
    374         type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    375         }); 
     374        default => '__local_rules_dir__/FuzzyOcr.scansets'
     375        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
     376    }); 
    376377    push (@cmds, { 
    377378        setting => 'focr_preprocessor_file', 
    378         default => "/etc/mail/spamassassin/FuzzyOcr.preps"
    379         type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    380         }); 
    381  
    382     push (@cmds, { 
    383             setting => 'focr_minimal_scanset', 
    384             default => 0, 
    385             type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    386         }); 
    387     push (@cmds, { 
    388             setting => 'focr_autosort_scanset', 
    389             default => 1, 
    390             type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    391         }); 
    392     push (@cmds, { 
    393             setting => 'focr_autosort_buffer', 
    394             default => 10, 
    395             type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    396         }); 
     379        default => '__local_rules_dir__/FuzzyOcr.preps'
     380        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
     381    }); 
     382 
     383    push (@cmds, { 
     384        setting => 'focr_minimal_scanset', 
     385        default => 0, 
     386        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
     387    }); 
     388    push (@cmds, { 
     389        setting => 'focr_autosort_scanset', 
     390        default => 1, 
     391        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
     392    }); 
     393    push (@cmds, { 
     394        setting => 'focr_autosort_buffer', 
     395        default => 10, 
     396        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
     397    }); 
    397398    push (@cmds, { 
    398399        setting => 'focr_mysql_host', 
    399400        default => 'localhost', 
    400401        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    401         }); 
     402    }); 
    402403 
    403404    push (@cmds, { 
     
    405406        default => 3306, 
    406407        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC 
    407         }); 
    408  
     408    }); 
    409409    push (@cmds, { 
    410410        setting => 'focr_mysql_socket', 
    411411        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    412         }); 
    413  
     412    }); 
    414413    push (@cmds, { 
    415414        setting => 'focr_mysql_db', 
    416415        default => 'FuzzyOcr', 
    417416        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    418         }); 
    419  
     417    }); 
    420418    push (@cmds, { 
    421419        setting => 'focr_mysql_hash', 
    422420        default => 'Hash', 
    423421        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    424         }); 
    425  
     422    }); 
    426423    push (@cmds, { 
    427424        setting => 'focr_mysql_safe', 
    428425        default => 'Safe', 
    429426        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    430         }); 
    431  
     427    }); 
    432428    push (@cmds, { 
    433429        setting => 'focr_mysql_update_hash', 
    434430        default => 0, 
    435431        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL 
    436         }); 
    437  
     432    }); 
    438433    foreach (qw/user pass/) { 
    439434        push (@cmds, { 
     
    441436            default => 'fuzzyocr', 
    442437            type =>  $Mail::SpamAssassin::Conf::CONF_TYPE_STRING 
    443             }); 
     438        }); 
    444439    } 
    445440 
     
    451446    if ($opts->{key} eq 'focr_end_config') { 
    452447        $conf = $opts->{conf}; 
     448        my $retcode; 
     449        info("FuzzyOcr: focr_end_config"); 
     450 
    453451        # Parse preprocessor file 
    454452        my $pfile = $conf->{'focr_preprocessor_file'}; 
    455453        debuglog("Starting preprocessor parser for file \"$pfile\"..."); 
    456         (my $retcode, @preprocessors) = parse_preprocessors($pfile); 
     454        ($retcode, @preprocessors) = parse_preprocessors($pfile); 
    457455        if ($retcode) { 
    458456            warn("Error parsing preprocessor file \"$pfile\", aborting..."); 
     
    475473sub finish_parsing_end { 
    476474    my ($self, $opts) = @_; 
     475    my $main = $self->{main}; 
    477476    $conf = $opts->{conf}; 
    478477 
    479     #add_facilities('FuzzyOcr'); 
     478 
     479    # fix paths 
     480    foreach (qw/focr_personal_wordlist focr_global_wordlist 
     481            focr_db_hash focr_db_safe focr_hash_db 
     482            focr_scanset_file focr_preprocessor_file/) { 
     483        next unless defined $conf->{$_}; 
     484        my $path = $main->sed_path($conf->{$_}); 
     485        $conf->{$_} = $path ? $path : $_; 
     486        debuglog("$_ => $path"); 
     487    } 
     488 
    480489    # find external binaries 
    481     @paths = split(/:/, $conf->{'focr_path_bin'}); 
     490    @paths = split(/:/, $conf->{focr_path_bin}); 
    482491    debuglog("Searching in: $_") foreach @paths; 
    483492    foreach my $a (@bin_utils) { 
    484         if (defined $conf->{"focr_bin_$a"} and ! -x $conf->{"focr_bin_$a"}) { 
     493        my $b = "focr_bin_$a"; 
     494        if (defined $conf->{$b} and ! -x $conf->{$b}) { 
    485495            debuglog("cannot exec $a, removing..."); 
    486             delete $conf->{"focr_bin_$a"}; 
     496            delete $conf->{$b}; 
    487497        }  
    488498        foreach my $p (@paths) { 
    489499            my $f = "$p/$a"; 
    490             if (! defined $conf->{"focr_bin_$a"} and -x $f) { 
    491                 $conf->{"focr_bin_$a"} = $f; 
     500            if (! defined $conf->{$b} and -x $f) { 
     501                $conf->{$b} = $f; 
    492502                last; 
    493503            } 
    494504        } 
    495         if (defined $conf->{"focr_bin_$a"}) { 
    496             debuglog("Using $a => ".$conf->{"focr_bin_$a"}); 
     505        if (defined $conf->{$b}) { 
     506            debuglog("Using $a => $b"); 
    497507        } else { 
    498508            debuglog("Cannot find executable for $a"); 
     
    500510    } 
    501511 
     512    # Display All Options 
    502513    foreach my $k (sort keys %{$conf}) { 
    503514        next unless $k =~ m/^focr_/; 
     
    535546        } 
    536547    } 
    537     if ($conf->{focr_enable_image_hashing} == 2 and -r $conf->{focr_digest_db}) { 
    538         my %DB; my $dbm; my $err = 0; 
    539         my $now = time - ($conf->{focr_db_max_days}*86400); 
    540         debuglog($conf->{focr_db_hash}); 
    541         tie %DB, 'MLDBM', $conf->{focr_db_hash} or $err++; 
    542         if ($err) { 
    543             debuglog("Could not open \"$conf->{focr_db_hash}\""); 
    544         } else { 
    545             my $hash = 0; 
    546             debuglog("Expiring records prior to: ".scalar(localtime($now))); 
    547             foreach my $k (keys %DB) { 
    548                 my $db = $DB{$k}; 
    549                 if ($db->{check} < $now) { 
    550                     debuglog("Expire: <$k> Reason: $db->{check} < $now"); 
    551                     delete $DB{$k}; $hash++; 
    552                 } 
    553             } 
    554             debuglog("Expired <$hash> Image Hashes after $conf->{focr_db_max_days} day(s)") 
    555                 if ($hash>0); 
    556             $hash = 0; 
    557             open HASH, $conf->{focr_digest_db}; 
    558             while (<HASH>) { 
    559                 chomp; 
    560                 my($score,$basic,$key) = split('::',$_,3); 
    561                 next if (defined $DB{$key}); 
    562                 $dbm = $DB{$key}; 
    563                 $dbm->{score} = $score; 
    564                 $dbm->{basic} = $basic; 
    565                 $dbm->{input} = 
    566                 $dbm->{check} = time; 
    567                 $dbm->{match} = 1; 
    568                 $DB{$key} = $dbm; 
    569                 $hash++; 
    570             } 
    571             close HASH; 
    572             debuglog("Imported <$hash> Image Hashes from \"$conf->{focr_digest_db}\"") 
    573                 if ($hash>0); 
    574             $hash = scalar(keys %DB); 
    575             debuglog("<$hash> Known BAD Image Hashes Available"); 
    576             untie %DB; 
    577         } 
    578         $err = 0; 
    579         tie %DB, 'MLDBM', $conf->{focr_db_safe} or $err++; 
    580         if ($err) { 
    581             debuglog("Could not open \"$conf->{focr_db_safe}\""); 
    582         } else { 
    583             my $hash = 0; 
    584             foreach my $k (keys %DB) { 
    585                 my $db = $DB{$k}; 
    586                 if ($db->{check} < $now) { 
    587                     debuglog("Expire: <$k> Reason: $db->{check} < $now"); 
    588                     delete $DB{$k}; $hash++; 
    589                 } 
    590             } 
    591             debuglog("Expired <$hash> Image Hashes after $conf->{focr_db_max_days} day(s)") 
    592                 if ($hash>0); 
    593             $hash = scalar(keys %DB); 
    594             debuglog("<$hash> Known GOOD Image Hashes Available"); 
    595             untie %DB; 
    596         } 
    597     } 
    598     if ($conf->{focr_enable_image_hashing} == 3) { 
    599         my $ddb = get_ddb(); 
    600         if (defined $ddb) { 
    601             my $db   = $conf->{focr_mysql_db}; 
    602             my $tab  = $conf->{focr_mysql_hash}; 
    603             my $file = $conf->{focr_db_hash}; 
     548    unless ($conf->{focr_skip_updates}) { 
     549        use DBI; 
     550        use MLDBM qw(DB_File Storable); 
     551        if ($conf->{focr_enable_image_hashing} == 2 and -r $conf->{focr_digest_db}) { 
    604552            my %DB; my $dbm; my $err = 0; 
    605             tie %DB, 'MLDBM', $file or $err++; 
     553            my $now = time - ($conf->{focr_db_max_days}*86400); 
     554            debuglog($conf->{focr_db_hash}); 
     555            tie %DB, 'MLDBM', $conf->{focr_db_hash} or $err++; 
    606556            if ($err) { 
    607                 debuglog("Could not open \"$file\""); 
     557                debuglog("Could not open \"$conf->{focr_db_hash}\""); 
    608558            } else { 
     559                my $hash = 0; 
     560                debuglog("Expiring records prior to: ".scalar(localtime($now))); 
    609561                foreach my $k (keys %DB) { 
    610                     my $dbm = $DB{$k}; 
    611                     my $sql = qq(select score from $db.$tab where $tab.key='$k'); 
    612                     my @data = $ddb->selectrow_array($sql); 
    613                     unless (scalar(@data)>0) { 
    614                         $sql  = "insert into $db.$tab values ('$k'"; 
    615                         foreach my $y (qw/basic fname ctype/) { 
    616                             my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
    617                             $sql .= ",'$val'"; 
     562                    my $db = $DB{$k}; 
     563                    if ($db->{check} < $now) { 
     564                        debuglog("Expire: <$k> Reason: $db->{check} < $now"); 
     565                        delete $DB{$k}; $hash++; 
     566                    } 
     567                } 
     568                debuglog("Expired <$hash> Image Hashes after $conf->{focr_db_max_days} day(s)") 
     569                    if ($hash>0); 
     570                $hash = 0; 
     571                open HASH, $conf->{focr_digest_db}; 
     572                while (<HASH>) { 
     573                    chomp; 
     574                    my($score,$basic,$key) = split('::',$_,3); 
     575                    next if (defined $DB{$key}); 
     576                    $dbm = $DB{$key}; 
     577                    $dbm->{score} = $score; 
     578                    $dbm->{basic} = $basic; 
     579                    $dbm->{input} = 
     580                    $dbm->{check} = time; 
     581                    $dbm->{match} = 1; 
     582                    $DB{$key} = $dbm; 
     583                    $hash++; 
     584                } 
     585                close HASH; 
     586                debuglog("Imported <$hash> Image Hashes from \"$conf->{focr_digest_db}\"") 
     587                    if ($hash>0); 
     588                $hash = scalar(keys %DB); 
     589                debuglog("<$hash> Known BAD Image Hashes Available"); 
     590                untie %DB; 
     591            } 
     592            $err = 0; 
     593            tie %DB, 'MLDBM', $conf->{focr_db_safe} or $err++; 
     594            if ($err) { 
     595                debuglog("Could not open \"$conf->{focr_db_safe}\""); 
     596            } else { 
     597                my $hash = 0; 
     598                foreach my $k (keys %DB) { 
     599                    my $db = $DB{$k}; 
     600                    if ($db->{check} < $now) { 
     601                        debuglog("Expire: <$k> Reason: $db->{check} < $now"); 
     602                        delete $DB{$k}; $hash++; 
     603                    } 
     604                } 
     605                debuglog("Expired <$hash> Image Hashes after $conf->{focr_db_max_days} day(s)") 
     606                    if ($hash>0); 
     607                $hash = scalar(keys %DB); 
     608                debuglog("<$hash> Known GOOD Image Hashes Available"); 
     609                untie %DB; 
     610            } 
     611        } 
     612        if ($conf->{focr_enable_image_hashing} == 3) { 
     613            my $ddb = get_mysql_ddb(); 
     614            if (defined $ddb) { 
     615                my $db   = $conf->{focr_mysql_db}; 
     616                my $tab  = $conf->{focr_mysql_hash}; 
     617                my $file = $conf->{focr_db_hash}; 
     618                my %DB; my $dbm; my $err = 0; 
     619                tie %DB, 'MLDBM', $file or $err++; 
     620                if ($err) { 
     621                    debuglog("Could not open \"$file\""); 
     622                } else { 
     623                    foreach my $k (keys %DB) { 
     624                        my $dbm = $DB{$k}; 
     625                        my $sql = qq(select score from $db.$tab where $tab.key='$k'); 
     626                        my @data = $ddb->selectrow_array($sql); 
     627                        unless (scalar(@data)>0) { 
     628                            $sql  = "insert into $db.$tab values ('$k'"; 
     629                            foreach my $y (qw/basic fname ctype/) { 
     630                                my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
     631                                $sql .= ",'$val'"; 
     632                            } 
     633                               if ($dbm->{ctype} =~ m/gif/i)      { $sql .= ",'1'"; } 
     634                            elsif ($dbm->{ctype} =~ m/jpg|jpeg/i) { $sql .= ",'2'"; } 
     635                            elsif ($dbm->{ctype} =~ m/png/i)      { $sql .= ",'3'"; } 
     636                            elsif ($dbm->{ctype} =~ m/bmp/i)      { $sql .= ",'4'"; } 
     637                            elsif ($dbm->{ctype} =~ m/tiff/i)     { $sql .= ",'5'"; } 
     638                            else                                  { $sql .= ",'0'"; } 
     639                            foreach my $y (qw/match input check score dinfo/) { 
     640                                my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
     641                                $sql .= ",'$val'"; 
     642                            } 
     643                            $sql .= ")"; 
     644                            debuglog($sql,2); 
     645                            $ddb->do($sql); $err++; 
    618646                        } 
    619                            if ($dbm->{ctype} =~ m/gif/i)      { $sql .= ",'1'"; } 
    620                         elsif ($dbm->{ctype} =~ m/jpg|jpeg/i) { $sql .= ",'2'"; } 
    621                         elsif ($dbm->{ctype} =~ m/png/i)      { $sql .= ",'3'"; } 
    622                         elsif ($dbm->{ctype} =~ m/bmp/i)      { $sql .= ",'4'"; } 
    623                         elsif ($dbm->{ctype} =~ m/tiff/i)     { $sql .= ",'5'"; } 
    624                         else { $sql .= ",'0'"; } 
    625                         foreach my $y (qw/match input check score dinfo/) { 
    626                             my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
    627                             $sql .= ",'$val'"; 
     647                    } 
     648                    untie %DB; 
     649                    debuglog("Stored [$err] Hashes in $db.$tab") if $err>0; 
     650                } 
     651                $tab  = $conf->{focr_mysql_safe}; 
     652                $file = $conf->{focr_db_safe}; 
     653                $err  = 0; 
     654                tie %DB, 'MLDBM', $file or $err++; 
     655                if ($err) { 
     656                    debuglog("Could not open \"$file\""); 
     657                } else { 
     658                    foreach my $k (keys %DB) { 
     659                        my $dbm = $DB{$k}; 
     660                        my $sql = qq(select score from $db.$tab where $tab.key='$k'); 
     661                        my @data = $ddb->selectrow_array($sql); 
     662                        unless (scalar(@data)>0) { 
     663                            $sql  = "insert into $db.$tab values ('$k'"; 
     664                            foreach my $y (qw/basic fname ctype/) { 
     665                                my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
     666                                $sql .= ",'$val'"; 
     667                            } 
     668                               if ($dbm->{ctype} =~ m/gif/i)      { $sql .= ",'1'"; } 
     669                            elsif ($dbm->{ctype} =~ m/jpg|jpeg/i) { $sql .= ",'2'"; } 
     670                            elsif ($dbm->{ctype} =~ m/png/i)      { $sql .= ",'3'"; } 
     671                            elsif ($dbm->{ctype} =~ m/bmp/i)      { $sql .= ",'4'"; } 
     672                            elsif ($dbm->{ctype} =~ m/tiff/i)     { $sql .= ",'5'"; } 
     673                            else                                  { $sql .= ",'0'"; } 
     674                            foreach my $y (qw/match input check score dinfo/) { 
     675                                my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
     676                                $sql .= ",'$val'"; 
     677                            } 
     678                            $sql .= ")"; 
     679                            debuglog($sql,2); 
     680                            $ddb->do($sql); $err++; 
    628681                        } 
    629                         $sql .= ")"; 
    630                         debuglog($sql,2); 
    631                         $ddb->do($sql); $err++; 
    632682                    } 
    633                 } 
    634                 untie %DB; 
    635                 debuglog("Stored [$err] Hashes in $db.$tab") if $err>0; 
    636             } 
    637             $tab  = $conf->{focr_mysql_safe}; 
    638             $file = $conf->{focr_db_safe}; 
    639             $err  = 0; 
    640             tie %DB, 'MLDBM', $file or $err++; 
    641             if ($err) { 
    642                 debuglog("Could not open \"$file\""); 
    643             } else { 
    644                 foreach my $k (keys %DB) { 
    645                     my $dbm = $DB{$k}; 
    646                     my $sql = qq(select score from $db.$tab where $tab.key='$k'); 
    647                     my @data = $ddb->selectrow_array($sql); 
    648                     unless (scalar(@data)>0) { 
    649                         $sql  = "insert into $db.$tab values ('$k'"; 
    650                         foreach my $y (qw/basic fname ctype/) { 
    651                             my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
    652                             $sql .= ",'$val'"; 
    653                         } 
    654                            if ($dbm->{ctype} =~ m/gif/i)      { $sql .= ",'1'"; } 
    655                         elsif ($dbm->{ctype} =~ m/jpg|jpeg/i) { $sql .= ",'2'"; } 
    656                         elsif ($dbm->{ctype} =~ m/png/i)      { $sql .= ",'3'"; } 
    657                         elsif ($dbm->{ctype} =~ m/bmp/i)      { $sql .= ",'4'"; } 
    658                         elsif ($dbm->{ctype} =~ m/tiff/i)     { $sql .= ",'5'"; } 
    659                         else { $sql .= ",'0'"; } 
    660                         foreach my $y (qw/match input check score dinfo/) { 
    661                             my $val = defined($dbm->{$y}) ? $dbm->{$y} : ''; 
    662                             $sql .= ",'$val'"; 
    663                         } 
    664                         $sql .= ")"; 
    665                         debuglog($sql,2); 
    666                         $ddb->do($sql); $err++; 
    667                     } 
    668                 } 
    669                 untie %DB; 
    670                 debuglog("Stored [$err] Hashes in $db.$tab") if $err>0; 
    671             } 
    672             $ddb->disconnect; 
    673             debuglog("done updating MySQL database"); 
    674         } 
    675     } 
    676     load_global_words( $conf->{focr_global_wordlist} ); 
    677 
    678  
    679 sub load_global_words { 
    680     unless ( -r $_[0] ) { 
    681         debuglog("Cannot read Global wordlist: \"$_[0]\"\n Please check file path and permissions are correct."); 
     683                    untie %DB; 
     684                    debuglog("Stored [$err] Hashes in $db.$tab") if $err>0; 
     685                } 
     686                $ddb->disconnect; 
     687                debuglog("done updating MySQL database"); 
     688            } 
     689        } 
     690    } 
     691    read_words( $conf->{focr_global_wordlist} , 'Global'); 
     692    1; 
     693
     694 
     695sub read_words { 
     696    my $wfile = $_[0]; 
     697    my $tfile = $_[1] || 'Personal'; 
     698    unless ( -r $wfile ) { 
     699        debuglog("Cannot read $tfile wordlist: \"$wfile\"\n Please check file path and permissions are correct."); 
    682700        return; 
    683701    } 
    684702    my $cnt = 0; 
    685     open WORDLIST, "<$_[0]"; 
     703    open WORDLIST, "<$wfile"; 
    686704    while(my $w = <WORDLIST>) { 
    687705        chomp($w); 
     
    701719    } 
    702720    close WORDLIST; 
    703     debuglog("Loaded <$cnt> words from \"$_[0]\""); 
    704 
    705  
    706 sub load_personal_words { 
    707     unless ( -e $_[0] ) { 
    708         #debuglog("Personal wordlist <$_[0]> not found, skipping..."); 
    709         return; 
    710     } 
    711     unless ( -r $_[0] ) { 
    712         debuglog("Cannot read from wordlist \"$_[0]\"\n Please make sure that permissions are correct."); 
    713         return; 
    714     } 
    715     my $cnt = 0; 
    716     open WORDLIST, "<$_[0]"; 
    717     while(my $w = <WORDLIST>) { 
    718         chomp($w); 
    719         $w =~ s/\s*//; 
    720         $w =~ s/#(.*)//; 
    721         next unless $w; 
    722         my $wt = $conf->{focr_threshold}; 
    723         if ($w =~ /^(.*?)::(0(\.\d+){0,1})/) { 
    724             ($w, $wt) = ($1, $2); 
    725             $wt = $conf->{focr_threshold} unless ($wt =~ m/[\d\.]+/); 
    726         } else { 
    727             $wt *= 0.750 if length($w) == 5; 
    728             $wt *= 0.500 if length($w) == 4; 
    729             $wt *= 0.250 if length($w)  < 4; 
    730         } 
    731         $words{$w} = $wt; $cnt++; 
    732     } 
    733     close WORDLIST; 
    734     debuglog("Updated Word List with $cnt words from $_[0]"); 
     721    debuglog("Added <$cnt> words from \"$wfile\"") if ($cnt>0); 
    735722} 
    736723 
  • trunk/devel/FuzzyOcr/Deanimate.pm

    r61 r68  
    55our @EXPORT_OK = qw(deanimate); 
    66 
    7 use lib "../"
     7use lib qw(..)
    88use FuzzyOcr::Config qw(get_config set_config get_tmpdir); 
    99use FuzzyOcr::Misc qw(save_execute); 
    10 use FuzzyOcr::Logging qw(debuglog logfile); 
     10use FuzzyOcr::Logging qw(debuglog); 
    1111 
    1212# Provide functions to deanimate gifs 
     
    7878    ($retcode, @stdout_data) = save_execute( 
    7979        "$conf->{focr_bin_gifsicle} --info $giffile", 
    80        undef, 
     80        undef, 
    8181        ">$imgdir/gifsicle.info", 
    8282        ">>$imgdir/gifsicle.err", 1); 
  • trunk/devel/FuzzyOcr/Hashing.pm

    r61 r68  
    77    calc_image_hash); 
    88 
    9 use lib "../"
     9use lib qw(..)
    1010use FuzzyOcr::Config qw(get_thresholds get_config set_config get_tmpdir); 
    1111use FuzzyOcr::Misc qw(save_execute); 
    12 use FuzzyOcr::Logging qw(debuglog logfile); 
     12use FuzzyOcr::Logging qw(debuglog); 
    1313use Fcntl; 
    1414use Fcntl ':flock';