aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/domain.cpp
blob: 2bfd5248acb3b904c3b5b2835ccaf8c53f900f13 (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
/*
 * 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 "domain.h"

Domain::Domain(const QString &domain)
    : m_domain(domain)
    , m_hash(qHash(domain, 0))
{
}

Domain::Domain(Domain &&other)
    : m_domain(std::move(other.m_domain))
    , m_hash(std::move(other.m_hash))
{
}

Domain &Domain::operator=(Domain &&other)
{
    m_domain = std::move(other.m_domain);
    m_hash = other.m_hash;
    return *this;
}

bool Domain::matches(const QUrl &url) const
{
    // empty domain matches all
    if(m_domain.isEmpty() || url.isEmpty())
        return true;

    const QString domain = url.host();

    // domain and filter are the same
    if(domain == m_domain) {
        return true;
    }

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

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

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

bool Domain::matchesExactly(uint hash) const
{
    return (m_hash == hash);
}

QString Domain::host() const
{
    return m_domain;
}