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