]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Merge insp20
[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 _WIN32
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         std::string* SetExt(LocalUser* user)
39         {
40                 const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString().c_str());
41                 if (!c)
42                         c = "UNK";
43
44                 std::string* cc = new std::string(c);
45                 ext.set(user, cc);
46                 return cc;
47         }
48
49  public:
50         ModuleGeoIP() : ext("geoip_cc", this), gi(NULL)
51         {
52         }
53
54         void init() CXX11_OVERRIDE
55         {
56                 gi = GeoIP_new(GEOIP_STANDARD);
57                 if (gi == NULL)
58                                 throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?");
59
60                 ServerInstance->Modules->AddService(ext);
61                 Implementation eventlist[] = { I_OnSetConnectClass, I_OnStats };
62                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
63
64                 for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
65                 {
66                         LocalUser* user = *i;
67                         if ((user->registered == REG_ALL) && (!ext.get(user)))
68                         {
69                                 SetExt(user);
70                         }
71                 }
72         }
73
74         ~ModuleGeoIP()
75         {
76                 if (gi)
77                         GeoIP_delete(gi);
78         }
79
80         Version GetVersion() CXX11_OVERRIDE
81         {
82                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_VENDOR);
83         }
84
85         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
86         {
87                 std::string* cc = ext.get(user);
88                 if (!cc)
89                         cc = SetExt(user);
90
91                 std::string geoip = myclass->config->getString("geoip");
92                 if (geoip.empty())
93                         return MOD_RES_PASSTHRU;
94                 irc::commasepstream list(geoip);
95                 std::string country;
96                 while (list.GetToken(country))
97                         if (country == *cc)
98                                 return MOD_RES_PASSTHRU;
99                 return MOD_RES_DENY;
100         }
101
102         ModResult OnStats(char symbol, User* user, string_list &out) CXX11_OVERRIDE
103         {
104                 if (symbol != 'G')
105                         return MOD_RES_PASSTHRU;
106
107                 unsigned int unknown = 0;
108                 std::map<std::string, unsigned int> results;
109                 for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
110                 {
111                         std::string* cc = ext.get(*i);
112                         if (cc)
113                                 results[*cc]++;
114                         else
115                                 unknown++;
116                 }
117
118                 std::string p = ServerInstance->Config->ServerName + " 801 " + user->nick + " :GeoIPSTATS ";
119                 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
120                 {
121                         out.push_back(p + i->first + " " + ConvToStr(i->second));
122                 }
123
124                 if (unknown)
125                         out.push_back(p + "Unknown " + ConvToStr(unknown));
126
127                 return MOD_RES_DENY;
128         }
129 };
130
131 MODULE_INIT(ModuleGeoIP)