diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-12-23 16:45:31 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-12-23 16:45:31 +0100 |
commit | f7ed63179fa4f99322d6c7716e17466ec4e3e4ce (patch) | |
tree | f187d1f73300f1ac0882a4be569d567bb4ed29f7 /lib/adblock/filterrule.cpp | |
parent | Removed SingleApplication::SessionParam (diff) | |
download | smolbote-f7ed63179fa4f99322d6c7716e17466ec4e3e4ce.tar.xz |
Request filter now properly takes hostlists
- hostslist directory is set in browser.filterPath
Diffstat (limited to 'lib/adblock/filterrule.cpp')
-rw-r--r-- | lib/adblock/filterrule.cpp | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/lib/adblock/filterrule.cpp b/lib/adblock/filterrule.cpp deleted file mode 100644 index 79ded6d..0000000 --- a/lib/adblock/filterrule.cpp +++ /dev/null @@ -1,90 +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: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "filterrule.h" -#include <QUrl> - -/* TODO - * - split this class into two: FilterRule that only deals with matching, and AdBlockRule, which only deals with parsing - */ - -/* The AdBlock standard is an incredible mess, vaguely explaiend on: - * - https://adblockplus.org/filters - * - https://adblockplus.org/filter-cheatsheet - */ - -FilterRule::FilterRule(const QString &line) -{ - valid = parse(line); -} - -FilterRule::~FilterRule() -{ -} - -bool FilterRule::isValid() const -{ - return valid; -} - -bool FilterRule::isException() const -{ - return exception; -} - -bool FilterRule::shouldBlock(const QUrl &requestUrl) const -{ - QRegularExpressionMatch match = rule.match(requestUrl.toString()); - return match.hasMatch(); -} - -bool FilterRule::parse(const QString &line) -{ - // skip for comments and empty rules - if(line.startsWith('!') || line.trimmed().isEmpty()) { - return false; - } - - // make a copy of the pattern so we can snap off the parts we've already parsed - QString pattern = line; - - if(pattern.startsWith("@@")) { - exception = true; - pattern = pattern.mid(2); // remove @@ - } - - rule.setPattern(createRegExpPattern(pattern)); - return true; -} - -QString createRegExpPattern(const QString &line) -{ - QString pattern = line; - - // replace . (any character) with \. (a dot) - pattern.replace('.', "\\."); - - // translate adblock special characters into regex - // replace wildcard (*) with '.*' (zero or more of any element) - pattern.replace('*', ".*"); - - // replace separator (^) with '($|\?|\/)' (end of string, or ?, or /, or :number) - pattern.replace('^', "($|\\?|\\/|:\\d+)"); - - // replace || with ^\w+://([\w,\d,\.]+)? - pattern.replace("||", "^\\w+://([\\w,\\d,\\.]+)?"); - - if(pattern.startsWith('|') && pattern.endsWith('|')) { - // replace | at start with ^ (start of string) - pattern.replace(0, 1, '^'); - // replace | at end with $ (end of string) - pattern.replace('|', '$'); - } - - return pattern; -} |