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