]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
d442cc16d321ab7fe5a28c23a29ab4950cb367d4
[user/henk/code/inspircd.git] / src / modules / extra / m_geoip.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 #include <GeoIP.h>
18
19 /* $ModDesc: Provides a way to restrict users by country using GeoIP lookup */
20 /* $LinkerFlags: -lGeoIP */
21
22 class ModuleGeoIP : public Module
23 {
24         GeoIP * gi;
25
26         bool banunknown;
27
28         std::map<std::string, std::string> GeoBans;
29
30
31  public:
32         ModuleGeoIP(InspIRCd *Me) : Module(Me)
33         {
34                 OnRehash(NULL);
35                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
36                 ServerInstance->Modules->Attach(eventlist, this, 2);
37
38                 gi = GeoIP_new(GEOIP_STANDARD);
39         }
40
41         virtual ~ModuleGeoIP()
42         {
43         }
44
45         virtual Version GetVersion()
46         {
47                 return Version("Provides a way to restrict users by country using GeoIP lookup", VF_VENDOR, API_VERSION);
48         }
49
50         virtual void OnRehash(User* user)
51         {
52                 GeoBans.clear();
53
54                 ConfigReader conf(ServerInstance);
55
56                 banunknown = conf.ReadFlag("geoip", "banunknown", 0);
57
58                 for (int i = 0; i < conf.Enumerate("geoban"); ++i)
59                 {
60                         std::string countrycode = conf.ReadValue("geoban", "country", i);
61                         std::string reason = conf.ReadValue("geoban", "reason", i);
62                         GeoBans[countrycode] = reason;
63                 }
64         }
65
66         virtual ModResult OnUserRegister(User* user)
67         {
68                 /* only do lookups on local users */
69                 if (IS_LOCAL(user))
70                 {
71                         const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
72                         if (c)
73                         {
74                                 std::map<std::string, std::string>::iterator x = GeoBans.find(c);
75                                 if (x != GeoBans.end())
76                                         ServerInstance->Users->QuitUser(user,  x->second);
77                         }
78                         else
79                         {
80                                 if (banunknown)
81                                         ServerInstance->Users->QuitUser(user, "Could not identify your country of origin. Access denied.");
82                         }
83                 }
84                 return MOD_RES_PASSTHRU;
85         }
86 };
87
88 MODULE_INIT(ModuleGeoIP)
89