Skip to content

Improved chat + Fix #460 (Chat messages sent twice) #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e2d6a80
Update playercolors.lua
Nico8340 Jan 16, 2024
ec9465e
Revert "Update playercolors.lua"
Nico8340 Jan 16, 2024
6f573a5
Add check for freeroam's state
Nico8340 Jan 16, 2024
add32d5
Revert "Add check for freeroam's state"
Nico8340 Jan 16, 2024
c2db63f
Ignore playercolors if freeroam is running
Nico8340 Jan 16, 2024
6a079e8
Revert "Ignore playercolors if freeroam is running"
Nico8340 Jan 16, 2024
e7f0756
Remove onPlayerChat handler from playercolors
Nico8340 Jan 16, 2024
56732e7
Remove onPlayerChat handler from assault
Nico8340 Jan 16, 2024
6e6251c
Remove onPlayerChat handler from ctv
Nico8340 Jan 16, 2024
3b15015
Remove onPlayerChat handler from tdma
Nico8340 Jan 16, 2024
431f699
Remove onPlayerChat handler from freeroam
Nico8340 Jan 16, 2024
1772c02
Remove uncomplete implementation from admin2
Nico8340 Jan 16, 2024
ef04d24
Add logic and support for admin
Nico8340 Jan 17, 2024
6fccd20
Add logic and support for admin2
Nico8340 Jan 17, 2024
9611228
Resolve conflicts with meta.xml
Nico8340 Jan 17, 2024
a1c637c
Merge branch 'master' into master
Nico8340 Jan 17, 2024
acc54f2
Fix lint #343
Nico8340 Jan 17, 2024
45ffa9d
Using get instead of unique xml
Nico8340 Jan 18, 2024
a8a43e3
Remove unintended check
Nico8340 Jan 18, 2024
92fd10e
Use named function for onPlayerChat
Nico8340 Jan 18, 2024
9356468
Remove unintended files and exports
Nico8340 Jan 18, 2024
8385863
Add option to enable / disable chat handler
Nico8340 Jan 18, 2024
8354207
Merge branch 'master' into master
Nico8340 Jan 18, 2024
672ef45
Fix chat history if 'isChatEnabled' is not true
Nico8340 Jan 27, 2024
9ce919d
Merge branch 'master' into master
Nico8340 Jan 27, 2024
4d24235
Remove unused config
Nico8340 Jan 27, 2024
f051b8a
Merge branch 'master' of https://github.com/Nico8340/mtasa-resources
Nico8340 Jan 27, 2024
7df0605
Fix chat history if 'isChatEnabled' is not true (admin2)
Nico8340 Jan 27, 2024
38c7d4a
Revert 'Remove unused config'
Nico8340 Jan 27, 2024
e77d4a0
Fix chat history logic
Nico8340 Jan 27, 2024
51cdd42
Fix chat history logic (new solution)
Nico8340 Jan 27, 2024
74c33da
Add message filter
Nico8340 Jan 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions [admin]/admin/conf/chat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<chat>
<chatconfig name="antiSpamEnabled" value="true" /> <!-- Is Anti-Spam protection enabled? (true or false) (default: true) -->
<chatconfig name="antiSpamTimeout" value="3" /> <!-- In case of spam, how long do you want to mute the player? (sec) (default: 3) (disable: 0) -->
<chatconfig name="antiSpamDelay" value="1" /> <!-- How much time should elapse between two messages to not be considered spam? (ms) (default: 1) -->
<chatconfig name="antiSpamRepeat" value="true" /> <!-- Do you want to mute the player even if he repeats himself several times in a row? (true or false) (default: true) -->

<chatconfig name="isPlayercolorsEnabled" value="true" /> <!-- Do you want to use the playercolors resource for coloring the player's name in the chat? (true or false) (default: true) -->
<chatconfig name="isCustomcolorsEnabled" value="false" /> <!-- Do you want to enable custom hex color codes in the chat? (true or false) (default: false) -->
</chat>
2 changes: 2 additions & 0 deletions [admin]/admin/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<script src="server/admin_server.lua" />
<script src="server/admin_sync.lua" />
<script src="server/admin_commands.lua" />
<script src="server/admin_chat.lua" />
<script src="server/admin_servermaps.lua" />
<script src="server/admin_ACL.lua" />
<script src="server/admin_settings.lua" />
Expand Down Expand Up @@ -58,6 +59,7 @@
<config src="conf/stats.xml" type="client" />
<config src="conf/messages.xml" />
<config src="conf/commands.xml" />
<config src="conf/chat.xml" />
<config src="conf/web.xml" />
<config src="conf/ACL.xml" />

Expand Down
158 changes: 158 additions & 0 deletions [admin]/admin/server/admin_chat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
--[[**********************************
*
* Multi Theft Auto - Admin Panel
*
* admin_chat.lua
*
* Original File by Nico834
*
**************************************]]

local chatConfig = {}
local chatMessages = {}
local chatTimeouts = {}

