From 98bf43608e1187b164481d83501226d897e77d57 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 1 May 2018 13:58:29 +0200 Subject: Add command to get join key, fix crash when joining channel --- include/Channel.hpp | 2 +- src/Channel.cpp | 4 ++-- src/main.cpp | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/include/Channel.hpp b/include/Channel.hpp index be6e7c7..535cce5 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -31,7 +31,7 @@ namespace dchat const std::string& getName() const; const std::vector getUsers() const; User* getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey); - std::shared_ptr getId(); + const odhtdb::DatabaseNode& getNodeInfo() const; // If timestamp is 0, then timestamp is not used void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds = 0); diff --git a/src/Channel.cpp b/src/Channel.cpp index b922833..f403984 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -100,9 +100,9 @@ namespace dchat return nullptr; } - std::shared_ptr Channel::getId() + const odhtdb::DatabaseNode& Channel::getNodeInfo() const { - return databaseNodeInfo.getRequestHash(); + return databaseNodeInfo; } void Channel::addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds) diff --git a/src/main.cpp b/src/main.cpp index 7245b6e..03f6a81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,19 +32,23 @@ string createChannelJoinKey(const unique_ptr &da return result; } +string createJoinKeyFromDatabaseNode(const odhtdb::DatabaseNode &databaseNode) +{ + return databaseNode.getRequestHash()->toString() + "&" + odhtdb::bin2hex((const char*)databaseNode.getNodeEncryptionKey()->data, databaseNode.getNodeEncryptionKey()->size); +} + odhtdb::DatabaseNode createDatabaseNodeFromJoinKey(const string &joinKey) { - odhtdb::DatabaseNode result; - string nodeHashStr = odhtdb::hex2bin(joinKey.c_str(), 64); - memcpy(result.getRequestHash()->getData(), nodeHashStr.data(), nodeHashStr.size()); + shared_ptr nodeHash = make_shared(); + memcpy(nodeHash->getData(), nodeHashStr.data(), nodeHashStr.size()); string nodeEncryptionKeyStr = odhtdb::hex2bin(joinKey.c_str() + 65, 64); - char *nodeEncryptionKeyRaw = new char[nodeEncryptionKeyStr.size()]; - result.getNodeEncryptionKey()->data = nodeEncryptionKeyRaw; - result.getNodeEncryptionKey()->size = nodeEncryptionKeyStr.size(); + shared_ptr encryptionKey = make_shared(); + encryptionKey->data = new char[nodeEncryptionKeyStr.size()]; + encryptionKey->size = nodeEncryptionKeyStr.size(); - return result; + return odhtdb::DatabaseNode(encryptionKey, nodeHash); } void channelAddStoredMessage(Channel *channel, odhtdb::DatabaseStorageObject *nodeStorageObject) @@ -62,7 +66,7 @@ void channelAddStoredMessage(Channel *channel, odhtdb::DatabaseStorageObject *no void channelAddStoredMessages(Channel *channel, const odhtdb::DatabaseStorageObjectList *nodeStorage) { - printf("Load %u messages in channel %s\n", nodeStorage->objects.size(), channel->getName().c_str()); + printf("Load %u message(s) in channel %s\n", nodeStorage->objects.size(), channel->getName().c_str()); for(auto nodeStorageAddedObject : nodeStorage->objects) { if(nodeStorageAddedObject->decryptedObject.operation == odhtdb::DatabaseOperation::ADD_DATA) @@ -135,7 +139,7 @@ int main(int argc, char **argv) lock_guard lock(channelMessageMutex); for(Channel *channel : channels) { - if(*request.nodeHash == *channel->getId()) + if(*request.nodeHash == *channel->getNodeInfo().getRequestHash()) { User *user = channel->getUserByPublicKey(request.creatorUser->getPublicKey()); if(!user) @@ -162,7 +166,7 @@ int main(int argc, char **argv) for(Channel *channel : channels) { - if(*request.nodeHash == *channel->getId()) + if(*request.nodeHash == *channel->getNodeInfo().getRequestHash()) { User *userToAdd = channel->getUserByPublicKey(request.userToAdd->getPublicKey()); if(userToAdd && currentUserKeyPair && request.userToAdd->getPublicKey() == currentUserKeyPair->getPublicKey() && channel->getLocalUser()->type != User::Type::ONLINE) @@ -424,6 +428,22 @@ int main(int argc, char **argv) Channel::getCurrent()->addLocalMessage(response, Channel::getCurrent()->getSystemUser()); }); + // Get channel join key + Command::add("joinkey", [¤tUserKeyPair, ¤tUserName, &offlineChannel](const vector &args) + { + Channel *currentChannel = Channel::getCurrent(); + if(!currentChannel || currentChannel == &offlineChannel) + { + Channel::getCurrent()->addLocalMessage("You are not inside a channel", Channel::getCurrent()->getSystemUser()); + return; + } + + string response = "Join key: "; + response += createJoinKeyFromDatabaseNode(currentChannel->getNodeInfo()); + printf("%s\n", response.c_str()); + Channel::getCurrent()->addLocalMessage(response, Channel::getCurrent()->getSystemUser()); + }); + sf::Event event; while (window.isOpen()) { -- cgit v1.2.3