aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-01 13:58:29 +0200
committerdec05eba <dec05eba@protonmail.com>2018-05-01 13:58:33 +0200
commit98bf43608e1187b164481d83501226d897e77d57 (patch)
tree507532093b5b3cf96dc27f43ef5f7e909f934402 /src
parent431c1dcded16649c10331b9dc4e57f20067cea0b (diff)
Add command to get join key, fix crash when joining channel
Diffstat (limited to 'src')
-rw-r--r--src/Channel.cpp4
-rw-r--r--src/main.cpp40
2 files changed, 32 insertions, 12 deletions
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<odhtdb::Hash> 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<odhtdb::DatabaseCreateResponse> &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<odhtdb::Hash> nodeHash = make_shared<odhtdb::Hash>();
+ 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<odhtdb::OwnedMemory> encryptionKey = make_shared<odhtdb::OwnedMemory>();
+ 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<recursive_mutex> 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", [&currentUserKeyPair, &currentUserName, &offlineChannel](const vector<string> &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())
{