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