aboutsummaryrefslogtreecommitdiff
path: root/staging/smolblok
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-02-10 20:58:39 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-05-30 11:01:35 +0300
commitd1287f43964633035938f4f4d4133bb6d9da7b3e (patch)
treed09efa4074815c20be9bd6348203fe4336dfe716 /staging/smolblok
parentFix segfault in release build (diff)
downloadsmolbote-d1287f43964633035938f4f4d4133bb6d9da7b3e.tar.xz
staging: smolblok
smolblok is a replacement for the current lib/urlfilter AdBlockPlus and hostlist format filter parser. It is a library that uses plugins to provide support for different filter formats. staging/adblock: AdBlockPlus parser plugin plugins/smolblok_hostlist: hostlist format parser plugin Headers will be installed to include/smolbote/ Remove lib/urlfilter
Diffstat (limited to 'staging/smolblok')
-rw-r--r--staging/smolblok/README.md8
-rw-r--r--staging/smolblok/filtermanager.hpp41
-rw-r--r--staging/smolblok/meson.build17
-rw-r--r--staging/smolblok/smolblok.cpp40
-rw-r--r--staging/smolblok/smolblok.hpp80
-rw-r--r--staging/smolblok/test/loader.cpp25
-rw-r--r--staging/smolblok/test/main.cpp22
-rw-r--r--staging/smolblok/test/sample-filters.txt10
8 files changed, 243 insertions, 0 deletions
diff --git a/staging/smolblok/README.md b/staging/smolblok/README.md
new file mode 100644
index 0000000..1793009
--- /dev/null
+++ b/staging/smolblok/README.md
@@ -0,0 +1,8 @@
+## smolblok
+
+### What is this
+This is a C++ library for URL filtering for Qt applications using QtWebEngine.
+
+### Supported formats
+- AdblockPlus without element hiding rules
+
diff --git a/staging/smolblok/filtermanager.hpp b/staging/smolblok/filtermanager.hpp
new file mode 100644
index 0000000..6ee4d3f
--- /dev/null
+++ b/staging/smolblok/filtermanager.hpp
@@ -0,0 +1,41 @@
+/*
+ * 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://library.iserlohn-fortress.net/aqua/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#pragma once
+
+#include <QWebEngineUrlRequestInterceptor>
+#include <smolbote/filterinterface.hpp>
+
+class FilterManager : public QWebEngineUrlRequestInterceptor
+{
+public:
+ FilterManager(QObject *parent = nullptr)
+ : QWebEngineUrlRequestInterceptor(parent)
+ {
+ }
+ ~FilterManager()
+ {
+ qDeleteAll(filters);
+ }
+
+ void addFilterList(FilterList *list) {
+ filters.append(list);
+ }
+
+ void interceptRequest(QWebEngineUrlRequestInfo &info) override
+ {
+ for(const auto *filter : qAsConst(filters)) {
+ if(filter->filter(info)) {
+ return;
+ }
+ }
+ }
+
+private:
+ QList<FilterList *> filters;
+};
diff --git a/staging/smolblok/meson.build b/staging/smolblok/meson.build
new file mode 100644
index 0000000..6105179
--- /dev/null
+++ b/staging/smolblok/meson.build
@@ -0,0 +1,17 @@
+dep_smolblok = declare_dependency(
+ include_directories: [ '.', smolbote_interfaces ],
+ link_with: library('smolblok',
+ [ 'smolblok.cpp' ],
+ include_directories: smolbote_interfaces,
+ dependencies: dep_qt5
+ )
+)
+
+smolblok_load = executable('smolblok-load',
+ dependencies: [ dep_qt5, dep_spdlog, dep_smolblok ],
+ sources: [ 'test/loader.cpp' ]
+)
+
+test('load', smolblok_load, suite: 'smolblok', should_fail: true)
+test('load', smolblok_load, suite: 'smolblok', args: files('meson.build'), should_fail: true)
+
diff --git a/staging/smolblok/smolblok.cpp b/staging/smolblok/smolblok.cpp
new file mode 100644
index 0000000..465c348
--- /dev/null
+++ b/staging/smolblok/smolblok.cpp
@@ -0,0 +1,40 @@
+/*
+ * 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://library.iserlohn-fortress.net/aqua/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "smolblok.hpp"
+#include <QFile>
+#include <QSettings>
+
+bool smolblok::addSubscriptions(const QString &filename)
+{
+ if(filename.isEmpty()) {
+ return false;
+ }
+
+ QSettings listconf(filename, QSettings::IniFormat);
+
+ for(auto &group : listconf.childGroups()) {
+ listconf.beginGroup(group);
+ const auto *loader = m_formats.value(listconf.value("Format").toString()).instance;
+ if(loader != nullptr) {
+ QFile f(listconf.value("File").toString());
+ if(!f.exists()) {
+ continue;
+ }
+
+ auto *list = loader->load(f);
+ f.seek(0);
+ if(loader->parse(list, f)) {
+ m_subscriptions.addFilterList(list);
+ }
+ }
+ listconf.endGroup();
+ }
+ return false;
+}
+
diff --git a/staging/smolblok/smolblok.hpp b/staging/smolblok/smolblok.hpp
new file mode 100644
index 0000000..e547d67
--- /dev/null
+++ b/staging/smolblok/smolblok.hpp
@@ -0,0 +1,80 @@
+/*
+ * 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://library.iserlohn-fortress.net/aqua/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef SMOLBOTE_SMOLBLOK_HPP
+#define SMOLBOTE_SMOLBLOK_HPP
+
+#include "filtermanager.hpp"
+#include <QPluginLoader>
+#include <QWebEngineUrlRequestInterceptor>
+#include <smolbote/filterinterface.hpp>
+
+class smolblok
+{
+public:
+ smolblok() = default;
+ ~smolblok()
+ {
+ for(auto &plugin : m_formats) {
+ delete plugin.loader;
+ }
+ }
+
+ auto registerFormatPlugin(const QString &format, const QString &filename)
+ {
+ struct {
+ bool loaded = false;
+ QString error;
+ } ret;
+
+ if(format.isEmpty() || filename.isEmpty()) {
+ ret.error = "Format or filename is empty";
+ return ret;
+ }
+
+ auto *plugin = new QPluginLoader(filename);
+ if(!plugin->load()) {
+ ret.error = plugin->errorString();
+ delete plugin;
+ return ret;
+ }
+
+ auto *instance = qobject_cast<FilterPlugin *>(plugin->instance());
+ if(instance == nullptr) {
+ ret.error = "Unable to cast";
+ delete plugin;
+ return ret;
+ }
+
+ m_formats[format] = PluginInfo{ plugin, instance };
+ ret.loaded = true;
+ return ret;
+ }
+
+ const auto formats() const
+ {
+ return m_formats.keys();
+ }
+
+ bool addSubscriptions(const QString &filename);
+ QWebEngineUrlRequestInterceptor *interceptor()
+ {
+ return &m_subscriptions;
+ }
+
+private:
+ struct PluginInfo {
+ QPluginLoader *loader = nullptr;
+ FilterPlugin *instance = nullptr;
+ };
+
+ QHash<QString, PluginInfo> m_formats;
+ FilterManager m_subscriptions;
+};
+
+#endif // SMOLBOTE_SMOLBLOK_HPP
diff --git a/staging/smolblok/test/loader.cpp b/staging/smolblok/test/loader.cpp
new file mode 100644
index 0000000..9e27a26
--- /dev/null
+++ b/staging/smolblok/test/loader.cpp
@@ -0,0 +1,25 @@
+#include "smolblok.hpp"
+#include <spdlog/spdlog.h>
+
+int main(int argc, char** argv)
+{
+ if(argc != 2) {
+ spdlog::error("usage: {} path/to/plugin.so", argv[0]);
+ return -1;
+ }
+
+ smolblok filter;
+ {
+ const auto r = filter.registerFormatPlugin("unused", argv[1]);
+ if(r.loaded) {
+ spdlog::info("Loaded plugin {}", argv[1]);
+ } else {
+ spdlog::error("Failed loading plugin {}", argv[1]);
+ spdlog::error(qUtf8Printable(r.error));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
diff --git a/staging/smolblok/test/main.cpp b/staging/smolblok/test/main.cpp
new file mode 100644
index 0000000..5624ee9
--- /dev/null
+++ b/staging/smolblok/test/main.cpp
@@ -0,0 +1,22 @@
+#define CATCH_CONFIG_MAIN
+
+#include "smolblok.hpp"
+#include <catch2/catch.hpp>
+
+SCENARIO("smolblok")
+{
+ smolblok s;
+
+ GIVEN("invalid plugins")
+ {
+ REQUIRE(!s.registerFormatPlugin("", ""));
+ REQUIRE(!s.registerFormatPlugin("Format", "missing.dll"));
+ }
+
+ GIVEN("invalid subscriptions")
+ {
+ REQUIRE(!s.addSubscriptions(""));
+ REQUIRE(!s.addSubscriptions("missing.txt"));
+ }
+}
+
diff --git a/staging/smolblok/test/sample-filters.txt b/staging/smolblok/test/sample-filters.txt
new file mode 100644
index 0000000..59e0e7b
--- /dev/null
+++ b/staging/smolblok/test/sample-filters.txt
@@ -0,0 +1,10 @@
+[easylist-noelemhide]
+Format = AdblockPlus
+File = easylist_noelemhide.txt
+Href = https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt
+
+[StevenBlack]
+Format = Hostlist
+File = stevenblack.txt
+Href = https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
+