• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • adblock
adblockrulefallbackimpl.cpp
Go to the documentation of this file.
1 /* ============================================================
2 * Copyright (c) 2013 Montel Laurent <montel@kde.org>
3 * based on code from rekonq
4 * This file is a part of the rekonq project
5 *
6 * Copyright (C) 2010-2011 by Benjamin Poulain <ikipou at gmail dot com>
7 * Copyright (C) 2011-2012 by Andrea Diamantini <adjam7 at gmail dot com>
8 *
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License or (at your option) version 3 or any later version
14 * accepted by the membership of KDE e.V. (or its successor approved
15 * by the membership of KDE e.V.), which shall act as a proxy
16 * defined in Section 14 of version 3 of the license.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * ============================================================ */
27 
28 
29 // Self Includes
30 #include "adblockrulefallbackimpl.h"
31 
32 
33 // Qt Includes
34 #include <QWebFrame>
35 #include <QNetworkReply>
36 #include <QStringList>
37 
38 
39 using namespace MessageViewer;
40 static inline bool isRegExpFilter(const QString &filter)
41 {
42  return filter.startsWith(QLatin1Char('/')) && filter.endsWith(QLatin1Char('/'));
43 }
44 
45 
46 AdBlockRuleFallbackImpl::AdBlockRuleFallbackImpl(const QString &filter)
47  : AdBlockRuleImpl(filter)
48  , m_unsupported(false)
49  , m_thirdPartyOption(false)
50  , m_thirdPartyOptionReversed(false)
51 {
52  m_regExp.setCaseSensitivity(Qt::CaseInsensitive);
53  m_regExp.setPatternSyntax(QRegExp::RegExp2);
54 
55  QString parsedLine = filter;
56 
57  if (isRegExpFilter(parsedLine))
58  {
59  parsedLine = parsedLine.mid(1, parsedLine.length() - 2);
60  }
61  else
62  {
63  const int optionsNumber = parsedLine.lastIndexOf(QLatin1Char('$'));
64 
65  if (optionsNumber >= 0)
66  {
67  QStringList options(parsedLine.mid(optionsNumber + 1).split(QLatin1Char(',')));
68  parsedLine = parsedLine.left(optionsNumber);
69 
70  if (options.removeOne(QLatin1String("match-case")))
71  m_regExp.setCaseSensitivity(Qt::CaseSensitive);
72 
73  if (options.removeOne(QLatin1String("third-party")))
74  m_thirdPartyOption = true;
75 
76  if (options.removeOne(QLatin1String("~third-party")))
77  {
78  m_thirdPartyOption = true;
79  m_thirdPartyOptionReversed = true;
80  }
81 
82  Q_FOREACH(const QString & option, options)
83  {
84  // Domain restricted filter
85  const QString domainKeyword(QLatin1String("domain="));
86  if (option.startsWith(domainKeyword))
87  {
88  options.removeOne(option);
89  const QStringList domainList = option.mid(domainKeyword.length()).split(QLatin1Char('|'));
90  Q_FOREACH(const QString & domain, domainList)
91  {
92  if (domain.startsWith(QLatin1Char('~')))
93  m_whiteDomains.insert(domain.toLower());
94  else
95  m_blackDomains.insert(domain.toLower());
96  }
97  break;
98  }
99  }
100 
101  // if there are yet options available we have to whitelist the rule
102  // to not be too much restrictive on adblocking
103  m_unsupported = (!options.isEmpty());
104  }
105 
106  parsedLine = convertPatternToRegExp(parsedLine);
107  }
108 
109  m_regExp.setPattern(parsedLine);
110 }
111 
112 
113 bool AdBlockRuleFallbackImpl::match(const QNetworkRequest &request, const QString &encodedUrl, const QString &) const
114 {
115  if (m_unsupported)
116  return false;
117 
118  if (m_thirdPartyOption)
119  {
120  const QString referer = QString::fromLatin1(request.rawHeader("referer"));
121  const QString host = request.url().host();
122 
123  bool isThirdParty = !referer.contains(host);
124 
125  if (!m_thirdPartyOptionReversed && !isThirdParty)
126  return false;
127 
128  if (m_thirdPartyOptionReversed && isThirdParty)
129  return false;
130  }
131 
132  const bool regexpMatch = m_regExp.indexIn(encodedUrl) != -1;
133 
134  if (regexpMatch && (!m_whiteDomains.isEmpty() || !m_blackDomains.isEmpty()))
135  {
136  Q_ASSERT(qobject_cast<QWebFrame*>(request.originatingObject()));
137  const QWebFrame *const origin = static_cast<QWebFrame * const>(request.originatingObject());
138 
139  const QString originDomain = origin->url().host();
140 
141  if (!m_whiteDomains.isEmpty())
142  {
143  // In this context, white domains means we block anything but what is in the list.
144  if (m_whiteDomains.contains(originDomain))
145  return false;
146  return true;
147  }
148  else if (m_blackDomains.contains(originDomain))
149  {
150  return true;
151  }
152  return false;
153  }
154  return regexpMatch;
155 }
156 
157 
158 QString AdBlockRuleFallbackImpl::convertPatternToRegExp(const QString &wildcardPattern)
159 {
160  QString pattern = wildcardPattern;
161 
162  // remove multiple wildcards
163  pattern.replace(QRegExp(QLatin1String("\\*+")), QLatin1String("*"));
164 
165  // remove anchors following separator placeholder
166  pattern.replace(QRegExp(QLatin1String("\\^\\|$")), QLatin1String("^"));
167 
168  // remove leading wildcards
169  pattern.replace(QRegExp(QLatin1String("^(\\*)")), QLatin1String(""));
170 
171  // remove trailing wildcards
172  pattern.replace(QRegExp(QLatin1String("(\\*)$")), QLatin1String(""));
173 
174  // escape special symbols
175  pattern.replace(QRegExp(QLatin1String("(\\W)")), QLatin1String("\\\\1"));
176 
177  // process extended anchor at expression start
178  pattern.replace(QRegExp(QLatin1String("^\\\\\\|\\\\\\|")), QLatin1String("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?"));
179 
180  // process separator placeholders
181  pattern.replace(QRegExp(QLatin1String("\\\\\\^")), QLatin1String("(?:[^\\w\\d\\-.%]|$)"));
182 
183  // process anchor at expression start
184  pattern.replace(QRegExp(QLatin1String("^\\\\\\|")), QLatin1String("^"));
185 
186  // process anchor at expression end
187  pattern.replace(QRegExp(QLatin1String("\\\\\\|$")), QLatin1String("$"));
188 
189  // replace wildcards by .*
190  pattern.replace(QRegExp(QLatin1String("\\\\\\*")), QLatin1String(".*"));
191 
192  // Finally, return...
193  return pattern;
194 }
195 
196 
197 QString AdBlockRuleFallbackImpl::ruleString() const
198 {
199  return m_regExp.pattern();
200 }
201 
202 
203 QString AdBlockRuleFallbackImpl::ruleType() const
204 {
205  return QLatin1String("AdBlockRuleFallbackImpl");
206 }
AdBlockRuleImpl
Definition: adblockruleimpl.h:32
MessageViewer::AdBlockRuleFallbackImpl::ruleString
QString ruleString() const
Definition: adblockrulefallbackimpl.cpp:197
MessageViewer::AdBlockRuleFallbackImpl::match
bool match(const QNetworkRequest &request, const QString &encodedUrl, const QString &encodedUrlLowerCase) const
Definition: adblockrulefallbackimpl.cpp:113
MessageViewer::AdBlockRuleFallbackImpl::ruleType
QString ruleType() const
Definition: adblockrulefallbackimpl.cpp:203
MessageViewer::AdBlockRuleFallbackImpl::AdBlockRuleFallbackImpl
AdBlockRuleFallbackImpl(const QString &filter)
Definition: adblockrulefallbackimpl.cpp:46
adblockrulefallbackimpl.h
isRegExpFilter
static bool isRegExpFilter(const QString &filter)
Definition: adblockrulefallbackimpl.cpp:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal