File indexing completed on 2025-03-09 04:54:26

0001 /*
0002     spamheaderanalyzer.h
0003 
0004     This file is part of KMail, the KDE mail client.
0005     SPDX-FileCopyrightText: 2004 Patrick Audley <paudley@blackcat.ca>
0006     SPDX-FileCopyrightText: 2004 Ingo Kloecker <kloecker@kde.org>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #pragma once
0012 
0013 #include <QList>
0014 #include <QString>
0015 namespace KMime
0016 {
0017 class Message;
0018 }
0019 namespace MessageViewer
0020 {
0021 enum SpamError {
0022     noError,
0023     uninitializedStructUsed,
0024     errorExtractingAgentString,
0025     couldNotConverScoreToFloat,
0026     couldNotConvertThresholdToFloatOrThresholdIsNegative,
0027     couldNotFindTheScoreField,
0028     couldNotFindTheThresholdField,
0029     couldNotConvertConfidenceToFloat
0030 };
0031 
0032 /**
0033     @short A simple tuple of error, agent, score, confidence and header.
0034 
0035     The score returned is positive if no error has occurred.
0036     error values indicate the following errors:
0037       noError                                               Spam Headers successfully parsed
0038       uninitializedStructUsed                               Uninitialized struct used
0039       errorExtractingAgentString                            Error extracting agent string
0040       couldNotConverScoreToFloat                            Couldn't convert score to float
0041       couldNotConvertThresholdToFloatOrThresholdIsNegative  Couldn't convert threshold to float or threshold is negative
0042       couldNotFindTheScoreField                             Couldn't find the score field
0043       couldNotFindTheThresholdField                         Couldn't find the threshold field
0044       couldNotConvertConfidenceToFloat                      Couldn't convert confidence to float
0045 */
0046 class SpamScore
0047 {
0048 public:
0049     SpamScore()
0050         : mError(noError)
0051         , mScore(-2.0)
0052         , mConfidence(-2.0)
0053     {
0054     }
0055 
0056     SpamScore(const QString &agent, SpamError error, float score, float confidence, const QString &header, const QString &cheader)
0057         : mAgent(agent)
0058         , mError(error)
0059         , mScore(score)
0060         , mConfidence(confidence)
0061         , mHeader(header)
0062         , mConfidenceHeader(cheader)
0063     {
0064     }
0065 
0066     [[nodiscard]] QString agent() const
0067     {
0068         return mAgent;
0069     }
0070 
0071     [[nodiscard]] float score() const
0072     {
0073         return mScore;
0074     }
0075 
0076     [[nodiscard]] float confidence() const
0077     {
0078         return mConfidence;
0079     }
0080 
0081     [[nodiscard]] SpamError error() const
0082     {
0083         return mError;
0084     }
0085 
0086     [[nodiscard]] QString spamHeader() const
0087     {
0088         return mHeader;
0089     }
0090 
0091     [[nodiscard]] QString confidenceHeader() const
0092     {
0093         return mConfidenceHeader;
0094     }
0095 
0096 private:
0097     QString mAgent;
0098     SpamError mError;
0099     float mScore;
0100     float mConfidence;
0101     QString mHeader;
0102     QString mConfidenceHeader;
0103 };
0104 using SpamScores = QList<SpamScore>;
0105 
0106 /**
0107     @short Flyweight for analysing spam headers.
0108     @author Patrick Audley <paudley@blackcat.ca>
0109   */
0110 class SpamHeaderAnalyzer
0111 {
0112 public:
0113     /**
0114       @short Extract scores from known anti-spam headers
0115       @param message A KMime::Message to examine
0116       @return A list of detected scores. See SpamScore
0117     */
0118     static SpamScores getSpamScores(KMime::Message *message);
0119 };
0120 }