diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-01 16:43:18 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-02 11:47:49 +0200 |
commit | 7d8cbdb9941532cd5bf560b21395f6ed371d1ab5 (patch) | |
tree | 9c5a2d72a3882050f2c3c95ec2d15ad21ff98a93 /lib/web/urlfilter/filterrule.cpp | |
parent | updater: windows fixes (diff) | |
download | smolbote-7d8cbdb9941532cd5bf560b21395f6ed371d1ab5.tar.xz |
Split off UrlFilter into library
- add more adblock filter options
Diffstat (limited to 'lib/web/urlfilter/filterrule.cpp')
-rw-r--r-- | lib/web/urlfilter/filterrule.cpp | 106 |
1 files changed, 0 insertions, 106 deletions
diff --git a/lib/web/urlfilter/filterrule.cpp b/lib/web/urlfilter/filterrule.cpp deleted file mode 100644 index 67ff4d7..0000000 --- a/lib/web/urlfilter/filterrule.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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(const QString &domain) const -{ - // no domains have been allowed or blocked -> allow on all domains - if(allowedDomains.isEmpty() && blockedDomains.isEmpty()) - return true; - - if(!blockedDomains.isEmpty()) { - // do not match rule if the domain has been blocked - if(blockedDomains.contains(domain)) - return false; - } - - if(!allowedDomains.isEmpty()) { - if(allowedDomains.contains(domain)) - return true; - } - - return false; -} - -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); - - } -} - -QString FilterRule::toString() const -{ - return originalFilter; -} |