extensions/geoip/extension.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 1293 2a71249ca0ab
child 1573 979f2ccfee5a
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
dvander@373
     1
/**
ds@757
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
ds@757
     4
 * SourceMod GeoIP Extension
dvander@1226
     5
 * Copyright (C) 2004-2007 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
faluco@316
    32
#include "extension.h"
faluco@316
    33
#include "GeoIP.h"
faluco@316
    34
ds@757
    35
/**
ds@757
    36
 * @file extension.cpp
ds@757
    37
 * @brief Implement extension code here.
ds@757
    38
 */
faluco@316
    39
GeoIP_Extension g_GeoIP;
faluco@316
    40
GeoIP *gi = NULL;
faluco@316
    41
faluco@316
    42
SMEXT_LINK(&g_GeoIP);
faluco@316
    43
ds@757
    44
bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
faluco@316
    45
{
ds@757
    46
	char path[PLATFORM_MAX_PATH];
faluco@321
    47
faluco@321
    48
	g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
faluco@316
    49
	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
faluco@316
    50
faluco@316
    51
	if (!gi)
faluco@316
    52
	{
dvander@1272
    53
		snprintf(error, maxlength, "Could not load configs/geoip/GeoIP.dat");
faluco@316
    54
		return false;
faluco@316
    55
	}
faluco@321
    56
faluco@321
    57
	g_pShareSys->AddNatives(myself, geoip_natives);
faluco@321
    58
	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
faluco@321
    59
faluco@316
    60
	return true;
faluco@316
    61
}
faluco@316
    62
faluco@316
    63
void GeoIP_Extension::SDK_OnUnload()
faluco@316
    64
{
faluco@316
    65
	GeoIP_delete(gi);
faluco@316
    66
	gi = NULL;
faluco@316
    67
}
faluco@316
    68
faluco@316
    69
/*******************************
faluco@316
    70
*                              *
faluco@316
    71
* GEOIP NATIVE IMPLEMENTATIONS *
faluco@316
    72
*                              *
faluco@316
    73
*******************************/
faluco@316
    74
faluco@316
    75
inline void StripPort(char *ip)
faluco@316
    76
{
faluco@316
    77
	char *tmp = strchr(ip, ':');
faluco@316
    78
	if (!tmp)
faluco@316
    79
		return;
faluco@316
    80
	*tmp = '\0';
faluco@316
    81
}
faluco@316
    82
faluco@316
    83
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
faluco@316
    84
{
faluco@316
    85
	char *ip;
faluco@316
    86
	const char *ccode;
faluco@316
    87
faluco@316
    88
	pCtx->LocalToString(params[1], &ip);
faluco@316
    89
	StripPort(ip);
faluco@316
    90
faluco@316
    91
	ccode = GeoIP_country_code_by_addr(gi, ip);
faluco@316
    92
dvander@1293
    93
	pCtx->StringToLocal(params[2], 3, ccode ? ccode : "");
dvander@1293
    94
dvander@1293
    95
	return ccode ? 1 : 0;
faluco@316
    96
}
faluco@316
    97
faluco@316
    98
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
faluco@316
    99
{
faluco@316
   100
	char *ip;
faluco@316
   101
	const char *ccode;
faluco@316
   102
faluco@316
   103
	pCtx->LocalToString(params[1], &ip);
faluco@316
   104
	StripPort(ip);
faluco@316
   105
faluco@316
   106
	ccode = GeoIP_country_code3_by_addr(gi, ip);
dvander@1293
   107
	pCtx->StringToLocal(params[2], 4, ccode ? ccode : "");
faluco@316
   108
dvander@1293
   109
	return ccode ? 1 : 0;
faluco@316
   110
}
faluco@316
   111
faluco@316
   112
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
faluco@316
   113
{
faluco@316
   114
	char *ip;
faluco@316
   115
	const char *ccode;
faluco@316
   116
faluco@316
   117
	pCtx->LocalToString(params[1], &ip);
faluco@316
   118
	StripPort(ip);
faluco@316
   119
faluco@316
   120
	ccode = GeoIP_country_name_by_addr(gi, ip);
dvander@1293
   121
	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "");
faluco@316
   122
dvander@1293
   123
	return ccode ? 1 : 0;
faluco@316
   124
}
faluco@321
   125
faluco@321
   126
const sp_nativeinfo_t geoip_natives[] = 
faluco@321
   127
{
faluco@321
   128
	{"GeoipCode2",			sm_Geoip_Code2},
faluco@321
   129
	{"GeoipCode3",			sm_Geoip_Code3},
faluco@321
   130
	{"GeoipCountry",		sm_Geoip_Country},
faluco@321
   131
	{NULL,					NULL},
dvander@355
   132
};