• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • antispam
spamheaderanalyzer.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
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 Qt library by Trolltech AS, Norway (or with modified versions
25  of Qt that use the same license as Qt), 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  Qt. 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 
35 
36 
37 #include "spamheaderanalyzer.h"
38 
39 #include "antispamconfig.h"
40 
41 #include <kmime/kmime_message.h>
42 #include <kmime/kmime_headers.h>
43 
44 #include <kdebug.h>
45 
46 #include <boost/shared_ptr.hpp>
47 
48 using namespace MessageViewer;
49 
50 // static
51 SpamScores SpamHeaderAnalyzer::getSpamScores( KMime::Message *message ) {
52  SpamScores scores;
53  const SpamAgents agents = AntiSpamConfig::instance()->uniqueAgents();
54  SpamAgents::const_iterator end( agents.constEnd() );
55  for ( SpamAgents::const_iterator it = agents.constBegin(); it != end; ++it ) {
56  float score = -2.0;
57 
58  SpamError spamError = noError;
59 
60  // Skip bogus agents
61  if ( (*it).scoreType() == SpamAgentNone )
62  continue;
63 
64  // Do we have the needed score field for this agent?
65  KMime::Headers::Base *header= message->headerByType( (*it).header() );
66  if ( !header )
67  continue;
68 
69  const QString mField = header->asUnicodeString();
70 
71  if ( mField.isEmpty() )
72  continue;
73 
74  QString scoreString;
75  bool scoreValid = false;
76 
77  if ( (*it).scoreType() != SpamAgentBool ) {
78  // Can we extract the score?
79  QRegExp scorePattern = (*it).scorePattern();
80  if ( scorePattern.indexIn( mField ) != -1 ) {
81  scoreString = scorePattern.cap( 1 );
82  scoreValid = true;
83  }
84  } else
85  scoreValid = true;
86 
87  if ( !scoreValid ) {
88  spamError = couldNotFindTheScoreField;
89  kDebug() << "Score could not be extracted from header '"
90  << mField << "'";
91  } else {
92  bool floatValid = false;
93  switch ( (*it).scoreType() ) {
94  case SpamAgentNone:
95  spamError = errorExtractingAgentString;
96  break;
97 
98  case SpamAgentBool:
99  if( (*it).scorePattern().indexIn( mField ) == -1 )
100  score = 0.0;
101  else
102  score = 100.0;
103  break;
104 
105  case SpamAgentFloat:
106  score = scoreString.toFloat( &floatValid );
107  if ( !floatValid ) {
108  spamError = couldNotConverScoreToFloat;
109  kDebug() << "Score (" << scoreString << ") is no number";
110  }
111  else
112  score *= 100.0;
113  break;
114 
115  case SpamAgentFloatLarge:
116  score = scoreString.toFloat( &floatValid );
117  if ( !floatValid ) {
118  spamError = couldNotConverScoreToFloat;
119  kDebug() << "Score (" << scoreString << ") is no number";
120  }
121  break;
122 
123  case SpamAgentAdjustedFloat:
124  score = scoreString.toFloat( &floatValid );
125  if ( !floatValid ) {
126  spamError = couldNotConverScoreToFloat;
127  kDebug() << "Score (" << scoreString << ") is no number";
128  break;
129  }
130 
131  // Find the threshold value.
132  QString thresholdString;
133  const QRegExp thresholdPattern = (*it).thresholdPattern();
134  if ( thresholdPattern.indexIn( mField ) != -1 ) {
135  thresholdString = thresholdPattern.cap( 1 );
136  } else {
137  spamError = couldNotFindTheThresholdField;
138  kDebug() << "Threshold could not be extracted from header '"
139  << mField << "'";
140  break;
141  }
142  const float threshold = thresholdString.toFloat( &floatValid );
143  if ( !floatValid || ( threshold <= 0.0 ) ) {
144  spamError = couldNotConvertThresholdToFloatOrThresholdIsNegative;
145  kDebug() << "Threshold (" << thresholdString << ") is no"
146  << "number or is negative";
147  break;
148  }
149 
150  // Normalize the score. Anything below 0 means 0%, anything above
151  // threshold mean 100%. Values between 0 and threshold are mapped
152  // linearily to 0% - 100%.
153  if ( score < 0.0 )
154  score = 0.0;
155  else if ( score > threshold )
156  score = 100.0;
157  else
158  score = score / threshold * 100.0;
159  break;
160  }
161  }
162  //Find the confidence
163  float confidence = -2.0;
164  QString confidenceString = QLatin1String("-2.0");
165  bool confidenceValid = false;
166  // Do we have the needed confidence field for this agent?
167  const QByteArray confidenceHeaderName = (*it).confidenceHeader();
168  QString mCField;
169  if( !confidenceHeaderName.isEmpty() )
170  {
171  KMime::Headers::Base *cHeader = message->headerByType( confidenceHeaderName );
172  if ( cHeader )
173  {
174  mCField = cHeader->asUnicodeString();
175  if ( ! mCField.isEmpty() ) {
176  // Can we extract the confidence?
177  QRegExp cScorePattern = (*it).confidencePattern();
178  if ( cScorePattern.indexIn( mCField ) != -1 ) {
179  confidenceString = cScorePattern.cap( 1 );
180  }
181  confidence = confidenceString.toFloat( &confidenceValid );
182  if( !confidenceValid) {
183  spamError = couldNotConvertConfidenceToFloat;
184  kDebug() << "Unable to convert confidence to float:" << confidenceString;
185  }
186  }
187  }
188  }
189  scores.append( SpamScore( (*it).name(), spamError, score, confidence*100, mField, mCField ) );
190  }
191 
192  return scores;
193 }
MessageViewer::SpamAgentFloatLarge
For straight percentages between 0.0 and 100.0.
Definition: antispamconfig.h:50
MessageViewer::SpamAgentFloat
For straight percentages between 0.0 and 1.0 (BogoFilter)
Definition: antispamconfig.h:49
MessageViewer::noError
Definition: spamheaderanalyzer.h:45
MessageViewer::AntiSpamConfig::instance
static AntiSpamConfig * instance()
Definition: antispamconfig.cpp:55
MessageViewer::SpamAgentAdjustedFloat
Use this when we need to compare against a threshold (SpamAssasssin)
Definition: antispamconfig.h:51
MessageViewer::SpamAgentBool
Simple Yes or No (Razor)
Definition: antispamconfig.h:48
MessageViewer::SpamHeaderAnalyzer::getSpamScores
static SpamScores getSpamScores(KMime::Message *message)
Extract scores from known anti-spam headers.
Definition: spamheaderanalyzer.cpp:51
MessageViewer::couldNotConverScoreToFloat
Definition: spamheaderanalyzer.h:48
MessageViewer::SpamScores
QVector< SpamScore > SpamScores
Definition: spamheaderanalyzer.h:102
MessageViewer::SpamError
SpamError
Definition: spamheaderanalyzer.h:44
MessageViewer::SpamScore
A simple tupel of error, agent, score, confidence and header.
Definition: spamheaderanalyzer.h:69
MessageViewer::couldNotFindTheScoreField
Definition: spamheaderanalyzer.h:50
MessageViewer::AntiSpamConfig::uniqueAgents
const SpamAgents uniqueAgents() const
Returns a list of unique agents, found on the system.
Definition: antispamconfig.cpp:113
MessageViewer::SpamAgentNone
Invalid SpamAgent, skip this agent.
Definition: antispamconfig.h:47
MessageViewer::SpamAgents
QVector< SpamAgent > SpamAgents
Definition: antispamconfig.h:80
MessageViewer::couldNotConvertConfidenceToFloat
Definition: spamheaderanalyzer.h:52
MessageViewer::couldNotFindTheThresholdField
Definition: spamheaderanalyzer.h:51
MessageViewer::errorExtractingAgentString
Definition: spamheaderanalyzer.h:47
antispamconfig.h
MessageViewer::couldNotConvertThresholdToFloatOrThresholdIsNegative
Definition: spamheaderanalyzer.h:49
spamheaderanalyzer.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal