extensions/geoip/extension.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4188 89782d5fc91b
permissions -rw-r--r--
Update CS:GO team score gamedata.
dvander@373
     1
/**
ds@757
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
ds@757
     4
 * SourceMod GeoIP Extension
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@373
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1226
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1226
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1226
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@373
    28
 *
dvander@373
    29
 * Version: $Id$
dvander@373
    30
 */
dvander@373
    31
dvander@2783
    32
#include <sourcemod_version.h>
faluco@316
    33
#include "extension.h"
faluco@316
    34
#include "GeoIP.h"
faluco@316
    35
ds@757
    36
/**
ds@757
    37
 * @file extension.cpp
ds@757
    38
 * @brief Implement extension code here.
ds@757
    39
 */
faluco@316
    40
GeoIP_Extension g_GeoIP;
faluco@316
    41
GeoIP *gi = NULL;
faluco@316
    42
faluco@316
    43
SMEXT_LINK(&g_GeoIP);
faluco@316
    44
ds@757
    45
bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
faluco@316
    46
{
ds@757
    47
	char path[PLATFORM_MAX_PATH];
faluco@321
    48
faluco@321
    49
	g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
faluco@316
    50
	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
faluco@316
    51
faluco@316
    52
	if (!gi)
faluco@316
    53
	{
dvander@1272
    54
		snprintf(error, maxlength, "Could not load configs/geoip/GeoIP.dat");
faluco@316
    55
		return false;
faluco@316
    56
	}
faluco@321
    57
faluco@321
    58
	g_pShareSys->AddNatives(myself, geoip_natives);
dvander@1573
    59
	g_pShareSys->RegisterLibrary(myself, "GeoIP");
faluco@321
    60
	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
faluco@321
    61
faluco@316
    62
	return true;
faluco@316
    63
}
faluco@316
    64
faluco@316
    65
void GeoIP_Extension::SDK_OnUnload()
faluco@316
    66
{
faluco@316
    67
	GeoIP_delete(gi);
faluco@316
    68
	gi = NULL;
faluco@316
    69
}
faluco@316
    70
dvander@2790
    71
const char *GeoIP_Extension::GetExtensionVerString()
dvander@2783
    72
{
dvander@4188
    73
	return SOURCEMOD_VERSION;
dvander@2783
    74
}
dvander@2783
    75
dvander@2790
    76
const char *GeoIP_Extension::GetExtensionDateString()
dvander@2783
    77
{
dvander@4188
    78
	return SOURCEMOD_BUILD_TIME;
dvander@2783
    79
}
dvander@2783
    80
faluco@316
    81
/*******************************
faluco@316
    82
*                              *
faluco@316
    83
* GEOIP NATIVE IMPLEMENTATIONS *
faluco@316
    84
*                              *
faluco@316
    85
*******************************/
faluco@316
    86
faluco@316
    87
inline void StripPort(char *ip)
faluco@316
    88
{
faluco@316
    89
	char *tmp = strchr(ip, ':');
faluco@316
    90
	if (!tmp)
faluco@316
    91
		return;
faluco@316
    92
	*tmp = '\0';
faluco@316
    93
}
faluco@316
    94
faluco@316
    95
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
faluco@316
    96
{
faluco@316
    97
	char *ip;
faluco@316
    98
	const char *ccode;
faluco@316
    99
faluco@316
   100
	pCtx->LocalToString(params[1], &ip);
faluco@316
   101
	StripPort(ip);
faluco@316
   102
faluco@316
   103
	ccode = GeoIP_country_code_by_addr(gi, ip);
faluco@316
   104
dvander@1293
   105
	pCtx->StringToLocal(params[2], 3, ccode ? ccode : "");
dvander@1293
   106
dvander@1293
   107
	return ccode ? 1 : 0;
faluco@316
   108
}
faluco@316
   109
faluco@316
   110
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
faluco@316
   111
{
faluco@316
   112
	char *ip;
faluco@316
   113
	const char *ccode;
faluco@316
   114
faluco@316
   115
	pCtx->LocalToString(params[1], &ip);
faluco@316
   116
	StripPort(ip);
faluco@316
   117
faluco@316
   118
	ccode = GeoIP_country_code3_by_addr(gi, ip);
dvander@1293
   119
	pCtx->StringToLocal(params[2], 4, ccode ? ccode : "");
faluco@316
   120
dvander@1293
   121
	return ccode ? 1 : 0;
faluco@316
   122
}
faluco@316
   123
faluco@316
   124
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
faluco@316
   125
{
faluco@316
   126
	char *ip;
faluco@316
   127
	const char *ccode;
faluco@316
   128
faluco@316
   129
	pCtx->LocalToString(params[1], &ip);
faluco@316
   130
	StripPort(ip);
faluco@316
   131
faluco@316
   132
	ccode = GeoIP_country_name_by_addr(gi, ip);
dvander@1293
   133
	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "");
faluco@316
   134
dvander@1293
   135
	return ccode ? 1 : 0;
faluco@316
   136
}
faluco@321
   137
faluco@321
   138
const sp_nativeinfo_t geoip_natives[] = 
faluco@321
   139
{
faluco@321
   140
	{"GeoipCode2",			sm_Geoip_Code2},
faluco@321
   141
	{"GeoipCode3",			sm_Geoip_Code3},
faluco@321
   142
	{"GeoipCountry",		sm_Geoip_Country},
faluco@321
   143
	{NULL,					NULL},
dvander@355
   144
};
dvander@2783
   145