]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_geoclass.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_geoclass.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019-2020 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                 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "The %s connect class is not suitable as the origin country (%s) is not any of %s",
72                         myclass->GetName().c_str(), code.c_str(), country.c_str());
73                 return MOD_RES_DENY;
74         }
75
76         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
77         {
78                 if (stats.GetSymbol() != 'G')
79                         return MOD_RES_PASSTHRU;
80
81                 // Counter for the number of users in each country.
82                 typedef std::map<Geolocation::Location*, size_t> CountryCounts;
83                 CountryCounts counts;
84
85                 // Counter for the number of users in an unknown country.
86                 size_t unknown = 0;
87
88                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
89                 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
90                 {
91                         Geolocation::Location* location = geoapi ? geoapi->GetLocation(*iter) : NULL;
92                         if (location)
93                                 counts[location]++;
94                         else
95                                 unknown++;
96                 }
97
98                 for (CountryCounts::const_iterator iter = counts.begin(); iter != counts.end(); ++iter)
99                 {
100                         Geolocation::Location* location = iter->first;
101                         stats.AddRow(RPL_STATSCOUNTRY, iter->second, location->GetCode(), location->GetName());
102                 }
103
104                 if (unknown)
105                         stats.AddRow(RPL_STATSCOUNTRY, unknown, "*", "Unknown Country");
106
107                 return MOD_RES_DENY;
108         }
109 };
110
111 MODULE_INIT(ModuleGeoClass)