Advertisement
Guest User

Base

a guest
Mar 19th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.37 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_http>
  3. #include <YSI/y_iterate>
  4.  
  5. #if !defined isnull
  6.     #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  7. #endif
  8.  
  9.  
  10. enum E_COUNTRY_DATA
  11. {
  12.             cntr_Hostname[60],
  13.             cntr_Code[3],
  14.             cntr_Country[92],
  15.             cntr_Region[43],
  16.             cntr_ISP[60],
  17.             cntr_Proxy
  18. }
  19.  
  20.  
  21. static
  22.             PlayerCountryData[MAX_PLAYERS][E_COUNTRY_DATA],
  23.             PlayerLookupRetries[MAX_PLAYERS],
  24.             PlayerSessionData[MAX_PLAYERS],
  25. Iterator:   PlayerSessionIndex<MAX_PLAYERS>;
  26.  
  27. static
  28.             MaxRetries = 5;
  29.  
  30.  
  31. forward OnLookupResponse(sessionid, response, data[]);
  32.  
  33.  
  34.  
  35.  
  36. _cntr_HandleLogin(playerid)
  37. {
  38.     _cntr_ClearData(playerid);
  39.     _cntr_UseDatabase(playerid);
  40.     _cntr_UseWeb(playerid);
  41. }
  42.  
  43. _cntr_ClearData(playerid)
  44. {
  45.     PlayerCountryData[playerid][cntr_Hostname][0] = EOS;
  46.     PlayerCountryData[playerid][cntr_Code][0] = EOS;
  47.     PlayerCountryData[playerid][cntr_Country][0] = EOS;
  48.     PlayerCountryData[playerid][cntr_Region][0] = EOS;
  49.     PlayerCountryData[playerid][cntr_ISP][0] = EOS;
  50.     PlayerCountryData[playerid][cntr_Proxy] = 0;
  51. }
  52.  
  53. _cntr_UseDatabase(playerid)
  54. {
  55. // Causes sqlitei errors for some reason, disabled for now.
  56. //  GetPlayerCountry(playerid, PlayerCountryData[playerid][cntr_Country], 45);
  57.  
  58.     if(!strcmp(PlayerCountryData[playerid][cntr_Country], "Unknown"))
  59.         return 0;
  60.  
  61.     return 1;
  62. }
  63.  
  64. _cntr_UseWeb(playerid)
  65. {
  66.     new
  67.         cell,
  68.         ip[16],
  69.         query[60];
  70.  
  71.     GetPlayerIp(playerid, ip, sizeof(ip));
  72.     format(query, sizeof(query), "iphub.info/api.php?ip=%s", ip);
  73.  
  74.     cell = Iter_Free(PlayerSessionIndex);
  75.     PlayerSessionData[cell] = playerid;
  76.     Iter_Add(PlayerSessionIndex, cell);
  77.  
  78.     HTTP(cell, HTTP_GET, query, "", "OnLookupResponse");
  79.  
  80.     return 1;
  81. }
  82.  
  83. public OnLookupResponse(sessionid, response, data[])
  84. {
  85.     if(!(0 <= sessionid < MAX_PLAYERS))
  86.     {
  87.         printf("ERROR: OnLookupResponse sessionid out of bounds (%d)", sessionid);
  88.         return;
  89.     }
  90.  
  91.     new playerid = PlayerSessionData[sessionid];
  92.     Iter_Remove(PlayerSessionIndex, sessionid);
  93.  
  94.     if(!IsPlayerConnected(playerid))
  95.         return;
  96.  
  97.     if(response != 200 || isnull(data))
  98.     {
  99.         if(PlayerLookupRetries[playerid] < MaxRetries)
  100.         {
  101.             PlayerLookupRetries[playerid]++;
  102.             _cntr_UseWeb(playerid);
  103.         }
  104.  
  105.         return;
  106.     }
  107.  
  108.     new
  109.         pos,
  110.         proxy[2];
  111.  
  112.     _cntr_GetXMLData(data, "host", PlayerCountryData[playerid][cntr_Hostname], pos, 60);
  113.     _cntr_GetXMLData(data, "code", PlayerCountryData[playerid][cntr_Code], pos, 3);
  114.     _cntr_GetXMLData(data, "country", PlayerCountryData[playerid][cntr_Country], pos, 92);
  115.     _cntr_GetXMLData(data, "region", PlayerCountryData[playerid][cntr_Region], pos, 43);
  116.     _cntr_GetXMLData(data, "isp", PlayerCountryData[playerid][cntr_ISP], pos, 60);
  117.     _cntr_GetXMLData(data, "proxy", proxy, pos, 2);
  118.  
  119.     PlayerCountryData[playerid][cntr_Proxy] = strval(proxy);
  120.  
  121.     printf("[COUNTRY] Player country: '%s' (host: '%s' proxy: %d)",
  122.         PlayerCountryData[playerid][cntr_Country],
  123.         PlayerCountryData[playerid][cntr_Hostname],
  124.         PlayerCountryData[playerid][cntr_Proxy]);
  125.  
  126.     if(PlayerCountryData[playerid][cntr_Proxy])
  127.         SendClientMessage(playerid, -1, "Máš proxy");
  128.         //KickPlayer(playerid, "Proxy connection detected, these are not allowed.");
  129.  
  130.     return;
  131. }
  132.  
  133. _cntr_GetXMLData(string[], tag[], output[], &start, maxlength = sizeof(output))
  134. {
  135.     new end = start = (strfind(string, tag, true, start) + strlen(tag) + 1);
  136.  
  137.     while(string[end] != '<' && string[end] != '\0' && end - start < maxlength)
  138.         end++;
  139.  
  140.     strmid(output, string, start, end, maxlength);
  141. }
  142.  
  143.  
  144. stock GetPlayerCountryDataAsString(playerid, output[], len = sizeof(output))
  145. {
  146.     if(!IsPlayerConnected(playerid))
  147.         return 0;
  148.  
  149.     format(output, len, "\
  150.         HHostname: '%s'\n\
  151.         Code: '%s'\n\
  152.         Country: '%s'\n\
  153.         Region: '%s'\n\
  154.         ISP: '%s'\n\
  155.         Proxy: %s",
  156.         PlayerCountryData[playerid][cntr_Hostname],
  157.         PlayerCountryData[playerid][cntr_Code],
  158.         PlayerCountryData[playerid][cntr_Country],
  159.         PlayerCountryData[playerid][cntr_Region],
  160.         PlayerCountryData[playerid][cntr_ISP],
  161.         PlayerCountryData[playerid][cntr_Proxy] ? ("Yes") : ("No"));
  162.  
  163.     return 1;
  164. }
  165.  
  166. // cntr_Hostname[60],
  167. stock GetPlayerCachedHostname(playerid, output[], len = sizeof(output))
  168. {
  169.     if(!strcat(PlayerCountryData[playerid][cntr_Hostname], "Unknown")
  170.     output[0] = EOS;
  171.     strcat(output, PlayerCountryData[playerid][cntr_Hostname], len);
  172.  
  173.     return 1;
  174. }
  175.  
  176. // cntr_Code[3],
  177. stock GetPlayerCachedCountryCode(playerid, output[], len = sizeof(output))
  178. {
  179.     if(!IsPlayerConnected(playerid))
  180.         return 0;
  181.  
  182.     output[0] = EOS;
  183.     strcat(output, PlayerCountryData[playerid][cntr_Code], len);
  184.  
  185.     return 1;
  186. }
  187.  
  188. // cntr_Country[45],
  189. stock GetPlayerCachedCountryName(playerid, output[], len = sizeof(output))
  190. {
  191.     if(!IsPlayerConnected(playerid))
  192.         return 0;
  193.  
  194.     output[0] = EOS;
  195.     strcat(output, PlayerCountryData[playerid][cntr_Country], len);
  196.  
  197.     return 1;
  198. }
  199.  
  200. // cntr_Region[43],
  201. stock GetPlayerCachedRegion(playerid, output[], len = sizeof(output))
  202. {
  203.     if(!IsPlayerConnected(playerid))
  204.         return 0;
  205.  
  206.     output[0] = EOS;
  207.     strcat(output, PlayerCountryData[playerid][cntr_Region], len);
  208.  
  209.     return 1;
  210. }
  211.  
  212. // cntr_ISP[60],
  213. stock GetPlayerCachedISP(playerid, output[], len = sizeof(output))
  214. {
  215.     if(!IsPlayerConnected(playerid))
  216.         return 0;
  217.  
  218.     output[0] = EOS;
  219.     strcat(output, PlayerCountryData[playerid][cntr_ISP], len);
  220.  
  221.     return 1;
  222. }
  223.  
  224. // cntr_Proxy
  225. stock IsPlayerUsingProxy(playerid)
  226. {
  227.     if(!IsPlayerConnected(playerid))
  228.         return 0;
  229.  
  230.     return PlayerCountryData[playerid][cntr_Proxy];
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement