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"
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod GeoIP Extension
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include "extension.h"
    34 #include "GeoIP.h"
    35 
    36 /**
    37  * @file extension.cpp
    38  * @brief Implement extension code here.
    39  */
    40 GeoIP_Extension g_GeoIP;
    41 GeoIP *gi = NULL;
    42 
    43 SMEXT_LINK(&g_GeoIP);
    44 
    45 bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
    46 {
    47 	char path[PLATFORM_MAX_PATH];
    48 
    49 	g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
    50 	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
    51 
    52 	if (!gi)
    53 	{
    54 		snprintf(error, maxlength, "Could not load configs/geoip/GeoIP.dat");
    55 		return false;
    56 	}
    57 
    58 	g_pShareSys->AddNatives(myself, geoip_natives);
    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 };