]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Merge the latest changes from insp20 into master.
[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("centos" "7.0") GeoIP-devel pkgconfig
24 /// $PackageInfo: require_system("darwin") geoip pkg-config
25 /// $PackageInfo: require_system("ubuntu") libgeoip-dev pkg-config
26
27 #include "inspircd.h"
28 #include "xline.h"
29
30 // Fix warnings about the use of commas at end of enumerator lists on C++03.
31 #if defined __clang__
32 # pragma clang diagnostic ignored "-Wc++11-extensions"
33 #elif defined __GNUC__
34 # pragma GCC diagnostic ignored "-pedantic"
35 #endif
36
37 #include <GeoIP.h>
38
39 #ifdef _WIN32
40 # pragma comment(lib, "GeoIP.lib")
41 #endif
42
43 class ModuleGeoIP : public Module
44 {
45         LocalStringExt ext;
46         GeoIP* gi;
47
48         std::string* SetExt(LocalUser* user)
49         {
50                 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString().c_str());
51                 if (!c)
52                         c = "UNK";
53
54                 std::string* cc = new std::string(c);
55                 ext.set(user, cc);
56                 return cc;
57         }
58
59  public:
60         ModuleGeoIP()
61                 : ext("geoip_cc", ExtensionItem::EXT_USER, this)
62                 , gi(NULL)
63         {
64         }
65
66         void init() CXX11_OVERRIDE
67         {
68                 gi = GeoIP_new(GEOIP_STANDARD);
69                 if (gi == NULL)
70                                 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
71
72                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
73                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
74                 {
75                         LocalUser* user = *i;
76                         if ((user->registered == REG_ALL) && (!ext.get(user)))
77                         {
78                                 SetExt(user);
79                         }
80                 }
81         }
82
83         ~ModuleGeoIP()
84         {
85                 if (gi)
86                         GeoIP_delete(gi);
87         }
88
89         Version GetVersion() CXX11_OVERRIDE
90         {
91                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
92         }
93
94         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
95         {
96                 std::string* cc = ext.get(user);
97                 if (!cc)
98                         cc = SetExt(user);
99
100                 std::string geoip = myclass->config->getString("geoip");
101                 if (geoip.empty())
102                         return MOD_RES_PASSTHRU;
103                 irc::commasepstream list(geoip);
104                 std::string country;
105                 while (list.GetToken(country))
106                         if (country == *cc)
107                                 return MOD_RES_PASSTHRU;
108                 return MOD_RES_DENY;
109         }
110
111         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
112         {
113                 if (stats.GetSymbol() != 'G')
114                         return MOD_RES_PASSTHRU;
115
116                 unsigned int unknown = 0;
117                 std::map<std::string, unsigned int> results;
118
119                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
120                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
121                 {
122                         std::string* cc = ext.get(*i);
123                         if (cc)
124                                 results[*cc]++;
125                         else
126                                 unknown++;
127                 }
128
129                 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
130                 {
131                         stats.AddRow(801, "GeoIPSTATS " + i->first + " " + ConvToStr(i->second));
132                 }
133
134                 if (unknown)
135                         stats.AddRow(801, "GeoIPSTATS Unknown " + ConvToStr(unknown));
136
137                 return MOD_RES_DENY;
138         }
139 };
140
141 MODULE_INIT(ModuleGeoIP)