Advertisement
Guest User

ulxtempadduser

a guest
Jul 31st, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. local CATEGORY_NAME = "User Management"
  2.  
  3.  
  4.  
  5. if !file.Exists( "ulx", "DATA" ) then
  6.  
  7. file.CreateDir( "ulx" )
  8.  
  9. end
  10.  
  11.  
  12.  
  13. if !file.Exists( "ulx/tempuserdata", "DATA" ) then
  14.  
  15. file.CreateDir( "ulx/tempuserdata" )
  16.  
  17. end
  18.  
  19.  
  20.  
  21. //--Code courtesy of Stickly Man.
  22.  
  23. ulx.tempuser_group_names = {}
  24.  
  25. local function updateNames()
  26.  
  27. table.Empty( ulx.tempuser_group_names ) -- Don't reassign so we don't lose our refs
  28.  
  29.  
  30.  
  31. for group_name, _ in pairs( ULib.ucl.groups ) do
  32.  
  33. table.insert( ulx.tempuser_group_names, group_name )
  34.  
  35. end
  36.  
  37. end
  38.  
  39. hook.Add( ULib.HOOK_UCLCHANGED, "ULXTempAddUesrGroupNamesUpdate", updateNames )
  40.  
  41. updateNames() -- Init
  42.  
  43.  
  44.  
  45. --[[
  46.  
  47. ulx.CheckExpiration( pl )
  48.  
  49. pl : PlayerObject - Player who is being checked for an expiration time.
  50.  
  51.  
  52.  
  53. This function checks to see if the called player has a temporary group configuration and will set their group to the new group if their time is expired or will
  54.  
  55. set up a timer to change the player's group when their time expires.
  56.  
  57. ]]
  58.  
  59. function ulx.CheckExpiration( pl )
  60.  
  61.  
  62.  
  63. local SID = pl:SteamID64() --We're going to use the 64bit SteamID because it's more string friendly.
  64.  
  65.  
  66.  
  67. if file.Exists( "ulx/tempuserdata/" .. SID .. ".txt", "DATA" ) then --If the authing user doesn't have a file for their SteamID then they clearly don't have a pending expiration.
  68.  
  69. local todecode = file.Read( "ulx/tempuserdata/" .. SID .. ".txt", "DATA" )
  70.  
  71. local tbl = string.Explode( "|", todecode)
  72.  
  73. local exptime = tonumber(tbl[ 1 ])
  74.  
  75. local rgroup = tbl[ 2 ]
  76.  
  77.  
  78.  
  79. if os.time() >= exptime then --If the users expiration time has already passed.
  80.  
  81. ulx.ExpireGroupChange( pl, rgroup )
  82.  
  83. else
  84.  
  85. if os.time() + 3600 >= exptime then --If the users group expires in the next 30 minutes.
  86.  
  87. timer.Create( "ULXGroupExpire_" .. SID, ( exptime - os.time() ), 1, function() ulx.ExpireGroupChange( pl, rgroup ) end )
  88.  
  89. end
  90.  
  91. end
  92.  
  93. end
  94.  
  95.  
  96.  
  97. end
  98.  
  99. hook.Add( "PlayerAuthed", "CheckExpiration", ulx.CheckExpiration )
  100.  
  101.  
  102.  
  103. --[[
  104.  
  105. ulx.PeriodicExpirationCheck()
  106.  
  107. This function checks every connected player to see if they have an expiration time on their current group.
  108.  
  109. If they do it sets their group to expire automatically.
  110.  
  111. ]]
  112.  
  113. function ulx.PeriodicExpirationCheck()
  114.  
  115.  
  116.  
  117. if CLIENT then return end
  118.  
  119.  
  120.  
  121. for _, pl in pairs (player.GetAll()) do
  122.  
  123. if not IsValid(pl) then continue end
  124.  
  125. if pl:IsConnected() then
  126.  
  127. ulx.CheckExpiration( pl )
  128.  
  129. end
  130.  
  131. end
  132.  
  133.  
  134.  
  135. end
  136.  
  137. timer.Create( "ulx_periodicexpirationcheck", 3600, 0, ulx.PeriodicExpirationCheck )
  138.  
  139.  
  140.  
  141.  
  142.  
  143. --[[
  144.  
  145. ulx.ExpireGroupChange( pl, group )
  146.  
  147. pl : PlayerObject - Player who will be having their group changed.
  148.  
  149. group : String - Group player will be set to. (setting this to 'user' will instead remove the player from the auth table)
  150.  
  151.  
  152.  
  153. This function changes the players group and removes their temp group file from the data directory.
  154.  
  155. ]]
  156.  
  157. function ulx.ExpireGroupChange( pl, group )
  158.  
  159.  
  160.  
  161. if not IsValid(pl) then return end
  162.  
  163. if not pl:IsConnected() then return end
  164.  
  165.  
  166.  
  167. local SID = pl:SteamID64()
  168.  
  169.  
  170.  
  171. if group == "user" then
  172.  
  173. ULib.ucl.removeUser(pl:SteamID())
  174.  
  175. else
  176.  
  177. ULib.ucl.addUser( pl:SteamID(), _, _, group )
  178.  
  179. end
  180.  
  181.  
  182.  
  183. ulx.fancyLogAdmin( pl, "#A had his/her group auto set to #s by a timed group script.", group )
  184.  
  185.  
  186.  
  187. timer.Remove("ULXGroupExpire_" .. SID)
  188.  
  189. if file.Exists("ulx/tempuserdata/" .. SID .. ".txt", "DATA") then
  190.  
  191. file.Delete("ulx/tempuserdata/"..SID..".txt")
  192.  
  193. end
  194.  
  195.  
  196.  
  197. end
  198.  
  199.  
  200.  
  201. --[[
  202.  
  203. ulx.CreateExpiration( pl, exp_time, return_group )
  204.  
  205. pl : PlayerObject - Player to set up an expiration on.
  206.  
  207. exp_time : Integer - time (in minutes) the player will remain in their new temp group.
  208.  
  209. return_group: String - the group the player will be set to after their current group expires.
  210.  
  211.  
  212.  
  213. This function sets up the data files so that the script will know when their group expires if they leave the server.
  214.  
  215. ]]
  216.  
  217. function ulx.CreateExpiration( pl, exp_time, return_group )
  218.  
  219.  
  220.  
  221. local SID = pl:SteamID64()
  222.  
  223. local exp_time_global = ( exp_time * 60 ) + os.time()
  224.  
  225.  
  226.  
  227. local tbl = {}
  228.  
  229. tbl[ "exptime" ] = exp_time_global
  230.  
  231. tbl[ "returngroup" ] = return_group
  232.  
  233.  
  234.  
  235. local toencode = exp_time_global .. "|" .. return_group
  236.  
  237.  
  238.  
  239. file.Write("ulx/tempuserdata/"..SID..".txt", toencode)
  240.  
  241.  
  242.  
  243. end
  244.  
  245.  
  246.  
  247. --[[
  248.  
  249. ulx.CreateExpirationByID( id, exp_time, return_group )
  250.  
  251. id : String - 64bit SteamID to set up an expiration on.
  252.  
  253. exp_time : Integer - time (in minutes) the player will remain in their new temp group.
  254.  
  255. return_group: String - the group the player will be set to after their current group expires.
  256.  
  257.  
  258.  
  259. This function sets up the data files so that the script will know when their group expires if they leave the server.
  260.  
  261. ]]
  262.  
  263. function ulx.CreateExpirationByID( id, exp_time, return_group )
  264.  
  265.  
  266.  
  267. local SID = id
  268.  
  269. local exp_time_global = ( exp_time * 60 ) + os.time()
  270.  
  271.  
  272.  
  273. local tbl = {}
  274.  
  275. tbl[ "exptime" ] = exp_time_global
  276.  
  277. tbl[ "returngroup" ] = return_group
  278.  
  279.  
  280.  
  281. local toencode = exp_time_global .. "|" .. return_group
  282.  
  283.  
  284.  
  285. file.Write("ulx/tempuserdata/"..SID..".txt", toencode)
  286.  
  287.  
  288.  
  289. end
  290.  
  291.  
  292.  
  293. --[[
  294.  
  295. ulx.tempadduser( calling_ply, target_ply, group_name, exp_time, return_group_name )
  296.  
  297. calling_ply : PlayerObject - Player running the command (usually an admin)
  298.  
  299. target_ply : PlayerObject - Player who will have their group temporarily set.
  300.  
  301. group_name : String - Group to give the player temporarily
  302.  
  303. exp_time : Integer - time (in minutes) the player will remain in their new temp group.
  304.  
  305. return_group_name: String - the group the player will be set to after their current group expires.
  306.  
  307.  
  308.  
  309. This is the actual command function. Calling this will set the player's group to a new temporary group and then after the set amount of time it will be set back to a new group.
  310.  
  311. ]]
  312.  
  313. function ulx.tempadduser( calling_ply, target_ply, group_name, exp_time, return_group_name )
  314.  
  315. group_name = group_name:lower()
  316.  
  317. return_group_name = return_group_name:lower()
  318.  
  319.  
  320.  
  321. local userInfo = ULib.ucl.authed[ target_ply:UniqueID() ]
  322.  
  323.  
  324.  
  325. local id = ULib.ucl.getUserRegisteredID( target_ply )
  326.  
  327. if not id then id = target_ply:SteamID() end
  328.  
  329.  
  330.  
  331. ULib.ucl.addUser( id, userInfo.allow, userInfo.deny, group_name )
  332.  
  333.  
  334.  
  335. ulx.fancyLogAdmin( calling_ply, "#A added #T to group #s for " .. exp_time .. " minutes.", target_ply, group_name )
  336.  
  337.  
  338.  
  339. ulx.CreateExpiration( target_ply, exp_time, return_group_name )
  340.  
  341.  
  342.  
  343. if exp_time <= 30 then
  344.  
  345. timer.Create( "ULXGroupExpire_" .. target_ply:SteamID64(), exp_time * 60, 1, function() ulx.ExpireGroupChange( target_ply, return_group_name ) end )
  346.  
  347. end
  348.  
  349. end
  350.  
  351. local tempadduser = ulx.command( CATEGORY_NAME, "ulx tempadduser", ulx.tempadduser )
  352.  
  353. tempadduser:addParam{ type=ULib.cmds.PlayerArg }
  354.  
  355. tempadduser:addParam{ type=ULib.cmds.StringArg, completes=ulx.tempuser_group_names, hint="Group to place user in temporarily", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
  356.  
  357. tempadduser:addParam{ type=ULib.cmds.NumArg, hint="Time (Minutes)" }
  358.  
  359. tempadduser:addParam{ type=ULib.cmds.StringArg, completes=ulx.tempuser_group_names, hint="Group to place user in after time expires", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
  360.  
  361. tempadduser:defaultAccess( ULib.ACCESS_SUPERADMIN )
  362.  
  363. tempadduser:help( "Add a user to specified group for a specified time." )
  364.  
  365.  
  366.  
  367. --[[
  368.  
  369. ulx.tempadduserid( calling_ply, target_id, group_name, exp_time, return_group_name )
  370.  
  371. calling_ply : PlayerObject - Player running the command (usually an admin)
  372.  
  373. target_ply : String - Player who will have their group temporarily set.
  374.  
  375. group_name : String - Group to give the player temporarily
  376.  
  377. exp_time : Integer - time (in minutes) the player will remain in their new temp group.
  378.  
  379. return_group_name: String - the group the player will be set to after their current group expires.
  380.  
  381.  
  382.  
  383. This is the actual command function. Calling this will set the player's group to a new temporary group and then after the set amount of time it will be set back to a new group.
  384.  
  385. ]]
  386.  
  387. function ulx.tempadduserid( calling_ply, target_id, group_name, exp_time, return_group_name )
  388.  
  389. group_name = group_name:lower()
  390.  
  391. return_group_name = return_group_name:lower()
  392.  
  393.  
  394.  
  395. /* Check if the player is actually connected */
  396.  
  397. new_id_64 = ulx.SteamIDTo64( target_id:upper() )
  398.  
  399.  
  400.  
  401. if new_id_64 == nil then
  402.  
  403. print( "Invalid SteamID" )
  404.  
  405. return
  406.  
  407. end
  408.  
  409.  
  410.  
  411. local target_ply = nil
  412.  
  413. for k, v in pairs( player.GetAll() ) do
  414.  
  415. if v:SteamID() == target_id then
  416.  
  417. target_ply = v
  418.  
  419. break
  420.  
  421. end
  422.  
  423. end
  424.  
  425.  
  426.  
  427. if target_ply then
  428.  
  429.  
  430.  
  431. local userInfo = ULib.ucl.authed[ target_ply:UniqueID() ]
  432.  
  433.  
  434.  
  435. local id = ULib.ucl.getUserRegisteredID( target_ply )
  436.  
  437. if not id then id = target_ply:SteamID() end
  438.  
  439.  
  440.  
  441. ULib.ucl.addUser( id, userInfo.allow, userInfo.deny, group_name )
  442.  
  443.  
  444.  
  445. ulx.fancyLogAdmin( calling_ply, "#A added #T to group #s for " .. exp_time .. " minutes.", target_ply, group_name )
  446.  
  447.  
  448.  
  449. ulx.CreateExpiration( target_ply, exp_time, return_group_name )
  450.  
  451.  
  452.  
  453. if exp_time <= 30 then
  454.  
  455. timer.Create( "ULXGroupExpire_" .. target_ply:SteamID64(), exp_time * 60, 1, function() ulx.ExpireGroupChange( target_ply, return_group_name ) end )
  456.  
  457. end
  458.  
  459.  
  460.  
  461. else
  462.  
  463. ulx.fancyLogAdmin( calling_ply, "#A added " .. target_id .. " to group #s for " .. exp_time .. " minutes.", group_name )
  464.  
  465.  
  466.  
  467. ulx.CreateExpirationByID( new_id_64, exp_time, return_group_name )
  468.  
  469. end
  470.  
  471. end
  472.  
  473. local tempadduserid = ulx.command( CATEGORY_NAME, "ulx tempadduserid", ulx.tempadduserid )
  474.  
  475. tempadduserid:addParam{ type=ULib.cmds.StringArg }
  476.  
  477. tempadduserid:addParam{ type=ULib.cmds.StringArg, completes=ulx.tempuser_group_names, hint="Group to place user in temporarily", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
  478.  
  479. tempadduserid:addParam{ type=ULib.cmds.NumArg, hint="Time (Minutes)" }
  480.  
  481. tempadduserid:addParam{ type=ULib.cmds.StringArg, completes=ulx.tempuser_group_names, hint="Group to place user in after time expires", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
  482.  
  483. tempadduserid:defaultAccess( ULib.ACCESS_SUPERADMIN )
  484.  
  485. tempadduserid:help( "Add a user by SteamID to specified group for a specified time." )
  486.  
  487.  
  488.  
  489.  
  490.  
  491. --[[
  492.  
  493. ulx.SteamIDto64( id )
  494.  
  495. id : String - Regular SteamID
  496.  
  497.  
  498.  
  499. This is a work around for one of the inadequecies in current GMod. Currently the util.SteamIDTo64 is broken.
  500.  
  501. ]]
  502.  
  503. function ulx.SteamIDTo64( id )
  504.  
  505. id = string.Trim( id )
  506.  
  507. if string.sub( id, 1, 6 ) == 'STEAM_' then
  508.  
  509. local parts = string.Explode( ':', string.sub(id,7) )
  510.  
  511. local id_64 = (1197960265728 + tonumber(parts[2])) + (tonumber(parts[3]) * 2)
  512.  
  513. local str = string.format('%f',id_64)
  514.  
  515. return '7656'..string.sub( str, 1, string.find(str,'.',1,true)-1 )
  516.  
  517. else
  518.  
  519. return nil
  520.  
  521. end
  522.  
  523. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement