3 * ===============================================================
4 * SourceMod GeoIP Extension
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ===============================================================
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.
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.
24 #include "extension.h"
29 * @brief Implement extension code here.
31 GeoIP_Extension g_GeoIP;
36 bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
38 char path[PLATFORM_MAX_PATH];
40 g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
41 gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
45 snprintf(error, maxlength, "Failed to instantiate GeoIP!");
49 g_pShareSys->AddNatives(myself, geoip_natives);
50 g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
55 void GeoIP_Extension::SDK_OnUnload()
61 /*******************************
63 * GEOIP NATIVE IMPLEMENTATIONS *
65 *******************************/
67 inline void StripPort(char *ip)
69 char *tmp = strchr(ip, ':');
75 static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
80 pCtx->LocalToString(params[1], &ip);
83 ccode = GeoIP_country_code_by_addr(gi, ip);
84 pCtx->StringToLocal(params[2], 3, (ccode) ? ccode : "er");
89 static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
94 pCtx->LocalToString(params[1], &ip);
97 ccode = GeoIP_country_code3_by_addr(gi, ip);
98 pCtx->StringToLocal(params[2], 4, (ccode) ? ccode : "err");
103 static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
108 pCtx->LocalToString(params[1], &ip);
111 ccode = GeoIP_country_name_by_addr(gi, ip);
112 pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "error");
117 const sp_nativeinfo_t geoip_natives[] =
119 {"GeoipCode2", sm_Geoip_Code2},
120 {"GeoipCode3", sm_Geoip_Code3},
121 {"GeoipCountry", sm_Geoip_Country},