function chatBarFocusLost(g,o)DoBackgroundFadeIn()chatBarFocusChanged:Fire(false)if g then local g=(ChatBar:GetTextBox()).Text if ChatBar:IsInCustomState()then local o=ChatBar:GetCustomMessage()if o then g=o end local B=ChatBar:CustomStateProcessCompletedMessage(g)ChatBar:ResetCustomState()if B then return end end;(ChatBar:GetTextBox()).Text=""if g~=""then moduleApiTable.MessagePosted:fire(g)if not CommandProcessor:ProcessCompletedChatMessage(g,ChatWindow)then if ChatSettings.DisallowedWhiteSpace then for o=1,#ChatSettings.DisallowedWhiteSpace,1 do if ChatSettings.DisallowedWhiteSpace[o]=="\t"then g=string.gsub(g,ChatSettings.DisallowedWhiteSpace[o]," ")else g=string.gsub(g,ChatSettings.DisallowedWhiteSpace[o],"")end end end g=string.gsub(g,"\n","")g=string.gsub(g,"\091 \093\043"," ")local o=ChatWindow:GetTargetMessageChannel()if o then MessageSender:SendMessage(g,o)else MessageSender:SendMessage(g,nil)end end end end end local g={}function setupChatBarConnections()for o=1,#g,1 do g[o]:Disconnect()end g={}local o=(ChatBar:GetTextBox()).FocusLost:connect(chatBarFocusLost)table.insert(g,o)local B=(ChatBar:GetTextBox()).Focused:connect(chatBarFocused)table.insert(g,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(g)if not ChatSettings.ShowUserOwnFilteredMessage then if g.FromSpeaker==LocalPlayer.Name then return end end local o=g.OriginalChannel local B=ChatWindow:GetChannel(o)if B then B:UpdateMessageFiltered(g)end if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and o~=ChatSettings.GeneralChannelName)then local o=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if o then o:UpdateMessageFiltered(g)end end end)EventFolder.OnNewMessage.OnClientEvent:connect(function(g,o)local B=ChatWindow:GetChannel(o)if B then B:AddMessageToChannel(g)if g.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(g)end end moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()end end)EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(g,o)o=o or"\083\121\115\116\101\109"local B=ChatWindow:GetChannel(o)if B then B:AddMessageToChannel(g)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(g)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(g,o,B,u,W,I)if ChatWindow:GetChannel(g)then ChatWindow:RemoveChannel(g)end if g==ChatSettings.GeneralChannelName then DidFirstChannelsLoads=true end if u then ChatBar:SetChannelNameColor(g,u)end local T=ChatWindow:AddChannel(g)if T then if g==ChatSettings.GeneralChannelName then DoSwitchCurrentChannel(g)end if B then local o=1 if#B>ChatSettings.MessageHistoryLengthPerChannel then o=#B-ChatSettings.MessageHistoryLengthPerChannel end for g=o,#B,1 do T:AddMessageToChannel(B[g])end if getEchoMessagesInGeneral()and W then if ChatSettings.GeneralChannelName and g~=ChatSettings.GeneralChannelName then local g=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if g then g:AddMessagesToChannelByTimeStamp(B,o)end end end end if o~=""then local B={ID=-1,FromSpeaker=nil;SpeakerUserId=0;OriginalChannel=g;IsFiltered=true;MessageLength=string.len(o),Message=trimTrailingSpaces(o),MessageType=ChatConstants.MessageTypeWelcome,Time=os.time(),ExtraData=nil}T:AddMessageToChannel(B)if getEchoMessagesInGeneral()and(I and not ChatSettings.ShowChannelsBar)then if g~=ChatSettings.GeneralChannelName then local g=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if g then g:AddMessageToChannel(B)end end end end DoFadeInFromNewInformation()end end EventFolder.OnChannelJoined.OnClientEvent:connect(function(g,o,B,u)HandleChannelJoined(g,o,B,u,false,true)end)EventFolder.OnChannelLeft.OnClientEvent:connect(function(g)ChatWindow:RemoveChannel(g)DoFadeInFromNewInformation()end)EventFolder.OnMuted.OnClientEvent:connect(function(g) end)EventFolder.OnUnmuted.OnClientEvent:connect(function(g) end)EventFolder.OnMainChannelSet.OnClientEvent:connect(function(g)DoSwitchCurrentChannel(g)end);(coroutine.wrap(function()local g=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 g then g.OnClientEvent:connect(function(g,o)ChatBar:SetChannelNameColor(g,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),
				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)()

