From ade9cb1f847e1c0edc22a08ad2950454fe2b566b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 21 Oct 2018 15:58:30 +0200 Subject: Remove old discord code, update for odhtdb changes --- src/Channel.cpp | 15 -------------- src/main.cpp | 63 +++++++++------------------------------------------------ 2 files changed, 9 insertions(+), 69 deletions(-) (limited to 'src') diff --git a/src/Channel.cpp b/src/Channel.cpp index 6107a22..9164f9d 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -207,21 +207,6 @@ namespace dchat else addLocalMessage(msg, localUser, 0, odhtdb::Hash()); } - - void Channel::addDiscordMessage(const string &discordUserName, u64 discordUserId, const string &msg) - { - assert(database && localUser->type == User::Type::ONLINE_LOCAL_USER); - auto onlineLocalUser = static_cast(localUser); - - sibs::SafeSerializer serializer; - serializer.add(ChannelDataType::ADD_DISCORD_MESSAGE); - serializer.add(discordUserId); - serializer.add((u8)discordUserName.size()); - serializer.add((const u8*)discordUserName.data(), discordUserName.size()); - serializer.add((const u8*)msg.data(), msg.size()); - - database->addData(databaseNodeInfo, onlineLocalUser->keyPair, odhtdb::DataView(serializer.getBuffer().data(), serializer.getBuffer().size())); - } void Channel::deleteLocalMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser) { diff --git a/src/main.cpp b/src/main.cpp index e60f77c..77b1d5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,7 +102,7 @@ static void channelChangeChannelName(Channel *channel, const StringView data, co int userPermissionLevel = channel->getUserLowestPermissionLevel(user); if(userPermissionLevel != odhtdb::PERMISSION_LEVEL_ADMIN) { - fprintf(stderr, "Channel change name: attempted by user %s who is not an admin\n", user->getName().c_str()); + fprintf(stderr, "Channel change name: attempted by user %s who is not an admin (permission level: %d)\n", user->getName().c_str(), userPermissionLevel); return; } @@ -118,40 +118,6 @@ static void channelChangeChannelName(Channel *channel, const StringView data, co // We dont care if there is more data to read (malicious packet), we already got all the data we need } -static void channelAddDiscordMessage(Channel *channel, const StringView data, const odhtdb::Signature::PublicKey &userPublicKey, u64 timestamp, const odhtdb::Hash &requestHash) -{ - auto bridgeOwner = channel->getUserByPublicKey(userPublicKey); - if(!bridgeOwner) - { - fprintf(stderr, "Channel add discord message: user with public key %s not found in channel %s\n", userPublicKey.toString().c_str(), channel->getName().c_str()); - return; - } - - int userPermissionLevel = channel->getUserLowestPermissionLevel(bridgeOwner); - if(userPermissionLevel != odhtdb::PERMISSION_LEVEL_ADMIN) - { - fprintf(stderr, "Channel add discord message: attempted by user %s who is not an admin\n", bridgeOwner->getName().c_str()); - return; - } - - sibs::SafeDeserializer deserializer((const u8*)data.data, data.size); - u64 discordUserId = deserializer.extract(); - u8 discordNameLength = deserializer.extract(); - if(discordNameLength == 0) return; - - string discordUserName; - discordUserName.resize(discordNameLength); - deserializer.extract((u8*)&discordUserName[0], discordNameLength); - - usize msgSize = deserializer.getSize(); - if(msgSize == 0) return; - string msg(deserializer.getBuffer(), deserializer.getBuffer() + deserializer.getSize()); - - auto timestampSeconds = ntp::NtpTimestamp::fromCombined(timestamp).seconds; - channel->addLocalDiscordMessage(discordUserName, discordUserId, msg, bridgeOwner, timestampSeconds, requestHash); - // We dont care if there is more data to read (malicious packet), we already got all the data we need -} - static void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &requestHash, const odhtdb::Signature::PublicKey &creatorPublicKey, const StringView decryptedObject, u64 timestamp, bool loadedFromCache) { User *user = channel->getUserByPublicKey(creatorPublicKey); @@ -232,18 +198,6 @@ static void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &reques } break; } - case ChannelDataType::ADD_DISCORD_MESSAGE: - { - try - { - channelAddDiscordMessage(channel, decryptedData, creatorPublicKey, timestamp, requestHash); - } - catch(sibs::DeserializeException &e) - { - fprintf(stderr, "Failed to deserialize channel add discord message\n"); - } - break; - } default: fprintf(stderr, "Got unexpected channel data type: %u\n", channelDataType); break; @@ -300,7 +254,7 @@ int main(int argc, char **argv) using LocalUserMessageCallback = function; LocalUserMessageCallback onMessageByLocalUser = nullptr; - callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseCreateNodeRequest &request) + callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, &database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseCreateNodeRequest &request) { lock_guard lock(channelMessageMutex); printf("Create node callback func %s\n", request.nodeHash->toString().c_str()); @@ -318,6 +272,7 @@ int main(int argc, char **argv) memcpy(databaseNodeHash->getData(), request.nodeHash->getData(), odhtdb::HASH_BYTE_SIZE); odhtdb::DatabaseNode databaseNode(nodeUserData->second.nodeEncryptionKey, databaseNodeHash); Channel *channel = new Channel("NoChannelName", databaseNode, localUser, database); + printf("database: %llu\n", database.get()); ChannelSidePanel::addChannel(channel); channels.push_back(channel); Channel::setCurrent(channel); @@ -407,7 +362,7 @@ int main(int argc, char **argv) }; // Login to account - Command::add("login", [&localNodeUsers, database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector &args) + Command::add("login", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 2) @@ -445,7 +400,7 @@ int main(int argc, char **argv) }); // Register account - Command::add("register", [&localNodeUsers, database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector &args) + Command::add("register", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 2) @@ -482,7 +437,7 @@ int main(int argc, char **argv) // TODO: Use database->addData to change channel name // Create channel - Command::add("cc", [database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword, &lastFocusedTimer, addSystemMessage](const vector &args) + Command::add("cc", [&database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword, &lastFocusedTimer, addSystemMessage](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 1) @@ -515,7 +470,7 @@ int main(int argc, char **argv) }); // Create invite key - Command::add("invite", [&channelMessageMutex, &offlineChannel, database, addSystemMessage](const vector &args) + Command::add("invite", [&channelMessageMutex, &offlineChannel, &database, addSystemMessage](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 0) @@ -599,7 +554,7 @@ int main(int argc, char **argv) }); // Join channel using invite key - Command::add("jc", [&loggedIn, database, &localNodeUsers, &channelMessageMutex, &waitingToJoin, &waitingToJoinChannels, ¤tUsername, ¤tPassword](const vector &args) + Command::add("jc", [&loggedIn, &database, &localNodeUsers, &channelMessageMutex, &waitingToJoin, &waitingToJoinChannels, ¤tUsername, ¤tPassword](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 1) @@ -651,7 +606,7 @@ int main(int argc, char **argv) keySerializer.add((const u8*)encryptionKeyBinRaw.data(), odhtdb::ENCRYPTION_KEY_BYTE_SIZE); odhtdb::InfoHash key = odhtdb::Database::getInfoHash(keySerializer.getBuffer().data(), keySerializer.getBuffer().size()); database->sendCustomMessage(key, serializer.getBuffer().data(), serializer.getBuffer().size(), - [database, nodeHash, encryptionKey, &waitingToJoinChannels, &channelMessageMutex, keyPair, ¤tUsername, ¤tPassword, &localNodeUsers] + [&database, nodeHash, encryptionKey, &waitingToJoinChannels, &channelMessageMutex, keyPair, ¤tUsername, ¤tPassword, &localNodeUsers] (bool gotResponse, const void *data, usize size) { if(!gotResponse) -- cgit v1.2.3