2014-08-05 13:14:29 +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
|
|
|
|
//
|
|
|
|
|
2014-08-01 01:08:58 +04:00
|
|
|
#include "geoip_util.h"
|
|
|
|
#include "geoip_natives.h"
|
|
|
|
|
|
|
|
char *stripPort(char *ip)
|
|
|
|
{
|
|
|
|
char *tmp = strchr(ip, ':');
|
|
|
|
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
*tmp = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* stristr(const char* str, const char* substr)
|
|
|
|
{
|
|
|
|
register char *needle = (char *)substr;
|
|
|
|
register char *prevloc = (char *)str;
|
|
|
|
register char *haystack = (char *)str;
|
|
|
|
|
|
|
|
while (*haystack)
|
|
|
|
{
|
|
|
|
if (tolower(*haystack) == tolower(*needle))
|
|
|
|
{
|
|
|
|
haystack++;
|
|
|
|
|
|
|
|
if (!*++needle)
|
|
|
|
{
|
|
|
|
return prevloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
haystack = ++prevloc;
|
|
|
|
needle = (char *)substr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lookupByIp(const char *ip, const char **path, MMDB_entry_data_s *result)
|
|
|
|
{
|
|
|
|
int gai_error = 0, mmdb_error = 0;
|
|
|
|
MMDB_lookup_result_s lookup = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error);
|
|
|
|
|
2014-08-01 19:11:33 +04:00
|
|
|
if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !lookup.found_entry)
|
2014-08-01 01:08:58 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MMDB_entry_data_s entry_data;
|
|
|
|
MMDB_aget_value(&lookup.entry, &entry_data, path);
|
|
|
|
|
|
|
|
if (!entry_data.has_data)
|
|
|
|
{
|
2014-08-01 19:11:33 +04:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
// Dirty fall back to default language ("en") in case provided user's language is not localized.
|
|
|
|
|
|
|
|
// Searh "names" position.
|
2015-01-07 00:36:14 +03:00
|
|
|
while (path[i] && strcmp(path[i++], "names"));
|
2014-08-01 19:11:33 +04:00
|
|
|
|
|
|
|
// No localized entry or we use already default language.
|
2015-01-07 00:36:14 +03:00
|
|
|
if (!path[i] || !strcmp(path[i], "en"))
|
2014-08-01 19:11:33 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite user's language.
|
|
|
|
path[i] = "en";
|
|
|
|
|
|
|
|
// Try again.
|
|
|
|
gai_error = mmdb_error = 0;
|
|
|
|
MMDB_aget_value(&lookup.entry, &entry_data, path);
|
|
|
|
|
|
|
|
if (!entry_data.has_data)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-01 01:08:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = entry_data;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *lookupString(const char *ip, const char **path, int *length)
|
|
|
|
{
|
|
|
|
static char buffer[256]; // This should be large enough for long name in UTF-8.
|
|
|
|
MMDB_entry_data_s result;
|
|
|
|
|
|
|
|
if (!lookupByIp(ip, path, &result))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's avoid a crash in case we go over the buffer size.
|
2014-08-01 14:59:56 +04:00
|
|
|
size_t maxLength = ke::Min((size_t)result.data_size, sizeof(buffer)-1);
|
2014-08-01 01:08:58 +04:00
|
|
|
|
|
|
|
// Strings from database are not null terminated.
|
|
|
|
memcpy(buffer, result.utf8_string, maxLength);
|
2014-09-18 21:24:37 +04:00
|
|
|
buffer[maxLength] = '\0';
|
2014-08-01 01:08:58 +04:00
|
|
|
|
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
*length = maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
double lookupDouble(const char *ip, const char **path)
|
|
|
|
{
|
|
|
|
MMDB_entry_data_s result;
|
|
|
|
|
|
|
|
if (!lookupByIp(ip, path, &result))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.double_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getContinentId(const char *code)
|
|
|
|
{
|
|
|
|
#define CONTINENT_UNKNOWN 0
|
2014-08-06 00:57:48 +04:00
|
|
|
#define CONTINENT_AFRICA 1
|
|
|
|
#define CONTINENT_ANTARCTICA 2
|
|
|
|
#define CONTINENT_ASIA 3
|
|
|
|
#define CONTINENT_EUROPE 4
|
|
|
|
#define CONTINENT_NORTH_AMERICA 5
|
|
|
|
#define CONTINENT_OCEANIA 6
|
|
|
|
#define CONTINENT_SOUTH_AMERICA 7
|
2014-08-01 01:08:58 +04:00
|
|
|
|
|
|
|
int index = CONTINENT_UNKNOWN;
|
|
|
|
|
|
|
|
if (code)
|
|
|
|
{
|
|
|
|
switch (code[0])
|
|
|
|
{
|
|
|
|
case 'A':
|
|
|
|
{
|
2014-10-18 21:10:09 +04:00
|
|
|
switch (code[1])
|
|
|
|
{
|
|
|
|
case 'F': index = CONTINENT_AFRICA; break;
|
|
|
|
case 'N': index = CONTINENT_ANTARCTICA; break;
|
|
|
|
case 'S': index = CONTINENT_ASIA; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2014-08-01 01:08:58 +04:00
|
|
|
}
|
2014-10-18 21:10:09 +04:00
|
|
|
|
2014-08-01 01:08:58 +04:00
|
|
|
case 'E': index = CONTINENT_EUROPE; break;
|
|
|
|
case 'O': index = CONTINENT_OCEANIA; break;
|
|
|
|
case 'N': index = CONTINENT_NORTH_AMERICA; break;
|
|
|
|
case 'S': index = CONTINENT_SOUTH_AMERICA; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *getLang(int playerIndex)
|
|
|
|
{
|
|
|
|
static cvar_t *amxmodx_language = NULL;
|
|
|
|
static cvar_t *amxmodx_cl_langs = NULL;
|
|
|
|
|
|
|
|
if (!amxmodx_language)
|
|
|
|
amxmodx_language = CVAR_GET_POINTER("amx_language");
|
|
|
|
|
|
|
|
if (!amxmodx_cl_langs)
|
|
|
|
amxmodx_cl_langs = CVAR_GET_POINTER("amx_client_languages");
|
|
|
|
|
|
|
|
if (playerIndex >= 0 && amxmodx_cl_langs && amxmodx_language)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
const char *lang;
|
|
|
|
|
|
|
|
if (playerIndex == 0 || amxmodx_cl_langs->value <= 0 || !MF_IsPlayerIngame(playerIndex))
|
|
|
|
{
|
|
|
|
value = amxmodx_language->string;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = ENTITY_KEYVALUE(MF_GetPlayerEdict(playerIndex), "lang");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value && *value)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < LangList.length(); ++i)
|
|
|
|
{
|
|
|
|
lang = LangList.at(i).chars();
|
|
|
|
|
|
|
|
if (stristr(lang, value) != NULL)
|
|
|
|
{
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "en";
|
|
|
|
}
|