extensions/geoip/extension.cpp
author David Anderson <dvander@alliedmods.net>
Mon Dec 30 17:51:00 2013 -0500 (2013-12-30)
changeset 4188 89782d5fc91b
parent 3630 b72450a8be42
permissions -rw-r--r--
Move versioning to a static library to improve trivial build speed (bug 5997 part 9, r=ds).
     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 <sourcemod_version.h>
    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_pShareSys->RegisterLibrary(myself, "GeoIP");
    60 	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
    61 
    62 	return true;
    63 }
    64 
    65 void GeoIP_Extension::SDK_OnUnload()
    66 {
    67 	GeoIP_delete(gi);
    68 	gi = NULL;
    69 }
    70 
    71 const char *GeoIP_Extension::GetExtensionVerString()
    72 {
    73 	return SOURCEMOD_VERSION;
    74 }
    75 
    76 const char *GeoIP_Extension::GetExtensionDateString()
    77 {
    78 	return SOURCEMOD_BUILD_TIME;
    79 }
    80 
    81 /*******************************
    82 *                              *
    83 * GEOIP NATIVE IMPLEMENTATIONS *
    84 *                              *
    85 *******************************/
    86 
    87 inline void StripPort(char *ip)
    88 {
    89 	char *tmp = strchr(ip, ':');
    90 	if (!tmp)
    91 		return;
    92 	*tmp = '\0';
    93 }
    94 
    95 static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
    96 {
    97 	char *ip;
    98 	const char *ccode;
    99 
   100 	pCtx->LocalToString(params[1], &ip);
   101 	StripPort(ip);
   102 
   103 	ccode = GeoIP_country_code_by_addr(gi, ip);
   104 
   105 	pCtx->StringToLocal(params[2], 3, ccode ? ccode : "");
   106 
   107 	return ccode ? 1 : 0;
   108 }
   109 
   110 static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
   111 {
   112 	char *ip;
   113 	const char *ccode;
   114 
   115 	pCtx->LocalToString(params[1], &ip);
   116 	StripPort(ip);
   117 
   118 	ccode = GeoIP_country_code3_by_addr(gi, ip);
   119 	pCtx->StringToLocal(params[2], 4, ccode ? ccode : "");
   120 
   121 	return ccode ? 1 : 0;
   122 }
   123 
   124 static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
   125 {
   126 	char *ip;
   127 	const char *ccode;
   128 
   129 	pCtx->LocalToString(params[1], &ip);
   130 	StripPort(ip);
   131 
   132 	ccode = GeoIP_country_name_by_addr(gi, ip);
   133 	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "");
   134 
   135 	return ccode ? 1 : 0;
   136 }
   137 
   138 const sp_nativeinfo_t geoip_natives[] = 
   139 {
   140 	{"GeoipCode2",			sm_Geoip_Code2},
   141 	{"GeoipCode3",			sm_Geoip_Code3},
   142 	{"GeoipCountry",		sm_Geoip_Country},
   143 	{NULL,					NULL},
   144 };
   145