Advertisement
Guest User

Special Geoip 1.0.0.1

a guest
Mar 16th, 2014
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.04 KB | None | 0 0
  1. #include "amxxmodule.h"
  2. #include "GeoIP.h"
  3. #include "GeoIP_internal.h"
  4. #include "GeoIPCity.h"
  5.  
  6. #if defined __linux__
  7. #include <dirent.h>
  8. #else
  9. #include "pread.h"
  10. #include <io.h>
  11. #endif
  12.  
  13. #include "Header.h"
  14.  
  15. GeoIP * g_Handle = NULL;
  16. GeoIPRecord * g_Record = NULL;
  17.  
  18. char g_Database[256U];
  19. char g_String[256U];
  20.  
  21. char g_FileFound = FALSE;
  22. char g_CustomCharset = 8U;
  23.  
  24. const char * g_LocalInfo = NULL;
  25. const char * g_Address = NULL;
  26. const char * g_ModName = NULL;
  27.  
  28. static cell AMX_NATIVE_CALL GeoipInfo(AMX * Handle, cell * Parameter)
  29. {
  30.     if (!ReadLocation(MF_GetAmxString(Handle, Parameter[1U], 0U, 0U)))
  31.     {
  32.         MF_SetAmxString(Handle, Parameter[3U], "N/A", Parameter[4U]);
  33.  
  34.         return 0U;
  35.     }
  36.  
  37.     switch ((GeoInfo)Parameter[2U])
  38.     {
  39.     case GI_Country:
  40.         MF_SetAmxString(Handle, Parameter[3U], g_Record->country_name ? g_Record->country_name : "N/A", Parameter[4U]);
  41.  
  42.         break;
  43.  
  44.     case GI_CountryCode:
  45.         MF_SetAmxString(Handle, Parameter[3U], g_Record->country_code && strcmp(g_Record->country_code, "--") \
  46.             ? g_Record->country_code : "N/A", Parameter[4U]);
  47.  
  48.         break;
  49.  
  50.     case GI_CountryCode3:
  51.         MF_SetAmxString(Handle, Parameter[3U], g_Record->country_code3 && strcmp(g_Record->country_code3, "--") \
  52.             ? g_Record->country_code3 : "N/A", Parameter[4U]);
  53.  
  54.         break;
  55.  
  56.     case GI_Region:
  57.         if (!g_Record->country_code || !g_Record->region)
  58.         {
  59.             MF_SetAmxString(Handle, Parameter[3U], "N/A", Parameter[4U]);
  60.         }
  61.  
  62.         else
  63.         {
  64.             MF_SetAmxString(Handle, Parameter[3U], GeoIP_region_name_by_code(g_Record->country_code, g_Record->region) \
  65.                 ? GeoIP_region_name_by_code(g_Record->country_code, g_Record->region) : "N/A", Parameter[4U]);
  66.         }
  67.  
  68.         break;
  69.  
  70.     case GI_TimeZone:
  71.         if (!g_Record->country_code || !g_Record->region)
  72.         {
  73.             MF_SetAmxString(Handle, Parameter[3U], "N/A", Parameter[4U]);
  74.         }
  75.  
  76.         else
  77.         {
  78.             MF_SetAmxString(Handle, Parameter[3U], GeoIP_time_zone_by_country_and_region(g_Record->country_code, g_Record->region) \
  79.                 ? GeoIP_time_zone_by_country_and_region(g_Record->country_code, g_Record->region) : "N/A", Parameter[4U]);
  80.         }
  81.  
  82.         break;
  83.  
  84.     case GI_City:
  85.         MF_SetAmxString(Handle, Parameter[3U], g_Record->city ? g_Record->city : "N/A", Parameter[4U]);
  86.  
  87.         break;
  88.  
  89.     case GI_ContinentCode:
  90.         MF_SetAmxString(Handle, Parameter[3U], g_Record->continent_code ? g_Record->continent_code : "N/A", Parameter[4U]);
  91.  
  92.         break;
  93.  
  94.     case GI_RegionCode:
  95.         MF_SetAmxString(Handle, Parameter[3U], g_Record->region ? g_Record->region : "N/A", Parameter[4U]);
  96.  
  97.         break;
  98.  
  99.     case GI_Latitude:
  100.         _snprintf(g_String, sizeof(g_String), "%f", g_Record->latitude);
  101.  
  102.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  103.  
  104.         break;
  105.  
  106.     case GI_Longitude:
  107.         _snprintf(g_String, sizeof(g_String), "%f", g_Record->longitude);
  108.  
  109.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  110.  
  111.         break;
  112.  
  113.     case GI_AreaCode:
  114.         _snprintf(g_String, sizeof(g_String), "%i", g_Record->area_code);
  115.  
  116.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  117.  
  118.         break;
  119.  
  120.     case GI_PostalCode:
  121.         MF_SetAmxString(Handle, Parameter[3U], g_Record->postal_code ? g_Record->postal_code : "N/A", Parameter[4U]);
  122.  
  123.         break;
  124.  
  125.     case GI_MetroCode:
  126.         _snprintf(g_String, sizeof(g_String), "%i", g_Record->metro_code);
  127.  
  128.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  129.  
  130.         break;
  131.  
  132.     case GI_DmaCode:
  133.         _snprintf(g_String, sizeof(g_String), "%i", g_Record->dma_code);
  134.  
  135.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  136.  
  137.         break;
  138.  
  139.     case GI_NetMask:
  140.         _snprintf(g_String, sizeof(g_String), "%i", g_Record->netmask);
  141.  
  142.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  143.  
  144.         break;
  145.  
  146.     case GI_Continent:
  147.         MF_SetAmxString(Handle, Parameter[3U], GeoipContinentName(g_Record->continent_code), Parameter[4U]);
  148.  
  149.         break;
  150.  
  151.     case GI_Charset:
  152.         _snprintf(g_String, sizeof(g_String), "%i", g_Record->charset);
  153.  
  154.         MF_SetAmxString(Handle, Parameter[3U], g_String, Parameter[4U]);
  155.  
  156.         break;
  157.     }
  158.  
  159.     GeoIPRecord_delete(g_Record);
  160.  
  161.     g_Record = NULL;
  162.  
  163.     return 1U;
  164. }
  165.  
  166. static cell AMX_NATIVE_CALL GeoipCharset(AMX * Handle, cell * Parameter)
  167. {
  168.     switch (GeoIPCharset(Parameter[1U]))
  169.     {
  170.     case GEOIP_CHARSET_ISO_8859_1:
  171.         g_CustomCharset = GEOIP_CHARSET_ISO_8859_1;
  172.  
  173.         printf("[%s] Changing Geoip's charset to ISO-8859.\n", MODULE_LOGTAG);
  174.  
  175.         break;
  176.  
  177.     case GEOIP_CHARSET_UTF8:
  178.         g_CustomCharset = GEOIP_CHARSET_UTF8;
  179.  
  180.         printf("[%s] Changing Geoip's charset to UTF-8.\n", MODULE_LOGTAG);
  181.  
  182.         break;
  183.     }
  184.  
  185.     if (g_Handle)
  186.     {
  187.         g_Handle->charset = g_CustomCharset;
  188.     }
  189.  
  190.     if (g_Record)
  191.     {
  192.         g_Record->charset = g_CustomCharset;
  193.     }
  194.  
  195.     return 1U;
  196. }
  197.  
  198. const AMX_NATIVE_INFO g_Functions[] =
  199. {
  200.     { "GeoipInfo", GeoipInfo },
  201.     { "GeoipCharset", GeoipCharset },
  202.  
  203.     { NULL, NULL}
  204. };
  205.  
  206. const char * GeoipContinentName(const char * ContinentCode)
  207. {
  208.     if (!ContinentCode)
  209.     {
  210.         return "N/A";
  211.     }
  212.  
  213.     if (!strcmp(ContinentCode, "AF"))
  214.     {
  215.         return "Africa";
  216.     }
  217.  
  218.     else if (!strcmp(ContinentCode, "EU"))
  219.     {
  220.         return "Europe";
  221.     }
  222.  
  223.     else if (!strcmp(ContinentCode, "SA"))
  224.     {
  225.         return "South America";
  226.     }
  227.  
  228.     else if (!strcmp(ContinentCode, "NA"))
  229.     {
  230.         return "North America";
  231.     }
  232.  
  233.     else if (!strcmp(ContinentCode, "OC"))
  234.     {
  235.         return "Oceania";
  236.     }
  237.  
  238.     else if (!strcmp(ContinentCode, "AS"))
  239.     {
  240.         return "Asia";
  241.     }
  242.  
  243.     else if (!strcmp(ContinentCode, "AN"))
  244.     {
  245.         return "Antarctica";
  246.     }
  247.  
  248.     return "N/A";
  249. }
  250.  
  251. char LoadGeoip(char * Location)
  252. {
  253.     if (!g_Handle)
  254.     {
  255.         g_Handle = GeoIP_open(Location, GEOIP_INDEX_CACHE);
  256.  
  257.         if (g_Handle)
  258.         {
  259.             if (g_CustomCharset != 8U)
  260.             {
  261.                 g_Handle->charset = GEOIP_CHARSET_UTF8;
  262.             }
  263.  
  264.             else
  265.             {
  266.                 g_Handle->charset = g_CustomCharset;
  267.             }
  268.  
  269.             return TRUE;
  270.         }
  271.  
  272.         return FALSE;
  273.     }
  274.  
  275.     return FALSE;
  276. }
  277.  
  278. char CloseGeoip(void)
  279. {
  280.     if (g_Record)
  281.     {
  282.         GeoIPRecord_delete(g_Record);
  283.  
  284.         g_Record = NULL;
  285.     }
  286.  
  287.     if (g_Handle)
  288.     {
  289.         GeoIP_delete(g_Handle);
  290.  
  291.         g_Handle = NULL;
  292.  
  293.         return TRUE;
  294.     }
  295.  
  296.     return FALSE;
  297. }
  298.  
  299. char ReadLocation(char * Address)
  300. {
  301.     if (g_Handle && Address)
  302.     {
  303.         g_Record = GeoIP_record_by_addr(g_Handle, Address);
  304.  
  305.         if (g_Record)
  306.         {
  307.             if (g_CustomCharset != 8U)
  308.             {
  309.                 g_Record->charset = g_CustomCharset;
  310.             }
  311.  
  312.             else
  313.             {
  314.                 g_Record->charset = GEOIP_CHARSET_UTF8;
  315.             }
  316.  
  317.             return TRUE;
  318.         }
  319.  
  320.         return FALSE;
  321.     }
  322.  
  323.     return FALSE;
  324. }
  325.  
  326. void OnAmxxAttach(void)
  327. {
  328.     g_FileFound = FALSE;
  329.  
  330.     MF_AddNatives(g_Functions);
  331.  
  332.     if (!g_ModName)
  333.     {
  334.         g_ModName = MF_GetModname();
  335.     }
  336.  
  337.     g_LocalInfo = MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data");
  338.  
  339. #if defined __linux__
  340.     DIR * Handle = NULL;
  341.  
  342.     dirent * Data = NULL;
  343.  
  344.     _snprintf(g_Database, sizeof(g_Database), "%s/%s/", g_ModName, g_LocalInfo);
  345.  
  346.     if ((Handle = opendir(g_Database)))
  347.     {
  348.         while ((Data = readdir(Handle)))
  349.         {
  350.             if (strstr(Data->d_name, "Geo") && strstr(Data->d_name, "City"))
  351.             {
  352.                 _snprintf(g_Database, sizeof(g_Database), "%s/%s/%s", g_ModName, g_LocalInfo, \
  353.                     Data->d_name);
  354.  
  355.                 printf("[%s] Taking %s file as database.\n", MODULE_LOGTAG, Data->d_name);
  356.  
  357.                 g_FileFound = TRUE;
  358.             }
  359.         }
  360.  
  361.         closedir(Handle);
  362.     }
  363. #else
  364.     _finddata_t Data;
  365.  
  366.     intptr_t File = NULL;
  367.  
  368.     _snprintf(g_Database, sizeof(g_Database), "%s/%s/*", g_ModName, g_LocalInfo);
  369.  
  370.     if ((File = _findfirst(g_Database, &Data)) != -1L)
  371.     {
  372.         do
  373.         {
  374.             if (strstr(Data.name, "Geo") && strstr(Data.name, "City"))
  375.             {
  376.                 _snprintf(g_Database, sizeof(g_Database), "%s/%s/%s", g_ModName, g_LocalInfo, \
  377.                     Data.name);
  378.  
  379.                 printf("[%s] Taking %s file as database.\n", MODULE_LOGTAG, Data.name);
  380.  
  381.                 g_FileFound = TRUE;
  382.             }
  383.         }
  384.  
  385.         while (!_findnext(File, &Data));
  386.  
  387.         _findclose(File);
  388.     }
  389. #endif
  390.  
  391.     if (!g_FileFound)
  392.     {
  393.         printf("[%s] Unable to find any database.\n", MODULE_LOGTAG);
  394.     }
  395.  
  396.     else
  397.     {
  398.         if (LoadGeoip(g_Database))
  399.         {
  400.             printf("[%s] %s\n", MODULE_LOGTAG, GeoIP_database_info(g_Handle));
  401.         }
  402.  
  403.         else
  404.         {
  405.             printf("[%s] Unable to load database.\n", MODULE_LOGTAG);
  406.         }
  407.     }
  408. }
  409.  
  410. void OnAmxxDetach(void)
  411. {
  412.     if (CloseGeoip())
  413.     {
  414.         printf("[%s] Geoip's database has been closed.\n", MODULE_LOGTAG);
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement