1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(toxid): improve checks for valid ToxIDs

This commit is contained in:
sudden6 2016-12-30 13:49:25 +01:00
parent a223510cf7
commit 0b2f22d521
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
3 changed files with 22 additions and 10 deletions

View File

@ -573,9 +573,15 @@ void Core::acceptFriendRequest(const QString& userId)
void Core::requestFriendship(const QString& friendAddress, const QString& message) void Core::requestFriendship(const QString& friendAddress, const QString& message)
{ {
ToxId friendToxId(friendAddress);
const QString userId = friendAddress.mid(0, TOX_PUBLIC_KEY_SIZE * 2); const QString userId = friendAddress.mid(0, TOX_PUBLIC_KEY_SIZE * 2);
if (message.isEmpty()) if (!friendToxId.isValid())
{
emit failedToAddFriend(userId,
tr("Invalid Tox ID"));
}
else if (message.isEmpty())
{ {
emit failedToAddFriend(userId, tr("You need to write a message with your request")); emit failedToAddFriend(userId, tr("You need to write a message with your request"));
} }
@ -583,7 +589,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag
{ {
emit failedToAddFriend(userId, tr("Your message is too long!")); emit failedToAddFriend(userId, tr("Your message is too long!"));
} }
else if (hasFriendWithAddress(friendAddress)) else if (hasFriendWithAddress(userId))
{ {
emit failedToAddFriend(userId, tr("Friend is already added")); emit failedToAddFriend(userId, tr("Friend is already added"));
} }
@ -602,7 +608,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag
{ {
qDebug() << "Requested friendship of "<<friendId; qDebug() << "Requested friendship of "<<friendId;
// Update our friendAddresses // Update our friendAddresses
Settings::getInstance().updateFriendAddress(friendAddress); Settings::getInstance().updateFriendAddress(userId);
QString inviteStr = tr("/me offers friendship."); QString inviteStr = tr("/me offers friendship.");
if (message.length()) if (message.length())
inviteStr = tr("/me offers friendship, \"%1\"").arg(message); inviteStr = tr("/me offers friendship, \"%1\"").arg(message);

View File

@ -35,7 +35,7 @@
#define CHECKSUM_HEX_CHARS (2*CHECKSUM_BYTES) #define CHECKSUM_HEX_CHARS (2*CHECKSUM_BYTES)
#define TOXID_HEX_CHARS (2*TOX_ADDRESS_SIZE) #define TOXID_HEX_CHARS (2*TOX_ADDRESS_SIZE)
const QRegularExpression ToxId::ToxIdRegEx = QRegularExpression(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(TOXID_HEX_CHARS)); const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(TOXID_HEX_CHARS));
/** /**
* @class ToxId * @class ToxId
@ -58,7 +58,7 @@ const QRegularExpression ToxId::ToxIdRegEx = QRegularExpression(QString("(^|\\s)
* @brief The default constructor. Creates an empty Tox ID. * @brief The default constructor. Creates an empty Tox ID.
*/ */
ToxId::ToxId() ToxId::ToxId()
: toxId(TOX_ADDRESS_SIZE, 0x00) : toxId()
{} {}
/** /**
@ -80,6 +80,7 @@ ToxId::ToxId(const ToxId& other)
*/ */
ToxId::ToxId(const QString& id) ToxId::ToxId(const QString& id)
{ {
// TODO: remove construction from PK only
if (isToxId(id)) if (isToxId(id))
{ {
toxId = QByteArray::fromHex(id.toLatin1()); toxId = QByteArray::fromHex(id.toLatin1());
@ -90,7 +91,7 @@ ToxId::ToxId(const QString& id)
} }
else else
{ {
toxId = QByteArray(TOX_ADDRESS_SIZE, 0x00); // invalid id string toxId = QByteArray(); // invalid id string
} }
} }
@ -128,6 +129,7 @@ ToxId::ToxId(const uint8_t* rawId, int len)
void ToxId::constructToxId(const QByteArray& rawId) void ToxId::constructToxId(const QByteArray& rawId)
{ {
// TODO: remove construction from PK only
if(rawId.length() == TOX_SECRET_KEY_SIZE) if(rawId.length() == TOX_SECRET_KEY_SIZE)
{ {
toxId = QByteArray(rawId); // construct from PK only toxId = QByteArray(rawId); // construct from PK only
@ -139,7 +141,7 @@ void ToxId::constructToxId(const QByteArray& rawId)
} }
else else
{ {
toxId = QByteArray(TOX_ADDRESS_SIZE, 0x00); // invalid id string toxId = QByteArray(); // invalid id
} }
} }
@ -183,11 +185,16 @@ void ToxId::clear()
/** /**
* @brief Gets the ToxID as bytes, convenience function for toxcore interface. * @brief Gets the ToxID as bytes, convenience function for toxcore interface.
* @return The ToxID as uint8_t* * @return The ToxID as uint8_t* if isValid() is true, else a nullptr.
*/ */
const uint8_t* ToxId::getBytes() const const uint8_t* ToxId::getBytes() const
{ {
if(isValid())
{
return reinterpret_cast<const uint8_t*>(toxId.constData()); return reinterpret_cast<const uint8_t*>(toxId.constData());
}
return nullptr;
} }
/** /**

View File

@ -100,7 +100,6 @@ void Nexus::start()
qRegisterMetaType<ToxFile::FileDirection>("ToxFile::FileDirection"); qRegisterMetaType<ToxFile::FileDirection>("ToxFile::FileDirection");
qRegisterMetaType<std::shared_ptr<VideoFrame>>("std::shared_ptr<VideoFrame>"); qRegisterMetaType<std::shared_ptr<VideoFrame>>("std::shared_ptr<VideoFrame>");
qRegisterMetaType<ToxId>("ToxId"); qRegisterMetaType<ToxId>("ToxId");
qRegisterMetaType<ToxKey>("ToxKey");
loginScreen = new LoginScreen(); loginScreen = new LoginScreen();