Anope IRC Services  Version 2.0
cs_fantasy_stats.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 
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 
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 {
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 
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 
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 
177 
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
ServiceReference< SQL::Provider > sql
CommandCSGStats commandcsgstats
void Execute(CommandSource &source, const std::vector< Anope::string > &params)
CSStats(const Anope::string &modname, const Anope::string &creator)
const Anope::string & GetError() const
Definition: sql.h:153
MySQLInterface sqlinterface
void OnReload(Configuration::Conf *conf) anope_override
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
void Execute(CommandSource &source, const std::vector< Anope::string > &params)
void OnError(const SQL::Result &r) anope_override
Interface(Module *m)
Definition: sql.h:188
CommandCSStats commandcsstats
Definition: sql.h:96
CommandCSGStats(Module *creator)
void OnResult(const SQL::Result &r) anope_override
void Reply(const char *message,...)
Definition: command.cpp:96
static CSStats * me
#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
void DoStats(CommandSource &source, const bool is_global, const std::vector< Anope::string > &params)
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
CommandCSStats(Module *creator)
int Rows() const
Definition: sql.h:155
MySQLInterface(Module *o)
Anope::string prefix
#define _(x)
Definition: services.h:50
const Anope::string Get(size_t index, const Anope::string &col) const
Definition: sql.h:169