anope

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

cs_fantasy_stats.cpp (4499B)

      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 
     34 class CommandCSStats : public Command
     35 {
     36  public:
     37 	CommandCSStats(Module *creator) : Command (creator, "chanserv/stats", 0, 2)
     38 	{
     39 		this->SetDesc(_("Displays your Channel Stats"));
     40 		this->SetSyntax(_("[\037channel\037] [\037nick\037]"));
     41 	}
     42 
     43 	void Execute(CommandSource &source, const std::vector<Anope::string> &params);
     44 };
     45 
     46 class CommandCSGStats : public Command
     47 {
     48  public:
     49 	CommandCSGStats(Module *creator) : Command (creator, "chanserv/gstats", 0, 2)
     50 	{
     51 		this->SetDesc(_("Displays your Global Stats"));
     52 		this->SetSyntax(_("\037nick\037"));
     53 	}
     54 
     55 	void Execute(CommandSource &source, const std::vector<Anope::string> &params);
     56 };
     57 
     58 
     59 class CSStats;
     60 static CSStats *me;
     61 class CSStats : public Module
     62 {
     63 	CommandCSStats commandcsstats;
     64 	CommandCSGStats commandcsgstats;
     65 	ServiceReference<SQL::Provider> sql;
     66 	MySQLInterface sqlinterface;
     67 	Anope::string prefix;
     68  public:
     69 	CSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
     70 		commandcsstats(this), commandcsgstats(this), sql("", ""), sqlinterface(this)
     71 	{
     72 		me = this;
     73 
     74 	}
     75 
     76 	void OnReload(Configuration::Conf *conf) anope_override
     77 	{
     78 		prefix = conf->GetModule("m_chanstats")->Get<const Anope::string>("prefix", "anope_");
     79 		this->sql = ServiceReference<SQL::Provider>("SQL::Provider", conf->GetModule("m_chanstats")->Get<const Anope::string>("engine"));
     80 	}
     81 
     82 	SQL::Result RunQuery(const SQL::Query &query)
     83 	{
     84 		if (!this->sql)
     85 			throw SQL::Exception("Unable to locate SQL reference, is m_mysql loaded and configured correctly?");
     86 
     87 		SQL::Result res = this->sql->RunQuery(query);
     88 		if (!res.GetError().empty())
     89 			throw SQL::Exception(res.GetError());
     90 		return res;
     91 	}
     92 
     93 	void DoStats(CommandSource &source, const bool is_global, const std::vector<Anope::string> &params)
     94 	{
     95 		Anope::string display, channel;
     96 
     97 		/*
     98 		 * possible parameters are:
     99 		 *   stats [channel] [nick]
    100 		 *   stats [channel]
    101 		 *   stats [nick]
    102 		 *   stats
    103 		 */
    104 
    105 		switch (params.size())
    106 		{
    107 			case 2:
    108 				channel = params[0];
    109 				display = params[1];
    110 				break;
    111 			case 1:
    112 				if (params[0][0] == '#')
    113 					channel = params[0];
    114 				else
    115 				{
    116 					if (NickAlias *na = NickAlias::Find(params[0]))
    117 						display = na->nc->display;
    118 					else
    119 					{
    120 						source.Reply(_("%s not found."), params[0].c_str());
    121 						return;
    122 					}
    123 				}
    124 				break;
    125 		}
    126 
    127 		if (display.empty())
    128 			display = source.nc->display;
    129 
    130 		try
    131 		{
    132 			SQL::Query query;
    133 			query = "SELECT letters, words, line, smileys_happy+smileys_sad+smileys_other as smileys,"
    134 				"actions FROM `" + prefix + "chanstats` "
    135 				"WHERE `nick` = @nick@ AND `chan` = @channel@ AND `type` = 'total';";
    136 			if (is_global || channel.empty())
    137 				query.SetValue("channel", "");
    138 			else
    139 				query.SetValue("channel", channel);
    140 			query.SetValue("nick", display);
    141 			SQL::Result res = this->RunQuery(query);
    142 
    143 			if (res.Rows() > 0)
    144 			{
    145 				if (is_global)
    146 					source.Reply(_("Network stats for %s:"), display.c_str());
    147 				else
    148 					source.Reply(_("Channel stats for %s on %s:"), display.c_str(), channel.c_str());
    149 
    150 				source.Reply(_("letters: %s, words: %s, lines: %s, smileys: %s, actions: %s"),
    151 						res.Get(0, "letters").c_str(), res.Get(0, "words").c_str(),
    152 						res.Get(0, "line").c_str(), res.Get(0, "smileys").c_str(),
    153 						res.Get(0, "actions").c_str());
    154 			}
    155 			else
    156 				source.Reply(_("No stats for %s."), display.c_str());
    157 		}
    158 		catch (const SQL::Exception &ex)
    159 		{
    160 			Log(LOG_DEBUG) << ex.GetReason();
    161 		}
    162 
    163 	}
    164 };
    165 
    166 void CommandCSStats::Execute(CommandSource &source, const std::vector<Anope::string> &params)
    167 {
    168 	me->DoStats(source, false, params);
    169 }
    170 
    171 void CommandCSGStats::Execute(CommandSource &source, const std::vector<Anope::string> &params)
    172 {
    173 	me->DoStats(source, true, params);
    174 }
    175 
    176 MODULE_INIT(CSStats)