extensions/geoip/extension.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 757 b799caa9a190
child 1226 478550a4d5ec
permissions -rw-r--r--
Removed strange debugging code
dvander@373
     1
/**
ds@757
     2
 * vim: set ts=4 :
ds@757
     3
 * ===============================================================
ds@757
     4
 * SourceMod GeoIP Extension
ds@757
     5
 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
ds@757
     6
 * ===============================================================
dvander@373
     7
 *
dvander@373
     8
 * This program is free software; you can redistribute it and/or
dvander@373
     9
 * modify it under the terms of the GNU General Public License
dvander@373
    10
 * as published by the Free Software Foundation; either version 2
dvander@373
    11
 * of the License, or (at your option) any later version.
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@373
    16
 * 
dvander@373
    17
 * You should have received a copy of the GNU General Public License
dvander@373
    18
 * along with this program; if not, write to the Free Software
dvander@373
    19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
dvander@373
    20
 *
dvander@373
    21
 * Version: $Id$
dvander@373
    22
 */
dvander@373
    23
faluco@316
    24
#include "extension.h"
faluco@316
    25
#include "GeoIP.h"
faluco@316
    26
ds@757
    27
/**
ds@757
    28
 * @file extension.cpp
ds@757
    29
 * @brief Implement extension code here.
ds@757
    30
 */
faluco@316
    31
GeoIP_Extension g_GeoIP;
faluco@316
    32
GeoIP *gi = NULL;
faluco@316
    33
faluco@316
    34
SMEXT_LINK(&g_GeoIP);
faluco@316
    35
ds@757
    36
bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
faluco@316
    37
{
ds@757
    38
	char path[PLATFORM_MAX_PATH];
faluco@321
    39
faluco@321
    40
	g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
faluco@316
    41
	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
faluco@316
    42
faluco@316
    43
	if (!gi)
faluco@316
    44
	{
ds@757
    45
		snprintf(error, maxlength, "Failed to instantiate GeoIP!");
faluco@316
    46
		return false;
faluco@316
    47
	}
faluco@321
    48
faluco@321
    49
	g_pShareSys->AddNatives(myself, geoip_natives);
faluco@321
    50
	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
faluco@321
    51
faluco@316
    52
	return true;
faluco@316
    53
}
faluco@316
    54
faluco@316
    55
void GeoIP_Extension::SDK_OnUnload()
faluco@316
    56
{
faluco@316
    57
	GeoIP_delete(gi);
faluco@316
    58
	gi = NULL;
faluco@316
    59
}
faluco@316
    60
faluco@316
    61
/*******************************
faluco@316
    62
*                              *
faluco@316
    63
* GEOIP NATIVE IMPLEMENTATIONS *
faluco@316
    64
*                              *
faluco@316
    65
*******************************/
faluco@316
    66
faluco@316
    67
inline void StripPort(char *ip)
faluco@316
    68
{
faluco@316
    69
	char *tmp = strchr(ip, ':');
faluco@316
    70
	if (!tmp)
faluco@316
    71
		return;
faluco@316
    72
	*tmp = '\0';
faluco@316
    73
}
faluco@316
    74
faluco@316
    75
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
faluco@316
    76
{
faluco@316
    77
	char *ip;
faluco@316
    78
	const char *ccode;
faluco@316
    79
faluco@316
    80
	pCtx->LocalToString(params[1], &ip);
faluco@316
    81
	StripPort(ip);
faluco@316
    82
faluco@316
    83
	ccode = GeoIP_country_code_by_addr(gi, ip);
faluco@316
    84
	pCtx->StringToLocal(params[2], 3, (ccode) ? ccode : "er");
faluco@316
    85
faluco@316
    86
	return 1;
faluco@316
    87
}
faluco@316
    88
faluco@316
    89
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
faluco@316
    90
{
faluco@316
    91
	char *ip;
faluco@316
    92
	const char *ccode;
faluco@316
    93
faluco@316
    94
	pCtx->LocalToString(params[1], &ip);
faluco@316
    95
	StripPort(ip);
faluco@316
    96
faluco@316
    97
	ccode = GeoIP_country_code3_by_addr(gi, ip);
faluco@316
    98
	pCtx->StringToLocal(params[2], 4, (ccode) ? ccode : "err");
faluco@316
    99
faluco@316
   100
	return 1;
faluco@316
   101
}
faluco@316
   102
faluco@316
   103
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
faluco@316
   104
{
faluco@316
   105
	char *ip;
faluco@316
   106
	const char *ccode;
faluco@316
   107
faluco@316
   108
	pCtx->LocalToString(params[1], &ip);
faluco@316
   109
	StripPort(ip);
faluco@316
   110
faluco@316
   111
	ccode = GeoIP_country_name_by_addr(gi, ip);
faluco@316
   112
	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "error");
faluco@316
   113
faluco@316
   114
	return 1;
faluco@316
   115
}
faluco@321
   116
faluco@321
   117
const sp_nativeinfo_t geoip_natives[] = 
faluco@321
   118
{
faluco@321
   119
	{"GeoipCode2",			sm_Geoip_Code2},
faluco@321
   120
	{"GeoipCode3",			sm_Geoip_Code3},
faluco@321
   121
	{"GeoipCountry",		sm_Geoip_Country},
faluco@321
   122
	{NULL,					NULL},
dvander@355
   123
};