Advertisement
Guest User

Untitled

a guest
Sep 15th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. -- This module holds any type of chatting functions
  2. CATEGORY_NAME = "Chat"
  3.  
  4. ------------------------------ Psay ------------------------------
  5. function ulx.psay( calling_ply, target_ply, message )
  6.     ulx.fancyLog( { target_ply, calling_ply }, "#P to #P: " .. message, calling_ply, target_ply )
  7. end
  8. local psay = ulx.command( CATEGORY_NAME, "ulx psay", ulx.psay, "!p", true )
  9. psay:addParam{ type=ULib.cmds.PlayerArg, target="!^", ULib.cmds.ignoreCanTarget }
  10. psay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  11. psay:defaultAccess( ULib.ACCESS_ALL )
  12. psay:help( "Send a private message to target." )
  13.  
  14. ------------------------------ Asay ------------------------------
  15. local seeasayAccess = "ulx seeasay"
  16. if SERVER then ULib.ucl.registerAccess( seeasayAccess, ULib.ACCESS_OPERATOR, "Ability to see 'ulx asay'", "Other" ) end -- Give operators access to see asays echoes by default
  17.  
  18. function ulx.asay( calling_ply, message )
  19.     local format
  20.     local me = "/me "
  21.     if message:sub( 1, me:len() ) == me then
  22.         format = "(ADMINS) *** #P #s"
  23.         message = message:sub( me:len() + 1 )
  24.     else
  25.         format = "Hey #P! Use /report instead "
  26.     end
  27.  
  28.     local players = player.GetAll()
  29.     for i=#players, 1, -1 do
  30.         local v = players[ i ]
  31.         if not ULib.ucl.query( v, seeasayAccess ) and v ~= calling_ply then -- Calling player always gets to see the echo
  32.             table.remove( players, i )
  33.         end
  34.     end
  35.  
  36.     ulx.fancyLog( players, format, calling_ply, message )
  37. end
  38. local asay = ulx.command( CATEGORY_NAME, "ulx asay", ulx.asay, "@", true, true )
  39. asay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  40. asay:defaultAccess( ULib.ACCESS_ALL )
  41. asay:help( "Send a message to currently connected admins." )
  42.  
  43. ------------------------------ Tsay ------------------------------
  44. function ulx.tsay( calling_ply, message )
  45.     ULib.tsay( _, message )
  46.  
  47.     if util.tobool( GetConVarNumber( "ulx_logChat" ) ) then
  48.         ulx.logString( string.format( "(tsay from %s) %s", calling_ply:IsValid() and calling_ply:Nick() or "Console", message ) )
  49.     end
  50. end
  51. local tsay = ulx.command( CATEGORY_NAME, "ulx tsay", ulx.tsay, "@@", true, true )
  52. tsay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  53. tsay:defaultAccess( ULib.ACCESS_ADMIN )
  54. tsay:help( "Send a message to everyone in the chat box." )
  55.  
  56. ------------------------------ Csay ------------------------------
  57. function ulx.csay( calling_ply, message )
  58.     ULib.csay( _, message )
  59.  
  60.     if util.tobool( GetConVarNumber( "ulx_logChat" ) ) then
  61.         ulx.logString( string.format( "(csay from %s) %s", calling_ply:IsValid() and calling_ply:Nick() or "Console", message ) )
  62.     end
  63. end
  64. local csay = ulx.command( CATEGORY_NAME, "ulx csay", ulx.csay, "@@@", true, true )
  65. csay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  66. csay:defaultAccess( ULib.ACCESS_ADMIN )
  67. csay:help( "Send a message to everyone in the middle of their screen." )
  68.  
  69. ------------------------------ Thetime ------------------------------
  70. local waittime = 60
  71. local lasttimeusage = -waittime
  72. function ulx.thetime( calling_ply )
  73.     if lasttimeusage + waittime > CurTime() then
  74.         ULib.tsayError( calling_ply, "I just told you what time it is! Please wait " .. waittime .. " seconds before using this command again", true )
  75.         return
  76.     end
  77.  
  78.     lasttimeusage = CurTime()
  79.     ulx.fancyLog( "The time is now #s.", os.date( "%I:%M %p") )
  80. end
  81. local thetime = ulx.command( CATEGORY_NAME, "ulx thetime", ulx.thetime, "!thetime" )
  82. thetime:defaultAccess( ULib.ACCESS_ALL )
  83. thetime:help( "Shows you the server time." )
  84.  
  85. local adverts = {}
  86. ulx.adverts = adverts -- For XGUI, too lazy to change all refs
  87.  
  88. ------------------------------ Adverts ------------------------------
  89. local function doAdvert( group, id )
  90.  
  91.     if adverts[ group ][ id ] == nil then
  92.         if adverts[ group ].removed_last then
  93.             adverts[ group ].removed_last = nil
  94.             id = 1
  95.         else
  96.             id = #adverts[ group ]
  97.         end
  98.     end
  99.  
  100.     local info = adverts[ group ][ id ]
  101.  
  102.     local message = string.gsub( info.message, "%%curmap%%", game.GetMap() )
  103.     message = string.gsub( message, "%%host%%", GetConVarString( "hostname" ) )
  104.     message = string.gsub( message, "%%ulx_version%%", ulx.getVersion() )
  105.  
  106.     if not info.len then -- tsay
  107.         local lines = ULib.explode( "\\n", message )
  108.  
  109.         for i, line in ipairs( lines ) do
  110.             local trimmed = line:Trim()
  111.             if trimmed:len() > 0 then
  112.                 ULib.tsayColor( _, true, info.color, trimmed ) -- Delaying runs one message every frame (to ensure correct order)
  113.             end
  114.         end
  115.     else
  116.         ULib.csay( _, message, info.color, info.len )
  117.     end
  118.  
  119.     ULib.queueFunctionCall( function()
  120.         local nextid = math.fmod( id, #adverts[ group ] ) + 1
  121.         timer.Remove( "ULXAdvert" .. type( group ) .. group )
  122.         timer.Create( "ULXAdvert" .. type( group ) .. group, adverts[ group ][ nextid ].rpt, 1, function() doAdvert( group, nextid ) end )
  123.     end )
  124. end
  125.  
  126. -- Whether or not it's a csay is determined by whether there's a value specified in "len"
  127. function ulx.addAdvert( message, rpt, group, color, len )
  128.     local t
  129.  
  130.     if group then
  131.         t = adverts[ tostring( group ) ]
  132.         if not t then
  133.             t = {}
  134.             adverts[ tostring( group ) ] = t
  135.         end
  136.     else
  137.         group = table.insert( adverts, {} )
  138.         t = adverts[ group ]
  139.     end
  140.  
  141.     local id = table.insert( t, { message=message, rpt=rpt, color=color, len=len } )
  142.  
  143.     if not timer.Exists( "ULXAdvert" .. type( group ) .. group ) then
  144.         timer.Create( "ULXAdvert" .. type( group ) .. group, rpt, 1, function() doAdvert( group, id ) end )
  145.     end
  146. end
  147.  
  148. ------------------------------ Gimp ------------------------------
  149. local gimpSays = {} -- Holds gimp says
  150. ulx.gimpSays = gimpSays -- For XGUI, too lazy to change all refs
  151. local ID_GIMP = 1
  152. local ID_MUTE = 2
  153.  
  154. function ulx.addGimpSay( say )
  155.     table.insert( gimpSays, say )
  156. end
  157.  
  158. function ulx.clearGimpSays()
  159.     table.Empty( gimpSays )
  160. end
  161.  
  162. function ulx.gimp( calling_ply, target_plys, should_ungimp )
  163.     for i=1, #target_plys do
  164.         local v = target_plys[ i ]
  165.         if should_ungimp then
  166.             v.gimp = nil
  167.         else
  168.             v.gimp = ID_GIMP
  169.         end
  170.     end
  171.  
  172.     if not should_ungimp then
  173.         ulx.fancyLogAdmin( calling_ply, "#A gimped #T", target_plys )
  174.     else
  175.         ulx.fancyLogAdmin( calling_ply, "#A ungimped #T", target_plys )
  176.     end
  177. end
  178. local gimp = ulx.command( CATEGORY_NAME, "ulx gimp", ulx.gimp, "!gimp" )
  179. gimp:addParam{ type=ULib.cmds.PlayersArg }
  180. gimp:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  181. gimp:defaultAccess( ULib.ACCESS_ADMIN )
  182. gimp:help( "Gimps target(s) so they are unable to chat normally." )
  183. gimp:setOpposite( "ulx ungimp", {_, _, true}, "!ungimp" )
  184.  
  185. ------------------------------ Mute ------------------------------
  186. function ulx.mute( calling_ply, target_plys, should_unmute )
  187.     for i=1, #target_plys do
  188.         local v = target_plys[ i ]
  189.         if should_unmute then
  190.             v.gimp = nil
  191.         else
  192.             v.gimp = ID_MUTE
  193.         end
  194.     end
  195.  
  196.     if not should_unmute then
  197.         ulx.fancyLogAdmin( calling_ply, "#A muted #T", target_plys )
  198.     else
  199.         ulx.fancyLogAdmin( calling_ply, "#A unmuted #T", target_plys )
  200.     end
  201. end
  202. local mute = ulx.command( CATEGORY_NAME, "ulx mute", ulx.mute, "!mute" )
  203. mute:addParam{ type=ULib.cmds.PlayersArg }
  204. mute:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  205. mute:defaultAccess( ULib.ACCESS_ADMIN )
  206. mute:help( "Mutes target(s) so they are unable to chat." )
  207. mute:setOpposite( "ulx unmute", {_, _, true}, "!unmute" )
  208.  
  209. local function gimpCheck( ply, strText )
  210.     if ply.gimp == ID_MUTE then return "" end
  211.     if ply.gimp == ID_GIMP then
  212.         if #gimpSays < 1 then return nil end
  213.         return gimpSays[ math.random( #gimpSays ) ]
  214.     end
  215. end
  216. hook.Add( "PlayerSay", "ULXGimpCheck", gimpCheck, 18 ) -- Very low priority
  217.  
  218. ------------------------------ Gag ------------------------------
  219. function ulx.gag( calling_ply, target_plys, should_ungag )
  220.     local players = player.GetAll()
  221.     for i=1, #target_plys do
  222.         local v = target_plys[ i ]
  223.         v.ulx_gagged = not should_ungag
  224.     end
  225.  
  226.     if not should_ungag then
  227.         ulx.fancyLogAdmin( calling_ply, "#A gagged #T", target_plys )
  228.     else
  229.         ulx.fancyLogAdmin( calling_ply, "#A ungagged #T", target_plys )
  230.     end
  231. end
  232. local gag = ulx.command( CATEGORY_NAME, "ulx gag", ulx.gag, "!gag" )
  233. gag:addParam{ type=ULib.cmds.PlayersArg }
  234. gag:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  235. gag:defaultAccess( ULib.ACCESS_ADMIN )
  236. gag:help( "Gag target(s), disables microphone." )
  237. gag:setOpposite( "ulx ungag", {_, _, true}, "!ungag" )
  238.  
  239. local function gagHook( listener, talker )
  240.     if talker.ulx_gagged then
  241.         return false
  242.     end
  243. end
  244. hook.Add( "PlayerCanHearPlayersVoice", "ULXGag", gagHook )
  245.  
  246. -- Anti-spam stuff
  247. local chattime_cvar
  248. if SERVER then chattime_cvar = ulx.convar( "chattime", "1.5", "<time> - Players can only chat every x seconds (anti-spam). 0 to disable.", ULib.ACCESS_ADMIN ) end
  249. local function playerSay( ply )
  250.     if not ply.lastChatTime then ply.lastChatTime = 0 end
  251.  
  252.     local chattime = chattime_cvar:GetFloat()
  253.     if chattime <= 0 then return end
  254.  
  255.     if ply.lastChatTime + chattime > CurTime() then
  256.         return ""
  257.     else
  258.         ply.lastChatTime = CurTime()
  259.         return
  260.     end
  261. end
  262. hook.Add( "PlayerSay", "ulxPlayerSay", playerSay, 17 )
  263.  
  264. local function meCheck( ply, strText, bTeam )
  265.     if ply.gimp or not ULib.isSandbox() then return end -- Don't mess
  266.  
  267.     if strText:sub( 1, 4 ) == "/me " then
  268.         strText = string.format( "*** %s %s", ply:Nick(), strText:sub( 5 ) )
  269.         if not bTeam then
  270.             ULib.tsay( _, strText )
  271.         else
  272.             strText = "(TEAM) " .. strText
  273.             local teamid = ply:Team()
  274.             local players = team.GetPlayers( teamid )
  275.             for _, ply2 in ipairs( players ) do
  276.                 ULib.tsay( ply2, strText )
  277.             end
  278.         end
  279.  
  280.         if game.IsDedicated() then
  281.             Msg( strText .. "\n" ) -- Log to console
  282.         end
  283.         if util.tobool( GetConVarNumber( "ulx_logChat" ) ) then
  284.             ulx.logString( strText )
  285.         end
  286.  
  287.         return ""
  288.     end
  289. end
  290. hook.Add( "PlayerSay", "ULXMeCheck", meCheck, 18 ) -- Extremely low priority
  291.  
  292. local function showWelcome( ply )
  293.     local message = GetConVarString( "ulx_welcomemessage" )
  294.     if not message or message == "" then return end
  295.  
  296.     message = string.gsub( message, "%%curmap%%", game.GetMap() )
  297.     message = string.gsub( message, "%%host%%", GetConVarString( "hostname" ) )
  298.     message = string.gsub( message, "%%ulx_version%%", ulx.getVersion() )
  299.  
  300.     ply:ChatPrint( message ) -- We're not using tsay because ULib might not be loaded yet. (client side)
  301. end
  302. hook.Add( "PlayerInitialSpawn", "ULXWelcome", showWelcome )
  303. if SERVER then ulx.convar( "welcomemessage", "", "<msg> - This is shown to players on join.", ULib.ACCESS_ADMIN ) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement