diff options
-rw-r--r-- | include/smolbote/filterinterface.hpp | 16 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | staging/smolblok/filtermanager.cpp | 27 | ||||
-rw-r--r-- | staging/smolblok/filtermanager.hpp | 36 | ||||
-rw-r--r-- | staging/smolblok/meson.build | 11 | ||||
-rw-r--r-- | staging/smolblok/smolblok.cpp | 58 | ||||
-rw-r--r-- | staging/smolblok/smolblok.hpp | 48 | ||||
-rw-r--r-- | staging/smolblok/test/main.cpp | 28 | ||||
-rw-r--r-- | staging/smolblok/test/sample-filters.txt | 6 |
9 files changed, 152 insertions, 80 deletions
diff --git a/include/smolbote/filterinterface.hpp b/include/smolbote/filterinterface.hpp index 09c9269..1ae65cc 100644 --- a/include/smolbote/filterinterface.hpp +++ b/include/smolbote/filterinterface.hpp @@ -6,33 +6,35 @@ * SPDX-License-Identifier: MIT */ -#ifndef SMOLBOTE_FILTER_HPP -#define SMOLBOTE_FILTER_HPP +#ifndef SMOLBOTE_SMOLBLOK_INTERFACE_HPP +#define SMOLBOTE_SMOLBLOK_INTERFACE_HPP #include <QWebEngineUrlRequestInfo> #include <QtPlugin> -class Filter +class QIODevice; + +class FilterList { public: - virtual ~Filter() = default; + virtual ~FilterList() = default; [[nodiscard]] virtual bool filter(QWebEngineUrlRequestInfo &info) const = 0; [[nodiscard]] virtual bool isUpToDate() const = 0; }; // A class to provide filter interfaces -class QIODevice; class FilterPlugin { public: virtual ~FilterPlugin() = default; - virtual Filter* load(QIODevice* from) const = 0; + virtual FilterList *load(QIODevice* from) const = 0; + virtual bool update(QIODevice *f, const QUrl &upstream) const = 0; }; #define FilterPluginIid "net.iserlohn-fortress.smolbote.FilterPlugin" Q_DECLARE_INTERFACE(FilterPlugin, FilterPluginIid) -#endif // SMOLBOTE_FILTER_HPP +#endif // SMOLBOTE_SMOLBLOK_INTERFACE_HPP diff --git a/meson.build b/meson.build index a34b551..ce3dbf5 100644 --- a/meson.build +++ b/meson.build @@ -91,8 +91,6 @@ subdir('tools') subdir('test/firefox-bookmarks-json-parser') subdir('test/matcherbenchmark') -subdir('staging/adblock') -subdir('staging/hostlist') subdir('staging/smolblok') ssconfig = poi_sourceset.apply(cdata) diff --git a/staging/smolblok/filtermanager.cpp b/staging/smolblok/filtermanager.cpp deleted file mode 100644 index 05beb59..0000000 --- a/staging/smolblok/filtermanager.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "filtermanager.hpp" -#include "filterlist.h" -#include <QFile> - -Filter *FilterManager::addSubscription(const QString &filename, const QUrl &url) -{ - if(filename.isEmpty() && url.isEmpty()) { - return nullptr; - } - - QFile f(filename); - if(!f.exists()) { - // TODO download file - return nullptr; - } - - if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - return nullptr; - } - - try { - auto *filter = new AdblockPlus::FilterList(f); - return filter; - } catch(std::exception &) { - return nullptr; - } -} diff --git a/staging/smolblok/filtermanager.hpp b/staging/smolblok/filtermanager.hpp index 2620bd8..6ee4d3f 100644 --- a/staging/smolblok/filtermanager.hpp +++ b/staging/smolblok/filtermanager.hpp @@ -1,6 +1,13 @@ +/* + * 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 <QSettings> #include <QWebEngineUrlRequestInterceptor> #include <smolbote/filterinterface.hpp> @@ -16,26 +23,8 @@ public: qDeleteAll(filters); } - auto installSubscriptions(const QString &filename) - { - struct { - bool success = true; - } ret; - - QSettings listconf(filename, QSettings::IniFormat); - - for(const auto &group : listconf.childGroups()) { - listconf.beginGroup(group); - auto *r = addSubscription(listconf.value("File").toString(), listconf.value("Href").toString()); - if(r != nullptr) { - filters.append(r); - } else { - ret.success = false; - } - listconf.endGroup(); - } - - return ret; + void addFilterList(FilterList *list) { + filters.append(list); } void interceptRequest(QWebEngineUrlRequestInfo &info) override @@ -47,9 +36,6 @@ public: } } -protected: - static Filter *addSubscription(const QString &filename, const QUrl &url); - private: - QList<Filter *> filters; + QList<FilterList *> filters; }; diff --git a/staging/smolblok/meson.build b/staging/smolblok/meson.build index d563fcd..8ae0c74 100644 --- a/staging/smolblok/meson.build +++ b/staging/smolblok/meson.build @@ -1,18 +1,15 @@ dep_smolblok = declare_dependency( include_directories: [ '.', smolbote_interfaces ], link_with: library('smolblok', - [ - 'filtermanager.cpp', - ], + [ 'smolblok.cpp' ], include_directories: smolbote_interfaces, - dependencies: [ dep_qt5, dep_adblockfilter ] + dependencies: dep_qt5 ) ) test('smolblok', executable('filterlist', - dependencies: [ dep_qt5, dep_smolblok ], + dependencies: [ dep_qt5, dep_catch, dep_smolblok ], sources: [ 'test/main.cpp' ] - ), - args: files('test/sample-filters.txt') + ) ) diff --git a/staging/smolblok/smolblok.cpp b/staging/smolblok/smolblok.cpp new file mode 100644 index 0000000..6095082 --- /dev/null +++ b/staging/smolblok/smolblok.cpp @@ -0,0 +1,58 @@ +/* + * 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::registerFormatPlugin(const QString &format, const QString &filename) +{ + if(format.isEmpty() || filename.isEmpty()) { + return false; + } + + auto *plugin = new QPluginLoader(filename); + if(!plugin->load()) { + delete plugin; + return false; + } + + auto *instance = qobject_cast<FilterPlugin *>(plugin->instance()); + if(instance == nullptr) { + delete plugin; + return false; + } + + m_formats[format] = PluginInfo{ plugin, instance }; + return false; +} + +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() && !loader->update(&f, listconf.value("Href").toUrl())) { + continue; + } + + m_subscriptions.addFilterList(loader->load(&f)); + } + listconf.endGroup(); + } + return false; +} + diff --git a/staging/smolblok/smolblok.hpp b/staging/smolblok/smolblok.hpp new file mode 100644 index 0000000..d3e63fb --- /dev/null +++ b/staging/smolblok/smolblok.hpp @@ -0,0 +1,48 @@ +/* + * 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() + { + for(auto &plugin : m_formats) { + delete plugin.loader; + } + } + bool registerFormatPlugin(const QString &format, const QString &filename); + 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/main.cpp b/staging/smolblok/test/main.cpp index 11f2b80..5624ee9 100644 --- a/staging/smolblok/test/main.cpp +++ b/staging/smolblok/test/main.cpp @@ -1,18 +1,22 @@ -#include "filtermanager.hpp" +#define CATCH_CONFIG_MAIN -int main(int argc, char **argv) -{ - if(argc != 2) { - qDebug("Usage: %s filters.txt", argv[0]); - return 77; - } +#include "smolblok.hpp" +#include <catch2/catch.hpp> - FilterManager sub; - const auto r = sub.installSubscriptions(argv[1]); +SCENARIO("smolblok") +{ + smolblok s; - if(!r.success) { - return 77; + GIVEN("invalid plugins") + { + REQUIRE(!s.registerFormatPlugin("", "")); + REQUIRE(!s.registerFormatPlugin("Format", "missing.dll")); } - return 0; + 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 index 574501f..59e0e7b 100644 --- a/staging/smolblok/test/sample-filters.txt +++ b/staging/smolblok/test/sample-filters.txt @@ -1,4 +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 + |