11 #include "spamheaderanalyzer.h"
12 #include "antispamconfig.h"
13 #include "messageviewer_debug.h"
15 #include <KMime/Headers>
16 #include <KMime/KMimeMessage>
18 using namespace MessageViewer;
24 const SpamAgents agents = AntiSpamConfig::instance()->uniqueAgents();
29 SpamError spamError = noError;
32 if ((*it).scoreType() == SpamAgentNone) {
49 bool scoreValid =
false;
51 if ((*it).scoreType() != SpamAgentBool) {
53 QRegExp scorePattern = (*it).scorePattern();
54 if (scorePattern.
indexIn(mField) != -1) {
55 scoreString = scorePattern.
cap(1);
63 spamError = couldNotFindTheScoreField;
64 qCDebug(MESSAGEVIEWER_LOG) <<
"Score could not be extracted from header '" << mField <<
"'";
66 bool floatValid =
false;
67 switch ((*it).scoreType()) {
69 spamError = errorExtractingAgentString;
73 if ((*it).scorePattern().indexIn(mField) == -1) {
81 score = scoreString.
toFloat(&floatValid);
83 spamError = couldNotConverScoreToFloat;
84 qCDebug(MESSAGEVIEWER_LOG) <<
"Score (" << scoreString <<
") is no number";
90 case SpamAgentFloatLarge:
91 score = scoreString.
toFloat(&floatValid);
93 spamError = couldNotConverScoreToFloat;
94 qCDebug(MESSAGEVIEWER_LOG) <<
"Score (" << scoreString <<
") is no number";
98 case SpamAgentAdjustedFloat:
99 score = scoreString.
toFloat(&floatValid);
101 spamError = couldNotConverScoreToFloat;
102 qCDebug(MESSAGEVIEWER_LOG) <<
"Score (" << scoreString <<
") is no number";
108 const QRegExp thresholdPattern = (*it).thresholdPattern();
109 if (thresholdPattern.
indexIn(mField) != -1) {
110 thresholdString = thresholdPattern.
cap(1);
112 spamError = couldNotFindTheThresholdField;
113 qCDebug(MESSAGEVIEWER_LOG) <<
"Threshold could not be extracted from header '" << mField <<
"'";
116 const float threshold = thresholdString.
toFloat(&floatValid);
117 if (!floatValid || (threshold <= 0.0)) {
118 spamError = couldNotConvertThresholdToFloatOrThresholdIsNegative;
119 qCDebug(MESSAGEVIEWER_LOG) <<
"Threshold (" << thresholdString <<
") is no"
120 <<
"number or is negative";
129 }
else if (score > threshold) {
132 score = score / threshold * 100.0;
138 float confidence = -2.0;
139 QString confidenceString = QStringLiteral(
"-2.0");
140 bool confidenceValid =
false;
142 const QByteArray confidenceHeaderName = (*it).confidenceHeader();
144 if (!confidenceHeaderName.
isEmpty()) {
150 QRegExp cScorePattern = (*it).confidencePattern();
151 if (cScorePattern.
indexIn(mCField) != -1) {
152 confidenceString = cScorePattern.
cap(1);
154 confidence = confidenceString.
toFloat(&confidenceValid);
155 if (!confidenceValid) {
156 spamError = couldNotConvertConfidenceToFloat;
157 qCDebug(MESSAGEVIEWER_LOG) <<
"Unable to convert confidence to float:" << confidenceString;
162 scores.
append(
SpamScore((*it).name(), spamError, score, confidence * 100, mField, mCField));