/* * 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; }