aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/filterrule.cpp
blob: 739c78c3bbb1649f6f662e1c226b1f35dc5aa0a8 (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
/*
 * 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
 */

#include "filterrule.h"

inline bool isMatchingDomain(const QString &domain, const QString &filter)
{
    // domain and filter are the same
    if(domain == filter) {
        return true;
    }

    // domain can't be matched by filter if it doesn't end with filter
    // ex. example2.com isn't matched by example.com
    if(!domain.endsWith(filter)) {
        return false;
    }

    // match with subdomains
    // ex. subdomain.example.com is matched by example.com
    int index = domain.indexOf(filter);

    // match if (domain ends with filter) && (filter has been found) and (character before filter is '.')
    return index > 0 && domain[index - 1] == QLatin1Char('.');
}

bool FilterRule::isEnabled() const
{
    return m_isEnabled;
}

bool FilterRule::isBlocking() const
{
    return m_isBlocking;
}

bool FilterRule::matchesDomain(uint domainHash) const
{
    // no domains have been allowed or blocked -> allow on all domains
    if(allowedDomains_hashes.isEmpty() && blockedDomains_hashes.isEmpty()) {
        return true;
    }

    // blockedDomains prevents the rules from being matched on those domains
    if(blockedDomains_hashes.contains(domainHash)) {
        return false;
    }

    // allowedDomains means the rule should only be matched on those domains
    return allowedDomains_hashes.contains(domainHash);

}

bool FilterRule::matchesType(QWebEngineUrlRequestInfo::ResourceType type) const
{
    // no options have been specified -> match all resource types
    if(m_resourceTypeOptions.isEmpty())
        return true;

    // this resource type has not been specified -> reject it
    if(!m_resourceTypeOptions.contains(type))
        return false;

    // resource type has been specified; true to match, false to exception
    return m_resourceTypeOptions.value(type);
}

bool FilterRule::matchesUrl(const QUrl &url) const
{
    switch(urlMatchType) {
    case InvalidMatch:
        return false;

    case RegularExpressionMatch:
        return regexp.match(url.toString()).hasMatch();

    case StringContains:
        return url.toString().contains(match);

    case StringStartsWith:
        return url.toString().startsWith(match);

    case StringEndsWith:
        return url.toString().endsWith(match);

    case StringEquals:
        return url.toString() == match;

    case DomainMatch:
        return isMatchingDomain(url.host(), match);
    }
}