adblocksubscription.cpp 12.5 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2
/*
  Copyright (c) 2016 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
Laurent Montel's avatar
Laurent Montel committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
*/
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
/**
 * Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include "adblocksubscription.h"
#include "adblockmanager.h"
#include "adblocksearchtree.h"
Laurent Montel's avatar
Laurent Montel committed
63 64 65 66
//#include "networkmanager.h"
//#include "datapaths.h"
#include "adblockutil.h"
//#include "followredirectreply.h"
Laurent Montel's avatar
Laurent Montel committed
67 68 69 70

#include <QFile>
#include <QTimer>
#include <QNetworkReply>
Laurent Montel's avatar
Laurent Montel committed
71
using namespace AdBlock;
Laurent Montel's avatar
Laurent Montel committed
72

Laurent Montel's avatar
Laurent Montel committed
73
AdBlockSubscription::AdBlockSubscription(const QString &title, QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    : QObject(parent)
    , m_reply(0)
    , m_title(title)
    , m_updated(false)
{
}

QString AdBlockSubscription::title() const
{
    return m_title;
}

QString AdBlockSubscription::filePath() const
{
    return m_filePath;
}

void AdBlockSubscription::setFilePath(const QString &path)
{
    m_filePath = path;
}

QUrl AdBlockSubscription::url() const
{
    return m_url;
}

void AdBlockSubscription::setUrl(const QUrl &url)
{
    m_url = url;
}

void AdBlockSubscription::loadSubscription(const QStringList &disabledRules)
{
    QFile file(m_filePath);

    if (!file.exists()) {
        QTimer::singleShot(0, this, SLOT(updateSubscription()));
        return;
    }

    if (!file.open(QFile::ReadOnly)) {
        qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for reading" << m_filePath;
        QTimer::singleShot(0, this, SLOT(updateSubscription()));
        return;
    }

    QTextStream textStream(&file);
    textStream.setCodec("UTF-8");
    // Header is on 3rd line
    textStream.readLine(1024);
    textStream.readLine(1024);
    QString header = textStream.readLine(1024);

    if (!header.startsWith(QLatin1String("[Adblock")) || m_title.isEmpty()) {
        qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "invalid format of adblock file" << m_filePath;
        QTimer::singleShot(0, this, SLOT(updateSubscription()));
        return;
    }

    m_rules.clear();

    while (!textStream.atEnd()) {
Laurent Montel's avatar
Laurent Montel committed
137
        AdBlockRule *rule = new AdBlockRule(textStream.readLine(), this);
Laurent Montel's avatar
Laurent Montel committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

        if (disabledRules.contains(rule->filter())) {
            rule->setEnabled(false);
        }

        m_rules.append(rule);
    }

    // Initial update
    if (m_rules.isEmpty() && !m_updated) {
        QTimer::singleShot(0, this, SLOT(updateSubscription()));
    }
}

void AdBlockSubscription::saveSubscription()
{
}

void AdBlockSubscription::updateSubscription()
{
Laurent Montel's avatar
Laurent Montel committed
158
#if 0
Laurent Montel's avatar
Laurent Montel committed
159 160 161 162 163 164 165
    if (m_reply || !m_url.isValid()) {
        return;
    }

    m_reply = new FollowRedirectReply(m_url, mApp->networkManager());

    connect(m_reply, SIGNAL(finished()), this, SLOT(subscriptionDownloaded()));
Laurent Montel's avatar
Laurent Montel committed
166
#endif
Laurent Montel's avatar
Laurent Montel committed
167 168 169 170
}

void AdBlockSubscription::subscriptionDownloaded()
{
Laurent Montel's avatar
Laurent Montel committed
171
#if 0
Laurent Montel's avatar
Laurent Montel committed
172
    if (m_reply != qobject_cast<FollowRedirectReply *>(sender())) {
Laurent Montel's avatar
Laurent Montel committed
173 174 175 176 177 178 179
        return;
    }

    bool error = false;
    const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();

    if (m_reply->error() != QNetworkReply::NoError ||
Laurent Montel's avatar
Laurent Montel committed
180 181
            !response.startsWith(QByteArray("[Adblock")) ||
            !saveDownloadedData(response)
Laurent Montel's avatar
Laurent Montel committed
182 183 184 185 186 187 188 189
       ) {
        error = true;
    }

    m_reply->deleteLater();
    m_reply = 0;

    if (error) {
Laurent Montel's avatar
Laurent Montel committed
190
        Q_EMIT subscriptionError(tr("Cannot load subscription!"));
Laurent Montel's avatar
Laurent Montel committed
191 192 193 194 195
        return;
    }

    loadSubscription(AdBlockManager::instance()->disabledRules());

Laurent Montel's avatar
Laurent Montel committed
196 197
    Q_EMIT subscriptionUpdated();
    Q_EMIT subscriptionChanged();
Laurent Montel's avatar
Laurent Montel committed
198
#endif
Laurent Montel's avatar
Laurent Montel committed
199 200 201 202
}

bool AdBlockSubscription::saveDownloadedData(const QByteArray &data)
{
Laurent Montel's avatar
Laurent Montel committed
203
#if 0
Laurent Montel's avatar
Laurent Montel committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    QFile file(m_filePath);

    if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
        qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath;
        return false;
    }

    // Write subscription header
    file.write(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8());

    if (AdBlockManager::instance()->useLimitedEasyList() && m_url == QUrl(ADBLOCK_EASYLIST_URL)) {
        // Third-party advertisers rules are with start domain (||) placeholder which needs regexps
        // So we are ignoring it for keeping good performance
        // But we will use whitelist rules at the end of list

        QByteArray part1 = data.left(data.indexOf(QLatin1String("!-----------------------------Third-party adverts-----------------------------!")));
        QByteArray part2 = data.mid(data.indexOf(QLatin1String("!---------------------------------Whitelists----------------------------------!")));

        file.write(part1);
        file.write(part2);
        file.close();
        return true;
    }

    file.write(data);
    file.close();
Laurent Montel's avatar
Laurent Montel committed
230
#endif
Laurent Montel's avatar
Laurent Montel committed
231 232 233
    return true;
}

Laurent Montel's avatar
Laurent Montel committed
234
const AdBlockRule *AdBlockSubscription::rule(int offset) const
Laurent Montel's avatar
Laurent Montel committed
235
{
Laurent Montel's avatar
Laurent Montel committed
236
    if (!AdblockUtil::containsIndex(m_rules, offset)) {
Laurent Montel's avatar
Laurent Montel committed
237 238 239 240 241 242
        return 0;
    }

    return m_rules[offset];
}

Laurent Montel's avatar
Laurent Montel committed
243
QVector<AdBlockRule *> AdBlockSubscription::allRules() const
Laurent Montel's avatar
Laurent Montel committed
244 245 246 247
{
    return m_rules;
}

Laurent Montel's avatar
Laurent Montel committed
248
const AdBlockRule *AdBlockSubscription::enableRule(int offset)
Laurent Montel's avatar
Laurent Montel committed
249
{
Laurent Montel's avatar
Laurent Montel committed
250
    if (!AdblockUtil::containsIndex(m_rules, offset)) {
Laurent Montel's avatar
Laurent Montel committed
251 252 253
        return 0;
    }

Laurent Montel's avatar
Laurent Montel committed
254
    AdBlockRule *rule = m_rules[offset];
Laurent Montel's avatar
Laurent Montel committed
255
    rule->setEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
256
    //FIXME AdBlockManager::instance()->removeDisabledRule(rule->filter());
Laurent Montel's avatar
Laurent Montel committed
257

Laurent Montel's avatar
Laurent Montel committed
258
    Q_EMIT subscriptionChanged();
Laurent Montel's avatar
Laurent Montel committed
259

Laurent Montel's avatar
Laurent Montel committed
260
    if (rule->isCssRule()) {
Laurent Montel's avatar
Laurent Montel committed
261
        //FIXME mApp->reloadUserStyleSheet();
Laurent Montel's avatar
Laurent Montel committed
262
    }
Laurent Montel's avatar
Laurent Montel committed
263 264 265 266

    return rule;
}

Laurent Montel's avatar
Laurent Montel committed
267
const AdBlockRule *AdBlockSubscription::disableRule(int offset)
Laurent Montel's avatar
Laurent Montel committed
268
{
Laurent Montel's avatar
Laurent Montel committed
269
    if (!AdblockUtil::containsIndex(m_rules, offset)) {
Laurent Montel's avatar
Laurent Montel committed
270 271 272
        return 0;
    }

Laurent Montel's avatar
Laurent Montel committed
273
    AdBlockRule *rule = m_rules[offset];
Laurent Montel's avatar
Laurent Montel committed
274
    rule->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
275
    //FIXME AdBlockManager::instance()->addDisabledRule(rule->filter());
Laurent Montel's avatar
Laurent Montel committed
276

Laurent Montel's avatar
Laurent Montel committed
277
    Q_EMIT subscriptionChanged();
Laurent Montel's avatar
Laurent Montel committed
278

Laurent Montel's avatar
Laurent Montel committed
279
    if (rule->isCssRule()) {
Laurent Montel's avatar
Laurent Montel committed
280
        //FIXME mApp->reloadUserStyleSheet();
Laurent Montel's avatar
Laurent Montel committed
281
    }
Laurent Montel's avatar
Laurent Montel committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295

    return rule;
}

bool AdBlockSubscription::canEditRules() const
{
    return false;
}

bool AdBlockSubscription::canBeRemoved() const
{
    return true;
}

Laurent Montel's avatar
Laurent Montel committed
296
int AdBlockSubscription::addRule(AdBlockRule *rule)
Laurent Montel's avatar
Laurent Montel committed
297 298 299 300 301 302 303 304 305 306 307
{
    Q_UNUSED(rule)
    return -1;
}

bool AdBlockSubscription::removeRule(int offset)
{
    Q_UNUSED(offset)
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
308
const AdBlockRule *AdBlockSubscription::replaceRule(AdBlockRule *rule, int offset)
Laurent Montel's avatar
Laurent Montel committed
309 310 311 312 313 314 315 316 317 318 319 320 321
{
    Q_UNUSED(rule)
    Q_UNUSED(offset)
    return 0;
}

AdBlockSubscription::~AdBlockSubscription()
{
    qDeleteAll(m_rules);
}

// AdBlockCustomList

Laurent Montel's avatar
Laurent Montel committed
322
AdBlockCustomList::AdBlockCustomList(QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
323 324
    : AdBlockSubscription(tr("Custom Rules"), parent)
{
Laurent Montel's avatar
Laurent Montel committed
325
#if 0
Laurent Montel's avatar
Laurent Montel committed
326
    setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/customlist.txt"));
Laurent Montel's avatar
Laurent Montel committed
327
#endif
Laurent Montel's avatar
Laurent Montel committed
328 329 330 331
}

void AdBlockCustomList::loadSubscription(const QStringList &disabledRules)
{
Laurent Montel's avatar
Laurent Montel committed
332
#if 0
Laurent Montel's avatar
Laurent Montel committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346
    // DuckDuckGo ad whitelist rules
    // They cannot be removed, but can be disabled.
    // Please consider not disabling them. Thanks!

    const QString ddg1 = QSL("@@||duckduckgo.com^$document");
    const QString ddg2 = QSL("duckduckgo.com#@#.has-ad");

    const QString rules = QzTools::readAllFileContents(filePath());

    QFile file(filePath());
    if (file.open(QFile::WriteOnly | QFile::Append)) {
        QTextStream stream(&file);
        stream.setCodec("UTF-8");

Laurent Montel's avatar
Laurent Montel committed
347
        if (!rules.contains(ddg1 + QLatin1String("\n"))) {
Laurent Montel's avatar
Laurent Montel committed
348
            stream << ddg1 << endl;
Laurent Montel's avatar
Laurent Montel committed
349
        }
Laurent Montel's avatar
Laurent Montel committed
350

Laurent Montel's avatar
Laurent Montel committed
351
        if (!rules.contains(QLatin1String("\n") + ddg2)) {
Laurent Montel's avatar
Laurent Montel committed
352
            stream << ddg2 << endl;
Laurent Montel's avatar
Laurent Montel committed
353
        }
Laurent Montel's avatar
Laurent Montel committed
354 355 356 357
    }
    file.close();

    AdBlockSubscription::loadSubscription(disabledRules);
Laurent Montel's avatar
Laurent Montel committed
358
#endif
Laurent Montel's avatar
Laurent Montel committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
}

void AdBlockCustomList::saveSubscription()
{
    QFile file(filePath());

    if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
        qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << filePath();
        return;
    }

    QTextStream textStream(&file);
    textStream.setCodec("UTF-8");
    textStream << "Title: " << title() << endl;
    textStream << "Url: " << url().toString() << endl;
    textStream << "[Adblock Plus 1.1.1]" << endl;

Laurent Montel's avatar
Laurent Montel committed
376
    foreach (const AdBlockRule *rule, m_rules) {
Laurent Montel's avatar
Laurent Montel committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
        textStream << rule->filter() << endl;
    }

    file.close();
}

bool AdBlockCustomList::canEditRules() const
{
    return true;
}

bool AdBlockCustomList::canBeRemoved() const
{
    return false;
}

bool AdBlockCustomList::containsFilter(const QString &filter) const
{
Laurent Montel's avatar
Laurent Montel committed
395
    foreach (const AdBlockRule *rule, m_rules) {
Laurent Montel's avatar
Laurent Montel committed
396 397 398 399 400 401 402 403 404 405 406
        if (rule->filter() == filter) {
            return true;
        }
    }

    return false;
}

bool AdBlockCustomList::removeFilter(const QString &filter)
{
    for (int i = 0; i < m_rules.count(); ++i) {
Laurent Montel's avatar
Laurent Montel committed
407
        const AdBlockRule *rule = m_rules.at(i);
Laurent Montel's avatar
Laurent Montel committed
408 409 410 411 412 413 414 415 416

        if (rule->filter() == filter) {
            return removeRule(i);
        }
    }

    return false;
}

Laurent Montel's avatar
Laurent Montel committed
417
int AdBlockCustomList::addRule(AdBlockRule *rule)
Laurent Montel's avatar
Laurent Montel committed
418 419 420
{
    m_rules.append(rule);

Laurent Montel's avatar
Laurent Montel committed
421
    Q_EMIT subscriptionChanged();
Laurent Montel's avatar
Laurent Montel committed
422

Laurent Montel's avatar
Laurent Montel committed
423
    if (rule->isCssRule()) {
Laurent Montel's avatar
Laurent Montel committed
424
        //FIXME mApp->reloadUserStyleSheet();
Laurent Montel's avatar
Laurent Montel committed
425
    }
Laurent Montel's avatar
Laurent Montel committed
426 427 428 429 430 431

    return m_rules.count() - 1;
}

bool AdBlockCustomList::removeRule(int offset)
{
Laurent Montel's avatar
Laurent Montel committed
432
    if (!AdblockUtil::containsIndex(m_rules, offset)) {
Laurent Montel's avatar
Laurent Montel committed
433 434 435
        return false;
    }

Laurent Montel's avatar
Laurent Montel committed
436
    AdBlockRule *rule = m_rules.at(offset);
Laurent Montel's avatar
Laurent Montel committed
437 438 439 440
    const QString filter = rule->filter();

    m_rules.remove(offset);

Laurent Montel's avatar
Laurent Montel committed
441
    Q_EMIT subscriptionChanged();
Laurent Montel's avatar
Laurent Montel committed
442

Laurent Montel's avatar
Laurent Montel committed
443
    if (rule->isCssRule()) {
Laurent Montel's avatar
Laurent Montel committed
444
        //FIXME mApp->reloadUserStyleSheet();
Laurent Montel's avatar
Laurent Montel committed
445
    }
Laurent Montel's avatar
Laurent Montel committed
446

Laurent Montel's avatar
Laurent Montel committed
447
    //FIXME AdBlockManager::instance()->removeDisabledRule(filter);
Laurent Montel's avatar
Laurent Montel committed
448 449 450 451 452

    delete rule;
    return true;
}

Laurent Montel's avatar
Laurent Montel committed
453
const AdBlockRule *AdBlockCustomList::replaceRule(AdBlockRule *rule, int offset)
Laurent Montel's avatar
Laurent Montel committed
454
{
Laurent Montel's avatar
Laurent Montel committed
455
    if (!AdblockUtil::containsIndex(m_rules, offset)) {
Laurent Montel's avatar
Laurent Montel committed
456 457 458
        return 0;
    }

Laurent Montel's avatar
Laurent Montel committed
459
    AdBlockRule *oldRule = m_rules.at(offset);
Laurent Montel's avatar
Laurent Montel committed
460 461
    m_rules[offset] = rule;

Laurent Montel's avatar
Laurent Montel committed
462
    Q_EMIT subscriptionChanged();
Laurent Montel's avatar
Laurent Montel committed
463

Laurent Montel's avatar
Laurent Montel committed
464
    if (rule->isCssRule() || oldRule->isCssRule()) {
Laurent Montel's avatar
Laurent Montel committed
465
        //FIXME mApp->reloadUserStyleSheet();
Laurent Montel's avatar
Laurent Montel committed
466
    }
Laurent Montel's avatar
Laurent Montel committed
467 468 469 470

    delete oldRule;
    return m_rules[offset];
}