spamheaderanalyzer.cpp
00001 /* -*- mode: C++; c-file-style: "gnu" -*- 00002 spamheaderanalyzer.cpp 00003 00004 This file is part of KMail, the KDE mail client. 00005 Copyright (c) 2004 Patrick Audley <paudley@blackcat.ca> 00006 Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org> 00007 00008 KMail is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 KMail is distributed in the hope that it will be useful, but 00014 WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 00022 In addition, as a special exception, the copyright holders give 00023 permission to link the code of this program with any edition of 00024 the TQt library by Trolltech AS, Norway (or with modified versions 00025 of TQt that use the same license as TQt), and distribute linked 00026 combinations including the two. You must obey the GNU General 00027 Public License in all respects for all of the code used other than 00028 TQt. If you modify this file, you may extend this exception to 00029 your version of the file, but you are not obligated to do so. If 00030 you do not wish to do so, delete this exception statement from 00031 your version. 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 // static 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 // Skip bogus agents 00053 if ( (*it).scoreType() == SpamAgentNone ) 00054 continue; 00055 00056 // Do we have the needed field for this agent? 00057 TQString mField = message->headerField( (*it).header() ); 00058 if ( mField.isEmpty() ) 00059 continue; 00060 00061 TQString scoreString; 00062 bool scoreValid = false; 00063 00064 if ( (*it).scoreType() != SpamAgentBool ) { 00065 // Can we extract the score? 00066 TQRegExp 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 // Find the threshold value. 00122 TQString thresholdString; 00123 TQRegExp 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 // Normalize the score. Anything below 0 means 0%, anything above 00142 // threshold mean 100%. Values between 0 and threshold are mapped 00143 // linearily to 0% - 100%. 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 }