]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
cf87de51e252a2ea17339845eb1221747c8ad5e1
[user/henk/code/inspircd.git] / src / modules / extra / m_geoip.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "xline.h"
23
24 #include <GeoIP.h>
25
26 #ifdef WINDOWS
27 # pragma comment(lib, "GeoIP.lib")
28 #endif
29
30 /* $ModDesc: Provides a way to restrict users by country using GeoIP lookup */
31 /* $LinkerFlags: -lGeoIP */
32
33 class ModuleGeoIP : public Module
34 {
35         LocalStringExt ext;
36         GeoIP* gi;
37
38         void SetExt(LocalUser* user)
39         {
40                 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
41                 if (!c)
42                         c = "UNK";
43
44                 std::string* cc = new std::string(c);
45                 ext.set(user, cc);
46         }
47
48  public:
49         ModuleGeoIP() : ext("geoip_cc", this), gi(NULL)
50         {
51         }
52
53         void init()
54         {
55                 gi = GeoIP_new(GEOIP_STANDARD);
56                 if (gi == NULL)
57                                 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
58
59                 ServerInstance->Modules->AddService(ext);
60                 Implementation eventlist[] = { I_OnSetConnectClass, I_OnStats };
61                 ServerInstance->Modules->Attach(eventlist, this, 2);
62
63                 for (std::vector<LocalUser*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
64                 {
65                         LocalUser* user = *i;
66                         if ((user->registered == REG_ALL) && (!ext.get(user)))
67                         {
68                                 SetExt(user);
69                         }
70                 }
71         }
72
73         ~ModuleGeoIP()
74         {
75                 if (gi)
76                         GeoIP_delete(gi);
77         }
78
79         Version GetVersion()
80         {
81                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
82         }
83
84         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
85         {
86                 std::string* cc = ext.get(user);
87                 if (!cc)
88                         SetExt(user);
89
90                 std::string geoip = myclass->config->getString("geoip");
91                 if (geoip.empty())
92                         return MOD_RES_PASSTHRU;
93                 irc::commasepstream list(geoip);
94                 std::string country;
95                 while (list.GetToken(country))
96                         if (country == *cc)
97                                 return MOD_RES_PASSTHRU;
98                 return MOD_RES_DENY;
99         }
100
101         ModResult OnStats(char symbol, User* user, string_list &out)
102         {
103                 if (symbol != 'G')
104                         return MOD_RES_PASSTHRU;
105
106                 unsigned int unknown = 0;
107                 std::map<std::string, unsigned int> results;
108                 for (std::vector<LocalUser*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
109                 {
110                         std::string* cc = ext.get(*i);
111                         if (cc)
112                                 results[*cc]++;
113                         else
114                                 unknown++;
115                 }
116
117                 std::string p = ServerInstance->Config->ServerName + " 801 " + user->nick + " :GeoIPSTATS ";
118                 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
119                 {
120                         out.push_back(p + i->first + " " + ConvToStr(i->second));
121                 }
122
123                 if (unknown)
124                         out.push_back(p + "Unknown " + ConvToStr(unknown));
125
126                 return MOD_RES_DENY;
127         }
128 };
129
130 MODULE_INIT(ModuleGeoIP)
131