--[[
	MOD/lua/server.lua [#1932 (#2039), 2338826882]
	Ollie | STEAM_0:0:30709335 <85.23.158.207:27006> | [26.01.14 12:35:18PM]
	===BadFile===
]]

// Lua addons for chat!

if CLIENT then

	net.Receive("PC_ClientRunLua",function()
	
		local from = net.ReadEntity()
		local code = net.ReadString()
		
		if from:IsValid() and code then
		
			print("Running script from "..(from:Nick() or "Unknown").."!")
			
			trace = LocalPlayer():GetEyeTraceNoCursor()
			this = trace.Entity
			that = trace.Entity
			there = trace.HitPos
			hitnormal = trace.HitNormal
			here = LocalPlayer():GetPos()
			me = LocalPlayer()
			
			RunString(code)
		
		end
	
	end)

else

	AddCSLuaFile()
	
	util.AddNetworkString("PC_ClientLua")
	util.AddNetworkString("PC_ClientsLua")
	util.AddNetworkString("PC_ClientRunLua")
	util.AddNetworkString("PC_ServerLua")

	local meta = FindMetaTable("Player")
	function meta:HasLuaRights()
		return self:IsSuperAdmin()
	end
	
	net.Receive("PC_ClientLua",function(len,ply)

		if ply:IsValid() then
		
			if ply:HasLuaRights() != true then
				ply:ChatPrint("You have no rights to run lua!")
				return
			end
		
			local target = net.ReadEntity()
			local code = net.ReadString()
			
			if target:IsValid() and code then
			
				net.Start("PC_ClientRunLua")
					net.WriteEntity(ply)
					net.WriteString(code)
				net.Send(target)
			
			end
		
		end

	end)		
	
	net.Receive("PC_ClientsLua",function(len,ply)

		if ply:IsValid() then
		
			if ply:HasLuaRights() != true then
				ply:ChatPrint("You have no rights to run lua!")
				return
			end
		
			local code = net.ReadString()
			if code then
			
				net.Start("PC_ClientRunLua")
					net.WriteEntity(ply)
					net.WriteString(code)
				net.Send(player.GetAll())
			
			end
		
		end

	end)	
	
	net.Receive("PC_ServerLua",function(len,ply)

		if ply:IsValid() then
		
			if ply:HasLuaRights() != true then
				ply:ChatPrint("You have no rights to run lua!")
				return
			end
		
			local code = net.ReadString()
			
			print("Running serverside lua from "..ply:Nick().."!")
			RunString(code)
		
		end

	end)

end