Advertisement
bananaghost

SA-MP IP Lookup Script

May 17th, 2016
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <a_http>
  2.  
  3.  
  4. #define MAX_COUNTRY_NAME    (40)
  5. #define MAX_ZIP_LENGTH (15) // I don't know how long zip codes are in other countries.
  6.  
  7. forward HTTP_OnLookupResponse(index, response, data[]);
  8.  
  9. enum e_LookupData
  10. {
  11.     e_LookupCountry[MAX_COUNTRY_NAME char],
  12.     e_LookupRegion[MAX_COUNTRY_NAME char],
  13.     e_LookupCity[MAX_COUNTRY_NAME char],
  14.     e_LookupISP[MAX_COUNTRY_NAME char],
  15.     e_LookupTimezone[MAX_COUNTRY_NAME char],
  16.     e_LookupZipcode[MAX_ZIP_LENGTH char]
  17. };
  18.  
  19. new g_asLookupData[MAX_PLAYERS][e_LookupData];
  20.  
  21. #if defined OnLookupComplete
  22.     forward OnLookupComplete(playerid);
  23. #endif
  24.  
  25. public HTTP_OnLookupResponse(index, response, data[])
  26. {
  27.     new pos = -1;
  28.  
  29.     if (!IsPlayerConnected(index)) return 0;
  30.     else if (response == 200)
  31.     {
  32.         if (strfind(data, "Reserved", true) != -1 || strlen(data) < 15)
  33.         {
  34.             return 0;
  35.         }
  36.         else
  37.         {
  38.             new country[MAX_COUNTRY_NAME],region[MAX_COUNTRY_NAME],city[MAX_COUNTRY_NAME],isp[MAX_COUNTRY_NAME], timezone[MAX_COUNTRY_NAME], zip[10];
  39.  
  40.             if ((pos = strfind(data, "\"country\":")) != -1)
  41.             {
  42.                 pos = pos + 11;
  43.  
  44.                 strmid(country, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  45.             }
  46.             if ((pos = strfind(data, "\"regionName\":")) != -1)
  47.             {
  48.                 pos = pos + 14;
  49.  
  50.                 strmid(region, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  51.  
  52.             }
  53.             if ((pos = strfind(data, "\"city\":")) != -1)
  54.             {
  55.                 pos = pos + 8;
  56.  
  57.                 strmid(city, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  58.             }
  59.  
  60.             if ((pos = strfind(data, "\"isp\":")) != -1)
  61.             {
  62.                 pos = pos + 7;
  63.  
  64.                 strmid(isp, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  65.             }
  66.            
  67.             if ((pos = strfind(data, "\"timezone\":")) != -1)
  68.             {
  69.                 pos = pos + 12;
  70.  
  71.                 strmid(timezone, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  72.             }
  73.            
  74.             if ((pos = strfind(data, "\"zip\":")) != -1)
  75.             {
  76.                 pos = pos + 7;
  77.  
  78.                 strmid(zip, data, pos, strfind(data, "\"", true, pos), 10);
  79.             }
  80.  
  81.             if (pos != -1)
  82.             {
  83.                 strpack(g_asLookupData[index][e_LookupCountry], country, MAX_COUNTRY_NAME char);
  84.                 strpack(g_asLookupData[index][e_LookupRegion], region, MAX_COUNTRY_NAME char);
  85.                 strpack(g_asLookupData[index][e_LookupCity], city, MAX_COUNTRY_NAME char);
  86.                 strpack(g_asLookupData[index][e_LookupISP], isp, MAX_COUNTRY_NAME char);
  87.                 strpack(g_asLookupData[index][e_LookupTimezone], timezone, MAX_COUNTRY_NAME char);
  88.                 strpack(g_asLookupData[index][e_LookupZipcode], zip, MAX_ZIP_LENGTH char);
  89.                
  90.                 // Saving zip as a packed string because apparently strval wants to act funny.
  91.                 // Could be because it's 5am as I'm adding this shit to this include, will check it out later.
  92.                
  93.                 #if defined OnLookupComplete
  94.                 CallLocalFunction("OnLookupComplete", "d", index);
  95.                 #endif
  96.             }
  97.         }
  98.     }
  99.     return 0;
  100. }
  101.  
  102. public OnPlayerConnect(playerid)
  103. {
  104.     new ipstr[64];
  105.    
  106.     GetPlayerIp(playerid, ipstr, 64);
  107.    
  108.     strpack(g_asLookupData[playerid][e_LookupCountry], "Unknown", MAX_COUNTRY_NAME char);
  109.     strpack(g_asLookupData[playerid][e_LookupRegion], "Unknown", MAX_COUNTRY_NAME char);
  110.     strpack(g_asLookupData[playerid][e_LookupCity], "Unknown", MAX_COUNTRY_NAME char);
  111.     strpack(g_asLookupData[playerid][e_LookupISP], "Unknown", MAX_COUNTRY_NAME char);
  112.     strpack(g_asLookupData[playerid][e_LookupTimezone], "Unknown", MAX_COUNTRY_NAME char);
  113.     strpack(g_asLookupData[playerid][e_LookupZipcode], "Unknown", MAX_COUNTRY_NAME char);
  114.    
  115.     strins(ipstr, "ip-api.com/json/", 0);
  116.     HTTP(playerid, HTTP_GET, ipstr, "", "HTTP_OnLookupResponse");
  117.    
  118.     #if defined IPL_OnPlayerConnect
  119.         IPL_OnPlayerConnect(playerid);
  120.     #endif
  121.     return 1;
  122. }
  123. #if defined _ALS_OnPlayerConnect
  124.     #undef OnPlayerConnect
  125. #else
  126.     #define _ALS_OnPlayerConnect
  127. #endif
  128. #define OnPlayerConnect IPL_OnPlayerConnect
  129. #if defined IPL_OnPlayerConnect
  130.     forward IPL_OnPlayerConnect(playerid);
  131. #endif
  132.  
  133.  
  134. stock GetPlayerCountry(playerid, country[], size = sizeof(country))
  135. {
  136.     if (IsPlayerConnected(playerid))
  137.         return strunpack(country, g_asLookupData[playerid][e_LookupCountry], size);
  138.  
  139.     else
  140.         strunpack(country, !"Unknown", size);
  141.  
  142.     return 0;
  143. }
  144.  
  145. stock GetPlayerRegion(playerid, region[], size = sizeof(region))
  146. {
  147.     if (IsPlayerConnected(playerid))
  148.         return strunpack(region, g_asLookupData[playerid][e_LookupRegion], size);
  149.  
  150.     else
  151.         strunpack(region, !"Unknown", size);
  152.  
  153.     return 0;
  154. }
  155.  
  156. stock GetPlayerCity(playerid, city[], size = sizeof(city))
  157. {
  158.     if (IsPlayerConnected(playerid))
  159.         return strunpack(city, g_asLookupData[playerid][e_LookupCity], size);
  160.  
  161.     else
  162.         strunpack(city, !"Unknown", size);
  163.  
  164.     return 0;
  165. }
  166.  
  167. stock GetPlayerISP(playerid, isp[], size = sizeof(isp))
  168. {
  169.     if (IsPlayerConnected(playerid))
  170.         return strunpack(isp, g_asLookupData[playerid][e_LookupISP], size);
  171.  
  172.     else
  173.         strunpack(isp, !"Unknown", size);
  174.  
  175.     return 0;
  176. }
  177.  
  178. stock GetPlayerTimezone(playerid, timezone[], size = sizeof(timezone))
  179. {
  180.     if (IsPlayerConnected(playerid))
  181.         return strunpack(timezone, g_asLookupData[playerid][e_LookupTimezone], size);
  182.  
  183.     else
  184.         strunpack(timezone, !"Unknown", size);
  185.  
  186.     return 0;
  187. }
  188.  
  189. stock GetPlayerZipcode(playerid, zipcode[], size = sizeof(zipcode)) // Returns as a string for now, read note in the callback above.
  190. {
  191.     if (IsPlayerConnected(playerid))
  192.         return strunpack(zipcode, g_asLookupData[playerid][e_LookupZipcode], size);
  193.  
  194.     else
  195.         strunpack(zipcode, !"Unknown", size);
  196.  
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement