diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-02-03 19:24:40 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-02-03 19:24:40 +0100 |
commit | 05dbc6fb7d8833c183485ec0d846c151b6299c92 (patch) | |
tree | 570a9710d125e618a3641822802a89233bdbd5ee /src | |
parent | Listing rules in Blocker dialog (diff) | |
download | smolbote-05dbc6fb7d8833c183485ec0d846c151b6299c92.tar.xz |
option parsing
Diffstat (limited to 'src')
-rw-r--r-- | src/forms/blockerdialog.cpp | 4 | ||||
-rw-r--r-- | src/webengine/blockerrule.cpp | 66 | ||||
-rw-r--r-- | src/webengine/blockerrule.h | 32 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 4 |
4 files changed, 88 insertions, 18 deletions
diff --git a/src/forms/blockerdialog.cpp b/src/forms/blockerdialog.cpp index 5377c97..fa613bd 100644 --- a/src/forms/blockerdialog.cpp +++ b/src/forms/blockerdialog.cpp @@ -47,10 +47,10 @@ BlockerDialog::BlockerDialog(QWidget *parent) : // show subscription items for(BlockerRule *rule : m_subscription->urlBlacklist()) { - ui->blacklist_listWidget->addItem(rule->pattern()); + ui->blacklist_listWidget->addItem(rule->toString()); } for(BlockerRule *rule : m_subscription->urlWhitelist()) { - ui->whitelist_listWidget->addItem(rule->pattern()); + ui->whitelist_listWidget->addItem(rule->toString()); } } diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp index 3146854..8ecc1af 100644 --- a/src/webengine/blockerrule.cpp +++ b/src/webengine/blockerrule.cpp @@ -28,6 +28,7 @@ BlockerRule::BlockerRule(QString rule, QObject *parent) : QObject(parent) { + m_rule = rule; QString pattern = rule; // Empty rule or comment @@ -39,7 +40,23 @@ BlockerRule::BlockerRule(QString rule, QObject *parent) : // Exception if(pattern.startsWith("@@")) { m_exception = true; - pattern = pattern.remove(0, 2); + pattern.remove(0, 2); + } + + // Options + if(pattern.contains("$")) { + QString opts = pattern.mid(pattern.indexOf("$")+1); + for(QString opt : opts.split(',')) { + if(opt.endsWith("script")) { + m_blacklistOptions.setFlag(RuleOption::script, true); + m_whitelistOptions.setFlag(RuleOption::script, opt.startsWith("~")); + } else if(opt.endsWith("image")) { + m_blacklistOptions.setFlag(RuleOption::image, true); + m_whitelistOptions.setFlag(RuleOption::image, opt.startsWith("~")); + } + } + + pattern.remove(pattern.indexOf("$"), pattern.length()); } // Domain @@ -61,19 +78,41 @@ BlockerRule::BlockerRule(QString rule, QObject *parent) : m_type = RuleType::WildcardMatch; } -QString BlockerRule::pattern() const +bool BlockerRule::match(const QWebEngineUrlRequestInfo &info) { - return ruleExpression.pattern(); -} + bool shouldBlock = false; -QString BlockerRule::domain() const -{ - return m_domain; -} + switch (m_type) { + case RuleType::Invalid: + shouldBlock = false; + break; -bool BlockerRule::match(const QUrl &url) -{ - return ruleExpression.match(url.toString()).hasMatch(); + case DomainMatch: + if(info.requestUrl().host() == m_domain) { + shouldBlock = true; + } + break; + + case RegularExpressionMatch: + case WildcardMatch: + shouldBlock = ruleExpression.match(info.requestUrl().toString()).hasMatch(); + break; + } + + if(shouldBlock) { + if(m_whitelistOptions.testFlag(RuleOption::image) && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) { + shouldBlock = false; + } + } + if(shouldBlock) { + if(m_blacklistOptions.testFlag(RuleOption::image) && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) { + shouldBlock = true; + } else { + shouldBlock = false; + } + } + + return shouldBlock; } bool BlockerRule::isValid() @@ -89,6 +128,11 @@ bool BlockerRule::isException() return m_exception; } +QString BlockerRule::toString() const +{ + return m_rule; +} + QString BlockerRule::fromWildcardMatch(const QString &pattern) { QString parsed; diff --git a/src/webengine/blockerrule.h b/src/webengine/blockerrule.h index 95d82b7..df09d10 100644 --- a/src/webengine/blockerrule.h +++ b/src/webengine/blockerrule.h @@ -24,6 +24,7 @@ #include <QObject> #include <QUrl> #include <QRegularExpression> +#include <QWebEngineUrlRequestInfo> class BlockerRule : public QObject { @@ -36,13 +37,33 @@ public: WildcardMatch }; + enum RuleOption { + script = 0, + image = 1, + stylesheet = 2, + object = 4, + xmlhttprequest = 8, + objectsubrequest = 16, + subdocument = 32, + ping = 64, + websocket = 128, + document = 256, + + elemhide = 512, + generichide = 1024, + genericblock = 2048, + + other = 4096 + }; + + Q_DECLARE_FLAGS(RuleOptions, RuleOption) + explicit BlockerRule(QString rule, QObject *parent = 0); - QString pattern() const; - QString domain() const; - bool match(const QUrl &url); + bool match(const QWebEngineUrlRequestInfo &info); bool isValid(); bool isException(); + QString toString() const; signals: @@ -52,10 +73,15 @@ private: // TODO: subclass QRegularExpression and move this there QString fromWildcardMatch(const QString &pattern); + QString m_rule; + RuleType m_type; bool m_exception = false; QString m_domain; QRegularExpression ruleExpression; + + RuleOptions m_blacklistOptions; + RuleOptions m_whitelistOptions; }; #endif // ADBLOCKRULE_H diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index cc196ba..fbcef64 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -28,14 +28,14 @@ UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) : void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { for(BlockerRule *rule : m_sub->urlWhitelist()) { - if(rule->match(info.requestUrl())) { + if(rule->match(info)) { qDebug("OK %s", qUtf8Printable(info.requestUrl().toString())); return; } } for(BlockerRule *rule : m_sub->urlBlacklist()) { - if(rule->match(info.requestUrl())) { + if(rule->match(info)) { info.block(true); qDebug(" %s", qUtf8Printable(info.requestUrl().toString())); return; |