From 93ddb6dfdc6e683eb683b42b65253842c53e6d75 Mon Sep 17 00:00:00 2001 From: APTX Date: Tue, 28 Feb 2012 20:35:44 +0100 Subject: [PATCH] Make it possible to tell if a command's parameter combination will result in a valid command. Prevent client from sending invalid commands. --- abstractcommand.cpp | 5 +++++ abstractcommand.h | 1 + anidbudpclient_global.h | 1 + client.cpp | 8 ++++++++ filecommand.cpp | 8 ++++++++ filecommand.h | 1 + mylistaddcommand.cpp | 5 +++++ mylistaddcommand.h | 1 + mylistcommand.h | 1 - votecommand.cpp | 7 +++++++ votecommand.h | 1 + 11 files changed, 38 insertions(+), 1 deletion(-) diff --git a/abstractcommand.cpp b/abstractcommand.cpp index 9e13892..9d7d1a4 100644 --- a/abstractcommand.cpp +++ b/abstractcommand.cpp @@ -25,6 +25,11 @@ bool AbstractCommand::requiresSession() const return true; } +bool AbstractCommand::isValid() const +{ + return true; +} + // === AbstractReply::AbstractReply(const QByteArray &id, QObject *parent) : QObject(parent) diff --git a/abstractcommand.h b/abstractcommand.h index 63e2cc2..0a71104 100644 --- a/abstractcommand.h +++ b/abstractcommand.h @@ -31,6 +31,7 @@ public: **/ virtual bool waitForResult() const; virtual bool requiresSession() const; + virtual bool isValid() const; }; #define REPLY_DEFINITION_HELPER_INTERNAL(name, constructor) \ diff --git a/anidbudpclient_global.h b/anidbudpclient_global.h index 83e3129..d885f77 100644 --- a/anidbudpclient_global.h +++ b/anidbudpclient_global.h @@ -46,6 +46,7 @@ namespace AniDBUdpClient enum ReplyCode { + INVALID_COMMAND = -2, CLIENT_DESTROYED = -1, UNKNOWN_REPLY = 0, diff --git a/client.cpp b/client.cpp index 03f1e6a..6846181 100644 --- a/client.cpp +++ b/client.cpp @@ -1093,6 +1093,14 @@ void Client::enqueueControlCommand(AbstractReply *command, bool first) void Client::sendCommand(AbstractReply *command, bool controlCommand) { + if (!command->command().isValid()) + { + command->setRawReply(INVALID_COMMAND, QString()); + if (!command->command().waitForResult()) + command->deleteLater(); + return; + } + if (m_enableEncryption && command->command().requiresSession() && (m_salt.isEmpty() || !usingEncryption)) { if (controlCommand) diff --git a/filecommand.cpp b/filecommand.cpp index 4fa8836..18266e6 100644 --- a/filecommand.cpp +++ b/filecommand.cpp @@ -174,6 +174,14 @@ bool FileCommand::waitForResult() const return true; } +bool FileCommand::isValid() const +{ + const bool validAnameGE = !m_aname.isEmpty() && (!m_gname.isEmpty() && m_epno || m_gid && m_epno); + const bool validAidGE = m_aid && ((!m_gname.isEmpty() && m_epno) || (m_gid && m_epno)); + return (m_fid || (!m_ed2k.isEmpty() && m_size) || validAnameGE || validAidGE); + // fmask & amask don't need to be checked as all invalid bits are removed +} + Command FileCommand::rawCommand() const { Command cmd; diff --git a/filecommand.h b/filecommand.h index aa0496d..6edf2bb 100644 --- a/filecommand.h +++ b/filecommand.h @@ -62,6 +62,7 @@ public: void setAmask(FileAnimeFlags amask); bool waitForResult() const; + bool isValid() const; Command rawCommand() const; private: diff --git a/mylistaddcommand.cpp b/mylistaddcommand.cpp index 46e305e..cff7e92 100644 --- a/mylistaddcommand.cpp +++ b/mylistaddcommand.cpp @@ -114,6 +114,11 @@ bool MyListAddCommand::waitForResult() const return true; } +bool MyListAddCommand::isValid() const +{ + return m_fid || (!m_ed2k.isEmpty() && m_size) || (m_lid && m_edit); +} + Command MyListAddCommand::rawCommand() const { Command cmd; diff --git a/mylistaddcommand.h b/mylistaddcommand.h index 637635e..749e9a3 100644 --- a/mylistaddcommand.h +++ b/mylistaddcommand.h @@ -55,6 +55,7 @@ public: void setMyListState(const MyListState &state); bool waitForResult() const; + bool isValid() const; Command rawCommand() const; diff --git a/mylistcommand.h b/mylistcommand.h index 336b1bf..4360df4 100644 --- a/mylistcommand.h +++ b/mylistcommand.h @@ -49,7 +49,6 @@ public: bool waitForResult() const; Command rawCommand() const; - void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); private: void init(); diff --git a/votecommand.cpp b/votecommand.cpp index 5de0082..261eaf5 100644 --- a/votecommand.cpp +++ b/votecommand.cpp @@ -85,6 +85,13 @@ bool VoteCommand::waitForResult() const return true; } +bool VoteCommand::isValid() const +{ + return (m_id || !m_name.isEmpty()) + && (m_epno && m_voteType == AnimeVote || !m_epno); + // Value is set to 0 if not in valid range +} + Command VoteCommand::rawCommand() const { Command cmd; diff --git a/votecommand.h b/votecommand.h index 8e8e78f..6ed6dbb 100644 --- a/votecommand.h +++ b/votecommand.h @@ -41,6 +41,7 @@ public: void setEpno(int epno); bool waitForResult() const; + bool isValid() const; Command rawCommand() const; private: -- 2.52.0