summaryrefslogtreecommitdiff
path: root/modules/commands/cs_fantasy_stats.cpp
diff options
context:
space:
mode:
authorDukePyrolator <DukePyrolator@anope.org>2012-04-08 12:43:34 +0200
committerDukePyrolator <DukePyrolator@anope.org>2012-04-08 12:43:34 +0200
commitdeb5196101bb14d6656fb89b9ec9e5f8b96eb31d (patch)
tree733001e6fc4af6cb2a42915d586e448dde742f77 /modules/commands/cs_fantasy_stats.cpp
parent9e1fda2a44dee120e26acc6e51fbe779eea97712 (diff)
Added Chanstats. It uses a new, improved database format and is not compatible with current phpdenora or magirc installations.
Diffstat (limited to 'modules/commands/cs_fantasy_stats.cpp')
-rw-r--r--modules/commands/cs_fantasy_stats.cpp168
1 files changed, 168 insertions, 0 deletions
diff --git a/modules/commands/cs_fantasy_stats.cpp b/modules/commands/cs_fantasy_stats.cpp
new file mode 100644
index 000000000..7a4419e30
--- /dev/null
+++ b/modules/commands/cs_fantasy_stats.cpp
@@ -0,0 +1,168 @@
+/* Chanstats core functions
+ *
+ * (C) 2003-2012 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 "../extra/sql.h"
+
+class MySQLInterface : public SQLInterface
+{
+ public:
+ MySQLInterface(Module *o) : SQLInterface(o) { }
+
+ void OnResult(const SQLResult &r) anope_override
+ {
+ }
+
+ void OnError(const SQLResult &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->SetFlag(CFLAG_STRIP_CHANNEL);
+ this->SetDesc(_("Displays your Channel Stats"));
+ this->SetSyntax(_("\037nick\037"));
+ }
+
+ void Execute(CommandSource &source, const std::vector<Anope::string> &params);
+};
+
+class CommandCSGStats : public Command
+{
+ public:
+ CommandCSGStats(Module *creator) : Command (creator, "chanserv/gstats", 0, 2)
+ {
+ this->SetFlag(CFLAG_STRIP_CHANNEL);
+ this->SetDesc(_("Displays your Global Stats"));
+ this->SetSyntax(_("\037nick\037"));
+ }
+
+ void Execute(CommandSource &source, const std::vector<Anope::string> &params);
+};
+
+
+class CSStats;
+static CSStats *me;
+class CSStats : public Module
+{
+ CommandCSStats commandcsstats;
+ CommandCSGStats commandcsgstats;
+ service_reference<SQLProvider> sql;
+ MySQLInterface sqlinterface;
+ public:
+ CSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
+ commandcsstats(this), commandcsgstats(this), sql("", ""), sqlinterface(this)
+ {
+ me = this;
+ this->SetAuthor("Anope");
+
+ Implementation i[] = { I_OnReload };
+ ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
+ this->OnReload();
+ }
+
+ void OnReload() anope_override
+ {
+ ConfigReader config;
+ Anope::string engine = config.ReadValue("chanstats", "engine", "", 0);
+ this->sql = service_reference<SQLProvider>("SQLProvider", engine);
+ }
+
+ SQLResult RunQuery(const SQLQuery &query)
+ {
+ if (!this->sql)
+ throw SQLException("Unable to locate SQL reference, is m_mysql loaded and configured correctly?");
+
+ SQLResult res = this->sql->RunQuery(query);
+ if (!res.GetError().empty())
+ throw SQLException(res.GetError());
+ return res;
+ }
+
+ void DoStats(CommandSource &source, const bool is_global, const std::vector<Anope::string> &params)
+ {
+ if (!source.u || !source.c)
+ return;
+
+ Anope::string display;
+ if (params.empty())
+ display = source.u->Account()->display;
+ else if (NickAlias *na = findnick(params[0]))
+ display = na->nc->display;
+ else
+ {
+ source.Reply(_("%s not found."), params[0].c_str());
+ return;
+ }
+
+ try
+ {
+ SQLQuery query;
+ if (is_global)
+ query = "SELECT sum(letters) as letters, sum(words) as words, sum(line) as line,"
+ " sum(smileys) as smileys, sum(actions) as actions"
+ " FROM `anope_bs_chanstats_view_sum_all`"
+ " WHERE `nickserv_display` = @display@";
+ else
+ {
+ query = "SELECT letters, words, line, smileys, actions "
+ " FROM `anope_bs_chanstats_view_sum_all` "
+ " WHERE `nickserv_display` = @display@ AND `chanserv_name` = @channel@;";
+ query.setValue("channel", source.c->ci->name);
+ }
+ query.setValue("display", display);
+ SQLResult 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 SQLException &ex)
+ {
+ Log(LOG_DEBUG) << ex.GetReason();
+ }
+
+ }
+};
+
+void CommandCSStats::Execute(CommandSource &source, const std::vector<Anope::string> &params)
+{
+ me->DoStats(source, false, params);
+}
+
+void CommandCSGStats::Execute(CommandSource &source, const std::vector<Anope::string> &params)
+{
+ me->DoStats(source, true, params);
+}
+
+MODULE_INIT(CSStats) \ No newline at end of file