aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/matcher.h
blob: 6696958f0b075e48ddbb0ff6db1978afaab52657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef SMOLBOTE_URLFILTER_MATCHER
#define SMOLBOTE_URLFILTER_MATCHER

#include <QUrl>
#include <QString>
#include <utility>
#include <QRegularExpression>
#include <QStringMatcher>
#include <QWebEngineUrlRequestInfo>

/** An interface class so we can use templated ContentsMatcher interchangeably
 */
class Matcher
{
public:
    virtual ~Matcher() = default;

    virtual void setCaseSensitive(bool matchCase) = 0;
    virtual bool hasMatch(const QString &where) const = 0;
};

template <typename T>
class ContentsMatcher : public Matcher
{
public:
    ContentsMatcher(const QString &pattern, UrlFilter::MatchType type)
    : patternLength(pattern.length())
    , matchType(type)
    {
        if constexpr(std::is_same_v<T, QRegularExpression>) {
            matcher.setPatternOptions(matcher.patternOptions() | QRegularExpression::CaseInsensitiveOption);
            matcher.setPattern(pattern);
        } else if constexpr(std::is_same_v<T, QStringMatcher>) {
            matcher.setCaseSensitivity(Qt::CaseInsensitive);
            matcher.setPattern(pattern);
        } else if constexpr(std::is_same_v<T, QString>) {
            matcher = QUrl::fromUserInput(pattern).host();
        }
    }
    ~ContentsMatcher() = default;

    void setCaseSensitive(bool matchCase) override
    {
        if constexpr(std::is_same_v<T, QRegularExpression>) {
            auto options = matcher.patternOptions();
            options.setFlag(QRegularExpression::CaseInsensitiveOption, !matchCase);
            matcher.setPatternOptions(options);

        } else if constexpr(std::is_same_v<T, QStringMatcher>) {
            matcher.setCaseSensitivity(matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive);
        }
    }

    bool hasMatch(const QString &where) const override
    {
        if constexpr(std::is_same_v<T, QStringMatcher>) {
            switch (matchType) {
            case UrlFilter::InvalidMatch:
            case UrlFilter::RegularExpressionMatch:
            case UrlFilter::DomainMatch:
                qWarning("ContentsMatcher is a String Matcher, but not doing string matching!");
                return false;

            case UrlFilter::StringContains:
                return (matcher.indexIn(where) != -1);

            case UrlFilter::StringStartsWith:
                return (matcher.indexIn(where) == 0);

            case UrlFilter::StringEndsWith:
                return (matcher.indexIn(where) == where.length() - patternLength);

            case UrlFilter::StringEquals:
                return (matcher.indexIn(where) == 0) && (patternLength == where.length());
            }

        } else if constexpr(std::is_same_v<T, QRegularExpression>) {
            if(matchType != UrlFilter::RegularExpressionMatch)
                qWarning("ContentsMatcher is a regular expression, but not doing a regular expression match!");
            return matcher.match(where).hasMatch();
        } else if constexpr(std::is_same_v<T, QString>) {
            // TODO: fix
            if(matchType == UrlFilter::DomainMatch) {
//                qDebug("matching %s", qUtf8Printable(QUrl(where).host()));
                return QUrl(where).host().endsWith(matcher);
            } else
                return matcher == where;
        }

        qWarning("Matcher has no backend, returning false");
        return false;
    }

private:
    const int patternLength;
    const UrlFilter::MatchType matchType;
    T matcher;
};

#endif // SMOLBOTE_URLFILTER_MATCHER