extensions/geoip/extension.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 09 19:48:46 2007 +0000 (2007-08-09)
changeset 1293 2a71249ca0ab
parent 1272 95787f8d2b77
child 1330 ab94316bb52b
permissions -rw-r--r--
fixed silly design decisions in geoip that were copied over from amxmodx. natives now return true/false and the buffer is zeroed instead of filled with "ERROR"
dvander@373
     1
/**
ds@757
     2
 * vim: set ts=4 :
dvander@1226
     3
 * ================================================================
ds@757
     4
 * SourceMod GeoIP Extension
dvander@1226
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
dvander@1226
     6
 * ================================================================
dvander@373
     7
 *
dvander@1226
     8
 * This program is free software; you can redistribute it and/or 
dvander@1226
     9
 * modify it under the terms of the GNU General Public License, 
dvander@1226
    10
 * version 3.0, as published by the Free Software Foundation.
dvander@1226
    11
 * 
dvander@373
    12
 * This program is distributed in the hope that it will be useful,
dvander@373
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dvander@373
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dvander@373
    15
 * GNU General Public License for more details.
dvander@1226
    16
 *
dvander@373
    17
 * You should have received a copy of the GNU General Public License
dvander@1226
    18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1226
    19
 *
dvander@1226
    20
 * As a special exception, AlliedModders LLC gives you permission to 
dvander@1226
    21
 * link the code of this program (as well as its derivative works) to 
dvander@1226
    22
 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
dvander@1226
    23
 * Game MODs that run on software by the Valve Corporation.  You must 
dvander@1226
    24
 * obey the GNU General Public License in all respects for all other 
dvander@1226
    25
 * code used. Additionally, AlliedModders LLC grants this exception 
dvander@1226
    26
 * to all derivative works. AlliedModders LLC defines further 
dvander@1226
    27
 * exceptions, found in LICENSE.txt (as of this writing, version 
dvander@1226
    28
 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
dvander@373
    29
 *
dvander@373
    30
 * Version: $Id$
dvander@373
    31
 */
dvander@373
    32
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);
faluco@321
    59
	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
faluco@321
    60
faluco@316
    61
	return true;
faluco@316
    62
}
faluco@316
    63
faluco@316
    64
void GeoIP_Extension::SDK_OnUnload()
faluco@316
    65
{
faluco@316
    66
	GeoIP_delete(gi);
faluco@316
    67
	gi = NULL;
faluco@316
    68
}
faluco@316
    69
faluco@316
    70
/*******************************
faluco@316
    71
*                              *
faluco@316
    72
* GEOIP NATIVE IMPLEMENTATIONS *
faluco@316
    73
*                              *
faluco@316
    74
*******************************/
faluco@316
    75
faluco@316
    76
inline void StripPort(char *ip)
faluco@316
    77
{
faluco@316
    78
	char *tmp = strchr(ip, ':');
faluco@316
    79
	if (!tmp)
faluco@316
    80
		return;
faluco@316
    81
	*tmp = '\0';
faluco@316
    82
}
faluco@316
    83
faluco@316
    84
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
faluco@316
    85
{
faluco@316
    86
	char *ip;
faluco@316
    87
	const char *ccode;
faluco@316
    88
faluco@316
    89
	pCtx->LocalToString(params[1], &ip);
faluco@316
    90
	StripPort(ip);
faluco@316
    91
faluco@316
    92
	ccode = GeoIP_country_code_by_addr(gi, ip);
faluco@316
    93
dvander@1293
    94
	pCtx->StringToLocal(params[2], 3, ccode ? ccode : "");
dvander@1293
    95
dvander@1293
    96
	return ccode ? 1 : 0;
faluco@316
    97
}
faluco@316
    98
faluco@316
    99
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
faluco@316
   100
{
faluco@316
   101
	char *ip;
faluco@316
   102
	const char *ccode;
faluco@316
   103
faluco@316
   104
	pCtx->LocalToString(params[1], &ip);
faluco@316
   105
	StripPort(ip);
faluco@316
   106
faluco@316
   107
	ccode = GeoIP_country_code3_by_addr(gi, ip);
dvander@1293
   108
	pCtx->StringToLocal(params[2], 4, ccode ? ccode : "");
faluco@316
   109
dvander@1293
   110
	return ccode ? 1 : 0;
faluco@316
   111
}
faluco@316
   112
faluco@316
   113
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
faluco@316
   114
{
faluco@316
   115
	char *ip;
faluco@316
   116
	const char *ccode;
faluco@316
   117
faluco@316
   118
	pCtx->LocalToString(params[1], &ip);
faluco@316
   119
	StripPort(ip);
faluco@316
   120
faluco@316
   121
	ccode = GeoIP_country_name_by_addr(gi, ip);
dvander@1293
   122
	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "");
faluco@316
   123
dvander@1293
   124
	return ccode ? 1 : 0;
faluco@316
   125
}
faluco@321
   126
faluco@321
   127
const sp_nativeinfo_t geoip_natives[] = 
faluco@321
   128
{
faluco@321
   129
	{"GeoipCode2",			sm_Geoip_Code2},
faluco@321
   130
	{"GeoipCode3",			sm_Geoip_Code3},
faluco@321
   131
	{"GeoipCountry",		sm_Geoip_Country},
faluco@321
   132
	{NULL,					NULL},
dvander@355
   133
};