Skip to content

Commit

Permalink
Modern C++ for stanza errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Apr 26, 2024
1 parent e2efb63 commit bc922e1
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 267 deletions.
322 changes: 165 additions & 157 deletions src/xmpp/xmpp-core/xmpp_stanza.cpp

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions src/xmpp/xmpp-core/xmpp_stanza.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class Stanza {

class Error {
public:
enum ErrorType { Cancel = 1, Continue, Modify, Auth, Wait };
enum ErrorCond {
enum class ErrorType { Cancel = 1, Continue, Modify, Auth, Wait };
enum class ErrorCond {
BadRequest = 1,
Conflict,
FeatureNotImplemented,
Expand All @@ -67,17 +67,15 @@ class Stanza {
UnexpectedRequest
};

Error(int type = Cancel, int condition = UndefinedCondition, const QString &text = QString(),
const QDomElement &appSpec = QDomElement());
Error(ErrorType type = ErrorType::Cancel, ErrorCond condition = ErrorCond::UndefinedCondition,
const QString &text = QString(), const QDomElement &appSpec = QDomElement());

int type;
int condition;
ErrorType type;
ErrorCond condition;
QString text;
QString by;
QDomElement appSpec;

void reset();

int code() const;
bool fromCode(int code);

Expand Down
10 changes: 5 additions & 5 deletions src/xmpp/xmpp-im/filetransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ void FileTransferManager::pft_incoming(const FTRequest &req)
}

if (streamType.isEmpty()) {
d->pft->respondError(req.from, req.iq_id, Stanza::Error::NotAcceptable, "No valid stream types");
d->pft->respondError(req.from, req.iq_id, Stanza::Error::ErrorCond::NotAcceptable, "No valid stream types");
return;
}

Expand Down Expand Up @@ -485,7 +485,7 @@ void FileTransferManager::con_accept(FileTransfer *ft)

void FileTransferManager::con_reject(FileTransfer *ft)
{
d->pft->respondError(ft->d->peer, ft->d->iq_id, Stanza::Error::Forbidden, "Declined");
d->pft->respondError(ft->d->peer, ft->d->iq_id, Stanza::Error::ErrorCond::Forbidden, "Declined");
}

void FileTransferManager::unlink(FileTransfer *ft) { d->list.removeAll(ft); }
Expand Down Expand Up @@ -691,7 +691,7 @@ void JT_PushFT::respondSuccess(const Jid &to, const QString &id, qlonglong range
void JT_PushFT::respondError(const Jid &to, const QString &id, Stanza::Error::ErrorCond cond, const QString &str)
{
QDomElement iq = createIQ(doc(), "error", to.full(), id);
Stanza::Error error(Stanza::Error::Cancel, cond, str);
Stanza::Error error(Stanza::Error::ErrorType::Cancel, cond, str);
iq.appendChild(error.toXml(*client()->doc(), client()->stream().baseNS()));
send(iq);
}
Expand Down Expand Up @@ -719,7 +719,7 @@ bool JT_PushFT::take(const QDomElement &e)

QString fname = file.attribute("name");
if (fname.isEmpty()) {
respondError(from, id, Stanza::Error::BadRequest, "Bad file name");
respondError(from, id, Stanza::Error::ErrorCond::BadRequest, "Bad file name");
return true;
}

Expand All @@ -732,7 +732,7 @@ bool JT_PushFT::take(const QDomElement &e)
bool ok;
qlonglong size = file.attribute("size").toLongLong(&ok);
if (!ok || size < 0) {
respondError(from, id, Stanza::Error::BadRequest, "Bad file size");
respondError(from, id, Stanza::Error::ErrorCond::BadRequest, "Bad file size");
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/xmpp/xmpp-im/jingle-application.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define JINGLE_APPLICATION_H

#include "jingle-transport.h"
#include <optional>

class QTimer;

Expand Down Expand Up @@ -70,8 +71,8 @@ namespace XMPP { namespace Jingle {
Q_DECLARE_FLAGS(ApplicationFlags, ApplicationFlag)

virtual void setState(State state) = 0; // likely just remember the state and not generate any signals
virtual XMPP::Stanza::Error lastError() const = 0;
virtual Reason lastReason() const = 0;
virtual const std::optional<XMPP::Stanza::Error> &lastError() const = 0;
virtual Reason lastReason() const = 0;

inline ApplicationManagerPad::Ptr pad() const { return _pad; }
inline State state() const { return _state; }
Expand Down
34 changes: 17 additions & 17 deletions src/xmpp/xmpp-im/jingle-ft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@ namespace XMPP { namespace Jingle { namespace FileTransfer {
bool closeDeviceOnFinish = true;
bool streamingMode = false;
// bool endlessRange = false; // where range in accepted file doesn't have end
bool outgoingReceived = false;
bool writeLoggingStarted = false;
bool readLoggingStarted = false;
File file;
File acceptFile; // as it comes with "accept" response
XMPP::Stanza::Error lastError;
Reason lastReason;
Connection::Ptr connection;
QIODevice *device = nullptr;
std::optional<quint64> bytesLeft;
QList<Hash> outgoingChecksum;
QList<Hash> incomingChecksum;
QTimer *finalizeTimer = nullptr;
FileHasher *hasher = nullptr;
bool outgoingReceived = false;
bool writeLoggingStarted = false;
bool readLoggingStarted = false;
File file;
File acceptFile; // as it comes with "accept" response
std::optional<XMPP::Stanza::Error> lastError;
Reason lastReason;
Connection::Ptr connection;
QIODevice *device = nullptr;
std::optional<quint64> bytesLeft;
QList<Hash> outgoingChecksum;
QList<Hash> incomingChecksum;
QTimer *finalizeTimer = nullptr;
FileHasher *hasher = nullptr;

void setState(State s)
{
Expand Down Expand Up @@ -369,8 +369,8 @@ namespace XMPP { namespace Jingle { namespace FileTransfer {
qUtf8Printable(q->pad()->session()->peer().full()));
connection = newConnection;

lastReason = Reason();
lastError.reset();
lastReason = {};
lastError = {};

if (acceptFile.range().isValid()) {
if (acceptFile.range().length) {
Expand Down Expand Up @@ -517,7 +517,7 @@ namespace XMPP { namespace Jingle { namespace FileTransfer {

void Application::setState(State state) { d->setState(state); }

Stanza::Error Application::lastError() const { return d->lastError; }
const std::optional<Stanza::Error> &Application::lastError() const { return d->lastError; }

Reason Application::lastReason() const { return d->lastReason; }

Expand Down
9 changes: 5 additions & 4 deletions src/xmpp/xmpp-im/jingle-ft.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ namespace XMPP { namespace Jingle { namespace FileTransfer {
Application(const QSharedPointer<Pad> &pad, const QString &contentName, Origin creator, Origin senders);
~Application() override;

bool isValid() const;
void setState(State state) override;
XMPP::Stanza::Error lastError() const override;
Reason lastReason() const override;
bool isValid() const;
void setState(State state) override;

const std::optional<XMPP::Stanza::Error> &lastError() const override;
Reason lastReason() const override;

bool isTransportReplaceEnabled() const override;
void remove(Reason::Condition cond = Reason::Success, const QString &comment = QString()) override;
Expand Down
10 changes: 5 additions & 5 deletions src/xmpp/xmpp-im/jingle-s5b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ namespace XMPP { namespace Jingle { namespace S5B {
ce = ce.nextSiblingElement(candidateTag)) {
Candidate c(q, ce);
if (!c) {
throw Stanza::Error(Stanza::Error::Cancel, Stanza::Error::BadRequest);
throw Stanza::Error(Stanza::Error::ErrorType::Cancel, Stanza::Error::ErrorCond::BadRequest);
}
if (!p2pAllowed && c.type() != Candidate::Proxy) {
qDebug("new remote candidate discarded with forbidden p2p: %s", qPrintable(c));
Expand Down Expand Up @@ -1262,14 +1262,14 @@ namespace XMPP { namespace Jingle { namespace S5B {
if (it == localCandidates.end()) {
if (localCandidatesTrack.contains(cid))
return true; // likely discarded as not needed anymore
throw Stanza::Error(Stanza::Error::Cancel, Stanza::Error::ItemNotFound,
throw Stanza::Error(Stanza::Error::ErrorType::Cancel, Stanza::Error::ErrorCond::ItemNotFound,
QString("failed to find incoming candidate-used candidate %1").arg(cid));
}
auto &cUsed = it->second;
if (cUsed.state() == Candidate::Pending) {
if (cUsed.type() != Candidate::Proxy && !cUsed.isConnected()) {
throw Stanza::Error(
Stanza::Error::Cancel, Stanza::Error::NotAcceptable,
Stanza::Error::ErrorType::Cancel, Stanza::Error::ErrorCond::NotAcceptable,
QString("incoming candidate-used refers a candidate w/o active socks connection: %1")
.arg(QString(cUsed)));
}
Expand Down Expand Up @@ -1313,7 +1313,7 @@ namespace XMPP { namespace Jingle { namespace S5B {
if (!el.isNull()) {
QString cid = el.attribute(QStringLiteral("cid"));
if (cid.isEmpty()) {
throw Stanza::Error(Stanza::Error::Cancel, Stanza::Error::ItemNotFound,
throw Stanza::Error(Stanza::Error::ErrorType::Cancel, Stanza::Error::ErrorCond::ItemNotFound,
"failed to find incoming activated candidate");
}
auto c = remoteUsedCandidate;
Expand All @@ -1334,7 +1334,7 @@ namespace XMPP { namespace Jingle { namespace S5B {
if (!el.isNull()) {
auto it = localCandidates.find(el.attribute(QStringLiteral("cid")));
if (it == localCandidates.end()) {
throw Stanza::Error(Stanza::Error::Cancel, Stanza::Error::ItemNotFound,
throw Stanza::Error(Stanza::Error::ErrorType::Cancel, Stanza::Error::ErrorCond::ItemNotFound,
"failed to find incoming proxy-error candidate");
}
auto &c = it->second;
Expand Down
Loading

0 comments on commit bc922e1

Please sign in to comment.