mirror of
https://github.com/anope/anope.git
synced 2026-06-12 19:14:47 +02:00
203 lines
5.2 KiB
C++
203 lines
5.2 KiB
C++
// Anope IRC Services <https://www.anope.org/>
|
|
//
|
|
// Copyright (C) 2003-2026 Anope Contributors
|
|
//
|
|
// Anope is free software. You can use, modify, and/or distribute it under the
|
|
// terms of version 2 of the GNU General Public License. See docs/LICENSE.txt
|
|
// for the complete terms of this license and docs/AUTHORS.txt for a list of
|
|
// contributors.
|
|
//
|
|
// Based on the original code of Epona by Lara
|
|
// Based on the original code of Services by Andy Church
|
|
//
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#include "module.h"
|
|
#include "modules/sql.h"
|
|
|
|
class MySQLInterface final
|
|
: public SQL::Interface
|
|
{
|
|
public:
|
|
MySQLInterface(Module *o) : SQL::Interface(o) { }
|
|
|
|
void OnResult(const SQL::Result &r) override
|
|
{
|
|
}
|
|
|
|
void OnError(const SQL::Result &r) 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 final
|
|
: 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<Anope::string> ¶ms);
|
|
};
|
|
|
|
class CommandCSTop10 final
|
|
: 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<Anope::string> ¶ms);
|
|
};
|
|
|
|
class CommandCSGTop final
|
|
: 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<Anope::string> ¶ms);
|
|
};
|
|
|
|
class CommandCSGTop10 final
|
|
: 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<Anope::string> ¶ms);
|
|
};
|
|
|
|
|
|
class CSTop;
|
|
static CSTop *me;
|
|
class CSTop final
|
|
: public Module
|
|
{
|
|
CommandCSTop commandcstop;
|
|
CommandCSGTop commandcsgtop;
|
|
CommandCSTop10 commandcstop10;
|
|
CommandCSGTop10 commandcsgtop10;
|
|
ServiceReference<SQL::Provider> 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("SQL::Provider")
|
|
, sqlinterface(this)
|
|
{
|
|
me = this;
|
|
}
|
|
|
|
void OnReload(Configuration::Conf &conf) override
|
|
{
|
|
const auto &block = conf.GetModule("chanstats");
|
|
prefix = block.Get<const Anope::string>("prefix", "anope_");
|
|
this->sql.SetServiceName(block.Get<const Anope::string>("engine"));
|
|
}
|
|
|
|
SQL::Result RunQuery(const SQL::Query &query)
|
|
{
|
|
if (!this->sql)
|
|
throw SQL::Exception("Unable to locate SQL reference, is 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<Anope::string> ¶ms, bool is_global, int limit = 1)
|
|
{
|
|
|
|
Anope::string channel;
|
|
if (!params.empty())
|
|
channel = params[0];
|
|
else if (source.c && source.c->ci)
|
|
channel = source.c->ci->name;
|
|
|
|
if (!is_global && channel.empty())
|
|
is_global = true;
|
|
|
|
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(_("%2d \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(i, "smileys").c_str(), res.Get(i, "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<Anope::string> ¶ms)
|
|
{
|
|
me->DoTop(source, params, false, 3);
|
|
}
|
|
|
|
void CommandCSTop10::Execute(CommandSource &source, const std::vector<Anope::string> ¶ms)
|
|
{
|
|
me->DoTop(source, params, false, 10);
|
|
}
|
|
|
|
void CommandCSGTop::Execute(CommandSource &source, const std::vector<Anope::string> ¶ms)
|
|
{
|
|
me->DoTop(source, params, true, 3);
|
|
}
|
|
|
|
void CommandCSGTop10::Execute(CommandSource &source, const std::vector<Anope::string> ¶ms)
|
|
{
|
|
me->DoTop(source, params, true, 10);
|
|
}
|
|
|
|
MODULE_INIT(CSTop)
|