diff --git a/app/src/main/java/com/bitchat/android/ui/ChannelManager.kt b/app/src/main/java/com/bitchat/android/ui/ChannelManager.kt index f0d6b4c1..641c65ef 100644 --- a/app/src/main/java/com/bitchat/android/ui/ChannelManager.kt +++ b/app/src/main/java/com/bitchat/android/ui/ChannelManager.kt @@ -284,9 +284,21 @@ class ChannelManager( state.setShowPasswordPrompt(false) state.setPasswordPromptChannel(null) } - - fun setChannelPassword(channel: String, password: String): Boolean { - return verifyChannelPassword(channel, password) + + fun setChannelPassword(channel: String, password: String) { + + channelPasswords[channel] = password + + channelKeys[channel] = deriveChannelKey(password, channel) + + state.setPasswordProtectedChannels( + state.getPasswordProtectedChannelsValue().toMutableSet().apply { add(channel) } + ) + + dataManager.saveChannelData( + state.getJoinedChannelsValue(), + state.getPasswordProtectedChannelsValue() + ) } // MARK: - Emergency Clear diff --git a/app/src/main/java/com/bitchat/android/ui/CommandProcessor.kt b/app/src/main/java/com/bitchat/android/ui/CommandProcessor.kt index 616cf3c4..286c0934 100644 --- a/app/src/main/java/com/bitchat/android/ui/CommandProcessor.kt +++ b/app/src/main/java/com/bitchat/android/ui/CommandProcessor.kt @@ -39,6 +39,7 @@ class CommandProcessor( "/m", "/msg" -> handleMessageCommand(parts, meshService) "/w" -> handleWhoCommand() "/clear" -> handleClearCommand() + "/pass" -> handlePassCommand(parts, myPeerID) "/block" -> handleBlockCommand(parts, meshService) "/unblock" -> handleUnblockCommand(parts, meshService) "/hug" -> handleActionCommand(parts, "gives", "a warm hug 🫂", meshService, myPeerID, onSendMessage) @@ -54,7 +55,8 @@ class CommandProcessor( if (parts.size > 1) { val channelName = parts[1] val channel = if (channelName.startsWith("#")) channelName else "#$channelName" - val success = channelManager.joinChannel(channel, null, myPeerID) + val password = if (parts.size > 2) parts[2] else null + val success = channelManager.joinChannel(channel, password, myPeerID) if (success) { val systemMessage = BitchatMessage( sender = "system", @@ -165,6 +167,52 @@ class CommandProcessor( } } } + + private fun handlePassCommand(parts: List, peerID: String) { + val currentChannel = state.getCurrentChannelValue() + + if (currentChannel == null) { + val systemMessage = BitchatMessage( + sender = "system", + content = "you must be in a channel to set a password.", + timestamp = Date(), + isRelay = false + ) + messageManager.addMessage(systemMessage) + return + } + + if (parts.size == 2){ + if(!channelManager.isChannelCreator(channel = currentChannel, peerID = peerID)){ + val systemMessage = BitchatMessage( + sender = "system", + content = "you must be the channel creator to set a password.", + timestamp = Date(), + isRelay = false + ) + channelManager.addChannelMessage(currentChannel,systemMessage,null) + return + } + val newPassword = parts[1] + channelManager.setChannelPassword(currentChannel, newPassword) + val systemMessage = BitchatMessage( + sender = "system", + content = "password changed for channel $currentChannel", + timestamp = Date(), + isRelay = false + ) + channelManager.addChannelMessage(currentChannel,systemMessage,null) + } + else{ + val systemMessage = BitchatMessage( + sender = "system", + content = "usage: /pass ", + timestamp = Date(), + isRelay = false + ) + channelManager.addChannelMessage(currentChannel,systemMessage,null) + } + } private fun handleBlockCommand(parts: List, meshService: Any) { if (parts.size > 1) {