Guest User

Untitled

a guest
Oct 18th, 2015
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #if defined _INC_GEOIP
  2. #endinput
  3. #endif
  4. #define _INC_GEOIP
  5.  
  6. #include <a_samp>
  7. #include <a_http>
  8.  
  9. #if !defined _inc_sscanf2
  10. #tryinclude <sscanf2>
  11.  
  12. #if !defined _inc_sscanf2
  13. #error The sscanf2 include must be included before <geoip>
  14. #endif
  15. #endif
  16.  
  17. /*
  18. #if !defined isnull
  19. #define isnull(%1) \
  20. ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  21. #endif
  22. */
  23.  
  24. enum EX_GEO_DATA
  25. {
  26. gAllData[256],
  27. gIP[32],
  28. gCountry[32],
  29. gCity[32],
  30. gCountryCode[8],
  31. gRegionCode[8],
  32. gRegion[32],
  33. gZipCode,
  34. gTimeZone[64],
  35. Float:gLatitude,
  36. Float:gLongitude,
  37. gMetroCode
  38. };
  39.  
  40. // defines
  41.  
  42. #define Geo_GetIP(%0) Ex_GeoData[%0][gIP]
  43. #define Geo_GetAllData(%0) Ex_GeoData[%0][gAllData]
  44. #define Geo_GetCountry(%0) Ex_GeoData[%0][gCountry]
  45. #define Geo_GetCountryCode(%0) Ex_GeoData[%0][gCountryCode]
  46. #define Geo_GetCity(%0) Ex_GeoData[%0][gCity]
  47. #define Geo_GetRegionCode(%0) Ex_GeoData[%0][gRegionCode]
  48. #define Geo_GetRegion(%0) Ex_GeoData[%0][gRegion]
  49. #define Geo_GetZipCode(%0) Ex_GeoData[%0][gZipCode]
  50. #define Geo_GetTimeZone(%0) Ex_GeoData[%0][gTimeZone]
  51. #define Geo_GetLatitude(%0) Ex_GeoData[%0][gLatitude]
  52. #define Geo_GetLongitude(%0) Ex_GeoData[%0][gLongitude]
  53. #define Geo_GetMetroCode(%0) Ex_GeoData[%0][gMetroCode]
  54.  
  55. //
  56. #if !defined MAX_PLAYERS
  57. #define MAX_PLAYERS 100 // change it!
  58. #endif
  59. new Ex_GeoData[MAX_PLAYERS][EX_GEO_DATA];
  60.  
  61. public OnPlayerConnect(playerid)
  62. {
  63. Ex_Geo_LoadPlayerData(playerid);
  64. #if defined Ex_Geo_OnPlayerConnect
  65. return Ex_Geo_OnPlayerConnect(playerid);
  66. #else
  67. return 1;
  68. #endif
  69. }
  70.  
  71. static ConvertEncoding(string[])
  72. {
  73. static const
  74. scRealChars[256] =
  75. {
  76. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  77. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  78. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  79. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  80. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  81. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  82. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  83. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  84. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  85. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  86. 160, 94, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  87. 124, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 175,
  88. 128, 129, 130, 195, 131, 197, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
  89. 208, 173, 142, 143, 144, 213, 145, 215, 216, 146, 147, 148, 149, 221, 222, 150,
  90. 151, 152, 153, 227, 154, 229, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
  91. 240, 174, 165, 166, 167, 245, 168, 247, 248, 169, 170, 171, 172, 253, 254, 255
  92. };
  93. if (ispacked(string))
  94. {
  95. // In packed strings nothing is over 255.
  96. for (new i = 0, len = strlen(string); i != len; ++i)
  97. {
  98. string{i} = scRealChars[string{i}];
  99. }
  100. }
  101. else
  102. {
  103. for (new i = 0, len = strlen(string), ch; i != len; ++i)
  104. {
  105. // Check if this character is in our reduced range.
  106. if (0 <= (ch = string[i]) < 256)
  107. {
  108. string[i] = scRealChars[ch];
  109. }
  110. }
  111. }
  112. }
  113.  
  114. static Ex_Geo_LoadPlayerData(playerid)
  115. {
  116. new IP[32], url[64];
  117. GetPlayerIp(playerid, IP, 32);
  118. format(url, 64, "freegeoip.net/csv/%s", IP);
  119. HTTP(playerid, HTTP_GET, url, "", "OnGeoHTTPResponse");
  120. }
  121.  
  122. forward OnGeoHTTPResponse(playerid, response_code, data[]);
  123. public OnGeoHTTPResponse(playerid, response_code, data[])
  124. {
  125. if(response_code == 200)
  126. {
  127. ConvertEncoding(data);
  128. Ex_GeoData[playerid][gAllData][0] = EOS;
  129. strcat(Ex_GeoData[playerid][gAllData], data);
  130. sscanf(data, "p<,>s[32]s[8]s[32]s[8]s[32]s[32]is[64]ffi", Ex_GeoData[playerid][gIP], Ex_GeoData[playerid][gCountryCode], Ex_GeoData[playerid][gCountry], Ex_GeoData[playerid][gRegionCode], \
  131. Ex_GeoData[playerid][gRegion], Ex_GeoData[playerid][gCity], Ex_GeoData[playerid][gZipCode], Ex_GeoData[playerid][gTimeZone], Ex_GeoData[playerid][gLatitude], Ex_GeoData[playerid][gLongitude], Ex_GeoData[playerid][gMetroCode]);
  132. }
  133. else printf("[GeoIP] Couldn't get geoip stats for player %i. Response Code: %i", playerid, response_code);
  134. }
  135.  
  136. // OnPlayerConnect Hook
  137.  
  138. #if defined _ALS_OnPlayerConnect
  139. #undef OnPlayerConnect
  140. #else
  141. #define _ALS_OnPlayerConnect
  142. #endif
  143.  
  144. #define OnPlayerConnect Ex_Geo_OnPlayerConnect
  145.  
  146. #if defined Ex_Geo_OnPlayerConnect
  147. forward Ex_Geo_OnPlayerConnect(playerid);
  148. #endif
Advertisement
Add Comment
Please, Sign In to add comment