]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_geoclass.cpp
6251131fd3173e96ab103ba29214f48ec5c01cba
[user/henk/code/inspircd.git] / src / modules / m_geoclass.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/geolocation.h"
22 #include "modules/stats.h"
23
24 enum
25 {
26         // InspIRCd-specific.
27         RPL_STATSCOUNTRY = 801
28 };
29
30 class ModuleGeoClass
31         : public Module
32         , public Stats::EventListener
33 {
34  private:
35         Geolocation::API geoapi;
36
37  public:
38         ModuleGeoClass()
39                 : Stats::EventListener(this)
40                 , geoapi(this)
41         {
42         }
43
44         Version GetVersion() CXX11_OVERRIDE
45         {
46                 return Version("Allows the server administrator to assign users to connect classes by the country they are connecting from.", VF_VENDOR);
47         }
48
49         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
50         {
51                 const std::string country = myclass->config->getString("country");
52                 if (country.empty())
53                         return MOD_RES_PASSTHRU;
54
55                 // If we can't find the location of this user then we can't assign
56                 // them to a location-specific connect class.
57                 Geolocation::Location* location = geoapi ? geoapi->GetLocation(user) : NULL;
58                 const std::string code = location ? location->GetCode() : "XX";
59
60                 irc::spacesepstream codes(country);
61                 for (std::string token; codes.GetToken(token); )
62                 {
63                         // If the user matches this country code then they can use this
64                         // connect class.
65                         if (stdalgo::string::equalsci(token, code))
66                                 return MOD_RES_PASSTHRU;
67                 }
68
69                 // A list of country codes were specified but the user didn't match
70                 // any of them.
71                 return MOD_RES_DENY;
72         }
73
74         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
75         {
76                 if (stats.GetSymbol() != 'G')
77                         return MOD_RES_PASSTHRU;
78
79                 // Counter for the number of users in each country.
80                 typedef std::map<Geolocation::Location*, size_t> CountryCounts;
81                 CountryCounts counts;
82
83                 // Counter for the number of users in an unknown country.
84                 size_t unknown = 0;
85
86                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
87                 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
88                 {
89                         Geolocation::Location* location = geoapi ? geoapi->GetLocation(*iter) : NULL;
90                         if (location)
91                                 counts[location]++;
92                         else
93                                 unknown++;
94                 }
95
96                 for (CountryCounts::const_iterator iter = counts.begin(); iter != counts.end(); ++iter)
97                 {
98                         Geolocation::Location* location = iter->first;
99                         stats.AddRow(RPL_STATSCOUNTRY, iter->second, location->GetCode(), location->GetName());
100                 }
101
102                 if (unknown)
103                         stats.AddRow(RPL_STATSCOUNTRY, unknown, "*", "Unknown Country");
104
105                 return MOD_RES_DENY;
106         }
107 };
108
109 MODULE_INIT(ModuleGeoClass)