]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_geoban.cpp
221d6f8001691bbeaef6b48aa25fb6ac030b6643
[user/henk/code/inspircd.git] / src / modules / m_geoban.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Peter Powell <petpow@saberuk.com>
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/whois.h"
23
24 enum
25 {
26         // InspIRCd-specific.
27         RPL_WHOISCOUNTRY = 344
28 };
29
30 class ModuleGeoBan
31         : public Module
32         , public Whois::EventListener
33 {
34  private:
35         Geolocation::API geoapi;
36
37  public:
38         ModuleGeoBan()
39                 : Whois::EventListener(this)
40                 , geoapi(this)
41         {
42         }
43
44         Version GetVersion() CXX11_OVERRIDE
45         {
46                 return Version("Provides a way to ban users by country", VF_OPTCOMMON|VF_VENDOR);
47         }
48
49         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
50         {
51                 tokens["EXTBAN"].push_back('G');
52         }
53
54         ModResult OnCheckBan(User* user, Channel*, const std::string& mask) CXX11_OVERRIDE
55         {
56                 if ((mask.length() > 2) && (mask[0] == 'G') && (mask[1] == ':'))
57                 {
58                         Geolocation::Location* location = geoapi ? geoapi->GetLocation(user) : NULL;
59                         const std::string code = location ? location->GetCode() : "XX";
60
61                         // Does this user match against the ban?
62                         if (InspIRCd::Match(code, mask.substr(2)))
63                                 return MOD_RES_DENY;
64                 }
65                 return MOD_RES_PASSTHRU;
66         }
67
68         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
69         {
70                 Geolocation::Location* location = geoapi ? geoapi->GetLocation(whois.GetTarget()) : NULL;
71                 if (location)
72                         whois.SendLine(RPL_WHOISCOUNTRY, location->GetCode(), "is connecting from " + location->GetName());
73                 else
74                         whois.SendLine(RPL_WHOISCOUNTRY, "*", "is connecting from an unknown country");
75         }
76 };
77
78 MODULE_INIT(ModuleGeoBan)