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

messageviewer

  • sources
  • kde-4.14
  • 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 #ifndef Q_MOC_RUN
47 #include <boost/shared_ptr.hpp>
48 #endif
49 
50 using namespace MessageViewer;
51 
52 // static
53 SpamScores SpamHeaderAnalyzer::getSpamScores( KMime::Message *message ) {
54  SpamScores scores;
55  const SpamAgents agents = AntiSpamConfig::instance()->uniqueAgents();
56  SpamAgents::const_iterator end( agents.constEnd() );
57  for ( SpamAgents::const_iterator it = agents.constBegin(); it != end; ++it ) {
58  float score = -2.0;
59 
60  SpamError spamError = noError;
61 
62  // Skip bogus agents
63  if ( (*it).scoreType() == SpamAgentNone )
64  continue;
65 
66  // Do we have the needed score field for this agent?
67  KMime::Headers::Base *header= message->headerByType( (*it).header() );
68  if ( !header )
69  continue;
70 
71  const QString mField = header->asUnicodeString();
72 
73  if ( mField.isEmpty() )
74  continue;
75 
76  QString scoreString;
77  bool scoreValid = false;
78 
79  if ( (*it).scoreType() != SpamAgentBool ) {
80  // Can we extract the score?
81  QRegExp scorePattern = (*it).scorePattern();
82  if ( scorePattern.indexIn( mField ) != -1 ) {
83  scoreString = scorePattern.cap( 1 );
84  scoreValid = true;
85  }
86  } else
87  scoreValid = true;
88 
89  if ( !scoreValid ) {
90  spamError = couldNotFindTheScoreField;
91  kDebug() << "Score could not be extracted from header '"
92  << mField << "'";
93  } else {
94  bool floatValid = false;
95  switch ( (*it).scoreType() ) {
96  case SpamAgentNone:
97  spamError = errorExtractingAgentString;
98  break;
99 
100  case SpamAgentBool:
101  if( (*it).scorePattern().indexIn( mField ) == -1 )
102  score = 0.0;
103  else
104  score = 100.0;
105  break;
106 
107  case SpamAgentFloat:
108  score = scoreString.toFloat( &floatValid );
109  if ( !floatValid ) {
110  spamError = couldNotConverScoreToFloat;
111  kDebug() << "Score (" << scoreString << ") is no number";
112  }
113  else
114  score *= 100.0;
115  break;
116 
117  case SpamAgentFloatLarge:
118  score = scoreString.toFloat( &floatValid );
119  if ( !floatValid ) {
120  spamError = couldNotConverScoreToFloat;
121  kDebug() << "Score (" << scoreString << ") is no number";
122  }
123  break;
124 
125  case SpamAgentAdjustedFloat:
126  score = scoreString.toFloat( &floatValid );
127  if ( !floatValid ) {
128  spamError = couldNotConverScoreToFloat;
129  kDebug() << "Score (" << scoreString << ") is no number";
130  break;
131  }
132 
133  // Find the threshold value.
134  QString thresholdString;
135  const QRegExp thresholdPattern = (*it).thresholdPattern();
136  if ( thresholdPattern.indexIn( mField ) != -1 ) {
137  thresholdString = thresholdPattern.cap( 1 );
138  } else {
139  spamError = couldNotFindTheThresholdField;
140  kDebug() << "Threshold could not be extracted from header '"
141  << mField << "'";
142  break;
143  }
144  const float threshold = thresholdString.toFloat( &floatValid );
145  if ( !floatValid || ( threshold <= 0.0 ) ) {
146  spamError = couldNotConvertThresholdToFloatOrThresholdIsNegative;
147  kDebug() << "Threshold (" << thresholdString << ") is no"
148  << "number or is negative";
149  break;
150  }
151 
152  // Normalize the score. Anything below 0 means 0%, anything above
153  // threshold mean 100%. Values between 0 and threshold are mapped
154  // linearily to 0% - 100%.
155  if ( score < 0.0 )
156  score = 0.0;
157  else if ( score > threshold )
158  score = 100.0;
159  else
160  score = score / threshold * 100.0;
161  break;
162  }
163  }
164  //Find the confidence
165  float confidence = -2.0;
166  QString confidenceString = QLatin1String("-2.0");
167  bool confidenceValid = false;
168  // Do we have the needed confidence field for this agent?
169  const QByteArray confidenceHeaderName = (*it).confidenceHeader();
170  QString mCField;
171  if( !confidenceHeaderName.isEmpty() )
172  {
173  KMime::Headers::Base *cHeader = message->headerByType( confidenceHeaderName );
174  if ( cHeader )
175  {
176  mCField = cHeader->asUnicodeString();
177  if ( ! mCField.isEmpty() ) {
178  // Can we extract the confidence?
179  QRegExp cScorePattern = (*it).confidencePattern();
180  if ( cScorePattern.indexIn( mCField ) != -1 ) {
181  confidenceString = cScorePattern.cap( 1 );
182  }
183  confidence = confidenceString.toFloat( &confidenceValid );
184  if( !confidenceValid) {
185  spamError = couldNotConvertConfidenceToFloat;
186  kDebug() << "Unable to convert confidence to float:" << confidenceString;
187  }
188  }
189  }
190  }
191  scores.append( SpamScore( (*it).name(), spamError, score, confidence*100, mField, mCField ) );
192  }
193 
194  return scores;
195 }
QRegExp::cap
QString cap(int nth) const
MessageViewer::SpamAgentFloatLarge
For straight percentages between 0.0 and 100.0.
Definition: antispamconfig.h:50
QVector::append
void append(const T &value)
QByteArray
MessageViewer::SpamAgentFloat
For straight percentages between 0.0 and 1.0 (BogoFilter)
Definition: antispamconfig.h:49
MessageViewer::noError
Definition: spamheaderanalyzer.h:45
QVector::constEnd
const_iterator constEnd() const
QByteArray::isEmpty
bool isEmpty() const
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:53
MessageViewer::couldNotConverScoreToFloat
Definition: spamheaderanalyzer.h:48
MessageViewer::SpamError
SpamError
Definition: spamheaderanalyzer.h:44
MessageViewer::SpamScore
A simple tupel of error, agent, score, confidence and header.
Definition: spamheaderanalyzer.h:69
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
QString::isEmpty
bool isEmpty() const
MessageViewer::couldNotFindTheScoreField
Definition: spamheaderanalyzer.h:50
QString
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
QVector::constBegin
const_iterator constBegin() const
QVector
MessageViewer::couldNotConvertConfidenceToFloat
Definition: spamheaderanalyzer.h:52
QLatin1String
QString::toFloat
float toFloat(bool *ok) const
MessageViewer::couldNotFindTheThresholdField
Definition: spamheaderanalyzer.h:51
MessageViewer::errorExtractingAgentString
Definition: spamheaderanalyzer.h:47
QVector::const_iterator
typedef const_iterator
antispamconfig.h
MessageViewer::couldNotConvertThresholdToFloatOrThresholdIsNegative
Definition: spamheaderanalyzer.h:49
spamheaderanalyzer.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 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
  • pimprint

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