addEventHandler("onResourceStart", resourceRoot,
function()
local configFile = getResourceConfig("conf/chat.xml")
local configData = xmlNodeGetChildren(configFile)

for k, v in pairs(configData) do
local configName = xmlNodeGetAttribute(v, "name")
local configValue = xmlNodeGetAttribute(v, "value")

chatConfig[configName] = autoType(configValue) or false
end

for k, v in pairs(getElementsByType("player")) do
chatMessages[v] = {}
end
end
)

addEventHandler("onPlayerJoin", root,
function()
chatMessages[source] = {}
end
)

addEventHandler("onPlayerQuit", root,
function()
chatMessages[source] = nil
chatTimeouts[source] = nil
end
)

addEventHandler("onPlayerChat", root,
function(messageContent, messageType)
if messageType ~= 0 then
return
end

cancelEvent()

if utf8.len(messageContent) < 1 then
outputChatBox("You can't send an empty message.", source, 255, 0, 0)
return
end

if chatConfig.antiSpamEnabled then
if chatConfig.antiSpamTimeout > 0 then
if chatTimeouts[source] then
if chatTimeouts[source] - getRealTime().timestamp <= 0 then
chatTimeouts[source] = nil
else
outputChatBox("You need to wait " .. math.ceil(chatTimeouts[source] - getRealTime().timestamp) .. " seconds before sending an another message.", source, 255, 0, 0)
return
end
end
end

local lastMessage = getMessageLast(source)

if chatConfig.antiSpamRepeat then
if lastMessage and type(lastMessage) == "table" then
local lastMessageContent = lastMessage[1]

if lastMessageContent then
local contentStart, contentStartIndex = string.find(lastMessageContent, "#ffffff")
local contentTrim = string.sub(lastMessageContent, contentStartIndex + 1)

if contentTrim == messageContent then
outputChatBox("Stop repeating yourself!", source, 255, 0, 0)
return
end
end
end
end

if lastMessage and type(lastMessage) == "table" then
local lastMessageTime = lastMessage[2]

if lastMessageTime then
if math.abs(getRealTime().timestamp - lastMessageTime) < chatConfig.antiSpamDelay then
if chatConfig.antiSpamTimeout > 0 then
outputChatBox("Your message has been marked as spam. You need to wait " .. chatConfig.antiSpamTimeout .. " seconds before sending an another message.", source, 255, 0, 0)
chatTimeouts[source] = getRealTime().timestamp + chatConfig.antiSpamTimeout
else
outputChatBox("Your message has been marked as spam.", source, 255, 0, 0)
end

return
end
end
end
end

local playerName = getPlayerName(source)
local playerNameColor = {197, 232, 242}

if not chatConfig.isCustomcolorsEnabled then
messageContent = messageContent:gsub("#%x%x%x%x%x%x", "")
end

if chatConfig.isPlayercolorsEnabled then
playerNameColor = {getPlayerNametagColor(source)}
playerNameColor = string.format("#%02X%02X%02X", playerNameColor[1], playerNameColor[2], playerNameColor[3])
else
playerNameColor = string.format("#%02X%02X%02X", playerNameColor[1], playerNameColor[2], playerNameColor[3])
end

messageContent = string.format("%s%s: #ffffff%s", playerNameColor, playerName, messageContent)
messageContent = messageContent:gsub("%s+", " ")

table.insert(chatMessages[source], {messageContent, getRealTime().timestamp})
outputChatBox(messageContent, root, 255, 255, 255, true)
outputServerLog(messageContent:gsub("#%x%x%x%x%x%x", ""))
end
)

function autoType(inputString)
if tonumber(inputString) then
return tonumber(inputString)
elseif inputString == "true" then
return true
else
return false
end
end

function getMessageLog(playerElement)
if playerElement and isElement(playerElement) then
if chatMessages[playerElement] then
return chatMessages[playerElement]
else
return false
end
end
end

