Advertisement
Guest User

Untitled

a guest
Dec 9th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. --[[**********************************
  2. *
  3. * Multi Theft Auto - Admin Panel
  4. *
  5. * admin_ip2c.lua
  6. *
  7. * Original File by lil_Toady
  8. *
  9. **************************************]]
  10. g_Root = getRootElement()
  11.  
  12. -- `flag image` addon by MX_Master
  13. do
  14. local uAdminGroup = aclGetGroup('Admin')
  15.  
  16. if uAdminGroup then
  17. local tObjList = aclGroupListObjects(uAdminGroup)
  18.  
  19. if tObjList then
  20. local bFound = false
  21.  
  22. for sObj in ipairs(tObjList) do
  23. if sObj == 'resource.scoreboard' then
  24. bFound = true
  25. break
  26. end
  27. end
  28.  
  29. if not bFound then
  30. aclGroupAddObject( uAdminGroup, 'resource.scoreboard' )
  31. aclSave()
  32. end
  33. end
  34. end
  35. end
  36. --
  37.  
  38. addEventHandler('onPlayerJoin', g_Root,
  39. function ()
  40. -- `flag image` + `country full name` addons by MX_Master
  41. local code, name = getPlayerCountry(source), '-'
  42.  
  43. if code and tCC2N[code] then
  44. name = tCC2N[code]
  45.  
  46. local sFlagImagePath = ':'..getResourceName( getThisResource() )..'/client/images/flags/'..string.lower(code)..'.png'
  47. if fileExists(sFlagImagePath) then
  48. setElementData( source, "countryFlagImage", sFlagImagePath )
  49. end
  50. else
  51. code = '-'
  52. end
  53.  
  54. setElementData( source, "country", name )
  55. end
  56. )
  57.  
  58. addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
  59. function()
  60. call(getResourceFromName("scoreboard"),"addScoreboardColumn","country", getRootElement(), 10, 150)
  61.  
  62. -- `flag image` + `country full name` addons by MX_Master
  63. call(getResourceFromName("scoreboard"),"addScoreboardColumn"," flag", getRootElement(), 9, 16)
  64.  
  65. local tPlayers = getElementsByType('player')
  66. for _,uPlayer in ipairs(tPlayers) do
  67. local code, name = getPlayerCountry(uPlayer), '-'
  68.  
  69. if code and tCC2N[code] then
  70. name = tCC2N[code]
  71.  
  72. local sFlagImagePath = ':'..getResourceName( getThisResource() )..'/client/images/flags/'..string.lower(code)..'.png'
  73. if fileExists(sFlagImagePath) then
  74. setElementData( uPlayer, "countryFlagImage", sFlagImagePath )
  75. end
  76. else
  77. code = '-'
  78. end
  79.  
  80. setElementData( uPlayer, "country", name )
  81. end
  82. --
  83. end
  84. )
  85. aCountries = {}
  86.  
  87.  
  88. function getPlayerCountry ( player )
  89. return getIpCountry ( getPlayerIP ( player ) )
  90. end
  91. function getIpCountry ( ip )
  92. local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
  93. local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
  94. if ( #aCountries == 0 ) then
  95. loadIPGroups ()
  96. end
  97. if ( not aCountries[ip_group] ) then
  98. aCountries[ip_group] = {}
  99. end
  100. for id, group in ipairs ( aCountries[ip_group] ) do
  101. if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
  102. return group.rcountry
  103. end
  104. end
  105. return false
  106. end
  107.  
  108.  
  109. -- Load all IP groups from "conf/IpToCountryCompact.csv"
  110. function loadIPGroups ()
  111. unrelPosReset()
  112.  
  113. local readFilename = "conf/IpToCountryCompact.csv";
  114. local hReadFile = fileOpen( readFilename, true )
  115. if not hReadFile then
  116. outputHere ( "Cannot read " .. readFilename )
  117. return
  118. end
  119.  
  120. local buffer = ""
  121. while true do
  122. local endpos = string.find(buffer, "\n")
  123.  
  124. -- If can't find CR, try to load more from the file
  125. if not endpos then
  126. if fileIsEOF( hReadFile ) then
  127. break
  128. end
  129. buffer = buffer .. fileRead( hReadFile, 500 )
  130. end
  131.  
  132. if endpos then
  133. -- Process line
  134. local line = string.sub(buffer, 1, endpos - 1)
  135. buffer = string.sub(buffer, endpos + 1)
  136.  
  137. local parts = split( line, string.byte(',') )
  138. if #parts > 2 then
  139. local rstart = tonumber(parts[1])
  140. local rend = tonumber(parts[2])
  141. local rcountry = parts[3]
  142.  
  143. -- Relative to absolute numbers
  144. rstart = unrelRange ( rstart )
  145. rend = unrelRange ( rend )
  146.  
  147. local group = math.floor( rstart / 0x1000000 )
  148.  
  149. if not aCountries[group] then
  150. aCountries[group] = {}
  151. end
  152. local count = #aCountries[group] + 1
  153. aCountries[group][count] = {}
  154. aCountries[group][count].rstart = rstart
  155. aCountries[group][count].rend = rend
  156. aCountries[group][count].rcountry = rcountry
  157. end
  158. end
  159. end
  160.  
  161. fileClose(hReadFile)
  162. end
  163.  
  164.  
  165.  
  166. -- Make a stream of absolute numbers relative to each other
  167. local relPos = 0
  168. function relPosReset()
  169. relPos = 0
  170. end
  171. function relRange( v )
  172. local rel = v - relPos
  173. relPos = v
  174. return rel
  175. end
  176.  
  177. -- Make a stream of relative numbers absolute
  178. local unrelPos = 0
  179. function unrelPosReset()
  180. unrelPos = 0
  181. end
  182. function unrelRange( v )
  183. local unrel = v + unrelPos
  184. unrelPos = unrel
  185. return unrel
  186. end
  187.  
  188.  
  189. -- IP2C logging
  190. function outputHere( msg )
  191. --outputServerLog ( msg )
  192. outputChatBox ( msg )
  193. end
  194.  
  195.  
  196. ----------------------------------------------------------------------------------------
  197. --
  198. -- Set to true to enable commands "makecsv" and "iptest"
  199. --
  200. ----------------------------------------------------------------------------------------
  201. local makeAndTestCompactCsv = false
  202.  
  203.  
  204. if makeAndTestCompactCsv then
  205.  
  206. local makeCor
  207.  
  208. -- Takes a 'IPV4 CSV' file sourced from http://software77.net/geo-ip/
  209. -- and makes a smaller one for use by Admin
  210. addCommandHandler ( "makecsv",
  211. function ()
  212. local status = makeCor and coroutine.status(makeCor)
  213. if (status == "suspended") then
  214. outputHere( "Please wait" )
  215. return
  216. end
  217. makeCor = coroutine.create ( makeCompactCsvWorker )
  218. coroutine.resume ( makeCor )
  219. end
  220. )
  221.  
  222.  
  223. function makeCompactCsvWorker ()
  224. outputHere ( "makeCompactCsv started" )
  225. relPosReset()
  226.  
  227. local readFilename = "conf/IpToCountry.csv";
  228. local hReadFile = fileOpen( readFilename, true )
  229. if not hReadFile then
  230. outputHere ( "Cannot read " .. readFilename )
  231. return
  232. end
  233.  
  234. local writeFilename = "conf/IpToCountryCompact.csv";
  235. local hWriteFile = fileCreate( writeFilename, true )
  236. if not hWriteFile then
  237. fileClose(hReadFile)
  238. outputHere ( "Cannot create " .. writeFilename )
  239. return
  240. end
  241.  
  242. local tick = getTickCount()
  243.  
  244. local buffer = ""
  245. while true do
  246.  
  247. if ( getTickCount() > tick + 50 ) then
  248. -- Execution exceeded 50ms so pause and resume in 50ms
  249. setTimer(function()
  250. local status = coroutine.status(makeCor)
  251. if (status == "suspended") then
  252. coroutine.resume(makeCor)
  253. elseif (status == "dead") then
  254. makeCor = nil
  255. end
  256. end, 50, 1)
  257. coroutine.yield()
  258. tick = getTickCount()
  259. end
  260.  
  261. local endpos = string.find(buffer, "\n")
  262.  
  263. -- If can't find CR, try to load more from the file
  264. if not endpos then
  265. if fileIsEOF( hReadFile ) then
  266. break
  267. end
  268. buffer = buffer .. fileRead( hReadFile, 500 )
  269. end
  270.  
  271. if endpos then
  272. -- Process line
  273. local line = string.sub(buffer, 1, endpos - 1)
  274. buffer = string.sub(buffer, endpos + 1)
  275.  
  276. -- If not a comment line
  277. if string.sub(line,1,1) ~= '#' then
  278. -- Parse out required fields
  279. local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
  280. if rcountry then
  281. -- Absolute to relative numbers
  282. rstart = relRange( rstart )
  283. rend = relRange( rend )
  284. -- Output line
  285. fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
  286. end
  287. end
  288. end
  289. end
  290.  
  291. fileClose(hWriteFile)
  292. fileClose(hReadFile)
  293. outputHere ( "makeCompactCsv done" )
  294. end
  295.  
  296.  
  297. function ipTestDo( ip )
  298. local country = getIpCountry ( ip )
  299. outputHere ( "ip " .. ip .. " is in " .. tostring(country) )
  300. end
  301.  
  302. function ipTest()
  303. ipTestDo ( "46.1.2.3" )
  304. ipTestDo ( "88.1.2.3" )
  305. ipTestDo ( "46.208.74.201" )
  306. ipTestDo ( "102.1.2.3" )
  307. end
  308.  
  309. addCommandHandler ( "iptest", ipTest )
  310. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement