]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Add support for the IRCv3 account-tag specification.
[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("debian") libgeoip-dev pkg-config
26 /// $PackageInfo: require_system("ubuntu") libgeoip-dev pkg-config
27
28 #include "inspircd.h"
29 #include "xline.h"
30 #include "modules/stats.h"
31 #include "modules/whois.h"
32
33 // Fix warnings about the use of commas at end of enumerator lists on C++03.
34 #if defined __clang__
35 # pragma clang diagnostic ignored "-Wc++11-extensions"
36 #elif defined __GNUC__
37 # pragma GCC diagnostic ignored "-pedantic"
38 #endif
39
40 #include <GeoIP.h>
41
42 #ifdef _WIN32
43 # pragma comment(lib, "GeoIP.lib")
44 #endif
45
46 enum
47 {
48         // InspIRCd-specific.
49         RPL_WHOISCOUNTRY = 344
50 };
51
52 class ModuleGeoIP : public Module, public Stats::EventListener, public Whois::EventListener
53 {
54         StringExtItem ext;
55         bool extban;
56         GeoIP* gi;
57
58         std::string* SetExt(User* user)
59         {
60                 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString().c_str());
61                 if (!c)
62                         c = "UNK";
63
64                 ext.set(user, c);
65                 return ext.get(user);
66         }
67
68  public:
69         ModuleGeoIP()
70                 : Stats::EventListener(this)
71                 , Whois::EventListener(this)
72                 , ext("geoip_cc", ExtensionItem::EXT_USER, this)
73                 , extban(true)
74                 , gi(NULL)
75         {
76         }
77
78         void init() CXX11_OVERRIDE
79         {
80                 gi = GeoIP_new(GEOIP_STANDARD);
81                 if (gi == NULL)
82                                 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
83
84                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
85                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
86                 {
87                         LocalUser* user = *i;
88                         if ((user->registered == REG_ALL) && (!ext.get(user)))
89                         {
90                                 SetExt(user);
91                         }
92                 }
93         }
94
95         ~ModuleGeoIP()
96         {
97                 if (gi)
98                         GeoIP_delete(gi);
99         }
100
101         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
102         {
103                 ConfigTag* tag = ServerInstance->Config->ConfValue("geoip");
104                 extban = tag->getBool("extban");
105         }
106
107         Version GetVersion() CXX11_OVERRIDE
108         {
109                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_OPTCOMMON|VF_VENDOR);
110         }
111
112         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
113         {
114                 if (extban)
115                         tokens["EXTBAN"].push_back('G');
116         }
117
118         ModResult OnCheckBan(User* user, Channel*, const std::string& mask) CXX11_OVERRIDE
119         {
120                 if (extban && (mask.length() > 2) && (mask[0] == 'G') && (mask[1] == ':'))
121                 {
122                         std::string* cc = ext.get(user);
123                         if (!cc)
124                                 cc = SetExt(user);
125
126                         if (InspIRCd::Match(*cc, mask.substr(2)))
127                                 return MOD_RES_DENY;
128                 }
129                 return MOD_RES_PASSTHRU;
130         }
131
132         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
133         {
134                 std::string* cc = ext.get(user);
135                 if (!cc)
136                         cc = SetExt(user);
137
138                 std::string geoip = myclass->config->getString("geoip");
139                 if (geoip.empty())
140                         return MOD_RES_PASSTHRU;
141                 irc::commasepstream list(geoip);
142                 std::string country;
143                 while (list.GetToken(country))
144                         if (country == *cc)
145                                 return MOD_RES_PASSTHRU;
146                 return MOD_RES_DENY;
147         }
148
149         void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
150         {
151                 // If user has sent NICK/USER, re-set the ExtItem as this is likely CGI:IRC changing the IP
152                 if (user->registered == REG_NICKUSER)
153                         SetExt(user);
154         }
155
156         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
157         {
158                 // If the extban is disabled we don't expose users location.
159                 if (!extban)
160                         return;
161
162                 std::string* cc = ext.get(whois.GetTarget());
163                 if (!cc)
164                         cc = SetExt(whois.GetTarget());
165
166                 whois.SendLine(RPL_WHOISCOUNTRY, *cc, "is located in this country");
167         }
168
169         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
170         {
171                 if (stats.GetSymbol() != 'G')
172                         return MOD_RES_PASSTHRU;
173
174                 unsigned int unknown = 0;
175                 std::map<std::string, unsigned int> results;
176
177                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
178                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
179                 {
180                         std::string* cc = ext.get(*i);
181                         if (cc)
182                                 results[*cc]++;
183                         else
184                                 unknown++;
185                 }
186
187                 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
188                 {
189                         stats.AddRow(801, "GeoIPSTATS " + i->first + " " + ConvToStr(i->second));
190                 }
191
192                 if (unknown)
193                         stats.AddRow(801, "GeoIPSTATS Unknown " + ConvToStr(unknown));
194
195                 return MOD_RES_DENY;
196         }
197 };
198
199 MODULE_INIT(ModuleGeoIP)