]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Merge pull request #109 from Justasic/insp20
[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 WINDOWS
27 # pragma comment(lib, "GeoIP.lib")
28 #endif
29
30 /* $ModDesc: Provides a way to restrict users by country using GeoIP lookup */
31 /* $LinkerFlags: -lGeoIP */
32
33 class ModuleGeoIP : public Module
34 {
35         LocalStringExt ext;
36         GeoIP* gi;
37
38  public:
39         ModuleGeoIP() : ext("geoip_cc", this), gi(NULL)
40         {
41         }
42
43         void init()
44         {
45                 gi = GeoIP_new(GEOIP_STANDARD);
46                 if (gi == NULL)
47                                 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
48
49                 ServerInstance->Modules->AddService(ext);
50                 Implementation eventlist[] = { I_OnSetConnectClass };
51                 ServerInstance->Modules->Attach(eventlist, this, 1);
52         }
53
54         ~ModuleGeoIP()
55         {
56                 GeoIP_delete(gi);
57         }
58
59         Version GetVersion()
60         {
61                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
62         }
63
64         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
65         {
66                 std::string* cc = ext.get(user);
67                 if (!cc)
68                 {
69                         const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
70                         if (!c)
71                                 c = "UNK";
72                         cc = new std::string(c);
73                         ext.set(user, cc);
74                 }
75                 std::string geoip = myclass->config->getString("geoip");
76                 if (geoip.empty())
77                         return MOD_RES_PASSTHRU;
78                 irc::commasepstream list(geoip);
79                 std::string country;
80                 while (list.GetToken(country))
81                         if (country == *cc)
82                                 return MOD_RES_PASSTHRU;
83                 return MOD_RES_DENY;
84         }
85 };
86
87 MODULE_INIT(ModuleGeoIP)
88