From 402c624e455d13cc811bef2e8d1639846e892a34 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 29 Jul 2013 12:02:40 +0000 Subject: Move chanstats stuff to stats/ --- modules/commands/cs_fantasy_stats.cpp | 158 --------- modules/commands/cs_fantasy_top.cpp | 195 ----------- modules/commands/cs_set.cpp | 63 +--- modules/commands/ns_set.cpp | 85 +---- modules/extra/m_chanstats.cpp | 464 ------------------------- modules/stats/cs_fantasy_stats.cpp | 158 +++++++++ modules/stats/cs_fantasy_top.cpp | 195 +++++++++++ modules/stats/m_chanstats.cpp | 624 ++++++++++++++++++++++++++++++++++ 8 files changed, 982 insertions(+), 960 deletions(-) delete mode 100644 modules/commands/cs_fantasy_stats.cpp delete mode 100644 modules/commands/cs_fantasy_top.cpp delete mode 100644 modules/extra/m_chanstats.cpp create mode 100644 modules/stats/cs_fantasy_stats.cpp create mode 100644 modules/stats/cs_fantasy_top.cpp create mode 100644 modules/stats/m_chanstats.cpp diff --git a/modules/commands/cs_fantasy_stats.cpp b/modules/commands/cs_fantasy_stats.cpp deleted file mode 100644 index d54ed4a92..000000000 --- a/modules/commands/cs_fantasy_stats.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/* Chanstats core functions - * - * (C) 2003-2013 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. - */ - -#include "module.h" -#include "modules/sql.h" - -class MySQLInterface : public SQL::Interface -{ - public: - MySQLInterface(Module *o) : SQL::Interface(o) { } - - void OnResult(const SQL::Result &r) anope_override - { - } - - void OnError(const SQL::Result &r) anope_override - { - if (!r.GetQuery().query.empty()) - Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError(); - else - Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError(); - } -}; - - -class CommandCSStats : public Command -{ - public: - CommandCSStats(Module *creator) : Command (creator, "chanserv/stats", 0, 2) - { - this->SetDesc(_("Displays your Channel Stats")); - this->SetSyntax(_("\037nick\037")); - } - - void Execute(CommandSource &source, const std::vector ¶ms); -}; - -class CommandCSGStats : public Command -{ - public: - CommandCSGStats(Module *creator) : Command (creator, "chanserv/gstats", 0, 2) - { - this->SetDesc(_("Displays your Global Stats")); - this->SetSyntax(_("\037nick\037")); - } - - void Execute(CommandSource &source, const std::vector ¶ms); -}; - - -class CSStats; -static CSStats *me; -class CSStats : public Module -{ - CommandCSStats commandcsstats; - CommandCSGStats commandcsgstats; - ServiceReference sql; - MySQLInterface sqlinterface; - Anope::string prefix; - public: - CSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), - commandcsstats(this), commandcsgstats(this), sql("", ""), sqlinterface(this) - { - me = this; - - } - - void OnReload(Configuration::Conf *conf) anope_override - { - prefix = conf->GetModule(this)->Get("prefix"); - if (prefix.empty()) - prefix = "anope_"; - this->sql = ServiceReference("SQL::Provider", conf->GetModule(this)->Get("engine")); - } - - SQL::Result RunQuery(const SQL::Query &query) - { - if (!this->sql) - throw SQL::Exception("Unable to locate SQL reference, is m_mysql loaded and configured correctly?"); - - SQL::Result res = this->sql->RunQuery(query); - if (!res.GetError().empty()) - throw SQL::Exception(res.GetError()); - return res; - } - - void DoStats(CommandSource &source, const bool is_global, const std::vector ¶ms) - { - if (!source.c) - return; - - Anope::string display; - if (params.empty()) - display = source.nc->display; - else if (const NickAlias *na = NickAlias::Find(params[0])) - display = na->nc->display; - else - { - source.Reply(_("%s not found."), params[0].c_str()); - return; - } - - try - { - SQL::Query query; - query = "SELECT letters, words, line, smileys_happy+smileys_sad+smileys_other as smileys," - "actions FROM `" + prefix + "chanstats` " - "WHERE `nick` = @nick@ AND `chan` = @channel@ AND `type` = 'total';"; - if (is_global) - query.SetValue("channel", ""); - else - query.SetValue("channel", source.c->ci->name); - query.SetValue("nick", display); - SQL::Result res = this->RunQuery(query); - - if (res.Rows() > 0) - { - if (is_global) - source.Reply(_("Network stats for %s:"), display.c_str()); - else - source.Reply(_("Channel stats for %s on %s:"), display.c_str(), source.c->name.c_str()); - - source.Reply(_("letters: %s, words: %s, lines: %s, smileys %s, actions: %s"), - res.Get(0, "letters").c_str(), res.Get(0, "words").c_str(), - res.Get(0, "line").c_str(), res.Get(0, "smileys").c_str(), - res.Get(0, "actions").c_str()); - } - else - source.Reply(_("No stats for %s."), display.c_str()); - } - catch (const SQL::Exception &ex) - { - Log(LOG_DEBUG) << ex.GetReason(); - } - - } -}; - -void CommandCSStats::Execute(CommandSource &source, const std::vector ¶ms) -{ - me->DoStats(source, false, params); -} - -void CommandCSGStats::Execute(CommandSource &source, const std::vector ¶ms) -{ - me->DoStats(source, true, params); -} - -MODULE_INIT(CSStats) - diff --git a/modules/commands/cs_fantasy_top.cpp b/modules/commands/cs_fantasy_top.cpp deleted file mode 100644 index bc08c4c9d..000000000 --- a/modules/commands/cs_fantasy_top.cpp +++ /dev/null @@ -1,195 +0,0 @@ -/* Chanstats core functions - * - * (C) 2003-2013 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. - */ - -#include "module.h" -#include "modules/sql.h" - -class MySQLInterface : public SQL::Interface -{ - public: - MySQLInterface(Module *o) : SQL::Interface(o) { } - - void OnResult(const SQL::Result &r) anope_override - { - } - - void OnError(const SQL::Result &r) anope_override - { - if (!r.GetQuery().query.empty()) - Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError(); - else - Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError(); - } -}; - -class CommandCSTop : public Command -{ - public: - CommandCSTop(Module *creator) : Command (creator, "chanserv/top", 0, 2) - { - this->SetDesc(_("Displays the top 3 users of a channel")); - this->SetSyntax(_("\037channel\037")); - } - - void Execute(CommandSource &source, const std::vector ¶ms); -}; - -class CommandCSTop10 : public Command -{ - public: - CommandCSTop10(Module *creator) : Command (creator, "chanserv/top10", 0, 2) - { - this->SetDesc(_("Displays the top 10 users of a channel")); - this->SetSyntax(_("\037channel\037")); - } - - void Execute(CommandSource &source, const std::vector ¶ms); -}; - -class CommandCSGTop : public Command -{ - public: - CommandCSGTop(Module *creator) : Command (creator, "chanserv/gtop", 0, 1) - { - this->SetDesc(_("Displays the top 3 users of the network")); - } - - void Execute(CommandSource &source, const std::vector ¶ms); -}; - -class CommandCSGTop10 : public Command -{ - public: - CommandCSGTop10(Module *creator) : Command (creator, "chanserv/gtop10", 0, 1) - { - this->SetDesc(_("Displays the top 10 users of the network")); - } - - void Execute(CommandSource &source, const std::vector ¶ms); -}; - - -class CSTop; -static CSTop *me; -class CSTop : public Module -{ - CommandCSTop commandcstop; - CommandCSGTop commandcsgtop; - CommandCSTop10 commandcstop10; - CommandCSGTop10 commandcsgtop10; - ServiceReference sql; - MySQLInterface sqlinterface; - Anope::string prefix; - - public: - CSTop(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), - commandcstop(this), commandcsgtop(this), commandcstop10(this), commandcsgtop10(this), sql("", ""), - sqlinterface(this) - { - me = this; - - } - - void OnReload(Configuration::Conf *conf) anope_override - { - prefix = conf->GetModule(this)->Get("prefix"); - if (prefix.empty()) - prefix = "anope_"; - this->sql = ServiceReference("SQL::Provider", conf->GetModule(this)->Get("engine")); - } - - SQL::Result RunQuery(const SQL::Query &query) - { - if (!this->sql) - throw SQL::Exception("Unable to locate SQL reference, is m_mysql loaded and configured correctly?"); - - SQL::Result res = sql->RunQuery(query); - if (!res.GetError().empty()) - throw SQL::Exception(res.GetError()); - return res; - } - - void DoTop(CommandSource &source, const std::vector ¶ms, bool is_global, int limit = 1) - { - if (!source.c || !source.c->ci) - return; - - Anope::string channel; - if (is_global || params.empty()) - channel = source.c->ci->name; - else if (!params.empty()) - channel = params[0]; - else - { - source.Reply(_("%s not found."), params[0].c_str()); - return; - } - - try - { - SQL::Query query; - query = "SELECT nick, letters, words, line, actions," - "smileys_happy+smileys_sad+smileys_other as smileys " - "FROM `" + prefix + "chanstats` " - "WHERE `nick` != '' AND `chan` = @channel@ AND `type` = 'total' " - "ORDER BY `letters` DESC LIMIT @limit@;"; - query.SetValue("limit", limit, false); - - if (is_global) - query.SetValue("channel", ""); - else - query.SetValue("channel", channel.c_str()); - - SQL::Result res = this->RunQuery(query); - - if (res.Rows() > 0) - { - source.Reply(_("Top %i of %s"), limit, (is_global ? "Network" : channel.c_str())); - for (int i = 0; i < res.Rows(); ++i) - { - source.Reply(_("%2lu \002%-16s\002 letters: %s, words: %s, lines: %s, smileys %s, actions: %s"), - i+1, res.Get(i, "nick").c_str(), res.Get(i, "letters").c_str(), - res.Get(i, "words").c_str(), res.Get(i, "line").c_str(), - res.Get(0, "smileys").c_str(), res.Get(0, "actions").c_str()); - } - } - else - source.Reply(_("No stats for %s."), is_global ? "Network" : channel.c_str()); - } - catch (const SQL::Exception &ex) - { - Log(LOG_DEBUG) << ex.GetReason(); - } - } -}; - -void CommandCSTop::Execute(CommandSource &source, const std::vector ¶ms) -{ - me->DoTop(source, params, false, 3); -} - -void CommandCSTop10::Execute(CommandSource &source, const std::vector ¶ms) -{ - me->DoTop(source, params, false, 10); -} - -void CommandCSGTop::Execute(CommandSource &source, const std::vector ¶ms) -{ - me->DoTop(source, params, true, 3); -} - -void CommandCSGTop10::Execute(CommandSource &source, const std::vector ¶ms) -{ - me->DoTop(source, params, true, 10); -} - - -MODULE_INIT(CSTop) diff --git a/modules/commands/cs_set.cpp b/modules/commands/cs_set.cpp index 161b0b725..129e5be06 100644 --- a/modules/commands/cs_set.cpp +++ b/modules/commands/cs_set.cpp @@ -173,60 +173,6 @@ class CommandCSSetBanType : public Command } }; -class CommandCSSetChanstats : public Command -{ - public: - CommandCSSetChanstats(Module *creator) : Command(creator, "chanserv/set/chanstats", 2, 2) - { - this->SetDesc(_("Turn chanstat statistics on or off")); - this->SetSyntax(_("\037channel\037 {ON | OFF}")); - } - - void Execute(CommandSource &source, const std::vector ¶ms) anope_override - { - ChannelInfo *ci = ChannelInfo::Find(params[0]); - if (!ci) - { - source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str()); - return; - } - - EventReturn MOD_RESULT; - FOREACH_RESULT(OnSetChannelOption, MOD_RESULT, (source, this, ci, params[1])); - if (MOD_RESULT == EVENT_STOP) - return; - - if (MOD_RESULT != EVENT_ALLOW && !source.AccessFor(ci).HasPriv("SET") && source.permission.empty() && !source.HasPriv("chanserv/administration")) - { - source.Reply(ACCESS_DENIED); - return; - } - - if (params[1].equals_ci("ON")) - { - ci->Extend("CS_STATS"); - source.Reply(_("Chanstats statistics are now enabled for this channel.")); - Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to enable chanstats"; - } - else if (params[1].equals_ci("OFF")) - { - Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to disable chanstats"; - ci->Shrink("CS_STATS"); - source.Reply(_("Chanstats statistics are now disabled for this channel.")); - } - else - this->OnSyntaxError(source, ""); - } - - bool OnHelp(CommandSource &source, const Anope::string &) anope_override - { - this->SendSyntax(source); - source.Reply(" "); - source.Reply("Turn Chanstats channel statistics ON or OFF."); - return true; - } -}; - class CommandCSSetDescription : public Command { public: @@ -989,13 +935,12 @@ class CommandCSSetNoexpire : public Command class CSSet : public Module { - SerializableExtensibleItem persist, noautoop, stats, peace, securefounder, + SerializableExtensibleItem persist, noautoop, peace, securefounder, restricted, secure, secureops, signkick, signkick_level, noexpire; CommandCSSet commandcsset; CommandCSSetAutoOp commandcssetautoop; CommandCSSetBanType commandcssetbantype; - CommandCSSetChanstats commandcssetchanstats; CommandCSSetDescription commandcssetdescription; CommandCSSetFounder commandcssetfounder; CommandCSSetPeace commandcssetpeace; @@ -1010,12 +955,12 @@ class CSSet : public Module public: CSSet(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), - persist(this, "PERSIST"), noautoop(this, "NOAUTOOP"), stats(this, "CS_STATS"), peace(this, "PEACE"), + persist(this, "PERSIST"), noautoop(this, "NOAUTOOP"), peace(this, "PEACE"), securefounder(this, "SECUREFOUNDER"), restricted(this, "RESTRICTED"), secure(this, "CS_SECURE"), secureops(this, "SECUREOPS"), signkick(this, "SIGNKICK"), signkick_level(this, "SIGNKICK_LEVEL"), noexpire(this, "CS_NO_EXPIRE"), - commandcsset(this), commandcssetautoop(this), commandcssetbantype(this), commandcssetchanstats(this), + commandcsset(this), commandcssetautoop(this), commandcssetbantype(this), commandcssetdescription(this), commandcssetfounder(this), commandcssetpeace(this), commandcssetpersist(this), commandcssetrestricted(this), commandcssetsecure(this), commandcssetsecurefounder(this), commandcssetsecureops(this), commandcssetsignkick(this), @@ -1130,8 +1075,6 @@ class CSSet : public Module info.AddOption(_("Persistent")); if (noexpire.HasExt(ci)) info.AddOption(_("No expire")); - if (stats.HasExt(ci)) - info.AddOption(_("Chanstats")); time_t chanserv_expire = Config->GetModule(this)->Get("expire", "14d"); if (!noexpire.HasExt(ci) && chanserv_expire && !Anope::NoExpire) diff --git a/modules/commands/ns_set.cpp b/modules/commands/ns_set.cpp index 6f47750cb..df35f0582 100644 --- a/modules/commands/ns_set.cpp +++ b/modules/commands/ns_set.cpp @@ -286,81 +286,6 @@ class CommandNSSASetAutoOp : public CommandNSSetAutoOp } }; -class CommandNSSetChanstats : public Command -{ - public: - CommandNSSetChanstats(Module *creator, const Anope::string &sname = "nickserv/set/chanstats", size_t min = 1 ) : Command(creator, sname, min, min + 1) - { - this->SetDesc(_("Turn chanstat statistic on or off")); - this->SetSyntax(_("{ON | OFF}")); - } - void Run(CommandSource &source, const Anope::string &user, const Anope::string ¶m) - { - NickAlias *na = NickAlias::Find(user); - if (!na) - { - source.Reply(NICK_X_NOT_REGISTERED, user.c_str()); - return; - } - - EventReturn MOD_RESULT; - FOREACH_RESULT(OnSetNickOption, MOD_RESULT, (source, this, na->nc, param)); - if (MOD_RESULT == EVENT_STOP) - return; - - if (param.equals_ci("ON")) - { - Log(na->nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to enable chanstats for " << na->nc->display; - na->nc->Extend("NS_STATS"); - source.Reply(_("Chanstat statistics are now enabled for your nick.")); - } - else if (param.equals_ci("OFF")) - { - Log(na->nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to disable chanstats for " << na->nc->display; - na->nc->Shrink("NS_STATS"); - source.Reply(_("Chanstat statistics are now disabled for your nick.")); - } - else - this->OnSyntaxError(source, "CHANSTATS"); - } - - void Execute(CommandSource &source, const std::vector ¶ms) anope_override - { - this->Run(source, source.nc->display, params[0]); - } - - bool OnHelp(CommandSource &source, const Anope::string &) anope_override - { - this->SendSyntax(source); - source.Reply(" "); - source.Reply(_("Turns Chanstats statistics ON or OFF.")); - return true; - } -}; - -class CommandNSSASetChanstats : public CommandNSSetChanstats -{ - public: - CommandNSSASetChanstats(Module *creator) : CommandNSSetChanstats(creator, "nickserv/saset/chanstats", 2) - { - this->ClearSyntax(); - this->SetSyntax(_("\037nickname\037 {ON | OFF}")); - } - - void Execute(CommandSource &source, const std::vector ¶ms) anope_override - { - this->Run(source, params[0], params[1]); - } - - bool OnHelp(CommandSource &source, const Anope::string &) anope_override - { - this->SendSyntax(source); - source.Reply(" "); - source.Reply(_("Turns chanstats channel statistics ON or OFF for this user.")); - return true; - } -}; - class CommandNSSetDisplay : public Command { public: @@ -1048,9 +973,6 @@ class NSSet : public Module CommandNSSetAutoOp commandnssetautoop; CommandNSSASetAutoOp commandnssasetautoop; - CommandNSSetChanstats commandnssetchanstats; - CommandNSSASetChanstats commandnssasetchanstats; - CommandNSSetDisplay commandnssetdisplay; CommandNSSASetDisplay commandnssasetdisplay; @@ -1074,7 +996,7 @@ class NSSet : public Module CommandNSSASetNoexpire commandnssasetnoexpire; - SerializableExtensibleItem autoop, chanstats, killprotect, kill_quick, kill_immed, + SerializableExtensibleItem autoop, killprotect, kill_quick, kill_immed, message, secure, noexpire; /* email, passcode */ @@ -1084,7 +1006,6 @@ class NSSet : public Module NSSet(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), commandnsset(this), commandnssaset(this), commandnssetautoop(this), commandnssasetautoop(this), - commandnssetchanstats(this), commandnssasetchanstats(this), commandnssetdisplay(this), commandnssasetdisplay(this), commandnssetemail(this), commandnssasetemail(this), commandnssetkill(this), commandnssasetkill(this), @@ -1094,7 +1015,7 @@ class NSSet : public Module commandnssetsecure(this), commandnssasetsecure(this), commandnssasetnoexpire(this), - autoop(this, "AUTOOP"), chanstats(this, "NS_STATS"), killprotect(this, "KILLPROTECT"), + autoop(this, "AUTOOP"), killprotect(this, "KILLPROTECT"), kill_quick(this, "KILL_QUICK"), kill_immed(this, "KILL_IMMED"), message(this, "MSG"), secure(this, "NS_SECURE"), noexpire(this, "NS_NO_EXPIRE"), @@ -1154,8 +1075,6 @@ class NSSet : public Module info.AddOption(_("Message mode")); if (autoop.HasExt(na->nc)) info.AddOption(_("Auto-op")); - if (chanstats.HasExt(na->nc)) - info.AddOption(_("Chanstats")); if (noexpire.HasExt(na)) info.AddOption(_("No expire")); } diff --git a/modules/extra/m_chanstats.cpp b/modules/extra/m_chanstats.cpp deleted file mode 100644 index 4ff4d55e3..000000000 --- a/modules/extra/m_chanstats.cpp +++ /dev/null @@ -1,464 +0,0 @@ -#include "module.h" -#include "modules/sql.h" - -class MySQLInterface : public SQL::Interface -{ - public: - MySQLInterface(Module *o) : SQL::Interface(o) { } - - void OnResult(const SQL::Result &r) anope_override - { - } - - void OnError(const SQL::Result &r) anope_override - { - if (!r.GetQuery().query.empty()) - Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError(); - else - Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError(); - } -}; - -class MChanstats : public Module -{ - ServiceReference sql; - MySQLInterface sqlinterface; - SQL::Query query; - Anope::string SmileysHappy, SmileysSad, SmileysOther, prefix; - std::vector TableList, ProcedureList, EventList; - - void RunQuery(const SQL::Query &q) - { - if (sql) - sql->Run(&sqlinterface, q); - } - - size_t CountWords(const Anope::string &msg) - { - size_t words = 0; - for (size_t pos = 0; pos != Anope::string::npos; pos = msg.find(" ", pos+1)) - words++; - return words; - } - size_t CountSmileys(const Anope::string &msg, const Anope::string &smileylist) - { - size_t smileys = 0; - spacesepstream sep(smileylist); - Anope::string buf; - - while (sep.GetToken(buf) && !buf.empty()) - { - for (size_t pos = msg.find(buf, 0); pos != Anope::string::npos; pos = msg.find(buf, pos+1)) - smileys++; - } - return smileys; - } - - const Anope::string GetDisplay(User *u) - { - if (u && u->Account() && u->Account()->HasExt("NS_STATS")) - return u->Account()->display; - else - return ""; - } - - void GetTables() - { - TableList.clear(); - ProcedureList.clear(); - EventList.clear(); - if (!sql) - return; - - SQL::Result r = this->sql->RunQuery(this->sql->GetTables(prefix)); - for (int i = 0; i < r.Rows(); ++i) - { - const std::map &map = r.Row(i); - for (std::map::const_iterator it = map.begin(); it != map.end(); ++it) - TableList.push_back(it->second); - } - query = "SHOW PROCEDURE STATUS WHERE `Db` = Database();"; - r = this->sql->RunQuery(query); - for (int i = 0; i < r.Rows(); ++i) - { - ProcedureList.push_back(r.Get(i, "Name")); - } - query = "SHOW EVENTS WHERE `Db` = Database();"; - r = this->sql->RunQuery(query); - for (int i = 0; i < r.Rows(); ++i) - { - EventList.push_back(r.Get(i, "Name")); - } - } - - bool HasTable(const Anope::string &table) - { - for (std::vector::const_iterator it = TableList.begin(); it != TableList.end(); ++it) - if (*it == table) - return true; - return false; - } - - bool HasProcedure(const Anope::string &table) - { - for (std::vector::const_iterator it = ProcedureList.begin(); it != ProcedureList.end(); ++it) - if (*it == table) - return true; - return false; - } - - bool HasEvent(const Anope::string &table) - { - for (std::vector::const_iterator it = EventList.begin(); it != EventList.end(); ++it) - if (*it == table) - return true; - return false; - } - - - void CheckTables() - { - this->GetTables(); - if (!this->HasTable(prefix +"chanstats")) - { - query = "CREATE TABLE `" + prefix + "chanstats` (" - "`id` int(11) NOT NULL AUTO_INCREMENT," - "`chan` varchar(255) NOT NULL DEFAULT ''," - "`nick` varchar(255) NOT NULL DEFAULT ''," - "`type` ENUM('total', 'monthly', 'weekly', 'daily') NOT NULL," - "`letters` int(10) unsigned NOT NULL DEFAULT '0'," - "`words` int(10) unsigned NOT NULL DEFAULT '0'," - "`line` int(10) unsigned NOT NULL DEFAULT '0'," - "`actions` int(10) unsigned NOT NULL DEFAULT '0'," - "`smileys_happy` int(10) unsigned NOT NULL DEFAULT '0'," - "`smileys_sad` int(10) unsigned NOT NULL DEFAULT '0'," - "`smileys_other` int(10) unsigned NOT NULL DEFAULT '0'," - "`kicks` int(10) unsigned NOT NULL DEFAULT '0'," - "`kicked` int(10) unsigned NOT NULL DEFAULT '0'," - "`modes` int(10) unsigned NOT NULL DEFAULT '0'," - "`topics` int(10) unsigned NOT NULL DEFAULT '0'," - "`time0` int(10) unsigned NOT NULL default '0'," - "`time1` int(10) unsigned NOT NULL default '0'," - "`time2` int(10) unsigned NOT NULL default '0'," - "`time3` int(10) unsigned NOT NULL default '0'," - "`time4` int(10) unsigned NOT NULL default '0'," - "`time5` int(10) unsigned NOT NULL default '0'," - "`time6` int(10) unsigned NOT NULL default '0'," - "`time7` int(10) unsigned NOT NULL default '0'," - "`time8` int(10) unsigned NOT NULL default '0'," - "`time9` int(10) unsigned NOT NULL default '0'," - "`time10` int(10) unsigned NOT NULL default '0'," - "`time11` int(10) unsigned NOT NULL default '0'," - "`time12` int(10) unsigned NOT NULL default '0'," - "`time13` int(10) unsigned NOT NULL default '0'," - "`time14` int(10) unsigned NOT NULL default '0'," - "`time15` int(10) unsigned NOT NULL default '0'," - "`time16` int(10) unsigned NOT NULL default '0'," - "`time17` int(10) unsigned NOT NULL default '0'," - "`time18` int(10) unsigned NOT NULL default '0'," - "`time19` int(10) unsigned NOT NULL default '0'," - "`time20` int(10) unsigned NOT NULL default '0'," - "`time21` int(10) unsigned NOT NULL default '0'," - "`time22` int(10) unsigned NOT NULL default '0'," - "`time23` int(10) unsigned NOT NULL default '0'," - "PRIMARY KEY (`id`)," - "UNIQUE KEY `chan` (`chan`,`nick`,`type`)," - "KEY `nick` (`nick`)," - "KEY `chan_` (`chan`)," - "KEY `type` (`type`)" - ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - this->RunQuery(query); - } - /* There is no CREATE OR REPLACE PROCEDURE in MySQL */ - if (this->HasProcedure(prefix + "chanstats_proc_update")) - { - query = "DROP PROCEDURE " + prefix + "chanstats_proc_update"; - this->RunQuery(query); - } - query = "CREATE PROCEDURE `" + prefix + "chanstats_proc_update`" - "(chan_ VARCHAR(255), nick_ VARCHAR(255), line_ INT(10), letters_ INT(10)," - "words_ INT(10), actions_ INT(10), sm_h_ INT(10), sm_s_ INT(10), sm_o_ INT(10)," - "kicks_ INT(10), kicked_ INT(10), modes_ INT(10), topics_ INT(10))" - "BEGIN " - "DECLARE time_ VARCHAR(20);" - "SET time_ = CONCAT('time', hour(now()));" - "INSERT IGNORE INTO `" + prefix + "chanstats` (`nick`,`chan`, `type`) VALUES " - "('', chan_, 'total'), ('', chan_, 'monthly')," - "('', chan_, 'weekly'), ('', chan_, 'daily');" - "IF nick_ != '' THEN " - "INSERT IGNORE INTO `" + prefix + "chanstats` (`nick`,`chan`, `type`) VALUES " - "(nick_, chan_, 'total'), (nick_, chan_, 'monthly')," - "(nick_, chan_, 'weekly'),(nick_, chan_, 'daily')," - "(nick_, '', 'total'), (nick_, '', 'monthly')," - "(nick_, '', 'weekly'), (nick_, '', 'daily');" - "END IF;" - "SET @update_query = CONCAT('UPDATE `" + prefix + "chanstats` SET line=line+', line_, '," - "letters=letters+', letters_, ' , words=words+', words_, ', actions=actions+', actions_, ', " - "smileys_happy=smileys_happy+', sm_h_, ', smileys_sad=smileys_sad+', sm_s_, ', " - "smileys_other=smileys_other+', sm_o_, ', kicks=kicks+', kicks_, ', kicked=kicked+', kicked_, ', " - "modes=modes+', modes_, ', topics=topics+', topics_, ', ', time_ , '=', time_, '+', line_ ,' " - "WHERE (nick='''' OR nick=''', nick_, ''') AND (chan='''' OR chan=''', chan_, ''')');" - "PREPARE update_query FROM @update_query;" - "EXECUTE update_query;" - "DEALLOCATE PREPARE update_query;" - "END"; - this->RunQuery(query); - - if (this->HasProcedure(prefix + "chanstats_proc_chgdisplay")) - { - query = "DROP PROCEDURE " + prefix + "chanstats_proc_chgdisplay;"; - this->RunQuery(query); - } - query = "CREATE PROCEDURE `" + prefix + "chanstats_proc_chgdisplay`" - "(old_nick varchar(255), new_nick varchar(255))" - "BEGIN " - "DECLARE res_count int(10) unsigned;" - "SELECT COUNT(nick) INTO res_count FROM `" + prefix + "chanstats` WHERE nick = new_nick;" - "IF res_count = 0 THEN " - "UPDATE `" + prefix + "chanstats` SET `nick` = new_nick WHERE `nick` = old_nick;" - "ELSE " - "my_cursor: BEGIN " - "DECLARE no_more_rows BOOLEAN DEFAULT FALSE;" - "DECLARE chan_ VARCHAR(255);" - "DECLARE type_ ENUM('total', 'monthly', 'weekly', 'daily');" - "DECLARE letters_, words_, line_, actions_, smileys_happy_," - "smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_," - "time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_, time9_," - "time10_, time11_, time12_, time13_, time14_, time15_, time16_, time17_, time18_," - "time19_, time20_, time21_, time22_, time23_ INT(10) unsigned;" - "DECLARE stats_cursor CURSOR FOR " - "SELECT chan, type, letters, words, line, actions, smileys_happy," - "smileys_sad, smileys_other, kicks, kicked, modes, topics, time0, time1," - "time2, time3, time4, time5, time6, time7, time8, time9, time10, time11," - "time12, time13, time14, time15, time16, time17, time18, time19, time20," - "time21, time22, time23 " - "FROM `" + prefix + "chanstats` " - "WHERE `nick` = old_nick;" - "DECLARE CONTINUE HANDLER FOR NOT FOUND " - "SET no_more_rows = TRUE;" - "OPEN stats_cursor;" - "the_loop: LOOP " - "FETCH stats_cursor " - "INTO chan_, type_, letters_, words_, line_, actions_, smileys_happy_," - "smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_," - "time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_," - "time9_, time10_, time11_, time12_, time13_, time14_, time15_, time16_," - "time17_, time18_, time19_, time20_, time21_, time22_, time23_;" - "IF no_more_rows THEN " - "CLOSE stats_cursor;" - "LEAVE the_loop;" - "END IF;" - "INSERT INTO `" + prefix + "chanstats` " - "(chan, nick, type, letters, words, line, actions, smileys_happy, " - "smileys_sad, smileys_other, kicks, kicked, modes, topics, time0, time1, " - "time2, time3, time4, time5, time6, time7, time8, time9, time10, time11," - "time12, time13, time14, time15, time16, time17, time18, time19, time20," - "time21, time22, time23)" - "VALUES (chan_, new_nick, type_, letters_, words_, line_, actions_, smileys_happy_," - "smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_," - "time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_, " - "time9_, time10_, time11_, time12_, time13_, time14_, time15_, time16_, " - "time17_, time18_, time19_, time20_, time21_, time22_, time23_)" - "ON DUPLICATE KEY UPDATE letters=letters+VALUES(letters), words=words+VALUES(words)," - "line=line+VALUES(line), actions=actions+VALUES(actions)," - "smileys_happy=smileys_happy+VALUES(smileys_happy)," - "smileys_sad=smileys_sad+VALUES(smileys_sad)," - "smileys_other=smileys_other+VALUES(smileys_other)," - "kicks=kicks+VALUES(kicks), kicked=kicked+VALUES(kicked)," - "modes=modes+VALUES(modes), topics=topics+VALUES(topics)," - "time1=time1+VALUES(time1), time2=time2+VALUES(time2), time3=time3+VALUES(time3)," - "time4=time4+VALUES(time4), time5=time5+VALUES(time5), time6=time6+VALUES(time6)," - "time7=time7+VALUES(time7), time8=time8+VALUES(time8), time9=time9+VALUES(time9)," - "time10=time10+VALUES(time10), time11=time11+VALUES(time11), time12=time12+VALUES(time12)," - "time13=time13+VALUES(time13), time14=time14+VALUES(time14), time15=time15+VALUES(time15)," - "time16=time16+VALUES(time16), time17=time17+VALUES(time17), time18=time18+VALUES(time18)," - "time19=time19+VALUES(time19), time20=time20+VALUES(time20), time21=time21+VALUES(time21)," - "time22=time22+VALUES(time22), time23=time23+VALUES(time23);" - "END LOOP;" - "DELETE FROM `" + prefix + "chanstats` WHERE `nick` = old_nick;" - "END my_cursor;" - "END IF;" - "END;"; - this->RunQuery(query); - - /* dont prepend any database prefix to events so we can always delete/change old events */ - if (this->HasEvent("chanstats_event_cleanup_daily")) - { - query = "DROP EVENT chanstats_event_cleanup_daily"; - this->RunQuery(query); - } - query = "CREATE EVENT `chanstats_event_cleanup_daily` " - "ON SCHEDULE EVERY 1 DAY STARTS CURRENT_DATE " - "DO UPDATE `" + prefix + "chanstats` SET letters=0, words=0, line=0, actions=0, smileys_happy=0," - "smileys_sad=0, smileys_other=0, kicks=0, modes=0, topics=0, time0=0, time1=0, time2=0," - "time3=0, time4=0, time5=0, time6=0, time7=0, time8=0, time9=0, time10=0, time11=0," - "time12=0, time13=0, time14=0, time15=0, time16=0, time17=0, time18=0, time19=0," - "time20=0, time21=0, time22=0, time23=0 " - "WHERE type='daily';"; - this->RunQuery(query); - - if (this->HasEvent("chanstats_event_cleanup_weekly")) - { - query = "DROP EVENT `chanstats_event_cleanup_weekly`"; - this->RunQuery(query); - } - query = "CREATE EVENT `chanstats_event_cleanup_weekly` " - "ON SCHEDULE EVERY 1 WEEK STARTS (DATE(CURRENT_TIMESTAMP)-WEEKDAY(CURRENT_TIMESTAMP)) " - "DO UPDATE `" + prefix + "chanstats` SET letters=0, words=0, line=0, actions=0, smileys_happy=0," - "smileys_sad=0, smileys_other=0, kicks=0, modes=0, topics=0, time0=0, time1=0, time2=0," - "time3=0, time4=0, time5=0, time6=0, time7=0, time8=0, time9=0, time10=0, time11=0," - "time12=0, time13=0, time14=0, time15=0, time16=0, time17=0, time18=0, time19=0," - "time20=0, time21=0, time22=0, time23=0 " - "WHERE type='weekly';"; - this->RunQuery(query); - - if (this->HasEvent("chanstats_event_cleanup_monthly")) - { - query = "DROP EVENT `chanstats_event_cleanup_monthly`;"; - this->RunQuery(query); - } - query = "CREATE EVENT `chanstats_event_cleanup_monthly` " - "ON SCHEDULE EVERY 1 MONTH STARTS LAST_DAY(CURRENT_TIMESTAMP) + INTERVAL 1 DAY " - "DO BEGIN " - "UPDATE `" + prefix + "chanstats` SET letters=0, words=0, line=0, actions=0, smileys_happy=0," - "smileys_sad=0, smileys_other=0, kicks=0, modes=0, topics=0, time0=0, time1=0, time2=0," - "time3=0, time4=0, time5=0, time6=0, time7=0, time8=0, time9=0, time10=0, time11=0," - "time12=0, time13=0, time14=0, time15=0, time16=0, time17=0, time18=0, time19=0, " - "time20=0, time21=0, time22=0, time23=0 " - "WHERE type='monthly';" - "OPTIMIZE TABLE `" + prefix + "chanstats`;" - "END;"; - this->RunQuery(query); - } - - - public: - MChanstats(const Anope::string &modname, const Anope::string &creator) : - Module(modname, creator, EXTRA | VENDOR), sql("", ""), sqlinterface(this) - { - } - - void OnReload(Configuration::Conf *conf) anope_override - { - Configuration::Block *block = conf->GetModule(this); - prefix = block->Get("prefix", "anope_"); - SmileysHappy = block->Get("SmileysHappy"); - SmileysSad = block->Get("SmileysSad"); - SmileysOther = block->Get("SmileysOther"); - - Anope::string engine = block->Get("engine"); - this->sql = ServiceReference("SQL::Provider", engine); - if (sql) - this->CheckTables(); - else - Log(this) << "no database connection to " << engine; - } - - void OnTopicUpdated(Channel *c, const Anope::string &user, const Anope::string &topic) anope_override - { - User *u = User::Find(user); - if (!u || !u->Account() || !c->ci || !c->ci->HasExt("CS_STATS")) - return; - query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);"; - query.SetValue("channel", c->name); - query.SetValue("nick", GetDisplay(u)); - this->RunQuery(query); - } - - EventReturn OnChannelModeSet(Channel *c, MessageSource &setter, ChannelMode *mode, const Anope::string ¶m) anope_override - { - this->OnModeChange(c, setter.GetUser()); - return EVENT_CONTINUE; - } - - EventReturn OnChannelModeUnset(Channel *c, MessageSource &setter, ChannelMode *, const Anope::string ¶m) anope_override - { - this->OnModeChange(c, setter.GetUser()); - return EVENT_CONTINUE; - } - - private: - void OnModeChange(Channel *c, User *u) - { - if (!u || !u->Account() || !c->ci || !c->ci->HasExt("CS_STATS")) - return; - - query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0);"; - query.SetValue("channel", c->name); - query.SetValue("nick", GetDisplay(u)); - this->RunQuery(query); - } - public: - void OnPreUserKicked(MessageSource &source, ChanUserContainer *cu, const Anope::string &kickmsg) anope_override - { - if (!cu->chan->ci || !cu->chan->ci->HasExt("CS_STATS")) - return; - - query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0);"; - query.SetValue("channel", cu->chan->name); - query.SetValue("nick", GetDisplay(cu->user)); - this->RunQuery(query); - - query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0);"; - query.SetValue("channel", cu->chan->name); - query.SetValue("nick", GetDisplay(source.GetUser())); - this->RunQuery(query); - } - void OnPrivmsg(User *u, Channel *c, Anope::string &msg) anope_override - { - if (!c->ci || !c->ci->HasExt("CS_STATS")) - return; - - size_t letters = msg.length(); - size_t words = this->CountWords(msg); - - size_t action = 0; - if (msg.find("\01ACTION")!=Anope::string::npos) - { - action = 1; - letters = letters - 7; - words--; - } - - // count smileys - size_t smileys_happy = CountSmileys(msg, SmileysHappy); - size_t smileys_sad = CountSmileys(msg, SmileysSad); - size_t smileys_other = CountSmileys(msg, SmileysOther); - - // do not count smileys as words - words = words - smileys_happy - smileys_sad - smileys_other; - query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 1, @letters@, @words@, @action@, " - "@smileys_happy@, @smileys_sad@, @smileys_other@, '0', '0', '0', '0');"; - query.SetValue("channel", c->name); - query.SetValue("nick", GetDisplay(u)); - query.SetValue("letters", letters); - query.SetValue("words", words); - query.SetValue("action", action); - query.SetValue("smileys_happy", smileys_happy); - query.SetValue("smileys_sad", smileys_sad); - query.SetValue("smileys_other", smileys_other); - this->RunQuery(query); - } - void OnDelCore(NickCore *nc) anope_override - { - query = "DELETE FROM `" + prefix + "chanstats` WHERE `nick` = @nick@;"; - query.SetValue("nick", nc->display); - this->RunQuery(query); - } - void OnChangeCoreDisplay(NickCore *nc, const Anope::string &newdisplay) anope_override - { - query = "CALL " + prefix + "chanstats_proc_chgdisplay(@old_display@, @new_display@);"; - query.SetValue("old_display", nc->display); - query.SetValue("new_display", newdisplay); - this->RunQuery(query); - } - void OnDelChan(ChannelInfo *ci) anope_override - { - query = "DELETE FROM `" + prefix + "chanstats` WHERE `chan` = @channel@;"; - query.SetValue("channel", ci->name); - this->RunQuery(query); - } -}; - -MODULE_INIT(MChanstats) - diff --git a/modules/stats/cs_fantasy_stats.cpp b/modules/stats/cs_fantasy_stats.cpp new file mode 100644 index 000000000..d54ed4a92 --- /dev/null +++ b/modules/stats/cs_fantasy_stats.cpp @@ -0,0 +1,158 @@ +/* Chanstats core functions + * + * (C) 2003-2013 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" +#include "modules/sql.h" + +class MySQLInterface : public SQL::Interface +{ + public: + MySQLInterface(Module *o) : SQL::Interface(o) { } + + void OnResult(const SQL::Result &r) anope_override + { + } + + void OnError(const SQL::Result &r) anope_override + { + if (!r.GetQuery().query.empty()) + Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError(); + else + Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError(); + } +}; + + +class CommandCSStats : public Command +{ + public: + CommandCSStats(Module *creator) : Command (creator, "chanserv/stats", 0, 2) + { + this->SetDesc(_("Displays your Channel Stats")); + this->SetSyntax(_("\037nick\037")); + } + + void Execute(CommandSource &source, const std::vector ¶ms); +}; + +class CommandCSGStats : public Command +{ + public: + CommandCSGStats(Module *creator) : Command (creator, "chanserv/gstats", 0, 2) + { + this->SetDesc(_("Displays your Global Stats")); + this->SetSyntax(_("\037nick\037")); + } + + void Execute(CommandSource &source, const std::vector ¶ms); +}; + + +class CSStats; +static CSStats *me; +class CSStats : public Module +{ + CommandCSStats commandcsstats; + CommandCSGStats commandcsgstats; + ServiceReference sql; + MySQLInterface sqlinterface; + Anope::string prefix; + public: + CSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandcsstats(this), commandcsgstats(this), sql("", ""), sqlinterface(this) + { + me = this; + + } + + void OnReload(Configuration::Conf *conf) anope_override + { + prefix = conf->GetModule(this)->Get("prefix"); + if (prefix.empty()) + prefix = "anope_"; + this->sql = ServiceReference("SQL::Provider", conf->GetModule(this)->Get("engine")); + } + + SQL::Result RunQuery(const SQL::Query &query) + { + if (!this->sql) + throw SQL::Exception("Unable to locate SQL reference, is m_mysql loaded and configured correctly?"); + + SQL::Result res = this->sql->RunQuery(query); + if (!res.GetError().empty()) + throw SQL::Exception(res.GetError()); + return res; + } + + void DoStats(CommandSource &source, const bool is_global, const std::vector ¶ms) + { + if (!source.c) + return; + + Anope::string display; + if (params.empty()) + display = source.nc->display; + else if (const NickAlias *na = NickAlias::Find(params[0])) + display = na->nc->display; + else + { + source.Reply(_("%s not found."), params[0].c_str()); + return; + } + + try + { + SQL::Query query; + query = "SELECT letters, words, line, smileys_happy+smileys_sad+smileys_other as smileys," + "actions FROM `" + prefix + "chanstats` " + "WHERE `nick` = @nick@ AND `chan` = @channel@ AND `type` = 'total';"; + if (is_global) + query.SetValue("channel", ""); + else + query.SetValue("channel", source.c->ci->name); + query.SetValue("nick", display); + SQL::Result res = this->RunQuery(query); + + if (res.Rows() > 0) + { + if (is_global) + source.Reply(_("Network stats for %s:"), display.c_str()); + else + source.Reply(_("Channel stats for %s on %s:"), display.c_str(), source.c->name.c_str()); + + source.Reply(_("letters: %s, words: %s, lines: %s, smileys %s, actions: %s"), + res.Get(0, "letters").c_str(), res.Get(0, "words").c_str(), + res.Get(0, "line").c_str(), res.Get(0, "smileys").c_str(), + res.Get(0, "actions").c_str()); + } + else + source.Reply(_("No stats for %s."), display.c_str()); + } + catch (const SQL::Exception &ex) + { + Log(LOG_DEBUG) << ex.GetReason(); + } + + } +}; + +void CommandCSStats::Execute(CommandSource &source, const std::vector ¶ms) +{ + me->DoStats(source, false, params); +} + +void CommandCSGStats::Execute(CommandSource &source, const std::vector ¶ms) +{ + me->DoStats(source, true, params); +} + +MODULE_INIT(CSStats) + diff --git a/modules/stats/cs_fantasy_top.cpp b/modules/stats/cs_fantasy_top.cpp new file mode 100644 index 000000000..bc08c4c9d --- /dev/null +++ b/modules/stats/cs_fantasy_top.cpp @@ -0,0 +1,195 @@ +/* Chanstats core functions + * + * (C) 2003-2013 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" +#include "modules/sql.h" + +class MySQLInterface : public SQL::Interface +{ + public: + MySQLInterface(Module *o) : SQL::Interface(o) { } + + void OnResult(const SQL::Result &r) anope_override + { + } + + void OnError(const SQL::Result &r) anope_override + { + if (!r.GetQuery().query.empty()) + Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError(); + else + Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError(); + } +}; + +class CommandCSTop : public Command +{ + public: + CommandCSTop(Module *creator) : Command (creator, "chanserv/top", 0, 2) + { + this->SetDesc(_("Displays the top 3 users of a channel")); + this->SetSyntax(_("\037channel\037")); + } + + void Execute(CommandSource &source, const std::vector ¶ms); +}; + +class CommandCSTop10 : public Command +{ + public: + CommandCSTop10(Module *creator) : Command (creator, "chanserv/top10", 0, 2) + { + this->SetDesc(_("Displays the top 10 users of a channel")); + this->SetSyntax(_("\037channel\037")); + } + + void Execute(CommandSource &source, const std::vector ¶ms); +}; + +class CommandCSGTop : public Command +{ + public: + CommandCSGTop(Module *creator) : Command (creator, "chanserv/gtop", 0, 1) + { + this->SetDesc(_("Displays the top 3 users of the network")); + } + + void Execute(CommandSource &source, const std::vector ¶ms); +}; + +class CommandCSGTop10 : public Command +{ + public: + CommandCSGTop10(Module *creator) : Command (creator, "chanserv/gtop10", 0, 1) + { + this->SetDesc(_("Displays the top 10 users of the network")); + } + + void Execute(CommandSource &source, const std::vector ¶ms); +}; + + +class CSTop; +static CSTop *me; +class CSTop : public Module +{ + CommandCSTop commandcstop; + CommandCSGTop commandcsgtop; + CommandCSTop10 commandcstop10; + CommandCSGTop10 commandcsgtop10; + ServiceReference sql; + MySQLInterface sqlinterface; + Anope::string prefix; + + public: + CSTop(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandcstop(this), commandcsgtop(this), commandcstop10(this), commandcsgtop10(this), sql("", ""), + sqlinterface(this) + { + me = this; + + } + + void OnReload(Configuration::Conf *conf) anope_override + { + prefix = conf->GetModule(this)->Get("prefix"); + if (prefix.empty()) + prefix = "anope_"; + this->sql = ServiceReference("SQL::Provider", conf->GetModule(this)->Get("engine")); + } + + SQL::Result RunQuery(const SQL::Query &query) + { + if (!this->sql) + throw SQL::Exception("Unable to locate SQL reference, is m_mysql loaded and configured correctly?"); + + SQL::Result res = sql->RunQuery(query); + if (!res.GetError().empty()) + throw SQL::Exception(res.GetError()); + return res; + } + + void DoTop(CommandSource &source, const std::vector ¶ms, bool is_global, int limit = 1) + { + if (!source.c || !source.c->ci) + return; + + Anope::string channel; + if (is_global || params.empty()) + channel = source.c->ci->name; + else if (!params.empty()) + channel = params[0]; + else + { + source.Reply(_("%s not found."), params[0].c_str()); + return; + } + + try + { + SQL::Query query; + query = "SELECT nick, letters, words, line, actions," + "smileys_happy+smileys_sad+smileys_other as smileys " + "FROM `" + prefix + "chanstats` " + "WHERE `nick` != '' AND `chan` = @channel@ AND `type` = 'total' " + "ORDER BY `letters` DESC LIMIT @limit@;"; + query.SetValue("limit", limit, false); + + if (is_global) + query.SetValue("channel", ""); + else + query.SetValue("channel", channel.c_str()); + + SQL::Result res = this->RunQuery(query); + + if (res.Rows() > 0) + { + source.Reply(_("Top %i of %s"), limit, (is_global ? "Network" : channel.c_str())); + for (int i = 0; i < res.Rows(); ++i) + { + source.Reply(_("%2lu \002%-16s\002 letters: %s, words: %s, lines: %s, smileys %s, actions: %s"), + i+1, res.Get(i, "nick").c_str(), res.Get(i, "letters").c_str(), + res.Get(i, "words").c_str(), res.Get(i, "line").c_str(), + res.Get(0, "smileys").c_str(), res.Get(0, "actions").c_str()); + } + } + else + source.Reply(_("No stats for %s."), is_global ? "Network" : channel.c_str()); + } + catch (const SQL::Exception &ex) + { + Log(LOG_DEBUG) << ex.GetReason(); + } + } +}; + +void CommandCSTop::Execute(CommandSource &source, const std::vector ¶ms) +{ + me->DoTop(source, params, false, 3); +} + +void CommandCSTop10::Execute(CommandSource &source, const std::vector ¶ms) +{ + me->DoTop(source, params, false, 10); +} + +void CommandCSGTop::Execute(CommandSource &source, const std::vector ¶ms) +{ + me->DoTop(source, params, true, 3); +} + +void CommandCSGTop10::Execute(CommandSource &source, const std::vector ¶ms) +{ + me->DoTop(source, params, true, 10); +} + + +MODULE_INIT(CSTop) diff --git a/modules/stats/m_chanstats.cpp b/modules/stats/m_chanstats.cpp new file mode 100644 index 000000000..810a0d5c2 --- /dev/null +++ b/modules/stats/m_chanstats.cpp @@ -0,0 +1,624 @@ +#include "module.h" +#include "modules/sql.h" + +class CommandCSSetChanstats : public Command +{ + public: + CommandCSSetChanstats(Module *creator) : Command(creator, "chanserv/set/chanstats", 2, 2) + { + this->SetDesc(_("Turn chanstat statistics on or off")); + this->SetSyntax(_("\037channel\037 {ON | OFF}")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) anope_override + { + ChannelInfo *ci = ChannelInfo::Find(params[0]); + if (!ci) + { + source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str()); + return; + } + + EventReturn MOD_RESULT; + FOREACH_RESULT(OnSetChannelOption, MOD_RESULT, (source, this, ci, params[1])); + if (MOD_RESULT == EVENT_STOP) + return; + + if (MOD_RESULT != EVENT_ALLOW && !source.AccessFor(ci).HasPriv("SET") && source.permission.empty() && !source.HasPriv("chanserv/administration")) + { + source.Reply(ACCESS_DENIED); + return; + } + + if (params[1].equals_ci("ON")) + { + ci->Extend("CS_STATS"); + source.Reply(_("Chanstats statistics are now enabled for this channel.")); + Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to enable chanstats"; + } + else if (params[1].equals_ci("OFF")) + { + Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to disable chanstats"; + ci->Shrink("CS_STATS"); + source.Reply(_("Chanstats statistics are now disabled for this channel.")); + } + else + this->OnSyntaxError(source, ""); + } + + bool OnHelp(CommandSource &source, const Anope::string &) anope_override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply("Turn Chanstats channel statistics ON or OFF."); + return true; + } +}; + +class CommandNSSetChanstats : public Command +{ + public: + CommandNSSetChanstats(Module *creator, const Anope::string &sname = "nickserv/set/chanstats", size_t min = 1 ) : Command(creator, sname, min, min + 1) + { + this->SetDesc(_("Turn chanstat statistic on or off")); + this->SetSyntax(_("{ON | OFF}")); + } + void Run(CommandSource &source, const Anope::string &user, const Anope::string ¶m) + { + NickAlias *na = NickAlias::Find(user); + if (!na) + { + source.Reply(NICK_X_NOT_REGISTERED, user.c_str()); + return; + } + + EventReturn MOD_RESULT; + FOREACH_RESULT(OnSetNickOption, MOD_RESULT, (source, this, na->nc, param)); + if (MOD_RESULT == EVENT_STOP) + return; + + if (param.equals_ci("ON")) + { + Log(na->nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to enable chanstats for " << na->nc->display; + na->nc->Extend("NS_STATS"); + source.Reply(_("Chanstat statistics are now enabled for your nick.")); + } + else if (param.equals_ci("OFF")) + { + Log(na->nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to disable chanstats for " << na->nc->display; + na->nc->Shrink("NS_STATS"); + source.Reply(_("Chanstat statistics are now disabled for your nick.")); + } + else + this->OnSyntaxError(source, "CHANSTATS"); + } + + void Execute(CommandSource &source, const std::vector ¶ms) anope_override + { + this->Run(source, source.nc->display, params[0]); + } + + bool OnHelp(CommandSource &source, const Anope::string &) anope_override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Turns Chanstats statistics ON or OFF.")); + return true; + } +}; + +class CommandNSSASetChanstats : public CommandNSSetChanstats +{ + public: + CommandNSSASetChanstats(Module *creator) : CommandNSSetChanstats(creator, "nickserv/saset/chanstats", 2) + { + this->ClearSyntax(); + this->SetSyntax(_("\037nickname\037 {ON | OFF}")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) anope_override + { + this->Run(source, params[0], params[1]); + } + + bool OnHelp(CommandSource &source, const Anope::string &) anope_override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Turns chanstats channel statistics ON or OFF for this user.")); + return true; + } +}; + +class MySQLInterface : public SQL::Interface +{ + public: + MySQLInterface(Module *o) : SQL::Interface(o) { } + + void OnResult(const SQL::Result &r) anope_override + { + } + + void OnError(const SQL::Result &r) anope_override + { + if (!r.GetQuery().query.empty()) + Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError(); + else + Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError(); + } +}; + +class MChanstats : public Module +{ + SerializableExtensibleItem cs_stats, ns_stats; + + CommandCSSetChanstats commandcssetchanstats; + + CommandNSSetChanstats commandnssetchanstats; + CommandNSSASetChanstats commandnssasetchanstats; + + ServiceReference sql; + MySQLInterface sqlinterface; + SQL::Query query; + Anope::string SmileysHappy, SmileysSad, SmileysOther, prefix; + std::vector TableList, ProcedureList, EventList; + + void RunQuery(const SQL::Query &q) + { + if (sql) + sql->Run(&sqlinterface, q); + } + + size_t CountWords(const Anope::string &msg) + { + size_t words = 0; + for (size_t pos = 0; pos != Anope::string::npos; pos = msg.find(" ", pos+1)) + words++; + return words; + } + size_t CountSmileys(const Anope::string &msg, const Anope::string &smileylist) + { + size_t smileys = 0; + spacesepstream sep(smileylist); + Anope::string buf; + + while (sep.GetToken(buf) && !buf.empty()) + { + for (size_t pos = msg.find(buf, 0); pos != Anope::string::npos; pos = msg.find(buf, pos+1)) + smileys++; + } + return smileys; + } + + const Anope::string GetDisplay(User *u) + { + if (u && u->Account() && ns_stats.HasExt(u->Account())) + return u->Account()->display; + else + return ""; + } + + void GetTables() + { + TableList.clear(); + ProcedureList.clear(); + EventList.clear(); + if (!sql) + return; + + SQL::Result r = this->sql->RunQuery(this->sql->GetTables(prefix)); + for (int i = 0; i < r.Rows(); ++i) + { + const std::map &map = r.Row(i); + for (std::map::const_iterator it = map.begin(); it != map.end(); ++it) + TableList.push_back(it->second); + } + query = "SHOW PROCEDURE STATUS WHERE `Db` = Database();"; + r = this->sql->RunQuery(query); + for (int i = 0; i < r.Rows(); ++i) + { + ProcedureList.push_back(r.Get(i, "Name")); + } + query = "SHOW EVENTS WHERE `Db` = Database();"; + r = this->sql->RunQuery(query); + for (int i = 0; i < r.Rows(); ++i) + { + EventList.push_back(r.Get(i, "Name")); + } + } + + bool HasTable(const Anope::string &table) + { + for (std::vector::const_iterator it = TableList.begin(); it != TableList.end(); ++it) + if (*it == table) + return true; + return false; + } + + bool HasProcedure(const Anope::string &table) + { + for (std::vector::const_iterator it = ProcedureList.begin(); it != ProcedureList.end(); ++it) + if (*it == table) + return true; + return false; + } + + bool HasEvent(const Anope::string &table) + { + for (std::vector::const_iterator it = EventList.begin(); it != EventList.end(); ++it) + if (*it == table) + return true; + return false; + } + + + void CheckTables() + { + this->GetTables(); + if (!this->HasTable(prefix +"chanstats")) + { + query = "CREATE TABLE `" + prefix + "chanstats` (" + "`id` int(11) NOT NULL AUTO_INCREMENT," + "`chan` varchar(255) NOT NULL DEFAULT ''," + "`nick` varchar(255) NOT NULL DEFAULT ''," + "`type` ENUM('total', 'monthly', 'weekly', 'daily') NOT NULL," + "`letters` int(10) unsigned NOT NULL DEFAULT '0'," + "`words` int(10) unsigned NOT NULL DEFAULT '0'," + "`line` int(10) unsigned NOT NULL DEFAULT '0'," + "`actions` int(10) unsigned NOT NULL DEFAULT '0'," + "`smileys_happy` int(10) unsigned NOT NULL DEFAULT '0'," + "`smileys_sad` int(10) unsigned NOT NULL DEFAULT '0'," + "`smileys_other` int(10) unsigned NOT NULL DEFAULT '0'," + "`kicks` int(10) unsigned NOT NULL DEFAULT '0'," + "`kicked` int(10) unsigned NOT NULL DEFAULT '0'," + "`modes` int(10) unsigned NOT NULL DEFAULT '0'," + "`topics` int(10) unsigned NOT NULL DEFAULT '0'," + "`time0` int(10) unsigned NOT NULL default '0'," + "`time1` int(10) unsigned NOT NULL default '0'," + "`time2` int(10) unsigned NOT NULL default '0'," + "`time3` int(10) unsigned NOT NULL default '0'," + "`time4` int(10) unsigned NOT NULL default '0'," + "`time5` int(10) unsigned NOT NULL default '0'," + "`time6` int(10) unsigned NOT NULL default '0'," + "`time7` int(10) unsigned NOT NULL default '0'," + "`time8` int(10) unsigned NOT NULL default '0'," + "`time9` int(10) unsigned NOT NULL default '0'," + "`time10` int(10) unsigned NOT NULL default '0'," + "`time11` int(10) unsigned NOT NULL default '0'," + "`time12` int(10) unsigned NOT NULL default '0'," + "`time13` int(10) unsigned NOT NULL default '0'," + "`time14` int(10) unsigned NOT NULL default '0'," + "`time15` int(10) unsigned NOT NULL default '0'," + "`time16` int(10) unsigned NOT NULL default '0'," + "`time17` int(10) unsigned NOT NULL default '0'," + "`time18` int(10) unsigned NOT NULL default '0'," + "`time19` int(10) unsigned NOT NULL default '0'," + "`time20` int(10) unsigned NOT NULL default '0'," + "`time21` int(10) unsigned NOT NULL default '0'," + "`time22` int(10) unsigned NOT NULL default '0'," + "`time23` int(10) unsigned NOT NULL default '0'," + "PRIMARY KEY (`id`)," + "UNIQUE KEY `chan` (`chan`,`nick`,`type`)," + "KEY `nick` (`nick`)," + "KEY `chan_` (`chan`)," + "KEY `type` (`type`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + this->RunQuery(query); + } + /* There is no CREATE OR REPLACE PROCEDURE in MySQL */ + if (this->HasProcedure(prefix + "chanstats_proc_update")) + { + query = "DROP PROCEDURE " + prefix + "chanstats_proc_update"; + this->RunQuery(query); + } + query = "CREATE PROCEDURE `" + prefix + "chanstats_proc_update`" + "(chan_ VARCHAR(255), nick_ VARCHAR(255), line_ INT(10), letters_ INT(10)," + "words_ INT(10), actions_ INT(10), sm_h_ INT(10), sm_s_ INT(10), sm_o_ INT(10)," + "kicks_ INT(10), kicked_ INT(10), modes_ INT(10), topics_ INT(10))" + "BEGIN " + "DECLARE time_ VARCHAR(20);" + "SET time_ = CONCAT('time', hour(now()));" + "INSERT IGNORE INTO `" + prefix + "chanstats` (`nick`,`chan`, `type`) VALUES " + "('', chan_, 'total'), ('', chan_, 'monthly')," + "('', chan_, 'weekly'), ('', chan_, 'daily');" + "IF nick_ != '' THEN " + "INSERT IGNORE INTO `" + prefix + "chanstats` (`nick`,`chan`, `type`) VALUES " + "(nick_, chan_, 'total'), (nick_, chan_, 'monthly')," + "(nick_, chan_, 'weekly'),(nick_, chan_, 'daily')," + "(nick_, '', 'total'), (nick_, '', 'monthly')," + "(nick_, '', 'weekly'), (nick_, '', 'daily');" + "END IF;" + "SET @update_query = CONCAT('UPDATE `" + prefix + "chanstats` SET line=line+', line_, '," + "letters=letters+', letters_, ' , words=words+', words_, ', actions=actions+', actions_, ', " + "smileys_happy=smileys_happy+', sm_h_, ', smileys_sad=smileys_sad+', sm_s_, ', " + "smileys_other=smileys_other+', sm_o_, ', kicks=kicks+', kicks_, ', kicked=kicked+', kicked_, ', " + "modes=modes+', modes_, ', topics=topics+', topics_, ', ', time_ , '=', time_, '+', line_ ,' " + "WHERE (nick='''' OR nick=''', nick_, ''') AND (chan='''' OR chan=''', chan_, ''')');" + "PREPARE update_query FROM @update_query;" + "EXECUTE update_query;" + "DEALLOCATE PREPARE update_query;" + "END"; + this->RunQuery(query); + + if (this->HasProcedure(prefix + "chanstats_proc_chgdisplay")) + { + query = "DROP PROCEDURE " + prefix + "chanstats_proc_chgdisplay;"; + this->RunQuery(query); + } + query = "CREATE PROCEDURE `" + prefix + "chanstats_proc_chgdisplay`" + "(old_nick varchar(255), new_nick varchar(255))" + "BEGIN " + "DECLARE res_count int(10) unsigned;" + "SELECT COUNT(nick) INTO res_count FROM `" + prefix + "chanstats` WHERE nick = new_nick;" + "IF res_count = 0 THEN " + "UPDATE `" + prefix + "chanstats` SET `nick` = new_nick WHERE `nick` = old_nick;" + "ELSE " + "my_cursor: BEGIN " + "DECLARE no_more_rows BOOLEAN DEFAULT FALSE;" + "DECLARE chan_ VARCHAR(255);" + "DECLARE type_ ENUM('total', 'monthly', 'weekly', 'daily');" + "DECLARE letters_, words_, line_, actions_, smileys_happy_," + "smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_," + "time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_, time9_," + "time10_, time11_, time12_, time13_, time14_, time15_, time16_, time17_, time18_," + "time19_, time20_, time21_, time22_, time23_ INT(10) unsigned;" + "DECLARE stats_cursor CURSOR FOR " + "SELECT chan, type, letters, words, line, actions, smileys_happy," + "smileys_sad, smileys_other, kicks, kicked, modes, topics, time0, time1," + "time2, time3, time4, time5, time6, time7, time8, time9, time10, time11," + "time12, time13, time14, time15, time16, time17, time18, time19, time20," + "time21, time22, time23 " + "FROM `" + prefix + "chanstats` " + "WHERE `nick` = old_nick;" + "DECLARE CONTINUE HANDLER FOR NOT FOUND " + "SET no_more_rows = TRUE;" + "OPEN stats_cursor;" + "the_loop: LOOP " + "FETCH stats_cursor " + "INTO chan_, type_, letters_, words_, line_, actions_, smileys_happy_," + "smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_," + "time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_," + "time9_, time10_, time11_, time12_, time13_, time14_, time15_, time16_," + "time17_, time18_, time19_, time20_, time21_, time22_, time23_;" + "IF no_more_rows THEN " + "CLOSE stats_cursor;" + "LEAVE the_loop;" + "END IF;" + "INSERT INTO `" + prefix + "chanstats` " + "(chan, nick, type, letters, words, line, actions, smileys_happy, " + "smileys_sad, smileys_other, kicks, kicked, modes, topics, time0, time1, " + "time2, time3, time4, time5, time6, time7, time8, time9, time10, time11," + "time12, time13, time14, time15, time16, time17, time18, time19, time20," + "time21, time22, time23)" + "VALUES (chan_, new_nick, type_, letters_, words_, line_, actions_, smileys_happy_," + "smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_," + "time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_, " + "time9_, time10_, time11_, time12_, time13_, time14_, time15_, time16_, " + "time17_, time18_, time19_, time20_, time21_, time22_, time23_)" + "ON DUPLICATE KEY UPDATE letters=letters+VALUES(letters), words=words+VALUES(words)," + "line=line+VALUES(line), actions=actions+VALUES(actions)," + "smileys_happy=smileys_happy+VALUES(smileys_happy)," + "smileys_sad=smileys_sad+VALUES(smileys_sad)," + "smileys_other=smileys_other+VALUES(smileys_other)," + "kicks=kicks+VALUES(kicks), kicked=kicked+VALUES(kicked)," + "modes=modes+VALUES(modes), topics=topics+VALUES(topics)," + "time1=time1+VALUES(time1), time2=time2+VALUES(time2), time3=time3+VALUES(time3)," + "time4=time4+VALUES(time4), time5=time5+VALUES(time5), time6=time6+VALUES(time6)," + "time7=time7+VALUES(time7), time8=time8+VALUES(time8), time9=time9+VALUES(time9)," + "time10=time10+VALUES(time10), time11=time11+VALUES(time11), time12=time12+VALUES(time12)," + "time13=time13+VALUES(time13), time14=time14+VALUES(time14), time15=time15+VALUES(time15)," + "time16=time16+VALUES(time16), time17=time17+VALUES(time17), time18=time18+VALUES(time18)," + "time19=time19+VALUES(time19), time20=time20+VALUES(time20), time21=time21+VALUES(time21)," + "ti