diff options
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | staging/adblock/filterlist.h | 4 | ||||
-rw-r--r-- | staging/hostlist/filterlist.cpp | 29 | ||||
-rw-r--r-- | staging/hostlist/filterlist.hpp | 43 | ||||
-rw-r--r-- | staging/hostlist/meson.build | 36 | ||||
-rw-r--r-- | staging/hostlist/test/rule.cpp | 57 |
6 files changed, 168 insertions, 2 deletions
diff --git a/meson.build b/meson.build index fdb9ae2..ef479a4 100644 --- a/meson.build +++ b/meson.build @@ -92,6 +92,7 @@ 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/adblock/filterlist.h b/staging/adblock/filterlist.h index b67c187..24464c8 100644 --- a/staging/adblock/filterlist.h +++ b/staging/adblock/filterlist.h @@ -6,17 +6,16 @@ * SPDX-License-Identifier: GPL-3.0 */ +#include "rule.h" #include <QDateTime> #include <QObject> #include <QString> #include <QStringList> #include <QVector> #include <smolbote/filterinterface.hpp> -#include "rule.h" namespace AdblockPlus { -class Rule; class FilterList : public Filter { public: @@ -43,6 +42,7 @@ public: } [[nodiscard]] static Rule *parseRule(const QByteArray &line); + private: void parseComment(const QString &line); diff --git a/staging/hostlist/filterlist.cpp b/staging/hostlist/filterlist.cpp new file mode 100644 index 0000000..483ba7d --- /dev/null +++ b/staging/hostlist/filterlist.cpp @@ -0,0 +1,29 @@ +/* + * 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 "filterlist.hpp" +#include <QIODevice> +#include <QTextStream> + +using namespace Hostlist; + +std::map<Filterlist::DomainHash, Filterlist::Rule> Filterlist::parseRule(const QString &line) +{ + auto parts = line.trimmed().split(' '); + if(parts.size() < 2) { + return {}; + } + + const auto redirect = (parts[0] == "0.0.0.0") ? QString() : parts[0]; + + std::map<DomainHash, Rule> r; + for(int i = 1; i < parts.size(); ++i) { + r.emplace(qHash(parts[i], 0), Filterlist::Rule{ parts[i], redirect }); + } + return r; +} diff --git a/staging/hostlist/filterlist.hpp b/staging/hostlist/filterlist.hpp new file mode 100644 index 0000000..24243e5 --- /dev/null +++ b/staging/hostlist/filterlist.hpp @@ -0,0 +1,43 @@ +/* + * 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 <map> +#include <smolbote/filterinterface.hpp> + +namespace Hostlist +{ + +class Filterlist final : public Filter +{ +public: + Filterlist() = default; + ~Filterlist() = default; + + [[nodiscard]] bool filter(QWebEngineUrlRequestInfo &info) const + { + return false; + } + [[nodiscard]] bool isUpToDate() const + { + return true; + } + + typedef uint DomainHash; + struct Rule { + QString domain; + QString redirect; + }; + + [[nodiscard]] static std::map<DomainHash, Rule> parseRule(const QString &line); + +private: + std::map<DomainHash, Rule> rules; +}; +} // namespace Hostlist diff --git a/staging/hostlist/meson.build b/staging/hostlist/meson.build new file mode 100644 index 0000000..d37db1b --- /dev/null +++ b/staging/hostlist/meson.build @@ -0,0 +1,36 @@ +lib_hostlistfilter = static_library('hostlistfilter', + ['filterlist.cpp'], + include_directories + : smolbote_interfaces, + dependencies + : [dep_qt5]) + + dep_hostlistfilter + = declare_dependency( + include_directories + : ['.', smolbote_interfaces], + link_with + : lib_hostlistfilter) + +#AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin', +#['plugin/plugin.cpp', +#mod_qt5.preprocess(include_directories \ + : smolbote_interfaces, +#moc_headers : 'plugin/plugin.h', dependencies : dep_qt5) +#], +#include_directories : smolbote_interfaces, +#link_with : lib_adblockfilter, +#dependencies : dep_qt5, +#install : true, +#install_dir : get_option('libdir') / 'smolbote/plugins' +#) + + test('hostlist: rule parsing', executable('rule', sources + : 'test/rule.cpp', dependencies + : [dep_qt5, dep_catch, dep_hostlistfilter])) + + subdir_done() + + test('hostlist: filterlist', executable('filterlist', sources + : 'test/filterlist.cpp', dependencies + : [dep_qt5, dep_catch, dep_hostlistfilter])) diff --git a/staging/hostlist/test/rule.cpp b/staging/hostlist/test/rule.cpp new file mode 100644 index 0000000..6a16f86 --- /dev/null +++ b/staging/hostlist/test/rule.cpp @@ -0,0 +1,57 @@ +#define CATCH_CONFIG_MAIN +#include "filterlist.hpp" +#include <catch2/catch.hpp> + +using namespace Hostlist; + +SCENARIO("MatcherRule") +{ + GIVEN("an invalid rule") + { + const auto rule = Filterlist::parseRule("0.0.0.0 "); + REQUIRE(rule.empty()); + } + GIVEN("127.0.0.1 localhost.localdomain") + { + auto rule = Filterlist::parseRule("127.0.0.1 localhost.localdomain"); + + REQUIRE(!rule.empty()); + REQUIRE(rule.size() == 1); + + // note: you need to force it to hash a string, rather than the address itself + const auto index = qHash(QString("localhost.localdomain"), 0); + REQUIRE(rule[index].domain == "localhost.localdomain"); + REQUIRE(rule[index].redirect == "127.0.0.1"); + } + + GIVEN("0.0.0.0 blockeddomain.com") + { + auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.com"); + + REQUIRE(!rule.empty()); + REQUIRE(rule.size() == 1); + + const auto index = qHash(QString("blockeddomain.com"), 0); + REQUIRE(rule[index].domain == "blockeddomain.com"); + REQUIRE(rule[index].redirect.isEmpty()); + ; + } + + GIVEN("0.0.0.0 blockeddomain.first blockeddomain.second") + { + auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.first blockeddomain.second"); + + REQUIRE(!rule.empty()); + REQUIRE(rule.size() == 2); + { + const auto index = qHash(QString("blockeddomain.first"), 0); + REQUIRE(rule[index].domain == "blockeddomain.first"); + REQUIRE(rule[index].redirect.isEmpty()); + } + { + const auto index = qHash(QString("blockeddomain.second"), 0); + REQUIRE(rule[index].domain == "blockeddomain.second"); + REQUIRE(rule[index].redirect.isEmpty()); + } + } +} |