aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/rule.h
blob: 90062ba662f091c7a8e0b9cf691ac9b3b7363eba (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
110
111
112
113
114
115
116
117
118
/*
 * 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://library.iserlohn-fortress.net/aqua/smolbote.git
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef SMOLBOTE_ADBLOCK_RULE_H
#define SMOLBOTE_ADBLOCK_RULE_H

#include "options.h"
#include <QObject>
#include <QRegularExpression>
#include <QString>
#include <QStringMatcher>

namespace AdblockPlus
{
class Rule
{
public:
    virtual ~Rule() = default;
    virtual bool hasMatch(const QStringRef &) const = 0;
    bool shouldRedirect() const
    {
        return options.redirect;
    }
    bool shouldBlock() const
    {
        return !options.exception;
    }

protected:
    Rule(const Options &opt)
        : options(opt)
    {
    }
    const Options options;
};

class MatcherRule : public Rule
{
public:
    enum MatchPosition {
        UrlStartsWith,
        UrlEndsWith,
        UrlContains,
        UrlEquals
    };

    MatcherRule(const QString &pattern, const Options &opt, const MatchPosition pos = UrlContains)
        : Rule(opt)
        , position(pos)
        , matcher(pattern, opt.matchcase ? Qt::CaseSensitive : Qt::CaseInsensitive)
        , patternLength(pattern.length())
    {
    }
    explicit MatcherRule(const MatcherRule &) = delete;
    MatcherRule &operator=(const MatcherRule &) = delete;

    explicit MatcherRule(MatcherRule &&) = delete;
    MatcherRule &operator=(MatcherRule &&) = delete;

    ~MatcherRule() = default;
    bool hasMatch(const QStringRef &url) const override
    {
        const auto index = matcher.indexIn(url);

        switch(position) {
        case UrlStartsWith:
            return (index == 0);
        case UrlEndsWith:
            return (index == url.length() - patternLength);
        case UrlContains:
            return (index != -1);
        case UrlEquals:
            return (index == 0 && patternLength == url.length());
        }
    }

private:
    const MatchPosition position;
    const QStringMatcher matcher;
    const int patternLength;
};

class RegexRule : public Rule
{
public:
    RegexRule(const QString &rule, const Options &opt)
        : Rule(opt)
        , regex(rule)
    {
        if(!opt.matchcase) {
            regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
        }
    }
    explicit RegexRule(const RegexRule &) = delete;
    RegexRule &operator=(const RegexRule &) = delete;

    explicit RegexRule(RegexRule &&) = delete;
    RegexRule &operator=(RegexRule &&) = delete;

    ~RegexRule() = default;
    bool hasMatch(const QStringRef &url) const override
    {
        const auto match = regex.match(url);
        return match.hasMatch();
    }

private:
    QRegularExpression regex;
};

} // namespace AdblockPlus

#endif // SMOLBOTE_ADBLOCK_RULE_H