3 * =============================================================================
4 * SourceMod GeoIP Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "extension.h"
37 * @brief Implement extension code here.
39 GeoIP_Extension g_GeoIP;
44 bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
46 char path[PLATFORM_MAX_PATH];
48 g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
49 gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
53 snprintf(error, maxlength, "Could not load configs/geoip/GeoIP.dat");
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));
64 void GeoIP_Extension::SDK_OnUnload()
70 /*******************************
72 * GEOIP NATIVE IMPLEMENTATIONS *
74 *******************************/
76 inline void StripPort(char *ip)
78 char *tmp = strchr(ip, ':');
84 static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
89 pCtx->LocalToString(params[1], &ip);
92 ccode = GeoIP_country_code_by_addr(gi, ip);
94 pCtx->StringToLocal(params[2], 3, ccode ? ccode : "");
99 static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
104 pCtx->LocalToString(params[1], &ip);
107 ccode = GeoIP_country_code3_by_addr(gi, ip);
108 pCtx->StringToLocal(params[2], 4, ccode ? ccode : "");
110 return ccode ? 1 : 0;
113 static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
118 pCtx->LocalToString(params[1], &ip);
121 ccode = GeoIP_country_name_by_addr(gi, ip);
122 pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "");
124 return ccode ? 1 : 0;
127 const sp_nativeinfo_t geoip_natives[] =
129 {"GeoipCode2", sm_Geoip_Code2},
130 {"GeoipCode3", sm_Geoip_Code3},
131 {"GeoipCountry", sm_Geoip_Country},