From f7ed63179fa4f99322d6c7716e17466ec4e3e4ce Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 23 Dec 2017 16:45:31 +0100 Subject: Request filter now properly takes hostlists - hostslist directory is set in browser.filterPath --- src/filter/blockermanager.cpp | 41 ---------- src/filter/blockermanager.h | 34 --------- src/filter/filter.cpp | 34 --------- src/filter/filter.h | 42 ----------- src/filter/filtercollection.cpp | 157 --------------------------------------- src/filter/filtercollection.h | 53 ------------- src/filter/filtertree.cpp | 151 ------------------------------------- src/filter/filtertree.h | 39 ---------- src/filter/regexp.cpp | 60 --------------- src/filter/regexp.h | 26 ------- src/filter/subscriptiondialog.ui | 67 ----------------- src/filter/subscriptionform.ui | 125 ------------------------------- 12 files changed, 829 deletions(-) delete mode 100644 src/filter/blockermanager.cpp delete mode 100644 src/filter/blockermanager.h delete mode 100644 src/filter/filter.cpp delete mode 100644 src/filter/filter.h delete mode 100644 src/filter/filtercollection.cpp delete mode 100644 src/filter/filtercollection.h delete mode 100644 src/filter/filtertree.cpp delete mode 100644 src/filter/filtertree.h delete mode 100644 src/filter/regexp.cpp delete mode 100644 src/filter/regexp.h delete mode 100644 src/filter/subscriptiondialog.ui delete mode 100644 src/filter/subscriptionform.ui (limited to 'src/filter') diff --git a/src/filter/blockermanager.cpp b/src/filter/blockermanager.cpp deleted file mode 100644 index 718f8a1..0000000 --- a/src/filter/blockermanager.cpp +++ /dev/null @@ -1,41 +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 "blockermanager.h" -#include "ui_subscriptiondialog.h" - -#include "browser.h" -#include -#include - -#include "filtercollection.h" - -BlockerManager::BlockerManager(QWidget *parent) : - QDialog(parent), - ui(new Ui::UrlInterceptorDialog) -{ - ui->setupUi(this); - - const QStringList subscriptions = browser->settings()->value("blocker.subscriptions").toStringList(); - QStringList::const_iterator i; - for(i = subscriptions.constBegin(); i != subscriptions.constEnd(); ++i) { - FilterCollection *sub = new FilterCollection(QString(*i), this); - m_subscriptions.append(sub); - ui->tabWidget->addTab(sub, sub->name()); - } -} - -BlockerManager::~BlockerManager() -{ - delete ui; -} - -QVector BlockerManager::subscriptions() const -{ - return m_subscriptions; -} diff --git a/src/filter/blockermanager.h b/src/filter/blockermanager.h deleted file mode 100644 index df426b3..0000000 --- a/src/filter/blockermanager.h +++ /dev/null @@ -1,34 +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 - */ - -#ifndef URLINTERCEPTORDIALOG_H -#define URLINTERCEPTORDIALOG_H - -#include - -namespace Ui { -class UrlInterceptorDialog; -} - -class FilterCollection; -class BlockerManager : public QDialog -{ - Q_OBJECT - -public: - explicit BlockerManager(QWidget *parent = 0); - ~BlockerManager(); - - QVector subscriptions() const; - -private: - Ui::UrlInterceptorDialog *ui; - QVector m_subscriptions; -}; - -#endif // URLINTERCEPTORDIALOG_H diff --git a/src/filter/filter.cpp b/src/filter/filter.cpp deleted file mode 100644 index 35eb9e7..0000000 --- a/src/filter/filter.cpp +++ /dev/null @@ -1,34 +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 "filter.h" -#include - -FilterRule::FilterRule(const QString &line) -{ - parse(line); -} - -FilterRule::~FilterRule() -{ -} - -bool FilterRule::shouldBlock(const QUrl &requestUrl) const -{ - if(matcher.indexIn(requestUrl.toString()) == -1) { - // pattern not found in url - return false; - } - - return true; -} - -void FilterRule::parse(const QString &line) -{ - matcher.setPattern(line); -} diff --git a/src/filter/filter.h b/src/filter/filter.h deleted file mode 100644 index f152e7a..0000000 --- a/src/filter/filter.h +++ /dev/null @@ -1,42 +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 - */ - -#ifndef FILTERRULE_H -#define FILTERRULE_H - -#include - -class QUrl; -class FilterRule -{ -public: - - FilterRule(const QString &line); - - // delete the copy constructor and assignment operator - FilterRule(const FilterRule&) = delete; - FilterRule& operator=(const FilterRule&) = delete; - - // move constructor - FilterRule(FilterRule&& other) { - matcher = other.matcher; - } - - ~FilterRule(); - - bool shouldBlock(const QUrl &requestUrl) const; - -private: - void parse(const QString &line); - - // QStringMatcher: holds a pattern you want to repeatedly match against some strings - QStringMatcher matcher; - -}; - -#endif // FILTERRULE_H diff --git a/src/filter/filtercollection.cpp b/src/filter/filtercollection.cpp deleted file mode 100644 index e151a10..0000000 --- a/src/filter/filtercollection.cpp +++ /dev/null @@ -1,157 +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 "filtercollection.h" -#include "ui_subscriptionform.h" - -#include "browser.h" -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include "filtertree.h" -#include "filter.h" - -FilterCollection::FilterCollection(const QString path, QWidget *parent) : - QWidget(parent), - ui(new Ui::SubscriptionForm) -{ - ui->setupUi(this); - - ui->homepage->setText(path); - - qDebug("Adding subscription [%s]", qUtf8Printable(path)); - - m_filters = new FilterTree(this); - ui->treeView->setModel(m_filters); - - QFile filterFile(path); - if(!filterFile.open(QIODevice::ReadOnly)) { - qWarning("Could not open filter!"); - return; - } - - QJsonDocument filters(QJsonDocument::fromJson(filterFile.readAll())); - qDebug("Added %i rules", load(filters.object())); -} - -FilterCollection::~FilterCollection() -{ - delete ui; -} - -QString FilterCollection::name() const -{ - return ui->title->text(); -} - -/** - * Check if a URL request should be blocked or not - * @param info - * @return true if it should be blocked; false otherwise - */ -bool FilterCollection::match(QWebEngineUrlRequestInfo &info) -{ - for(QString domain : m_filterlist.keys()) { - if(domain.isEmpty() || info.firstPartyUrl().toString().contains(domain)) { - // domain matched - - for(QString rule : m_filterlist.value(domain).keys()) { - if(rule.isEmpty() || info.requestUrl().toString().contains(rule)) { - return m_filterlist.value(domain).value(rule); - } - } - } - } - - return false; -} - -/** - * Load rules from JSON object - * @param json - */ -int FilterCollection::load(const QJsonObject &json) -{ - int number = 0; - - ui->title->setText(json["name"].toString()); - ui->homepage->setText(json["url"].toString()); - - QJsonObject rules = json["rules"].toObject(); - for(QString key : rules.keys()) { - ++number; - qDebug("+ '%s'", qUtf8Printable(key)); - - QJsonObject domain = rules.value(key).toObject(); - QHash d; - - for(QString r : domain.keys()) { - d.insert(r, domain.value(r).toBool()); - qDebug("|-'%s': %i", qUtf8Printable(r), domain.value(r).toBool()); - } - - m_filterlist.insert(key, d); - - } - return number; -} - -Filter::Resources FilterCollection::parseJsonRules(const QJsonValue &obj) -{ - Filter::Resources res; - for(QJsonValue v : obj.toArray()) { - QString t = v.toString(); - if(t == "MainFrame") { - res.setFlag(Filter::ResourceType::MainFrame); - } else if(t == "SubFrame") { - res.setFlag(Filter::ResourceType::SubFrame); - } else if(t == "Stylesheet") { - res.setFlag(Filter::ResourceType::Stylesheet); - } else if(t == "Script") { - res.setFlag(Filter::ResourceType::Script); - } else if(t == "Image") { - res.setFlag(Filter::ResourceType::Image); - } else if(t == "FontResource") { - res.setFlag(Filter::ResourceType::Font); - } else if(t == "SubResource") { - res.setFlag(Filter::ResourceType::SubResource); - } else if(t == "Object") { - res.setFlag(Filter::ResourceType::Object); - } else if(t == "Media") { - res.setFlag(Filter::ResourceType::Media); - } else if(t == "Worker") { - res.setFlag(Filter::ResourceType::Worker); - } else if(t == "SharedWorker") { - res.setFlag(Filter::ResourceType::SharedWorker); - } else if(t == "Prefetch") { - res.setFlag(Filter::ResourceType::Prefetch); - } else if(t == "Favicon") { - res.setFlag(Filter::ResourceType::Favicon); - } else if(t == "Xhr") { - res.setFlag(Filter::ResourceType::Xhr); - } else if(t == "Ping") { - res.setFlag(Filter::ResourceType::Ping); - } else if(t == "ServiceWorker") { - res.setFlag(Filter::ResourceType::ServiceWorker); - } else if(t == "CspWorker") { - res.setFlag(Filter::ResourceType::CspReport); - } else if(t == "PluginResource") { - res.setFlag(Filter::ResourceType::PluginResource); - } - } - - return res; -} diff --git a/src/filter/filtercollection.h b/src/filter/filtercollection.h deleted file mode 100644 index 4249eea..0000000 --- a/src/filter/filtercollection.h +++ /dev/null @@ -1,53 +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 - */ - -#ifndef SUBSCRIPTIONFORM_H -#define SUBSCRIPTIONFORM_H - -#include -#include -#include "filtertree.h" - -#include - -namespace Ui { -class SubscriptionForm; -} - -class FilterCollection : public QWidget -{ - Q_OBJECT - -public: - struct MatchResult { - bool match; - bool block; - QString pattern; - }; - - explicit FilterCollection(const QString path, QWidget *parent = 0); - ~FilterCollection(); - - QString name() const; - bool match(QWebEngineUrlRequestInfo &info); - -private slots: - int load(const QJsonObject &json); - -private: - Filter::Resources parseJsonRules(const QJsonValue &obj); - - Ui::SubscriptionForm *ui; - - FilterTree *m_filters; - - QHash> m_filterlist; - -}; - -#endif // SUBSCRIPTIONFORM_H diff --git a/src/filter/filtertree.cpp b/src/filter/filtertree.cpp deleted file mode 100644 index 73cc261..0000000 --- a/src/filter/filtertree.cpp +++ /dev/null @@ -1,151 +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 "filtertree.h" - -FilterTree::FilterTree(QObject *parent) : - QAbstractItemModel(parent) -{ - rootItem = new Filter("", "", Filter::ResourceRules(), false); -} - -FilterTree::~FilterTree() -{ - delete rootItem; -} - -QModelIndex FilterTree::index(int row, int column, const QModelIndex &parent) const -{ - if(!hasIndex(row, column, parent)) { - return QModelIndex(); - } - - Filter *parentItem; - if(!parent.isValid()) { - parentItem = rootItem; - } else { - parentItem = static_cast(parent.internalPointer()); - } - - Filter *childItem = parentItem->child(row); - if(childItem) { - return createIndex(row, column, childItem); - } else { - return QModelIndex(); - } -} - -QModelIndex FilterTree::parent(const QModelIndex &index) const -{ - if(!index.isValid()) { - return QModelIndex(); - } - - Filter *childItem = static_cast(index.internalPointer()); - Filter *parentItem = childItem->parentItem(); - - if(parentItem == rootItem) { - return QModelIndex(); - } - - return createIndex(parentItem->row(), 0, parentItem); -} - -int FilterTree::rowCount(const QModelIndex &parent) const -{ - Filter *parentItem; - if(parent.column() > 0) { - return 0; - } - - if(!parent.isValid()) { - parentItem = rootItem; - } else { - parentItem = static_cast(parent.internalPointer()); - } - - return parentItem->childCount(); -} - -int FilterTree::columnCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent) - return 5; -} - -QVariant FilterTree::data(const QModelIndex &index, int role) const -{ - if(!index.isValid()) { - return QVariant(); - } - - if(role != Qt::DisplayRole) { - return QVariant(); - } - - Filter *n = static_cast(index.internalPointer()); - switch (index.column()) { - case 0: // domain - return QVariant(n->domain()); - case 1: // request - return QVariant(n->request()); - case 2: // should block - return QVariant(n->isBlocking() ? "true" : "false"); - case 3: // allowed types - return QVariant(n->allowedTypes()); - case 4: // blocked types - return QVariant(n->blockedTypes()); - default: - return QVariant(); - } -} - -QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const -{ - if(orientation != Qt::Horizontal) { - return QVariant(); - } - - if(role != Qt::DisplayRole) { - return QVariant(); - } - - switch (section) { - case 0: - return QVariant(tr("Domain")); - case 1: - return QVariant(tr("Request")); - case 2: - return QVariant(tr("Should Block")); - case 3: - return QVariant(tr("Allowed Types")); - case 4: - return QVariant(tr("Blocked Types")); - default: - return QVariant(); - } -} - -Filter *FilterTree::addFilter(const QString &domain, const QString &request, Filter::ResourceRules rules, bool shouldBlock) -{ - Filter *node = new Filter(domain, request, rules, shouldBlock, rootItem); - node->enable(); - rootItem->appendChild(node); - return node; -} - -QVector FilterTree::filters(const QString &domain) -{ - QVector nodes; - for(int i = 0; i < rootItem->childCount(); ++i) { - if(rootItem->child(i)->hasDomainMatch(domain)) { - nodes.append(rootItem->child(i)); - } - } - return nodes; -} diff --git a/src/filter/filtertree.h b/src/filter/filtertree.h deleted file mode 100644 index cbf339f..0000000 --- a/src/filter/filtertree.h +++ /dev/null @@ -1,39 +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 - */ - -#ifndef FILTERTREE_H -#define FILTERTREE_H - -#include -#include -#include -#include "filter.h" - -class FilterTree : public QAbstractItemModel -{ - Q_OBJECT - -public: - explicit FilterTree(QObject *parent = 0); - ~FilterTree(); - - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; - QModelIndex parent(const QModelIndex &index) const override; - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role) const override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - - Filter *addFilter(const QString &domain, const QString &request, Filter::ResourceRules rules, bool shouldBlock); - QVector filters(const QString &domain = ""); - -private: - Filter *rootItem; -}; - -#endif // FILTERTREE_H diff --git a/src/filter/regexp.cpp b/src/filter/regexp.cpp deleted file mode 100644 index e46bc75..0000000 --- a/src/filter/regexp.cpp +++ /dev/null @@ -1,60 +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 "regexp.h" - -RegExp::RegExp(const QString &pattern, PatternOptions options) : - QRegularExpression() -{ - setPattern(pattern); - setPatternOptions(options); -} - -bool RegExp::hasMatch(const QString &subject, int offset, MatchType matchType, MatchOptions matchOptions) const -{ - // Empty matches all - if(pattern().isEmpty()) { - return true; - } - - return QRegularExpression::match(subject, offset, matchType, matchOptions).hasMatch(); -} - -void RegExp::setWildcardPattern(const QString &pattern) -{ - QString parsed; - - for(int i=0; i - -/*! - * Regular Expression class for AdBlockPlus filters - */ -class RegExp : public QRegularExpression -{ -public: - explicit RegExp(const QString &pattern = "", PatternOptions options = NoPatternOption); - - bool hasMatch(const QString &subject, int offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const; - void setWildcardPattern(const QString &pattern); -}; - -#endif // REGEXP_H diff --git a/src/filter/subscriptiondialog.ui b/src/filter/subscriptiondialog.ui deleted file mode 100644 index 7a63cc9..0000000 --- a/src/filter/subscriptiondialog.ui +++ /dev/null @@ -1,67 +0,0 @@ - - - UrlInterceptorDialog - - - - 0 - 0 - 640 - 480 - - - - Blocker - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Close - - - - - - - - - buttonBox - accepted() - UrlInterceptorDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - UrlInterceptorDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/src/filter/subscriptionform.ui b/src/filter/subscriptionform.ui deleted file mode 100644 index 0a20582..0000000 --- a/src/filter/subscriptionform.ui +++ /dev/null @@ -1,125 +0,0 @@ - - - SubscriptionForm - - - - 0 - 0 - 640 - 480 - - - - Form - - - - - - Subscription - - - - - - - - Title - - - - - - - TextLabel - - - - - - - Homepage - - - - - - - TextLabel - - - - - - - License - - - - - - - TextLabel - - - - - - - - - - - Version - - - - - - - TextLabel - - - - - - - Last Modified - - - - - - - TextLabel - - - - - - - Expires - - - - - - - TextLabel - - - - - - - - - - - - - - - - -- cgit v1.2.1