aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-01-27 17:14:49 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-01-27 17:14:49 +0100
commited7430d2352b3f87991f68fbc0acfe4dbae39b56 (patch)
treebd6d9d0c3c0f6e66131f581bed978c49febada7f
parentBlocker UI (diff)
downloadsmolbote-ed7430d2352b3f87991f68fbc0acfe4dbae39b56.tar.xz
URL blocking whitelist
-rw-r--r--src/forms/blockerdialog.cpp21
-rw-r--r--src/forms/blockerdialog.h3
-rw-r--r--src/mainwindow.cpp3
-rw-r--r--src/webengine/blockerrule.cpp21
-rw-r--r--src/webengine/blockerrule.h4
-rw-r--r--src/webengine/blockersubscription.cpp28
-rw-r--r--src/webengine/blockersubscription.h8
-rw-r--r--src/webengine/urlinterceptor.cpp33
-rw-r--r--src/webengine/urlinterceptor.h7
-rw-r--r--test/blocklist.txt2
10 files changed, 103 insertions, 27 deletions
diff --git a/src/forms/blockerdialog.cpp b/src/forms/blockerdialog.cpp
index f755af6..ee6bdfc 100644
--- a/src/forms/blockerdialog.cpp
+++ b/src/forms/blockerdialog.cpp
@@ -11,21 +11,26 @@ BlockerDialog::BlockerDialog(QWidget *parent) :
Settings settings;
ui->setupUi(this);
- subscription = new BlockerSubscription(this);
+ m_subscription = new BlockerSubscription(this);
QString sublocation = settings.value("blocker/subscription").toString();
if(!sublocation.isEmpty()) {
- subscription->loadFromFile(sublocation);
+ m_subscription->loadFromFile(sublocation);
}
- ui->title->setText(subscription->title());
- ui->homepage->setText(subscription->homepage());
- ui->license->setText(subscription->license());
- ui->version->setText(subscription->version());
- ui->lastModified->setText(subscription->lastModified().toString());
- ui->expires->setText(subscription->expires().toString());
+ ui->title->setText(m_subscription->title());
+ ui->homepage->setText(m_subscription->homepage());
+ ui->license->setText(m_subscription->license());
+ ui->version->setText(m_subscription->version());
+ ui->lastModified->setText(m_subscription->lastModified().toString());
+ ui->expires->setText(m_subscription->expires().toString());
}
BlockerDialog::~BlockerDialog()
{
delete ui;
}
+
+BlockerSubscription* BlockerDialog::subscription()
+{
+ return m_subscription;
+}
diff --git a/src/forms/blockerdialog.h b/src/forms/blockerdialog.h
index 0c8e8ba..59dce0a 100644
--- a/src/forms/blockerdialog.h
+++ b/src/forms/blockerdialog.h
@@ -15,10 +15,11 @@ class BlockerDialog : public QDialog
public:
explicit BlockerDialog(QWidget *parent = 0);
~BlockerDialog();
+ BlockerSubscription *subscription();
private:
Ui::UrlInterceptorDialog *ui;
- BlockerSubscription *subscription;
+ BlockerSubscription *m_subscription;
};
#endif // URLINTERCEPTORDIALOG_H
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 1062823..8f54b0b 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -137,7 +137,8 @@ void MainWindow::loadProfile(const QString &name)
profile = new WebEngineProfile(profileName, this);
}
- AdBlockInterceptor *interceptor = new AdBlockInterceptor(this);
+ UrlRequestInterceptor *interceptor = new UrlRequestInterceptor(this);
+ interceptor->setSubscription(blocklistManager->subscription());
profile->setRequestInterceptor(interceptor);
connect(profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), downloadManager, SLOT(addDownload(QWebEngineDownloadItem*)));
}
diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp
index a4a0982..340e8bd 100644
--- a/src/webengine/blockerrule.cpp
+++ b/src/webengine/blockerrule.cpp
@@ -3,10 +3,29 @@
BlockerRule::BlockerRule(QString rule, QObject *parent) :
QObject(parent)
{
- ruleExpression.setPattern(rule);
+ QString pattern = rule;
+
+ if(rule.startsWith("@@")) {
+ m_exception = true;
+ pattern = pattern.remove(0, 2);
+ } else {
+ m_exception = false;
+ }
+
+ ruleExpression.setPattern(pattern);
+ m_valid = true;
}
bool BlockerRule::match(const QUrl &url)
{
return ruleExpression.match(url.toString()).hasMatch();
}
+
+bool BlockerRule::isValid()
+{
+ return m_valid;
+}
+bool BlockerRule::isException()
+{
+ return m_exception;
+}
diff --git a/src/webengine/blockerrule.h b/src/webengine/blockerrule.h
index 9d73004..ddbd1c9 100644
--- a/src/webengine/blockerrule.h
+++ b/src/webengine/blockerrule.h
@@ -12,12 +12,16 @@ public:
explicit BlockerRule(QString rule, QObject *parent = 0);
bool match(const QUrl &url);
+ bool isValid();
+ bool isException();
signals:
public slots:
private:
+ bool m_valid;
+ bool m_exception;
QRegularExpression ruleExpression;
};
diff --git a/src/webengine/blockersubscription.cpp b/src/webengine/blockersubscription.cpp
index 5a0664b..c7f647b 100644
--- a/src/webengine/blockersubscription.cpp
+++ b/src/webengine/blockersubscription.cpp
@@ -24,6 +24,9 @@ int BlockerSubscription::loadFromFile(const QString &file)
return -1;
}
+ // clear all lists
+ m_urlBlacklist.clear();
+ m_urlWhitelist.clear();
int rules = 0;
while(!subscription.atEnd()) {
@@ -33,14 +36,23 @@ int BlockerSubscription::loadFromFile(const QString &file)
parseComment(line);
} else {
// The line is not a comment
- BlockerRule *rule = new BlockerRule(line, this);
- m_urlBlacklist.append(rule);
+
rules++;
+ BlockerRule *rule = new BlockerRule(line, this);
+
+ if(rule->isValid()) {
+ if(rule->isException()) {
+ m_urlWhitelist.append(rule);
+ } else {
+ m_urlBlacklist.append(rule);
+ }
+ }
+
}
}
}
- qDebug("Loaded %i rules from subscription %s", rules, qUtf8Printable(file));
+ qDebug("Loaded %i/%i rules from subscription %s", m_urlBlacklist.count() + m_urlWhitelist.count(), rules, qUtf8Printable(file));
return rules;
}
@@ -74,6 +86,16 @@ const QDateTime BlockerSubscription::expires()
return m_expires;
}
+const QList<BlockerRule*> BlockerSubscription::urlBlacklist()
+{
+ return m_urlBlacklist;
+}
+
+const QList<BlockerRule*> BlockerSubscription::urlWhitelist()
+{
+ return m_urlWhitelist;
+}
+
void BlockerSubscription::parseComment(const QString &line)
{
if(line.startsWith("! Title: ")) {
diff --git a/src/webengine/blockersubscription.h b/src/webengine/blockersubscription.h
index 1379936..1bb8919 100644
--- a/src/webengine/blockersubscription.h
+++ b/src/webengine/blockersubscription.h
@@ -20,6 +20,9 @@ public:
const QDateTime lastModified();
const QDateTime expires();
+ const QList<BlockerRule*> urlBlacklist();
+ const QList<BlockerRule*> urlWhitelist();
+
signals:
public slots:
@@ -35,7 +38,10 @@ private:
QDateTime m_lastModified;
QDateTime m_expires;
- QList<BlockerRule*> m_urlBlacklist;
+ QList<BlockerRule*> m_urlWhitelist; // exception rules
+ QList<BlockerRule*> m_urlBlacklist; // block rules
+ // element exceptions
+ // element blocking
};
#endif // URLINTERCEPTORSUBSCRIPTION_H
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp
index 87879a5..2bd50b8 100644
--- a/src/webengine/urlinterceptor.cpp
+++ b/src/webengine/urlinterceptor.cpp
@@ -1,19 +1,32 @@
#include "urlinterceptor.h"
-AdBlockInterceptor::AdBlockInterceptor(QObject *parent) :
+UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) :
QWebEngineUrlRequestInterceptor(parent)
{
}
-void AdBlockInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
+void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
-// bool blocked = false;
-// for(AdBlockRule *rule : m_urlBlacklist) {
-// if(rule->match(info.requestUrl())) {
-// info.block(true);
-// blocked = true;
-// }
-// }
+ for(BlockerRule *rule : m_sub->urlWhitelist()) {
+ if(rule->match(info.requestUrl())) {
+ qDebug("OK %s", qUtf8Printable(info.requestUrl().toString()));
+ return;
+ }
+ }
- qDebug("%i %i %s %s", info.navigationType(), info.resourceType(), qUtf8Printable(info.requestMethod()), qUtf8Printable(info.requestUrl().toString()));
+ for(BlockerRule *rule : m_sub->urlBlacklist()) {
+ if(rule->match(info.requestUrl())) {
+ info.block(true);
+ qDebug(" %s", qUtf8Printable(info.requestUrl().toString()));
+ return;
+ }
+ }
+
+ qDebug("OK %s", qUtf8Printable(info.requestUrl().toString()));
+
+}
+
+void UrlRequestInterceptor::setSubscription(BlockerSubscription *subscription)
+{
+ m_sub = subscription;
}
diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h
index 7834430..31f1256 100644
--- a/src/webengine/urlinterceptor.h
+++ b/src/webengine/urlinterceptor.h
@@ -2,20 +2,23 @@
#define ADBLOCKINTERCEPTOR_H
#include <QWebEngineUrlRequestInterceptor>
+#include "blockersubscription.h"
-class AdBlockInterceptor : public QWebEngineUrlRequestInterceptor
+class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
Q_OBJECT
public:
- explicit AdBlockInterceptor(QObject *parent = 0);
+ explicit UrlRequestInterceptor(QObject *parent = 0);
void interceptRequest(QWebEngineUrlRequestInfo &info);
+ void setSubscription(BlockerSubscription *subscription);
signals:
public slots:
private:
+ BlockerSubscription *m_sub;
};
#endif // ADBLOCKINTERCEPTOR_H
diff --git a/test/blocklist.txt b/test/blocklist.txt
index 376b168..1c22798 100644
--- a/test/blocklist.txt
+++ b/test/blocklist.txt
@@ -12,3 +12,5 @@
!
! -----------------------General advert blocking filters-----------------------!
!css
+1@@s1252.css
+!@@t1252.css