| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "MsgPartQNAM.h" |
| 19 | #include <QUrl> |
| 20 | #include <QUrlQuery> |
| 21 | #include <QNetworkRequest> |
| 22 | #include <qmailmessage.h> |
| 23 | #include "MsgPartReply.h" |
| 24 | #include "MsgReply.h" |
| 25 | #include "ForbiddenReply.h" |
| 26 | |
| 27 | const QStringList Allowed::urlSchemes = QStringList() << QStringLiteral("dekko-msg") |
| 28 | << QStringLiteral("dekko-part") |
| 29 | << QStringLiteral("cid"); |
| 30 | const QStringList Allowed::hosts = QStringList() << QStringLiteral("msg") |
| 31 | << QStringLiteral("www.gravatar.com") |
| 32 | << QStringLiteral("cdn.libravatar.org"); |
| 33 | |
| 34 | MsgPartQNAM::MsgPartQNAM(QObject *parent) : QNetworkAccessManager(parent) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | MsgPartQNAM::~MsgPartQNAM() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | QNetworkReply *MsgPartQNAM::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData) |
| 43 | { |
| 44 | |
| 45 | qDebug() << "[MsgPartQNAM]" << __func__ << request.url(); |
| 46 | QString host = request.url().host(); |
| 47 | if (request.url().scheme() == QStringLiteral("cid")) { |
| 48 | // Do nothing |
| 49 | } else if (!Allowed::hosts.contains(host)) { |
| 50 | qDebug() << "[MsgPartQNAM]" << __func__ << "Not in allowed hosts"; |
| 51 | return new ForbiddenReply(this); |
| 52 | } else if (host != QStringLiteral("msg")) { |
| 53 | // This is our avatar requests |
| 54 | QNetworkRequest req(request); |
| 55 | req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); |
| 56 | return QNetworkAccessManager::createRequest(op, req, outgoingData); |
| 57 | } |
| 58 | QString scheme = request.url().scheme(); |
| 59 | if (!Allowed::urlSchemes.contains(scheme)) { |
| 60 | qDebug() << "[MsgPartQNAM]" << __func__ << "Not in allowed schemes"; |
| 61 | return new ForbiddenReply(this, QString("%1 is not an allowed scheme").arg(scheme)); |
| 62 | } |
| 63 | // Ok so this is now either a msg or cid part we are requesting |
| 64 | QUrlQuery query(request.url()); |
| 65 | if (!query.hasQueryItem("messageId")) { |
| 66 | qDebug() << "[MsgPartQNAM]" << __func__ << "Missing messageId"; |
| 67 | return new ForbiddenReply(this, QStringLiteral("Missing message id")); |
| 68 | } |
| 69 | QMailMessageId messageId(query.queryItemValue(QStringLiteral("messageId")).toInt()); |
| 70 | if (!messageId.isValid()) { |
| 71 | qDebug() << "Invalid message id: " << messageId.toULongLong(); |
| 72 | return new ForbiddenReply(this, QStringLiteral("Invalid message id")); |
| 73 | } |
| 74 | |
| 75 | if (scheme == QStringLiteral("cid")) { |
| 76 | QString cidPath = request.url().path(); |
| 77 | return new MsgPartReply(this, messageId, cidPath); |
| 78 | } else if (scheme == QStringLiteral("dekko-part")) { // implies we have a location qeury |
| 79 | if (!query.hasQueryItem(QStringLiteral("location"))) { |
| 80 | qDebug() << "dekko-part missing location query item"; |
| 81 | // TODO: should we try and determine at least a viewable part instead of bailing out? |
| 82 | return new ForbiddenReply(this, QStringLiteral("Missing message part location")); |
| 83 | } |
| 84 | QString location = query.queryItemValue(QStringLiteral("location")); |
| 85 | bool isPlainText = query.hasQueryItem(QStringLiteral("requestFormatting")); |
| 86 | return new MsgPartReply(this, messageId, location, isPlainText, query.hasQueryItem(QStringLiteral("requestFormatting"))); |
| 87 | |
| 88 | } else if (scheme == QStringLiteral("dekko-msg")) { |
| 89 | // This is a non multipart message so we treat that a little different as it doesn't have parts |
| 90 | return new MsgReply(this, messageId, query.hasQueryItem(QStringLiteral("requestFormatting"))); |
| 91 | } else { |
| 92 | return new ForbiddenReply(this); |
| 93 | } |
| 94 | } |
| 95 |