1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* Chanstats core functions
*
* (C) 2003-2017 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "module.h"
#include "modules/sql.h"
class MySQLInterface : 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(LogType::DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError();
else
Log(LogType::DEBUG) << "Chanstats: Error executing query: " << r.GetError();
}
};
class CommandCSTop : 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 : 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 : 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 : 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 : 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("", ""),
sqlinterface(this)
{
me = this;
}
void OnReload(Configuration::Conf *conf) override
{
prefix = conf->GetModule("chanstats")->Get<Anope::string>("prefix", "anope_");
this->sql = ServiceReference<SQL::Provider>("SQL::Provider", conf->GetModule("chanstats")->Get<Anope::string>("engine"));
}
SQL::Result RunQuery(const SQL::Query &query)
{
if (!this->sql)
throw SQL::Exception("Unable to locate SQL reference, is m_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->GetName();
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(_("%2lu \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(LogType::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)
|