Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- New chatbox for da server
- local Tag="chatbox"
- module(Tag,package.seeall)
- ------------------------------------
- -- Transportation
- ------------------------------------
- function ValidMessage( msg )
- return (msg and type(msg)=="string" and msg:len()>0)
- end
- local Cmd_SayGlon="gsay"
- function UserIDToPly( plyid )
- for _,pl in pairs(player.GetAll()) do
- if pl:UserID()==plyid then
- return pl
- end
- end
- return false
- end
- if SERVER then
- -- Spamming
- spam_messages=3
- spam_message_expirytime=4
- SpamTbl={} setmetatable(SpamTbl, { __mode = 'v' })
- function DecreaseSpam( ply )
- local count = SpamTbl[ ply ]
- if count then
- SpamTbl[ ply ] = count - 1
- -- Never happens. Should not..
- if count < 1 then error("Ply",ply,"spam count impossible??") end
- end
- end
- function SpamWatch( ply )
- local count = SpamTbl[ ply ] or 0
- count = count + 1
- if count > spam_messages then
- ply:ChatPrint("Slow down a bit with that typing...")
- return true
- end
- SpamTbl[ ply ] = count
- timer.Simple(spam_message_expirytime,DecreaseSpam,ply)
- return false
- end
- -- Sending
- function SendChatMessage( ply, msg )
- local data={
- [1]=ply:UserID(),
- [2]=msg
- }
- -- Usermessage option for speed?
- datastream.StreamToClients(player.GetAll(), Tag, data)
- end
- -- Receiving
- function ReceiveChatMessage( ply, msg )
- -- Receives raw data. Check if any of it is valid.
- if not ValidMessage ( msg ) then return false end
- -- Anti spam
- if SpamWatch( ply ) then
- -- ErrorNoHalt("Spam detected from ",ply:Name().." Msg='".. msg:sub(0,200).."'\n")
- -- --return false
- end
- local teamchat = false -- This chatbox is for sandbox
- local result = hook.Call("PlayerSay", GAMEMODE, ply, msg, not teamchat --[[inverted for some reason]])
- if result == true then -- Kill the message
- return result
- elseif result == false then
- -- Let the message pass
- elseif type(result) == "string" then -- Replace the message
- if not ValidMessage ( result ) then return result end
- msg = result
- end
- -- Print to Console
- local print=_print or print -- EPOE workaround..
- print(ply:Name()..": ".. msg)
- SendChatMessage( ply, msg )
- end
- -- Receiving facilities
- datastream.Hook( Tag, function ( ply, _, _, _, data )
- local msg = data
- ReceiveChatMessage( ply, msg )
- end )
- hook.Add('AcceptStream',Tag,function( pl, handler, id )
- if handler==Tag then return true end
- end)
- concommand.Add(Cmd_SayGlon,function(ply,_,args)
- local msg = glon.decode( args[1] )
- ReceiveChatMessage( ply, msg )
- end)
- return
- elseif CLIENT then
- -- Sending
- function SendChatMessage(msg)
- if not ValidMessage ( msg ) then return false end
- local data=msg
- local encoded=glon.encode( data )
- if encoded:len()<200 then
- RunConsoleCommand("cmd",Cmd_SayGlon,encoded)
- else
- datastream.StreamToServer( Tag, data )
- end
- end
- -- Receiving
- function ReceiveChatMessage( ply, msg )
- --if ply == LocalPlayer() then return end
- local teamchat = false
- hook.Call( "OnPlayerChat", GAMEMODE, ply, msg, teamchat, !ply:Alive() )
- end
- function ReceivedData( data, retrycount )
- local plyid = data[1]
- local msg = data[2]
- local ply = UserIDToPly( plyid )
- -- Players do not exist immediatelly. TODO: Wait for them to exist.
- if not IsValid( ply ) or not ply:IsPlayer() then
- retrycount = retrycount or 0
- --Msg("["..Tag.."] ")print("Retrying finding player...",plyid)
- timer.Simple(0.5,function() -- delayed recurse
- ReceivedData( data, retrycount + 1 )
- end)
- if retrycount > 20 then
- Msg("WARNING: Player ",plyid," sent message '",msg,"' but is now vanished!\n")
- end
- return
- end
- ReceiveChatMessage(ply, msg)
- end
- -- Receive using datastream. Flexible, slow
- datastream.Hook( Tag, function(_,_,_, decoded )
- ReceivedData( decoded )
- end )
- end
Advertisement
Add Comment
Please, Sign In to add comment