extensions/geoip/extension.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jun 22 17:24:48 2007 +0000 (2007-06-22)
changeset 1002 786f5abf93ea
parent 757 b799caa9a190
child 1226 478550a4d5ec
permissions -rw-r--r--
fixed a bug where the "keyOnly" parameter to KvGotoNextKey was read backwards
     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  * as published by the Free Software Foundation; either version 2
    11  * of the License, or (at your option) any later version.
    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, write to the Free Software
    19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    20  *
    21  * Version: $Id$
    22  */
    23 
    24 #include "extension.h"
    25 #include "GeoIP.h"
    26 
    27 /**
    28  * @file extension.cpp
    29  * @brief Implement extension code here.
    30  */
    31 GeoIP_Extension g_GeoIP;
    32 GeoIP *gi = NULL;
    33 
    34 SMEXT_LINK(&g_GeoIP);
    35 
    36 bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
    37 {
    38 	char path[PLATFORM_MAX_PATH];
    39 
    40 	g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
    41 	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
    42 
    43 	if (!gi)
    44 	{
    45 		snprintf(error, maxlength, "Failed to instantiate GeoIP!");
    46 		return false;
    47 	}
    48 
    49 	g_pShareSys->AddNatives(myself, geoip_natives);
    50 	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
    51 
    52 	return true;
    53 }
    54 
    55 void GeoIP_Extension::SDK_OnUnload()
    56 {
    57 	GeoIP_delete(gi);
    58 	gi = NULL;
    59 }
    60 
    61 /*******************************
    62 *                              *
    63 * GEOIP NATIVE IMPLEMENTATIONS *
    64 *                              *
    65 *******************************/
    66 
    67 inline void StripPort(char *ip)
    68 {
    69 	char *tmp = strchr(ip, ':');
    70 	if (!tmp)
    71 		return;
    72 	*tmp = '\0';
    73 }
    74 
    75 static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
    76 {
    77 	char *ip;
    78 	const char *ccode;
    79 
    80 	pCtx->LocalToString(params[1], &ip);
    81 	StripPort(ip);
    82 
    83 	ccode = GeoIP_country_code_by_addr(gi, ip);
    84 	pCtx->StringToLocal(params[2], 3, (ccode) ? ccode : "er");
    85 
    86 	return 1;
    87 }
    88 
    89 static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
    90 {
    91 	char *ip;
    92 	const char *ccode;
    93 
    94 	pCtx->LocalToString(params[1], &ip);
    95 	StripPort(ip);
    96 
    97 	ccode = GeoIP_country_code3_by_addr(gi, ip);
    98 	pCtx->StringToLocal(params[2], 4, (ccode) ? ccode : "err");
    99 
   100 	return 1;
   101 }
   102 
   103 static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
   104 {
   105 	char *ip;
   106 	const char *ccode;
   107 
   108 	pCtx->LocalToString(params[1], &ip);
   109 	StripPort(ip);
   110 
   111 	ccode = GeoIP_country_name_by_addr(gi, ip);
   112 	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "error");
   113 
   114 	return 1;
   115 }
   116 
   117 const sp_nativeinfo_t geoip_natives[] = 
   118 {
   119 	{"GeoipCode2",			sm_Geoip_Code2},
   120 	{"GeoipCode3",			sm_Geoip_Code3},
   121 	{"GeoipCountry",		sm_Geoip_Country},
   122 	{NULL,					NULL},
   123 };