From 7d8cbdb9941532cd5bf560b21395f6ed371d1ab5 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 1 Oct 2018 16:43:18 +0200 Subject: Split off UrlFilter into library - add more adblock filter options --- lib/urlfilter/filterrule.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 lib/urlfilter/filterrule.cpp (limited to 'lib/urlfilter/filterrule.cpp') diff --git a/lib/urlfilter/filterrule.cpp b/lib/urlfilter/filterrule.cpp new file mode 100644 index 0000000..22a2f06 --- /dev/null +++ b/lib/urlfilter/filterrule.cpp @@ -0,0 +1,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/smolbote.hg + * + * 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); + } +} -- cgit v1.2.1