diff options
author | attilamolnar <attilamolnar@hush.com> | 2012-06-11 18:44:53 +0200 |
---|---|---|
committer | Sir Poggles <sir.pogsalot@gmail.com> | 2012-06-12 22:49:29 -0700 |
commit | cc3daff2126623e04824892a65c853be5a3ee0ff (patch) | |
tree | 81cf81aa4546e2582c1d6dfbbdffb83939fd2dec | |
parent | 1a28c9c768f85b76c4e38aadf3dc128b1c877f2c (diff) |
m_geoip Add support for /stats G
m_geoip Change /stats G numeric to be 801
-rw-r--r-- | src/modules/extra/m_geoip.cpp | 63 |
1 files changed, 53 insertions, 10 deletions
diff --git a/src/modules/extra/m_geoip.cpp b/src/modules/extra/m_geoip.cpp index 939752875..cf87de51e 100644 --- a/src/modules/extra/m_geoip.cpp +++ b/src/modules/extra/m_geoip.cpp @@ -35,6 +35,16 @@ class ModuleGeoIP : public Module LocalStringExt ext; GeoIP* gi; + void SetExt(LocalUser* user) + { + const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString()); + if (!c) + c = "UNK"; + + std::string* cc = new std::string(c); + ext.set(user, cc); + } + public: ModuleGeoIP() : ext("geoip_cc", this), gi(NULL) { @@ -47,13 +57,23 @@ class ModuleGeoIP : public Module throw ModuleException("Unable to initialize geoip, are you missing GeoIP.dat?"); ServerInstance->Modules->AddService(ext); - Implementation eventlist[] = { I_OnSetConnectClass }; - ServerInstance->Modules->Attach(eventlist, this, 1); + Implementation eventlist[] = { I_OnSetConnectClass, I_OnStats }; + ServerInstance->Modules->Attach(eventlist, this, 2); + + for (std::vector<LocalUser*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i) + { + LocalUser* user = *i; + if ((user->registered == REG_ALL) && (!ext.get(user))) + { + SetExt(user); + } + } } ~ModuleGeoIP() { - GeoIP_delete(gi); + if (gi) + GeoIP_delete(gi); } Version GetVersion() @@ -65,13 +85,8 @@ class ModuleGeoIP : public Module { std::string* cc = ext.get(user); if (!cc) - { - const char* c = GeoIP_country_code_by_addr(gi, user->GetIPString()); - if (!c) - c = "UNK"; - cc = new std::string(c); - ext.set(user, cc); - } + SetExt(user); + std::string geoip = myclass->config->getString("geoip"); if (geoip.empty()) return MOD_RES_PASSTHRU; @@ -82,6 +97,34 @@ class ModuleGeoIP : public Module return MOD_RES_PASSTHRU; return MOD_RES_DENY; } + + ModResult OnStats(char symbol, User* user, string_list &out) + { + if (symbol != 'G') + return MOD_RES_PASSTHRU; + + unsigned int unknown = 0; + std::map<std::string, unsigned int> results; + for (std::vector<LocalUser*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i) + { + std::string* cc = ext.get(*i); + if (cc) + results[*cc]++; + else + unknown++; + } + + std::string p = ServerInstance->Config->ServerName + " 801 " + user->nick + " :GeoIPSTATS "; + for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i) + { + out.push_back(p + i->first + " " + ConvToStr(i->second)); + } + + if (unknown) + out.push_back(p + "Unknown " + ConvToStr(unknown)); + + return MOD_RES_DENY; + } }; MODULE_INIT(ModuleGeoIP) |