The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_geoip.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 #include "xline.h"
00016 
00017 #include <GeoIP.h>
00018 
00019 /* $ModDesc: Provides a way to restrict users by country using GeoIP lookup */
00020 /* $LinkerFlags: -lGeoIP */
00021 
00022 class ModuleGeoIP : public Module
00023 {
00024         GeoIP * gi;
00025 
00026         bool banunknown;
00027 
00028         std::map<std::string, std::string> GeoBans;
00029 
00030 
00031  public:
00032         ModuleGeoIP(InspIRCd *Me) : Module(Me)
00033         {
00034                 OnRehash(NULL, "");
00035                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
00036                 ServerInstance->Modules->Attach(eventlist, this, 2);
00037 
00038                 gi = GeoIP_new(GEOIP_STANDARD);
00039         }
00040 
00041         virtual ~ModuleGeoIP()
00042         {
00043         }
00044 
00045         virtual Version GetVersion()
00046         {
00047                 return Version("$Id: m_geoip.cpp 10290 2008-08-25 20:35:36Z w00t $", VF_VENDOR, API_VERSION);
00048         }
00049 
00050         virtual void OnRehash(User* user, const std::string &parameter)
00051         {
00052                 GeoBans.clear();
00053 
00054                 ConfigReader conf(ServerInstance);
00055 
00056                 banunknown = conf.ReadFlag("geoip", "banunknown", 0);
00057 
00058                 for (int i = 0; i < conf.Enumerate("geoban"); ++i)
00059                 {
00060                         std::string countrycode = conf.ReadValue("geoban", "country", i);
00061                         std::string reason = conf.ReadValue("geoban", "reason", i);
00062                         GeoBans[countrycode] = reason;
00063                 }
00064         }
00065 
00066         virtual int OnUserRegister(User* user)
00067         {
00068                 /* only do lookups on local users */
00069                 if (IS_LOCAL(user))
00070                 {
00071                         const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString());
00072                         if (c)
00073                         {
00074                                 std::map<std::string, std::string>::iterator x = GeoBans.find(c);
00075                                 if (x != GeoBans.end())
00076                                         ServerInstance->Users->QuitUser(user,  x->second);
00077                         }
00078                         else
00079                         {
00080                                 if (banunknown)
00081                                         ServerInstance->Users->QuitUser(user, "Could not identify your country of origin. Access denied.");
00082                         }
00083                 }
00084                 return 0;
00085         }
00086 };
00087 
00088 MODULE_INIT(ModuleGeoIP)
00089