/** * @brief Create a Tox ID from a QString. * * If the given rawId is not a valid Tox ID, but can be a Public Key then: * publicKey == rawId and noSpam == 0 == checkSum. * If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created. * * @param id Tox ID string to convert to ToxId object */ ToxId::ToxId(const QString& id) { // TODO: remove construction from PK only if (isToxId(id)) { toxId = QByteArray::fromHex(id.toLatin1()); } else if (id.length() >= PUBLIC_KEY_HEX_CHARS) { toxId = QByteArray::fromHex(id.left(PUBLIC_KEY_HEX_CHARS).toLatin1()); } else { toxId = QByteArray(); // invalid id string } }
void ToxId::constructToxId(const QByteArray& rawId) { // TODO: remove construction from PK only if (rawId.length() == TOX_SECRET_KEY_SIZE) { toxId = QByteArray(rawId); // construct from PK only } else if (rawId.length() == TOX_ADDRESS_SIZE && isToxId(rawId.toHex().toUpper())) { toxId = QByteArray(rawId); // construct from full toxid } else { toxId = QByteArray(); // invalid id } }
/** @brief Create a Tox ID from QString. If the given id is not a valid Tox ID, then: publicKey == id and noSpam == "" == checkSum. @param id Tox ID string to convert to ToxId object */ ToxId::ToxId(const QString &id) { if (isToxId(id)) { publicKey = id.left(TOX_ID_PUBLIC_KEY_LENGTH); noSpam = id.mid(TOX_ID_PUBLIC_KEY_LENGTH, TOX_ID_NO_SPAM_LENGTH); checkSum = id.mid(TOX_ID_PUBLIC_KEY_LENGTH + TOX_ID_NO_SPAM_LENGTH, TOX_ID_CHECKSUM_LENGTH); } else { publicKey = id; } }
void AddFriendForm::onSendTriggered() { QString id = toxId.text().trimmed(); if (id.isEmpty()) { showWarning(tr("Please fill in a valid Tox ID","Tox ID of the friend you're sending a friend request to")); } else if (isToxId(id)) { emit friendRequested(id, getMessage()); this->toxId.setText(""); this->message.setText(""); } else { id = id.replace("@", "._tox."); dns.setName(id); dns.lookup(); } }
void AddFriendForm::handleDnsLookup() { const QString idKeyWord("id="); if (dns.error() == QDnsLookup::NotFoundError) { showWarning(tr("This address does not exist","The DNS gives the Tox ID associated to toxme.se addresses")); return; } else if (dns.error() != QDnsLookup::NoError) { showWarning(tr("Error while looking up DNS","The DNS gives the Tox ID associated to toxme.se addresses")); return; } const QList<QDnsTextRecord> textRecords = dns.textRecords(); if (textRecords.length() != 1) { showWarning(tr("Unexpected number of text records", "Error with the DNS")); return; } const QList<QByteArray> textRecordValues = textRecords.first().values(); if (textRecordValues.length() != 1) { showWarning(tr("Unexpected number of values in text record", "Error with the DNS")); return; } const QString entry(textRecordValues.first()); int idx = entry.indexOf(idKeyWord); if (idx < 0) { showWarning(tr("The DNS lookup does not contain any Tox ID", "Error with the DNS")); return; } idx += idKeyWord.length(); if (entry.length() < idx + static_cast<int>(TOX_ID_LENGTH)) { showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS")); return; } const QString friendAdress = entry.mid(idx, TOX_ID_LENGTH); if (!isToxId(friendAdress)) { showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS")); return; } // finally we got it emit friendRequested(friendAdress, getMessage()); }
void AddFriendForm::onSendTriggered() { QString id = toxId.text().trimmed(); if (id.isEmpty()) { showWarning(tr("Please fill in a valid Tox ID","Tox ID of the friend you're sending a friend request to")); } else if (isToxId(id)) { if (id.toUpper() == Core::getInstance()->getSelfId().toString().toUpper()) showWarning(tr("You can't add yourself as a friend!","When trying to add your own Tox ID as friend")); else emit friendRequested(id, getMessage()); this->toxId.setText(""); this->message.setText(""); } else { id = id.replace("@", "._tox."); dns.setName(id); dns.lookup(); } }
void AddFriendForm::handleDnsLookup() { const QString idKeyWord("id="), verKeyWord("v="); if (dns.error() == QDnsLookup::NotFoundError) { showWarning(tr("This address does not exist","The DNS gives the Tox ID associated to toxme.se addresses")); return; } else if (dns.error() != QDnsLookup::NoError) { showWarning(tr("Error while looking up DNS","The DNS gives the Tox ID associated to toxme.se addresses")); return; } const QList<QDnsTextRecord> textRecords = dns.textRecords(); if (textRecords.length() != 1) { showWarning(tr("Unexpected number of text records", "Error with the DNS")); return; } const QList<QByteArray> textRecordValues = textRecords.first().values(); if (textRecordValues.length() != 1) { showWarning(tr("Unexpected number of values in text record", "Error with the DNS")); return; } const QString entry(textRecordValues.first()); // Check toxdns protocol version, we only support tox1 for now int verx = entry.indexOf(verKeyWord); if (verx) { verx += verKeyWord.size(); int verend = entry.indexOf(';', verx); if (verend) { QString ver = entry.mid(verx, verend); if (ver != "tox1") { showWarning(tr("The version of Tox DNS used by this server is not supported", "Error with the DNS")); return; } } } int idx = entry.indexOf(idKeyWord); if (idx < 0) { showWarning(tr("The DNS lookup does not contain any Tox ID", "Error with the DNS")); return; } idx += idKeyWord.length(); if (entry.length() < idx + static_cast<int>(TOX_ID_LENGTH)) { showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS")); return; } const QString friendAdress = entry.mid(idx, TOX_ID_LENGTH); if (!isToxId(friendAdress)) { showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS")); return; } // finally we got it emit friendRequested(friendAdress, getMessage()); this->toxId.setText(""); this->message.setText(""); }
/** * @brief Check, that id is a valid Tox ID. * @param id Tox ID to check. * @return True if id is a valid Tox ID, false otherwise. * @note Validates the checksum. */ bool ToxId::isValidToxId(const QString& id) { return isToxId(id) && ToxId(id).isValid(); }