anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

cs_fantasy_top.cpp (5109B)

      1 /* Chanstats core functions
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #include "module.h"
     13 #include "modules/sql.h"
     14 
     15 class MySQLInterface : public SQL::Interface
     16 {
     17  public:
     18 	MySQLInterface(Module *o) : SQL::Interface(o) { }
     19 
     20 	void OnResult(const SQL::Result &r) anope_override
     21 	{
     22 	}
     23 
     24 	void OnError(const SQL::Result &r) anope_override
     25 	{
     26 		if (!r.GetQuery().query.empty())
     27 			Log(LOG_DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError();
     28 		else
     29 			Log(LOG_DEBUG) << "Chanstats: Error executing query: " << r.GetError();
     30 	}
     31 };
     32 
     33 class CommandCSTop : public Command
     34 {
     35  public:
     36 	CommandCSTop(Module *creator) : Command (creator, "chanserv/top", 0, 2)
     37 	{
     38 		this->SetDesc(_("Displays the top 3 users of a channel"));
     39 		this->SetSyntax(_("\037channel\037"));
     40 	}
     41 
     42 	void Execute(CommandSource &source, const std::vector<Anope::string> &params);
     43 };
     44 
     45 class CommandCSTop10 : public Command
     46 {
     47  public:
     48 	CommandCSTop10(Module *creator) : Command (creator, "chanserv/top10", 0, 2)
     49 	{
     50 		this->SetDesc(_("Displays the top 10 users of a channel"));
     51 		this->SetSyntax(_("\037channel\037"));
     52 	}
     53 
     54 	void Execute(CommandSource &source, const std::vector<Anope::string> &params);
     55 };
     56 
     57 class CommandCSGTop : public Command
     58 {
     59  public:
     60 	CommandCSGTop(Module *creator) : Command (creator, "chanserv/gtop", 0, 1)
     61 	{
     62 		this->SetDesc(_("Displays the top 3 users of the network"));
     63 	}
     64 
     65 	void Execute(CommandSource &source, const std::vector<Anope::string> &params);
     66 };
     67 
     68 class CommandCSGTop10 : public Command
     69 {
     70  public:
     71 	CommandCSGTop10(Module *creator) : Command (creator, "chanserv/gtop10", 0, 1)
     72 	{
     73 		this->SetDesc(_("Displays the top 10 users of the network"));
     74 	}
     75 
     76 	void Execute(CommandSource &source, const std::vector<Anope::string> &params);
     77 };
     78 
     79 
     80 class CSTop;
     81 static CSTop *me;
     82 class CSTop : public Module
     83 {
     84 	CommandCSTop commandcstop;
     85 	CommandCSGTop commandcsgtop;
     86 	CommandCSTop10 commandcstop10;
     87 	CommandCSGTop10 commandcsgtop10;
     88 	ServiceReference<SQL::Provider> sql;
     89 	MySQLInterface sqlinterface;
     90 	Anope::string prefix;
     91 
     92  public:
     93 	CSTop(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
     94 		commandcstop(this), commandcsgtop(this), commandcstop10(this), commandcsgtop10(this), sql("", ""),
     95 		sqlinterface(this)
     96 	{
     97 		me = this;
     98 
     99 	}
    100 
    101 	void OnReload(Configuration::Conf *conf) anope_override
    102 	{
    103 		prefix = conf->GetModule("m_chanstats")->Get<const Anope::string>("prefix", "anope_");
    104 		this->sql = ServiceReference<SQL::Provider>("SQL::Provider", conf->GetModule("m_chanstats")->Get<const Anope::string>("engine"));
    105 	}
    106 
    107 	SQL::Result RunQuery(const SQL::Query &query)
    108 	{
    109 		if (!this->sql)
    110 			throw SQL::Exception("Unable to locate SQL reference, is m_mysql loaded and configured correctly?");
    111 
    112 		SQL::Result res = sql->RunQuery(query);
    113 		if (!res.GetError().empty())
    114 			throw SQL::Exception(res.GetError());
    115 		return res;
    116 	}
    117 
    118 	void DoTop(CommandSource &source, const std::vector<Anope::string> &params, bool is_global, int limit = 1)
    119 	{
    120 
    121 		Anope::string channel;
    122 		if (!params.empty())
    123 			channel = params[0];
    124 		else if (source.c && source.c->ci)
    125 			channel = source.c->ci->name;
    126 
    127 		if (!is_global && channel.empty())
    128 			is_global = true;
    129 
    130 		try
    131 		{
    132 			SQL::Query query;
    133 			query = "SELECT nick, letters, words, line, actions,"
    134 				"smileys_happy+smileys_sad+smileys_other as smileys "
    135 				"FROM `" + prefix + "chanstats` "
    136 				"WHERE `nick` != '' AND `chan` = @channel@ AND `type` = 'total' "
    137 				"ORDER BY `letters` DESC LIMIT @limit@;";
    138 			query.SetValue("limit", limit, false);
    139 
    140 			if (is_global)
    141 				query.SetValue("channel", "");
    142 			else
    143 				query.SetValue("channel", channel.c_str());
    144 
    145 			SQL::Result res = this->RunQuery(query);
    146 
    147 			if (res.Rows() > 0)
    148 			{
    149 				source.Reply(_("Top %i of %s"), limit, (is_global ? "Network" : channel.c_str()));
    150 				for (int i = 0; i < res.Rows(); ++i)
    151 				{
    152 					source.Reply(_("%2lu \002%-16s\002 letters: %s, words: %s, lines: %s, smileys: %s, actions: %s"),
    153 						i+1, res.Get(i, "nick").c_str(), res.Get(i, "letters").c_str(),
    154 						res.Get(i, "words").c_str(), res.Get(i, "line").c_str(),
    155 						res.Get(i, "smileys").c_str(), res.Get(i, "actions").c_str());
    156 				}
    157 			}
    158 			else
    159 				source.Reply(_("No stats for %s."), is_global ? "Network" : channel.c_str());
    160 		}
    161 		catch (const SQL::Exception &ex)
    162 		{
    163 			Log(LOG_DEBUG) << ex.GetReason();
    164 		}
    165 	}
    166 };
    167 
    168 void CommandCSTop::Execute(CommandSource &source, const std::vector<Anope::string> &params)
    169 {
    170 	me->DoTop(source, params, false, 3);
    171 }
    172 
    173 void CommandCSTop10::Execute(CommandSource &source, const std::vector<Anope::string> &params)
    174 {
    175 	me->DoTop(source, params, false, 10);
    176 }
    177 
    178 void CommandCSGTop::Execute(CommandSource &source, const std::vector<Anope::string> &params)
    179 {
    180 	me->DoTop(source, params, true, 3);
    181 }
    182 
    183 void CommandCSGTop10::Execute(CommandSource &source, const std::vector<Anope::string> &params)
    184 {
    185 	me->DoTop(source, params, true, 10);
    186 }
    187 
    188 MODULE_INIT(CSTop)