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