2014-08-04 14:13:52 +04:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
|
|
|
//
|
|
|
|
// GeoIP Module
|
|
|
|
//
|
|
|
|
|
2006-06-04 11:44:55 +04:00
|
|
|
#include <stdio.h>
|
2004-05-28 12:51:23 +04:00
|
|
|
#include "geoip_amxx.h"
|
|
|
|
|
|
|
|
GeoIP *gi = NULL;
|
|
|
|
|
2006-07-16 17:23:56 +04:00
|
|
|
inline void StripPort(char *ip)
|
|
|
|
{
|
|
|
|
char *tmp = strchr(ip, ':');
|
|
|
|
if (!tmp)
|
|
|
|
return;
|
|
|
|
*tmp = '\0';
|
|
|
|
}
|
|
|
|
|
2004-05-28 12:51:23 +04:00
|
|
|
static cell AMX_NATIVE_CALL amx_geoip_code2(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
char *ip = MF_GetAmxString(amx, params[1], 0, &len);
|
2006-07-16 17:23:56 +04:00
|
|
|
StripPort(ip);
|
2004-05-28 12:51:23 +04:00
|
|
|
const char *ccode = GeoIP_country_code_by_addr(gi, ip);
|
|
|
|
return MF_SetAmxString(amx, params[2], ccode?ccode:"error", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell AMX_NATIVE_CALL amx_geoip_code3(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
char *ip = MF_GetAmxString(amx, params[1], 0, &len);
|
2006-07-16 17:23:56 +04:00
|
|
|
StripPort(ip);
|
2004-05-28 12:51:23 +04:00
|
|
|
const char *ccode = GeoIP_country_code3_by_addr(gi, ip);
|
|
|
|
return MF_SetAmxString(amx, params[2], ccode?ccode:"error", 4);
|
|
|
|
}
|
2007-07-19 22:37:13 +04:00
|
|
|
static cell AMX_NATIVE_CALL amx_geoip_code2_ex(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
char *ip = MF_GetAmxString(amx, params[1], 0, &len);
|
|
|
|
StripPort(ip);
|
|
|
|
const char *ccode = GeoIP_country_code_by_addr(gi, ip);
|
|
|
|
|
|
|
|
if (ccode == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MF_SetAmxString(amx, params[2], ccode, 2);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell AMX_NATIVE_CALL amx_geoip_code3_ex(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
char *ip = MF_GetAmxString(amx, params[1], 0, &len);
|
|
|
|
StripPort(ip);
|
|
|
|
const char *ccode = GeoIP_country_code3_by_addr(gi, ip);
|
|
|
|
|
|
|
|
if (ccode == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MF_SetAmxString(amx, params[2], ccode, 3);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2004-05-28 12:51:23 +04:00
|
|
|
|
|
|
|
static cell AMX_NATIVE_CALL amx_geoip_country(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
char *ip = MF_GetAmxString(amx, params[1], 0, &len);
|
2006-07-16 17:23:56 +04:00
|
|
|
StripPort(ip);
|
2004-05-28 12:51:23 +04:00
|
|
|
const char *ccode = GeoIP_country_name_by_addr(gi, ip);
|
|
|
|
return MF_SetAmxString(amx, params[2], ccode?ccode:"error", params[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnAmxxAttach()
|
|
|
|
{
|
2006-05-10 15:53:22 +04:00
|
|
|
char *path = MF_BuildPathname("%s/GeoIP.dat",MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
|
2006-09-09 15:41:48 +04:00
|
|
|
gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
|
2004-05-28 12:51:23 +04:00
|
|
|
if (gi == NULL) {
|
|
|
|
MF_Log("Failed to instantiate GeoIP!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *db_info = GeoIP_database_info(gi);
|
|
|
|
MF_Log("Database info: %s", db_info);
|
|
|
|
MF_AddNatives(geoip_natives);
|
|
|
|
}
|
|
|
|
|
2004-09-17 05:19:03 +04:00
|
|
|
void OnAmxxDetach()
|
|
|
|
{
|
|
|
|
GeoIP_delete(gi);
|
|
|
|
gi = NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-28 12:51:23 +04:00
|
|
|
AMX_NATIVE_INFO geoip_natives[] = {
|
|
|
|
{"geoip_code2", amx_geoip_code2},
|
|
|
|
{"geoip_code3", amx_geoip_code3},
|
2007-07-19 22:37:13 +04:00
|
|
|
|
|
|
|
{"geoip_code2_ex", amx_geoip_code2_ex},
|
|
|
|
{"geoip_code3_ex", amx_geoip_code3_ex},
|
|
|
|
|
2004-05-28 12:51:23 +04:00
|
|
|
{"geoip_country", amx_geoip_country},
|
|
|
|
{NULL, NULL},
|
2004-09-17 05:19:03 +04:00
|
|
|
};
|