]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Start of GeoIP module. This is deceptively simple.
[user/henk/code/inspircd.git] / src / modules / extra / m_geoip.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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  public:
27         ModuleGeoIP(InspIRCd *Me) : Module(Me)
28         {
29                 ReadConf();
30                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
31                 ServerInstance->Modules->Attach(eventlist, this, 2);
32
33                 gi = GeoIP_new(GEOIP_STANDARD);
34         }
35
36         virtual ~ModuleGeoIP()
37         {
38         }
39
40         virtual Version GetVersion()
41         {
42                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
43         }
44
45         virtual void ReadConf()
46         {
47                 ConfigReader *MyConf = new ConfigReader(ServerInstance);
48                 delete MyConf;
49         }
50
51         virtual void OnRehash(User* user, const std::string &parameter)
52         {
53                 ReadConf();
54         }
55
56         virtual int OnUserRegister(User* user)
57         {
58                 /* only do lookups on local users */
59                 if (IS_LOCAL(user))
60                 {
61                         const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
62                         if (c)
63                         {
64                                 std::string country(c);
65                                 ServerInstance->Logs->Log("m_geoip", DEBUG, "*** Country: %s", country.c_str());
66                         }
67                         else
68                         {
69                                 ServerInstance->Logs->Log("m_geoip", DEBUG, "*** No country for %s!", user->GetIPString());
70                         }
71                 }
72
73                 /* don't do anything with this hot potato */
74                 return 0;
75         }
76 };
77
78 MODULE_INIT(ModuleGeoIP)
79