kmail
spamheaderanalyzer.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "spamheaderanalyzer.h"
00035
00036 #include "antispamconfig.h"
00037 #include "kmmessage.h"
00038
00039 #include <kdebug.h>
00040
00041 using namespace KMail;
00042
00043
00044 SpamScores SpamHeaderAnalyzer::getSpamScores( const KMMessage* message ) {
00045 kdDebug(5006) << k_funcinfo << endl;
00046 SpamScores scores;
00047 SpamAgents agents = AntiSpamConfig::instance()->uniqueAgents();
00048
00049 for ( SpamAgentsIterator it = agents.begin(); it != agents.end(); ++it ) {
00050 float score = -2.0;
00051
00052
00053 if ( (*it).scoreType() == SpamAgentNone )
00054 continue;
00055
00056
00057 QString mField = message->headerField( (*it).header() );
00058 if ( mField.isEmpty() )
00059 continue;
00060
00061 QString scoreString;
00062 bool scoreValid = false;
00063
00064 if ( (*it).scoreType() != SpamAgentBool ) {
00065
00066 QRegExp scorePattern = (*it).scorePattern();
00067 if ( scorePattern.search( mField ) != -1 ) {
00068 scoreString = scorePattern.cap( 1 );
00069 scoreValid = true;
00070 }
00071 } else
00072 scoreValid = true;
00073
00074 if ( !scoreValid ) {
00075 score = -5.0;
00076 kdDebug(5006) << "Score could not be extracted from header '"
00077 << mField << "'" << endl;
00078 } else {
00079 bool floatValid = false;
00080 switch ( (*it).scoreType() ) {
00081 case SpamAgentNone:
00082 score = -2.0;
00083 break;
00084
00085 case SpamAgentBool:
00086 if( (*it).scorePattern().search( mField ) == -1 )
00087 score = 0.0;
00088 else
00089 score = 100.0;
00090 break;
00091
00092 case SpamAgentFloat:
00093 score = scoreString.toFloat( &floatValid );
00094 if ( !floatValid ) {
00095 score = -3.0;
00096 kdDebug(5006) << "Score (" << scoreString << ") is no number"
00097 << endl;
00098 }
00099 else
00100 score *= 100.0;
00101 break;
00102
00103 case SpamAgentFloatLarge:
00104 score = scoreString.toFloat( &floatValid );
00105 if ( !floatValid ) {
00106 score = -3.0;
00107 kdDebug(5006) << "Score (" << scoreString << ") is no number"
00108 << endl;
00109 }
00110 break;
00111
00112 case SpamAgentAdjustedFloat:
00113 score = scoreString.toFloat( &floatValid );
00114 if ( !floatValid ) {
00115 score = -3.0;
00116 kdDebug(5006) << "Score (" << scoreString << ") is no number"
00117 << endl;
00118 break;
00119 }
00120
00121
00122 QString thresholdString;
00123 QRegExp thresholdPattern = (*it).thresholdPattern();
00124 if ( thresholdPattern.search( mField ) != -1 ) {
00125 thresholdString = thresholdPattern.cap( 1 );
00126 }
00127 else {
00128 score = -6.0;
00129 kdDebug(5006) << "Threshold could not be extracted from header '"
00130 << mField << "'" << endl;
00131 break;
00132 }
00133 float threshold = thresholdString.toFloat( &floatValid );
00134 if ( !floatValid || ( threshold <= 0.0 ) ) {
00135 score = -4.0;
00136 kdDebug(5006) << "Threshold (" << thresholdString << ") is no "
00137 << "number or is negative" << endl;
00138 break;
00139 }
00140
00141
00142
00143
00144 if ( score < 0.0 )
00145 score = 0.0;
00146 else if ( score > threshold )
00147 score = 100.0;
00148 else
00149 score = score / threshold * 100.0;
00150 break;
00151 }
00152 }
00153 scores.append( SpamScore( (*it).name(), score, mField ) );
00154 }
00155
00156 return scores;
00157 }
|