Python1320

omghax

Jul 15th, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. -- New chatbox for da server
  2. local Tag="chatbox"
  3. module(Tag,package.seeall)
  4.  
  5.  
  6. ------------------------------------
  7. -- Transportation
  8. ------------------------------------
  9.  
  10. function ValidMessage( msg )
  11.     return (msg and type(msg)=="string" and msg:len()>0)
  12. end
  13.  
  14. local Cmd_SayGlon="gsay"
  15.  
  16. function UserIDToPly( plyid )
  17.     for _,pl in pairs(player.GetAll()) do
  18.         if pl:UserID()==plyid then
  19.             return pl
  20.         end
  21.     end
  22.     return false
  23. end
  24.  
  25. if SERVER then
  26.    
  27.     -- Spamming
  28.    
  29.     spam_messages=3
  30.     spam_message_expirytime=4
  31.    
  32.     SpamTbl={} setmetatable(SpamTbl, { __mode = 'v' })
  33.    
  34.     function DecreaseSpam( ply )
  35.         local count = SpamTbl[ ply ]
  36.         if count then
  37.        
  38.             SpamTbl[ ply ] = count - 1
  39.            
  40.             -- Never happens. Should not..
  41.             if count < 1 then error("Ply",ply,"spam count impossible??") end
  42.            
  43.         end
  44.     end
  45.    
  46.     function SpamWatch( ply )
  47.         local count = SpamTbl[ ply ] or 0
  48.         count = count + 1
  49.        
  50.        
  51.         if count > spam_messages then
  52.             ply:ChatPrint("Slow down a bit with that typing...")
  53.             return true
  54.         end
  55.         SpamTbl[ ply ] = count
  56.        
  57.         timer.Simple(spam_message_expirytime,DecreaseSpam,ply)
  58.        
  59.         return false
  60.     end
  61.    
  62.     -- Sending
  63.     function SendChatMessage( ply, msg )
  64.        
  65.         local data={
  66.             [1]=ply:UserID(),
  67.             [2]=msg
  68.         }
  69.         -- Usermessage option for speed?
  70.  
  71.         datastream.StreamToClients(player.GetAll(), Tag, data)
  72.        
  73.     end
  74.    
  75.     -- Receiving
  76.     function ReceiveChatMessage( ply, msg )
  77.         -- Receives raw data. Check if any of it is valid.
  78.         if not ValidMessage ( msg ) then return false end
  79.        
  80.         -- Anti spam
  81.         if SpamWatch( ply ) then
  82.         --  ErrorNoHalt("Spam detected from ",ply:Name().." Msg='".. msg:sub(0,200).."'\n")
  83.         --  --return false
  84.         end
  85.        
  86.         local teamchat = false -- This chatbox is for sandbox
  87.        
  88.         local result = hook.Call("PlayerSay", GAMEMODE, ply, msg, not teamchat --[[inverted for some reason]])
  89.         if result == true then -- Kill the message
  90.             return result
  91.         elseif result == false then
  92.             -- Let the message pass
  93.         elseif type(result) == "string" then -- Replace the message
  94.             if not ValidMessage ( result ) then return result end
  95.             msg = result
  96.         end
  97.        
  98.         -- Print to Console
  99.         local print=_print or print -- EPOE workaround..
  100.         print(ply:Name()..": ".. msg)
  101.        
  102.         SendChatMessage( ply, msg )
  103.        
  104.     end
  105.    
  106.     -- Receiving facilities
  107.     datastream.Hook( Tag, function ( ply, _, _, _, data )
  108.         local msg = data
  109.         ReceiveChatMessage( ply, msg )
  110.     end )
  111.     hook.Add('AcceptStream',Tag,function( pl, handler, id )
  112.         if handler==Tag then return true end
  113.     end)
  114.    
  115.     concommand.Add(Cmd_SayGlon,function(ply,_,args)
  116.         local msg = glon.decode( args[1] )
  117.         ReceiveChatMessage( ply, msg )     
  118.     end)
  119.    
  120.     return
  121.    
  122. elseif CLIENT then
  123.    
  124.     -- Sending
  125.    
  126.     function SendChatMessage(msg)
  127.         if not ValidMessage ( msg ) then return false end
  128.        
  129.         local data=msg
  130.         local encoded=glon.encode( data )
  131.         if encoded:len()<200 then
  132.             RunConsoleCommand("cmd",Cmd_SayGlon,encoded)
  133.         else
  134.             datastream.StreamToServer( Tag, data )
  135.         end
  136.     end
  137.    
  138.    
  139.     -- Receiving
  140.    
  141.     function ReceiveChatMessage( ply, msg )
  142.  
  143.         --if ply == LocalPlayer() then return end
  144.         local teamchat = false
  145.        
  146.         hook.Call( "OnPlayerChat", GAMEMODE, ply, msg, teamchat, !ply:Alive() )
  147.        
  148.     end
  149.    
  150.     function ReceivedData( data, retrycount )
  151.         local plyid = data[1]
  152.         local msg   = data[2]
  153.        
  154.         local ply = UserIDToPly( plyid )
  155.  
  156.         -- Players do not exist immediatelly. TODO: Wait for them to exist.
  157.         if not IsValid( ply ) or not ply:IsPlayer() then
  158.            
  159.             retrycount = retrycount or 0
  160.             --Msg("["..Tag.."] ")print("Retrying finding player...",plyid)
  161.             timer.Simple(0.5,function() -- delayed recurse
  162.                 ReceivedData( data, retrycount + 1 )
  163.             end)
  164.            
  165.             if retrycount > 20 then
  166.                 Msg("WARNING: Player ",plyid," sent message '",msg,"' but is now vanished!\n")
  167.             end
  168.            
  169.             return
  170.            
  171.         end
  172.        
  173.         ReceiveChatMessage(ply, msg)
  174.        
  175.     end
  176.    
  177.     -- Receive using datastream. Flexible, slow
  178.     datastream.Hook( Tag, function(_,_,_, decoded )
  179.         ReceivedData( decoded )
  180.     end )
  181.  
  182.    
  183. end
Advertisement
Add Comment
Please, Sign In to add comment