function chatBarFocusLost(J,O)DoBackgroundFadeIn()chatBarFocusChanged:Fire(false)if J then local J=(ChatBar:GetTextBox()).Text if ChatBar:IsInCustomState()then local O=ChatBar:GetCustomMessage()if O then J=O end local b=ChatBar:CustomStateProcessCompletedMessage(J)ChatBar:ResetCustomState()if b then return end end;(ChatBar:GetTextBox()).Text=""if J~=""then moduleApiTable.MessagePosted:fire(J)if not CommandProcessor:ProcessCompletedChatMessage(J,ChatWindow)then if ChatSettings.DisallowedWhiteSpace then for O=1,#ChatSettings.DisallowedWhiteSpace,1 do if ChatSettings.DisallowedWhiteSpace[O]=="\t"then J=string.gsub(J,ChatSettings.DisallowedWhiteSpace[O]," ")else J=string.gsub(J,ChatSettings.DisallowedWhiteSpace[O],"")end end end J=string.gsub(J,"\n","")J=string.gsub(J,"\091 \093\043"," ")local O=ChatWindow:GetTargetMessageChannel()if O then MessageSender:SendMessage(J,O)else MessageSender:SendMessage(J,nil)end end end end end local J={}function setupChatBarConnections()for O=1,#J,1 do J[O]:Disconnect()end J={}local O=(ChatBar:GetTextBox()).FocusLost:connect(chatBarFocusLost)table.insert(J,O)local b=(ChatBar:GetTextBox()).Focused:connect(chatBarFocused)table.insert(J,b)end setupChatBarConnections()ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)function getEchoMessagesInGeneral()if ChatSettings.EchoMessagesInGeneralChannel==nil then return true end return ChatSettings.EchoMessagesInGeneralChannel end EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(J)if not ChatSettings.ShowUserOwnFilteredMessage then if J.FromSpeaker==LocalPlayer.Name then return end end local O=J.OriginalChannel local b=ChatWindow:GetChannel(O)if b then b:UpdateMessageFiltered(J)end if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and O~=ChatSettings.GeneralChannelName)then local O=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if O then O:UpdateMessageFiltered(J)end end end)EventFolder.OnNewMessage.OnClientEvent:connect(function(J,O)local b=ChatWindow:GetChannel(O)if b then b:AddMessageToChannel(J)if J.FromSpeaker~=LocalPlayer.Name then ChannelsBar:UpdateMessagePostedInChannel(O)end if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and O~=ChatSettings.GeneralChannelName)then local O=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if O then O:AddMessageToChannel(J)end end moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()end end)EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(J,O)O=O or"\083\121\115\116\101\109"local b=ChatWindow:GetChannel(O)if b then b:AddMessageToChannel(J)ChannelsBar:UpdateMessagePostedInChannel(O)moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and O~=ChatSettings.GeneralChannelName)then local O=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if O then O:AddMessageToChannel(J)end end else warn(string.format("\074\117\115\116 \114\101\099\101\105\118\101\100 \115\121\115\116\101\109 \109\101\115\115\097\103\101 \102\111\114 \099\104\097\110\110\101\108 \073\'\109 \110\111\116 \105\110 \091\037\115\093",O))end end)function HandleChannelJoined(J,O,b,D,m,W)if ChatWindow:GetChannel(J)then ChatWindow:RemoveChannel(J)end if J==ChatSettings.GeneralChannelName then DidFirstChannelsLoads=true end if D then ChatBar:SetChannelNameColor(J,D)end local C=ChatWindow:AddChannel(J)if C then if J==ChatSettings.GeneralChannelName then DoSwitchCurrentChannel(J)end if b then local O=1 if#b>ChatSettings.MessageHistoryLengthPerChannel then O=#b-ChatSettings.MessageHistoryLengthPerChannel end for J=O,#b,1 do C:AddMessageToChannel(b[J])end if getEchoMessagesInGeneral()and m then if ChatSettings.GeneralChannelName and J~=ChatSettings.GeneralChannelName then local J=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if J then J:AddMessagesToChannelByTimeStamp(b,O)end end end end if O~=""then local b={ID=-1,FromSpeaker=nil;SpeakerUserId=0,OriginalChannel=J;IsFiltered=true;MessageLength=string.len(O);MessageLengthUtf8=utf8.len(utf8.nfcnormalize(O)),Message=trimTrailingSpaces(O);MessageType=ChatConstants.MessageTypeWelcome;Time=os.time();ExtraData=nil}C:AddMessageToChannel(b)if getEchoMessagesInGeneral()and(W and not ChatSettings.ShowChannelsBar)then if J~=ChatSettings.GeneralChannelName then local J=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if J then J:AddMessageToChannel(b)end end end end DoFadeInFromNewInformation()end end EventFolder.OnChannelJoined.OnClientEvent:connect(function(J,O,b,D)HandleChannelJoined(J,O,b,D,false,true)end)EventFolder.OnChannelLeft.OnClientEvent:connect(function(J)ChatWindow:RemoveChannel(J)DoFadeInFromNewInformation()end)EventFolder.OnMuted.OnClientEvent:connect(function(J) end)EventFolder.OnUnmuted.OnClientEvent:connect(function(J) end)EventFolder.OnMainChannelSet.OnClientEvent:connect(function(J)DoSwitchCurrentChannel(J)end);(coroutine.wrap(function()local J=DefaultChatSystemChatEvents:WaitForChild("\067\104\097\110\110\101\108\078\097\109\101\067\111\108\111\114\085\112\100\097\116\101\100",5)if J then J.OnClientEvent:connect(function(J,O)ChatBar:SetChannelNameColor(J,O)end)end end))()

