• 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
  • scamdetection
scamdetection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "scamdetection.h"
19 #include "scamdetectiondetailsdialog.h"
20 #include "scamcheckshorturl.h"
21 #include "settings/globalsettings.h"
22 
23 #include <QWebElement>
24 #include <QWebFrame>
25 #include <QDebug>
26 
27 using namespace MessageViewer;
28 static QString IPv4_PATTERN = QLatin1String("\\b[0-9]{1,3}\\.[0-9]{1,3}(?:\\.[0-9]{0,3})?(?:\\.[0-9]{0,3})?");
29 static QString addWarningColor(const QString &url)
30 {
31  const QString error = QString::fromLatin1("<font color=#FF0000>%1</font>").arg(url);
32  return error;
33 }
34 
35 ScamDetection::ScamDetection(QObject *parent)
36  : QObject(parent),
37  mCheckShortUrl(new ScamCheckShortUrl(this))
38 {
39 }
40 
41 ScamDetection::~ScamDetection()
42 {
43 }
44 
45 ScamCheckShortUrl *ScamDetection::scamCheckShortUrl() const
46 {
47  return mCheckShortUrl;
48 }
49 
50 void ScamDetection::scanPage(QWebFrame *frame)
51 {
52 #ifndef KDEPIM_NO_WEBKIT
53  if (GlobalSettings::self()->scamDetectionEnabled()) {
54  mDetails.clear();
55  mDetails = QLatin1String("<b>") + i18n("Details:") + QLatin1String("</b><ul>");
56  bool foundScam = false;
57  const QWebElement rootElement = frame->documentElement();
58  bool result = scanFrame(rootElement, mDetails);
59  if (result) {
60  foundScam = true;
61  }
62  foreach(QWebFrame *childFrame, frame->childFrames()) {
63  result = scanFrame(childFrame->documentElement(), mDetails);
64  if (result) {
65  foundScam = true;
66  }
67  }
68  if (foundScam)
69  Q_EMIT messageMayBeAScam();
70  }
71 #endif
72 }
73 
74 bool ScamDetection::scanFrame(const QWebElement &rootElement, QString &details)
75 {
76 #ifndef KDEPIM_NO_WEBKIT
77  bool foundScam = false;
78  QRegExp ip4regExp;
79  ip4regExp.setPattern(IPv4_PATTERN);
80  const QWebElementCollection allAnchor = rootElement.findAll(QLatin1String("a"));
81  Q_FOREACH (const QWebElement &anchorElement, allAnchor) {
82  //1) detect if title has a url and title != href
83  const QString href = anchorElement.attribute(QLatin1String("href"));
84  const QString title = anchorElement.attribute(QLatin1String("title"));
85  const QUrl url(href);
86  if (!title.isEmpty()) {
87  if (title.startsWith(QLatin1String("http:"))
88  || title.startsWith(QLatin1String("https:"))
89  || title.startsWith(QLatin1String("www."))) {
90  if (title.startsWith(QLatin1String("www."))) {
91  const QString completUrl = url.scheme() + QLatin1String("://") + title;
92  if ( completUrl != href &&
93  href != (completUrl + QLatin1Char('/'))) {
94  foundScam = true;
95  }
96  } else {
97  if (href != title) {
98  // http://www.kde.org == http://www.kde.org/
99  if (href != (title + QLatin1Char('/'))) {
100  foundScam = true;
101  }
102  }
103  }
104  if (foundScam) {
105  details += QLatin1String("<li>") + i18n("This email contains a link which reads as '%1' in the text, but actually points to '%2'. This is often the case in scam emails to mislead the recipient", addWarningColor(title), addWarningColor(href)) + QLatin1String("</li>");
106  }
107  }
108  }
109  if (!foundScam) {
110  //2) detect if url href has ip and not server name.
111  const QString hostname = url.host();
112  if (hostname.contains(ip4regExp) && !hostname.contains(QLatin1String("127.0.0.1"))) { //hostname
113  details += QLatin1String("<li>") + i18n("This email contains a link which points to a numerical IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname))+QLatin1String("</li>");
114  foundScam = true;
115  } else if (hostname.contains(QLatin1Char('%'))) { //Hexa value for ip
116  details += QLatin1String("<li>") + i18n("This email contains a link which points to a hexadecimal IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname))+QLatin1String("</li>");
117  foundScam = true;
118  } else if (url.toString().contains(QLatin1String("url?q="))) { //4) redirect url.
119  details += QLatin1String("<li>") + i18n("This email contains a link (%1) which has a redirection", addWarningColor(url.toString())) +QLatin1String("</li>");
120  foundScam = true;
121  } else if ((url.toString().count(QLatin1String("http://")) > 1) ||
122  (url.toString().count(QLatin1String("https://")) > 1)) { //5) more that 1 http in url.
123  details += QLatin1String("<li>") + i18n("This email contains a link (%1) which contains multiple http://. This is often the case in scam emails.", addWarningColor(url.toString())) + QLatin1String("</li>");
124  foundScam = true;
125  }
126  }
127  //Check shortUrl
128  if (!foundScam) {
129  if (ScamCheckShortUrl::isShortUrl(url)) {
130  details += QLatin1String("<li>") + i18n("This email contains a shorturl (%1). It can redirect to another server.", addWarningColor(url.toString())) + QLatin1String("</li>");
131  foundScam = true;
132  }
133  }
134  }
135  //3) has form
136  if (rootElement.findAll(QLatin1String("form")).count() > 0) {
137  details += QLatin1String("<li></b>") + i18n("Message contains form element. This is often the case in scam emails.") + QLatin1String("</b></li>");
138  foundScam = true;
139  }
140  details += QLatin1String("</ul>");
141  return foundScam;
142 #else
143  return false;
144 #endif
145 }
146 
147 void ScamDetection::showDetails()
148 {
149  if (!mDetailsDialog) {
150  mDetailsDialog = new MessageViewer::ScamDetectionDetailsDialog;
151  }
152 
153  mDetailsDialog->setDetails(mDetails);
154  mDetailsDialog->show();
155 }
156 
157 
158 #include "scamdetection.moc"
globalsettings.h
scamdetectiondetailsdialog.h
QObject
MessageViewer::ScamDetection::showDetails
void showDetails()
Definition: scamdetection.cpp:147
addWarningColor
static QString addWarningColor(const QString &url)
Definition: scamdetection.cpp:29
MessageViewer::ScamDetection::messageMayBeAScam
void messageMayBeAScam()
MessageViewer::ScamCheckShortUrl
Definition: scamcheckshorturl.h:35
MessageViewer::ScamCheckShortUrl::isShortUrl
static bool isShortUrl(const KUrl &url)
Definition: scamcheckshorturl.cpp:107
MessageViewer::ScamDetectionDetailsDialog
Definition: scamdetectiondetailsdialog.h:28
MessageViewer::ScamDetection::scanPage
void scanPage(QWebFrame *frame)
Definition: scamdetection.cpp:50
MessageViewer::ScamDetection::ScamDetection
ScamDetection(QObject *parent=0)
Definition: scamdetection.cpp:35
scamdetection.h
MessageViewer::GlobalSettings::self
static GlobalSettings * self()
Definition: globalsettings.cpp:34
IPv4_PATTERN
static QString IPv4_PATTERN
Definition: scamdetection.cpp:28
MessageViewer::ScamDetection::scanFrame
static bool scanFrame(const QWebElement &rootElement, QString &details)
Definition: scamdetection.cpp:74
scamcheckshorturl.h
MessageViewer::ScamDetection::~ScamDetection
~ScamDetection()
Definition: scamdetection.cpp:41
MessageViewer::ScamDetection::scamCheckShortUrl
ScamCheckShortUrl * scamCheckShortUrl() const
Definition: scamdetection.cpp:45
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