function getMessageLast(playerElement)
if playerElement and isElement(playerElement) then
if chatMessages[playerElement] then
return chatMessages[playerElement][#chatMessages[playerElement]]
else
return false
end
end
end
49 changes: 26 additions & 23 deletions [admin]/admin/server/admin_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ aWeathers = {}
aNickChangeTime = {}

local aUnmuteTimerList = {}
local chatHistory = {}

function notifyPlayerLoggedIn(player)
outputChatBox ( "Press 'p' to open your admin panel", player )
Expand Down Expand Up @@ -460,13 +459,11 @@ function aPlayerInitialize ( player )
aPlayers[player] = {}
aPlayers[player]["money"] = getPlayerMoney ( player )
updatePlayerCountry ( player )
chatHistory[player] = {}
end

addEventHandler ( "onPlayerQuit", root, function ()
aPlayers[source] = nil
aNickChangeTime[source] = nil
chatHistory[source] = nil
end )

addEvent ( "aPlayerVersion", true )
Expand Down Expand Up @@ -1452,30 +1449,36 @@ addEventHandler ( "aServer", root, function ( action, data, data2 )
return false
end )

addEventHandler ( "onPlayerChat", root, function ( message )
local size = #chatHistory[source]
if ( size == g_Prefs.maxchatmsgs ) then
table.remove( chatHistory[source], 1 )
size = size - 1
end
chatHistory[source][size + 1] = message
end )
function getPlayerChatHistory(playerElement, historyCount)
if playerElement and isElement(playerElement) then
local chatLog = getMessageLog(playerElement)
local chatLogText = {}

if chatLog and type(chatLog) == "table" then
if not historyCount then
historyCount = #chatLog
end

function getPlayerChatHistory ( player, chunk )
if ( player and isElement ( player ) ) then
local size = #chatHistory[player]
chunk = tonumber(chunk)
if ( chunk and chunk < size ) then
size = chunk
if #chatLog < historyCount then
historyCount = #chatLog
end

for i = 1, historyCount do
local contentStart, contentStartIndex = string.find(chatLog[i][1], "#ffffff")
local contentTrim = string.sub(chatLog[i][1], contentStartIndex + 1)

chatLogText[i] = contentTrim
end
end
local text = ""
for i=1, size do
text = text .. chatHistory[player][i] .. "\n"

if chatLogText and #chatLogText >= 1 then
chatLogText = table.concat(chatLogText, "\n")
end
return text
else
return false

return chatLogText
end

return false
end

addEvent ( "aMessage", true )
Expand Down
9 changes: 9 additions & 0 deletions [admin]/admin2/conf/chat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<chat>
<chatconfig name="antiSpamEnabled" value="true" /> <!-- Is Anti-Spam protection enabled? (true or false) (default: true) -->
<chatconfig name="antiSpamTimeout" value="3" /> <!-- In case of spam, how long do you want to mute the player? (sec) (default: 3) (disable: 0) -->
<chatconfig name="antiSpamDelay" value="1" /> <!-- How much time should elapse between two messages to not be considered spam? (ms) (default: 1) -->
<chatconfig name="antiSpamRepeat" value="true" /> <!-- Do you want to mute the player even if he repeats himself several times in a row? (true or false) (default: true) -->

<chatconfig name="isPlayercolorsEnabled" value="true" /> <!-- Do you want to use the playercolors resource for coloring the player's name in the chat? (true or false) (default: true) -->
<chatconfig name="isCustomcolorsEnabled" value="false" /> <!-- Do you want to enable custom hex color codes in the chat? (true or false) (default: false) -->
</chat>
16 changes: 12 additions & 4 deletions [admin]/admin2/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

<!--
Admin System Meta File.

WARNING: We give no support for edited
versions of the admin system.
-->
Expand All @@ -17,7 +16,7 @@

<script src="admin_definitions.lua" type="shared" cache="false"/>
<script src="admin_coroutines.lua" type="shared" cache="false"/>
<script src="server/admin_wrapper.lua" type="server"/>
<script src="server/admin_session.lua" type="server"/>
<script src="server/admin_proxy.lua" type="server"/>
Expand All @@ -26,13 +25,14 @@
<script src="server/admin_database.lua" type="server"/>
<script src="server/admin_storage.lua" type="server"/>
<script src="server/admin_sync.lua" type="server"/>
<script src="server/admin_chat.lua" type="server" />
<script src="server/admin_commands.lua" type="server"/>
<script src="server/admin_ACL.lua" type="server"/>
<script src="server/admin_bans.lua" type="server"/>
<script src="server/admin_network.lua" type="server"/>
<script src="server/admin_messagebox.lua" type="server"/>
<script src="server/admin_screenshot.lua" type="server"/>

<script src="client/admin_wrapper.lua" type="client" cache="false"/>
<script src="client/admin_gui.lua" type="client" cache="false"/>
<script src="client/admin_settings.lua" type="client" cache="false"/>
Expand Down Expand Up @@ -67,6 +67,13 @@
<script src="client/widgets/admin_warp.lua" type="client" cache="false"/>
<script src="client/widgets/admin_report.lua" type="client" cache="false"/>
<script src="client/widgets/admin_weapon.lua" type="client" cache="false"/>

<script src="shared/utils.lua" type="shared" cache="false"/>

<html src="http/screenshots.htm"/>
<html src="http/screenshot.htm"/>

<export function="dataExchange" http="true"/>

<script src="shared/utils.lua" type="shared" cache="false"/>

Expand All @@ -85,6 +92,7 @@
<config src="conf/stats.xml" type="client"/>
<config src="conf/vehicles.xml" type="client"/>
<config src="conf/messages.xml" type="server"/>
<config src="conf/chat.xml" type="server"/>
<config src="conf/commands.xml" type="server"/>
<config src="conf/ACL.xml" type="server"/>

Expand All @@ -102,7 +110,7 @@
<file src="client/images/palette.png"/>
<file src="client/images/black.png"/>
<file src="client/images/blue.png"/>
<settings>
<setting name="#pingkicker" value="0" group="Automatic scripts" friendlyname="Ping kicker" accept="0-1000" desc="Max ping allowed (0 - off)"/>
<setting name="#fpskicker" value="0" group="Automatic scripts" friendlyname="FPS kicker" accept="0-100" desc="Lowest fps allowed: 25-75 (0 - off)"/>
Expand Down
Loading