]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Merge pull request #1219 from SaberUK/master+directive
[user/henk/code/inspircd.git] / src / modules / extra / m_geoip.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20 /// $CompilerFlags: find_compiler_flags("geoip" "")
21 /// $LinkerFlags: find_linker_flags("geoip" "-lGeoIP")
22
23 /// $PackageInfo: require_system("darwin") geoip pkg-config
24 /// $PackageInfo: require_system("ubuntu") libgeoip-dev pkg-config
25
26 #include "inspircd.h"
27 #include "xline.h"
28
29 #include <GeoIP.h>
30
31 #ifdef _WIN32
32 # pragma comment(lib, "GeoIP.lib")
33 #endif
34
35 class ModuleGeoIP : public Module
36 {
37         LocalStringExt ext;
38         GeoIP* gi;
39
40         std::string* SetExt(LocalUser* user)
41         {
42                 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString().c_str());
43                 if (!c)
44                         c = "UNK";
45
46                 std::string* cc = new std::string(c);
47                 ext.set(user, cc);
48                 return cc;
49         }
50
51  public:
52         ModuleGeoIP()
53                 : ext("geoip_cc", ExtensionItem::EXT_USER, this)
54                 , gi(NULL)
55         {
56         }
57
58         void init() CXX11_OVERRIDE
59         {
60                 gi = GeoIP_new(GEOIP_STANDARD);
61                 if (gi == NULL)
62                                 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
63
64                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
65                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
66                 {
67                         LocalUser* user = *i;
68                         if ((user->registered == REG_ALL) && (!ext.get(user)))
69                         {
70                                 SetExt(user);
71                         }
72                 }
73         }
74
75         ~ModuleGeoIP()
76         {
77                 if (gi)
78                         GeoIP_delete(gi);
79         }
80
81         Version GetVersion() CXX11_OVERRIDE
82         {
83                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
84         }
85
86         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
87         {
88                 std::string* cc = ext.get(user);
89                 if (!cc)
90                         cc = SetExt(user);
91
92                 std::string geoip = myclass->config->getString("geoip");
93                 if (geoip.empty())
94                         return MOD_RES_PASSTHRU;
95                 irc::commasepstream list(geoip);
96                 std::string country;
97                 while (list.GetToken(country))
98                         if (country == *cc)
99                                 return MOD_RES_PASSTHRU;
100                 return MOD_RES_DENY;
101         }
102
103         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
104         {
105                 if (stats.GetSymbol() != 'G')
106                         return MOD_RES_PASSTHRU;
107
108                 unsigned int unknown = 0;
109                 std::map<std::string, unsigned int> results;
110
111                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
112                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
113                 {
114                         std::string* cc = ext.get(*i);
115                         if (cc)
116                                 results[*cc]++;
117                         else
118                                 unknown++;
119                 }
120
121                 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
122                 {
123                         stats.AddRow(801, "GeoIPSTATS " + i->first + " " + ConvToStr(i->second));
124                 }
125
126                 if (unknown)
127                         stats.AddRow(801, "GeoIPSTATS Unknown " + ConvToStr(unknown));
128
129                 return MOD_RES_DENY;
130         }
131 };
132
133 MODULE_INIT(ModuleGeoIP)