]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Simplify ClearAllConnections code and fix possible crash
[user/henk/code/inspircd.git] / src / modules / extra / m_geoip.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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         LocalStringExt ext;
25         GeoIP* gi;
26
27  public:
28         ModuleGeoIP() : ext("geoip_cc", this)
29         {
30                 gi = GeoIP_new(GEOIP_STANDARD);
31         }
32
33         void init()
34         {
35                 ServerInstance->Modules->AddService(ext);
36                 Implementation eventlist[] = { I_OnSetConnectClass };
37                 ServerInstance->Modules->Attach(eventlist, this, 1);
38         }
39
40         ~ModuleGeoIP()
41         {
42                 GeoIP_delete(gi);
43         }
44
45         Version GetVersion()
46         {
47                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
48         }
49
50         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
51         {
52                 std::string* cc = ext.get(user);
53                 if (!cc)
54                 {
55                         const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
56                         if (!c)
57                                 c = "UNK";
58                         cc = new std::string(c);
59                         ext.set(user, cc);
60                 }
61                 std::string geoip = myclass->config->getString("geoip");
62                 if (geoip.empty())
63                         return MOD_RES_PASSTHRU;
64                 irc::commasepstream list(geoip);
65                 std::string country;
66                 while (list.GetToken(country))
67                         if (country == *cc)
68                                 return MOD_RES_PASSTHRU;
69                 return MOD_RES_DENY;
70         }
71 };
72
73 MODULE_INIT(ModuleGeoIP)
74