]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Remove InspIRCd* parameters and fields
[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()   {
33                 OnRehash(NULL);
34                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
35                 ServerInstance->Modules->Attach(eventlist, this, 2);
36
37                 gi = GeoIP_new(GEOIP_STANDARD);
38         }
39
40         virtual ~ModuleGeoIP()
41         {
42         }
43
44         virtual Version GetVersion()
45         {
46                 return Version("Provides a way to restrict users by country using GeoIP lookup", VF_VENDOR, API_VERSION);
47         }
48
49         virtual void OnRehash(User* user)
50         {
51                 GeoBans.clear();
52
53                 ConfigReader conf;
54
55                 banunknown = conf.ReadFlag("geoip", "banunknown", 0);
56
57                 for (int i = 0; i < conf.Enumerate("geoban"); ++i)
58                 {
59                         std::string countrycode = conf.ReadValue("geoban", "country", i);
60                         std::string reason = conf.ReadValue("geoban", "reason", i);
61                         GeoBans[countrycode] = reason;
62                 }
63         }
64
65         virtual ModResult OnUserRegister(User* user)
66         {
67                 /* only do lookups on local users */
68                 if (IS_LOCAL(user))
69                 {
70                         const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
71                         if (c)
72                         {
73                                 std::map<std::string, std::string>::iterator x = GeoBans.find(c);
74                                 if (x != GeoBans.end())
75                                         ServerInstance->Users->QuitUser(user,  x->second);
76                         }
77                         else
78                         {
79                                 if (banunknown)
80                                         ServerInstance->Users->QuitUser(user, "Could not identify your country of origin. Access denied.");
81                         }
82                 }
83                 return MOD_RES_PASSTHRU;
84         }
85 };
86
87 MODULE_INIT(ModuleGeoIP)
88