function chatBarFocusLost(enterPressed, inputObject)
	DoBackgroundFadeIn()
	chatBarFocusChanged:Fire(false)

	if (enterPressed) then
		local message = ChatBar:GetTextBox().Text

		if ChatBar:IsInCustomState() then
			local customMessage = ChatBar:GetCustomMessage()
			if customMessage then
				message = customMessage
			end
			local messageSunk = ChatBar:CustomStateProcessCompletedMessage(message)
			ChatBar:ResetCustomState()
			if messageSunk then
				return
			end
		end

		ChatBar:GetTextBox().Text = ""

		if message ~= "" then
			--// Sends signal to eventually call Player:Chat() to handle C++ side legacy stuff.
			moduleApiTable.MessagePosted:fire(message)

			if not CommandProcessor:ProcessCompletedChatMessage(message, ChatWindow) then
				if ChatSettings.DisallowedWhiteSpace then
					for i = 1, #ChatSettings.DisallowedWhiteSpace do
						if ChatSettings.DisallowedWhiteSpace[i] == "\t" then
							message = string.gsub(message, ChatSettings.DisallowedWhiteSpace[i], " ")
						else
							message = string.gsub(message, ChatSettings.DisallowedWhiteSpace[i], "")
						end
					end
				end
				message = string.gsub(message, "\n", "")
				message = string.gsub(message, "[ ]+", " ")

				local targetChannel = ChatWindow:GetTargetMessageChannel()
				if targetChannel then
					MessageSender:SendMessage(message, targetChannel)
				else
					MessageSender:SendMessage(message, nil)
				end
			end
		end

	end
end

local ChatBarConnections = {}
function setupChatBarConnections()
	for i = 1, #ChatBarConnections do
		ChatBarConnections[i]:Disconnect()
	end
	ChatBarConnections = {}

	local focusLostConnection = ChatBar:GetTextBox().FocusLost:connect(chatBarFocusLost)
	table.insert(ChatBarConnections, focusLostConnection)

	local focusGainedConnection = ChatBar:GetTextBox().Focused:connect(chatBarFocused)
	table.insert(ChatBarConnections, focusGainedConnection)
end

setupChatBarConnections()
ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)

function getEchoMessagesInGeneral()
	if ChatSettings.EchoMessagesInGeneralChannel == nil then
		return true
	end
	return ChatSettings.EchoMessagesInGeneralChannel
end

EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(messageData)
	if not ChatSettings.ShowUserOwnFilteredMessage then
		if messageData.FromSpeaker == LocalPlayer.Name then
			return
		end
	end

	local channelName = messageData.OriginalChannel
	local channelObj = ChatWindow:GetChannel(channelName)
	if channelObj then
		channelObj:UpdateMessageFiltered(messageData)
	end

	if getEchoMessagesInGeneral() and ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
		local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
		if generalChannel then
			generalChannel:UpdateMessageFiltered(messageData)
		end
	end
end)

EventFolder.OnNewMessage.OnClientEvent:connect(function(messageData, channelName)
	local channelObj = ChatWindow:GetChannel(channelName)
	if (channelObj) then
		channelObj:AddMessageToChannel(messageData)

		if (messageData.FromSpeaker ~= LocalPlayer.Name) then
			ChannelsBar:UpdateMessagePostedInChannel(channelName)
		end

		if getEchoMessagesInGeneral() and ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
			local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
			if generalChannel then
				generalChannel:AddMessageToChannel(messageData)
			end
		end

		moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
		moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)

		DoFadeInFromNewInformation()
	end
