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 /* $LinkerFlags: -lGeoIP */
32 class ModuleGeoIP : public Module
37 std::string* SetExt(LocalUser* user)
39 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString().c_str());
43 std::string* cc = new std::string(c);
49 ModuleGeoIP() : ext("geoip_cc", this), gi(NULL)
53 void init() CXX11_OVERRIDE
55 gi = GeoIP_new(GEOIP_STANDARD);
57 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
59 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
60 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
63 if ((user->registered == REG_ALL) && (!ext.get(user)))
76 Version GetVersion() CXX11_OVERRIDE
78 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
81 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
83 std::string* cc = ext.get(user);
87 std::string geoip = myclass->config->getString("geoip");
89 return MOD_RES_PASSTHRU;
90 irc::commasepstream list(geoip);
92 while (list.GetToken(country))
94 return MOD_RES_PASSTHRU;
98 ModResult OnStats(char symbol, User* user, string_list &out) CXX11_OVERRIDE
101 return MOD_RES_PASSTHRU;
103 unsigned int unknown = 0;
104 std::map<std::string, unsigned int> results;
106 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
107 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
109 std::string* cc = ext.get(*i);
116 std::string p = "801 " + user->nick + " :GeoIPSTATS ";
117 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
119 out.push_back(p + i->first + " " + ConvToStr(i->second));
123 out.push_back(p + "Unknown " + ConvToStr(unknown));
129 MODULE_INIT(ModuleGeoIP)