2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 # pragma comment(lib, "GeoIP.lib")
30 /* $ModDesc: Provides a way to restrict users by country using GeoIP lookup */
31 /* $LinkerFlags: -lGeoIP */
33 class ModuleGeoIP : public Module
38 void SetExt(LocalUser* user)
40 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString().c_str());
44 std::string* cc = new std::string(c);
49 ModuleGeoIP() : ext("geoip_cc", this), gi(NULL)
55 gi = GeoIP_new(GEOIP_STANDARD);
57 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
59 ServerInstance->Modules->AddService(ext);
60 Implementation eventlist[] = { I_OnSetConnectClass, I_OnStats };
61 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
63 for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
66 if ((user->registered == REG_ALL) && (!ext.get(user)))
81 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
84 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
86 std::string* cc = ext.get(user);
90 std::string geoip = myclass->config->getString("geoip");
92 return MOD_RES_PASSTHRU;
93 irc::commasepstream list(geoip);
95 while (list.GetToken(country))
97 return MOD_RES_PASSTHRU;
101 ModResult OnStats(char symbol, User* user, string_list &out)
104 return MOD_RES_PASSTHRU;
106 unsigned int unknown = 0;
107 std::map<std::string, unsigned int> results;
108 for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
110 std::string* cc = ext.get(*i);
117 std::string p = ServerInstance->Config->ServerName + " 801 " + user->nick + " :GeoIPSTATS ";
118 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
120 out.push_back(p + i->first + " " + ConvToStr(i->second));
124 out.push_back(p + "Unknown " + ConvToStr(unknown));
130 MODULE_INIT(ModuleGeoIP)