kmail

spamheaderanalyzer.cpp
1/*
2 spamheaderanalyzer.cpp
3
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2004 Patrick Audley <paudley@blackcat.ca>
6 Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org>
7
8 KMail is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 KMail is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22 In addition, as a special exception, the copyright holders give
23 permission to link the code of this program with any edition of
24 the TQt library by Trolltech AS, Norway (or with modified versions
25 of TQt that use the same license as TQt), and distribute linked
26 combinations including the two. You must obey the GNU General
27 Public License in all respects for all of the code used other than
28 TQt. If you modify this file, you may extend this exception to
29 your version of the file, but you are not obligated to do so. If
30 you do not wish to do so, delete this exception statement from
31 your version.
32*/
33
34#include "spamheaderanalyzer.h"
35
36#include "antispamconfig.h"
37#include "kmmessage.h"
38
39#include <kdebug.h>
40
41using namespace KMail;
42
43// static
44SpamScores SpamHeaderAnalyzer::getSpamScores( const KMMessage* message ) {
45 kdDebug(5006) << k_funcinfo << endl;
46 SpamScores scores;
47 SpamAgents agents = AntiSpamConfig::instance()->uniqueAgents();
48
49 for ( SpamAgentsIterator it = agents.begin(); it != agents.end(); ++it ) {
50 float score = -2.0;
51
52 // Skip bogus agents
53 if ( (*it).scoreType() == SpamAgentNone )
54 continue;
55
56 // Do we have the needed field for this agent?
57 TQString mField = message->headerField( (*it).header() );
58 if ( mField.isEmpty() )
59 continue;
60
61 TQString scoreString;
62 bool scoreValid = false;
63
64 if ( (*it).scoreType() != SpamAgentBool ) {
65 // Can we extract the score?
66 TQRegExp scorePattern = (*it).scorePattern();
67 if ( scorePattern.search( mField ) != -1 ) {
68 scoreString = scorePattern.cap( 1 );
69 scoreValid = true;
70 }
71 } else
72 scoreValid = true;
73
74 if ( !scoreValid ) {
75 score = -5.0;
76 kdDebug(5006) << "Score could not be extracted from header '"
77 << mField << "'" << endl;
78 } else {
79 bool floatValid = false;
80 switch ( (*it).scoreType() ) {
81 case SpamAgentNone:
82 score = -2.0;
83 break;
84
85 case SpamAgentBool:
86 if( (*it).scorePattern().search( mField ) == -1 )
87 score = 0.0;
88 else
89 score = 100.0;
90 break;
91
92 case SpamAgentFloat:
93 score = scoreString.toFloat( &floatValid );
94 if ( !floatValid ) {
95 score = -3.0;
96 kdDebug(5006) << "Score (" << scoreString << ") is no number"
97 << endl;
98 }
99 else
100 score *= 100.0;
101 break;
102
104 score = scoreString.toFloat( &floatValid );
105 if ( !floatValid ) {
106 score = -3.0;
107 kdDebug(5006) << "Score (" << scoreString << ") is no number"
108 << endl;
109 }
110 break;
111
113 score = scoreString.toFloat( &floatValid );
114 if ( !floatValid ) {
115 score = -3.0;
116 kdDebug(5006) << "Score (" << scoreString << ") is no number"
117 << endl;
118 break;
119 }
120
121 // Find the threshold value.
122 TQString thresholdString;
123 TQRegExp thresholdPattern = (*it).thresholdPattern();
124 if ( thresholdPattern.search( mField ) != -1 ) {
125 thresholdString = thresholdPattern.cap( 1 );
126 }
127 else {
128 score = -6.0;
129 kdDebug(5006) << "Threshold could not be extracted from header '"
130 << mField << "'" << endl;
131 break;
132 }
133 float threshold = thresholdString.toFloat( &floatValid );
134 if ( !floatValid || ( threshold <= 0.0 ) ) {
135 score = -4.0;
136 kdDebug(5006) << "Threshold (" << thresholdString << ") is no "
137 << "number or is negative" << endl;
138 break;
139 }
140
141 // Normalize the score. Anything below 0 means 0%, anything above
142 // threshold mean 100%. Values between 0 and threshold are mapped
143 // linearily to 0% - 100%.
144 if ( score < 0.0 )
145 score = 0.0;
146 else if ( score > threshold )
147 score = 100.0;
148 else
149 score = score / threshold * 100.0;
150 break;
151 }
152 }
153 scores.append( SpamScore( (*it).name(), score, mField ) );
154 }
155
156 return scores;
157}
This is a Mime Message.
Definition: kmmessage.h:68
TQString headerField(const TQCString &name) const
Returns the value of a header field with the given name.
Definition: kmmessage.cpp:2289
A simple tupel of agent, score and header.
folderdiaquotatab.h
Definition: aboutdata.cpp:40
@ SpamAgentFloatLarge
For straight percentages between 0.0 and 100.0.
@ SpamAgentBool
Simple Yes or No (Razor)
@ SpamAgentFloat
For straight percentages between 0.0 and 1.0 (BogoFilter)
@ SpamAgentNone
Invalid SpamAgent, skip this agent.
@ SpamAgentAdjustedFloat
Use this when we need to compare against a threshold (SpamAssasssin)