end)

EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(messageData, channelName)
	channelName = channelName or "System"

	local channelObj = ChatWindow:GetChannel(channelName)
	if (channelObj) then
		channelObj:AddMessageToChannel(messageData)

		ChannelsBar:UpdateMessagePostedInChannel(channelName)

		moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
		moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)

		DoFadeInFromNewInformation()

		if getEchoMessagesInGeneral() and ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
			local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
			if generalChannel then
				generalChannel:AddMessageToChannel(messageData)
			end
		end
	else
		warn(string.format("Just received system message for channel I'm not in [%s]", channelName))
	end
end)


function HandleChannelJoined(channel, welcomeMessage, messageLog, channelNameColor, addHistoryToGeneralChannel,
	addWelcomeMessageToGeneralChannel)
	if ChatWindow:GetChannel(channel) then
		--- If the channel has already been added, remove it first.
		ChatWindow:RemoveChannel(channel)
	end

	if (channel == ChatSettings.GeneralChannelName) then
		DidFirstChannelsLoads = true
	end

	if channelNameColor then
		ChatBar:SetChannelNameColor(channel, channelNameColor)
	end

	local channelObj = ChatWindow:AddChannel(channel)

	if (channelObj) then
		if (channel == ChatSettings.GeneralChannelName) then
			DoSwitchCurrentChannel(channel)
		end

		if (messageLog) then
			local startIndex = 1
			if #messageLog > ChatSettings.MessageHistoryLengthPerChannel then
				startIndex = #messageLog - ChatSettings.MessageHistoryLengthPerChannel
			end

			for i = startIndex, #messageLog do
				channelObj:AddMessageToChannel(messageLog[i])
			end

			if getEchoMessagesInGeneral() and addHistoryToGeneralChannel then
				if ChatSettings.GeneralChannelName and channel ~= ChatSettings.GeneralChannelName then
					local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
					if generalChannel then
						generalChannel:AddMessagesToChannelByTimeStamp(messageLog, startIndex)
					end
				end
			end
		end

		if (welcomeMessage ~= "") then
			local welcomeMessageObject = {
				ID = -1,
				FromSpeaker = nil,
				SpeakerUserId = 0,
				OriginalChannel = channel,
				IsFiltered = true,
				MessageLength = string.len(welcomeMessage),
				MessageLengthUtf8 = utf8.len(utf8.nfcnormalize(welcomeMessage)),
				Message = trimTrailingSpaces(welcomeMessage),
				MessageType = ChatConstants.MessageTypeWelcome,
				Time = os.time(),
				ExtraData = nil,
			}
			channelObj:AddMessageToChannel(welcomeMessageObject)

			if getEchoMessagesInGeneral() and addWelcomeMessageToGeneralChannel and not ChatSettings.ShowChannelsBar then
				if channel ~= ChatSettings.GeneralChannelName then
					local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
					if generalChannel then
						generalChannel:AddMessageToChannel(welcomeMessageObject)
					end
				end
			end
		end

		DoFadeInFromNewInformation()
	end

end

EventFolder.OnChannelJoined.OnClientEvent:connect(function(channel, welcomeMessage, messageLog, channelNameColor)
	HandleChannelJoined(channel, welcomeMessage, messageLog, channelNameColor, false, true)
end)

EventFolder.OnChannelLeft.OnClientEvent:connect(function(channel)
	ChatWindow:RemoveChannel(channel)

	DoFadeInFromNewInformation()
end)

EventFolder.OnMuted.OnClientEvent:connect(function(channel)
	--// Do something eventually maybe?
	--// This used to take away the chat bar in channels the player was muted in.
	--// We found out this behavior was inconvenient for doing chat commands though.
end)

EventFolder.OnUnmuted.OnClientEvent:connect(function(channel)
	--// Same as above.
end)

EventFolder.OnMainChannelSet.OnClientEvent:connect(function(channel)
	DoSwitchCurrentChannel(channel)
end)

coroutine.wrap(function()
	-- ChannelNameColorUpdated may not exist if the client version is older than the server version.
	local ChannelNameColorUpdated = DefaultChatSystemChatEvents:WaitForChild("ChannelNameColorUpdated", 5)
	if ChannelNameColorUpdated then
		ChannelNameColorUpdated.OnClientEvent:connect(function(channelName, channelNameColor)
			ChatBar:SetChannelNameColor(channelName, channelNameColor)
		end)
	end
end)()

