Anope IRC Services  Version 2.0
cs_fantasy_top.cpp
Go to the documentation of this file.
1 /* Chanstats core functions
2  *
3  * (C) 2003-2014 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:
19 
21  {
22  }
23 
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 {
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 
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 
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 
CommandCSGTop commandcsgtop
CommandCSTop commandcstop
Reference< Channel > c
Definition: commands.h:65
const Anope::string & GetError() const
Definition: sql.h:153
MySQLInterface sqlinterface
CommandCSGTop10 commandcsgtop10
void Execute(CommandSource &source, const std::vector< Anope::string > &params)
void Execute(CommandSource &source, const std::vector< Anope::string > &params)
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
Serialize::Reference< ChannelInfo > ci
Definition: channels.h:46
void SetValue(const Anope::string &key, const T &value, bool escape=true)
Definition: sql.h:121
CommandCSGTop10(Module *creator)
void OnError(const SQL::Result &r) anope_override
CSTop(const Anope::string &modname, const Anope::string &creator)
Interface(Module *m)
Definition: sql.h:188
CommandCSTop10 commandcstop10
void OnReload(Configuration::Conf *conf) anope_override
Definition: sql.h:96
Anope::string prefix
void OnResult(const SQL::Result &r) anope_override
void Execute(CommandSource &source, const std::vector< Anope::string > &params)
void Reply(const char *message,...)
Definition: command.cpp:96
void Execute(CommandSource &source, const std::vector< Anope::string > &params)
CommandCSTop10(Module *creator)
ServiceReference< SQL::Provider > sql
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
#define MODULE_INIT(x)
Definition: modules.h:45
Definition: sql.h:8
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
SQL::Result RunQuery(const SQL::Query &query)
virtual const Anope::string & GetReason() const
Definition: anope.h:672
CommandCSGTop(Module *creator)
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
void DoTop(CommandSource &source, const std::vector< Anope::string > &params, bool is_global, int limit=1)
int Rows() const
Definition: sql.h:155
MySQLInterface(Module *o)
#define _(x)
Definition: services.h:50
CommandCSTop(Module *creator)
const Anope::string Get(size_t index, const Anope::string &col) const
Definition: sql.h:169
static CSTop * me