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