From abdaf8cfae8d5749ada4f73234074696f2d57634 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 16 Feb 2020 16:27:59 +0200 Subject: staging/adblock: implement FilterList::isUpToDate --- staging/adblock/filterlist.cpp | 34 +++++++++++++++++++++++++++------- staging/adblock/filterlist.h | 20 +++++++++++++++++--- staging/adblock/meson.build | 3 ++- staging/adblock/test/parser.cpp | 7 ++++--- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/staging/adblock/filterlist.cpp b/staging/adblock/filterlist.cpp index be2bd4e..9fb53ad 100644 --- a/staging/adblock/filterlist.cpp +++ b/staging/adblock/filterlist.cpp @@ -14,19 +14,18 @@ /** * Documentation: * - * https://help.eyeo.com/en/adblockplus/how-to-write-filters + * https://adblockplus.org/filter-cheatsheet + * https://help.eyeo.com/adblockplus/how-to-write-filters * * https://github.com/gorhill/uBlock/wiki/Introduction-to-basic-filtering-syntax * https://github.com/gorhill/uBlock/wiki/Static-filter-syntax * */ -using namespace AdblockPlus; +const QLatin1String comment_lastModified("! Last modified: "); +const QLatin1String comment_expires("! Expires: "); -FilterList::FilterList(QObject *parent) - : QObject(parent) -{ -} +using namespace AdblockPlus; FilterList::~FilterList() { @@ -77,7 +76,18 @@ FilterList::ParseResult FilterList::parse(QTextStream &stream) void FilterList::parseComment(QString &line) { - m_comments.append(line); + if(line.startsWith(comment_lastModified)) { + lastModified = QDateTime::fromString(line.mid(comment_lastModified.size()), "dd MMM yyyy HH:mm 'UTC'"); + expires = lastModified; + + } else if(line.startsWith(comment_expires)) { + const QRegularExpression time_re("(?:(\\d+) days)|(?:(\\d+) hours)"); + const auto match = time_re.match(line); + if(match.hasMatch()) { + expires = expires.addDays(match.captured(1).toInt()); + expires = expires.addSecs(match.captured(2).toInt() * 60 * 60); + } + } } bool FilterList::parseRule(const QString &line) @@ -135,3 +145,13 @@ bool FilterList::parseRule(const QString &line) return true; } + +void FilterList::filter(QWebEngineUrlRequestInfo &info) const +{ +} + +bool FilterList::isUpToDate() const +{ + const auto current = QDateTime::currentDateTime(); + return expires > current; +} diff --git a/staging/adblock/filterlist.h b/staging/adblock/filterlist.h index 3ee18b3..59717af 100644 --- a/staging/adblock/filterlist.h +++ b/staging/adblock/filterlist.h @@ -6,6 +6,8 @@ * SPDX-License-Identifier: GPL-3.0 */ +#include "filterinterface.h" +#include #include #include #include @@ -16,12 +18,12 @@ class QTextStream; namespace AdblockPlus { class Rule; -class FilterList : public QObject +class FilterList : public Filter { Q_DISABLE_COPY(FilterList) public: - explicit FilterList(QObject *parent = nullptr); + explicit FilterList() = default; ~FilterList(); enum ParseResultState { @@ -39,11 +41,23 @@ public: }; ParseResult parse(QTextStream &stream); + void filter(QWebEngineUrlRequestInfo &info) const override; + bool isUpToDate() const override; + QDateTime modified() const + { + return lastModified; + } + QDateTime expiresOn() const + { + return expires; + } + private: void parseComment(QString &line); bool parseRule(const QString &line); - QStringList m_comments; + QDateTime lastModified; + QDateTime expires; QVector m_rules; }; diff --git a/staging/adblock/meson.build b/staging/adblock/meson.build index 1fc4c65..f797607 100644 --- a/staging/adblock/meson.build +++ b/staging/adblock/meson.build @@ -1,8 +1,9 @@ dep_adblockfilter = declare_dependency( include_directories: include_directories('.'), + dependencies: dep_plugininterface, link_with: static_library('adblockfilter', [ 'filterlist.cpp', 'rule.cpp', 'options.cpp' ], - dependencies: dep_qt5 + dependencies: [ dep_qt5, dep_plugininterface ] ) ) diff --git a/staging/adblock/test/parser.cpp b/staging/adblock/test/parser.cpp index 0ce1121..2d2e59e 100644 --- a/staging/adblock/test/parser.cpp +++ b/staging/adblock/test/parser.cpp @@ -20,11 +20,12 @@ int main(int argc, char **argv) QTextStream stream(&f); const auto result = list.parse(stream); qDebug("[%s]: %s", argv[i], (result.state == AdblockPlus::FilterList::Ok) ? "okay" : "failed"); - qDebug(" total: %i", result.lines_total); - qDebug("comments: %i", result.lines_comments); - qDebug(" ignored: %i", result.lines_ignored); + qDebug(" total: %i\t\tmodified: %s", result.lines_total, qUtf8Printable(list.modified().toString())); + qDebug("comments: %i\t\t expires: %s", result.lines_comments, qUtf8Printable(list.expiresOn().toString())); + qDebug(" ignored: %i\t\t valid: %s", result.lines_ignored, list.isUpToDate() ? "yes" : "no"); qDebug(" parsed: %i", result.lines_parsed); qDebug(" failed: %i", result.lines_failed); + qDebug("-------- --------"); f.close(); } -- cgit v1.2.1