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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
* Anope IRC Services
*
* Copyright (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
* Copyright (C) 2008-2016 Anope Team <team@anope.org>
*
* This file is part of Anope. Anope is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
#include "services.h"
#include "commands.h"
#include "users.h"
#include "language.h"
#include "config.h"
#include "opertype.h"
#include "channels.h"
#include "event.h"
#include "bots.h"
#include "protocol.h"
#include "modules/botserv.h"
#include "modules/chanserv.h"
CommandSource::CommandSource(const Anope::string &n, User *user, NickServ::Account *core, CommandReply *r, ServiceBot *bi)
: nick(n)
, u(user)
, nc(core)
, reply(r)
, service(bi)
{
}
const Anope::string &CommandSource::GetNick() const
{
return this->nick;
}
User *CommandSource::GetUser()
{
return this->u;
}
NickServ::Account *CommandSource::GetAccount()
{
return this->nc;
}
Anope::string CommandSource::GetSource()
{
if (u)
if (nc)
return this->u->GetMask() + " (" + this->nc->GetDisplay() + ")";
else
return this->u->GetMask();
else if (nc)
return nc->GetDisplay();
else
return this->nick;
}
const Anope::string &CommandSource::GetCommand() const
{
return this->command.cname;
}
void CommandSource::SetCommand(const Anope::string &command)
{
this->command.cname = command;
}
const Anope::string &CommandSource::GetPermission() const
{
return this->command.permission;
}
const CommandInfo &CommandSource::GetCommandInfo() const
{
return this->command;
}
void CommandSource::SetCommandInfo(const CommandInfo &ci)
{
this->command = ci;
}
ChanServ::AccessGroup CommandSource::AccessFor(ChanServ::Channel *ci)
{
if (this->u)
return ci->AccessFor(this->u);
else if (this->nc)
return ci->AccessFor(this->nc);
else
return ChanServ::AccessGroup();
}
bool CommandSource::IsFounder(ChanServ::Channel *ci)
{
if (this->u)
return ci->IsFounder(this->u);
else if (this->nc)
return *this->nc == ci->GetFounder();
return false;
}
bool CommandSource::HasCommand(const Anope::string &cmd)
{
if (this->u)
return this->u->HasCommand(cmd);
if (this->nc && this->nc->GetOper())
return this->nc->GetOper()->HasCommand(cmd);
return false;
}
bool CommandSource::HasPriv(const Anope::string &cmd)
{
if (this->u)
return this->u->HasPriv(cmd);
if (this->nc && this->nc->GetOper())
return this->nc->GetOper()->HasPriv(cmd);
return false;
}
bool CommandSource::IsServicesOper()
{
if (this->u)
return this->u->IsServicesOper();
else if (this->nc)
return this->nc->GetOper() != nullptr;
return false;
}
bool CommandSource::IsOper()
{
if (this->u)
return this->u->HasMode("OPER");
else if (this->nc)
return this->nc->GetOper() != nullptr;
return false;
}
bool CommandSource::HasOverridePriv(const Anope::string &priv)
{
if (!HasPriv(priv))
return false;
override = true;
return true;
}
bool CommandSource::HasOverrideCommand(const Anope::string &priv)
{
if (!HasCommand(priv))
return false;
override = true;
return true;
}
bool CommandSource::IsOverride() const
{
return override;
}
void CommandSource::Reply(const Anope::string &message)
{
const char *translated_message = Language::Translate(this->nc, message.c_str());
sepstream sep(translated_message, '\n', true);
Anope::string tok;
while (sep.GetToken(tok))
this->reply->SendMessage(*this->service, tok);
}
Command::Command(Module *o, const Anope::string &sname, size_t minparams, size_t maxparams) : Service(o, NAME, sname)
, max_params(maxparams)
, min_params(minparams)
, module(o)
, logger(this)
{
}
void Command::SetDesc(const Anope::string &d)
{
this->desc = d;
}
void Command::ClearSyntax()
{
this->syntax.clear();
}
void Command::SetSyntax(const Anope::string &s)
{
this->syntax.push_back(s);
}
void Command::SendSyntax(CommandSource &source)
{
Anope::string s = Language::Translate(source.GetAccount(), _("Syntax"));
if (!this->syntax.empty())
{
source.Reply("{0}: \002{1} {2}\002", s, source.GetCommand(), Language::Translate(source.GetAccount(), this->syntax[0].c_str()));
Anope::string spaces(s.length(), ' ');
for (unsigned i = 1, j = this->syntax.size(); i < j; ++i)
source.Reply("{0} \002{1} {2}\002", spaces, source.GetCommand(), Language::Translate(source.GetAccount(), this->syntax[i].c_str()));
}
else
source.Reply("{0}: \002{1}\002", s, source.GetCommand());
}
bool Command::AllowUnregistered() const
{
return this->allow_unregistered;
}
void Command::AllowUnregistered(bool b)
{
this->allow_unregistered = b;
}
bool Command::RequireUser() const
{
return this->require_user;
}
void Command::RequireUser(bool b)
{
this->require_user = b;
}
const Anope::string Command::GetDesc(CommandSource &) const
{
return this->desc;
}
void Command::OnServHelp(CommandSource &source)
{
source.Reply(Anope::printf(" %-14s %s", source.GetCommand().c_str(), Language::Translate(source.nc, this->GetDesc(source).c_str())));
}
bool Command::OnHelp(CommandSource &source, const Anope::string &subcommand) { return false; }
void Command::OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
{
this->SendSyntax(source);
bool has_help = source.service->commands.find("HELP") != source.service->commands.end();
if (has_help)
source.Reply(_("\002{0}{1} HELP {2}\002 for more information."), Config->StrictPrivmsg, source.service->nick, source.GetCommand());
}
void Command::Run(CommandSource &source, const Anope::string &message)
{
std::vector<Anope::string> params;
spacesepstream(message).GetTokens(params);
bool has_help = source.service->commands.find("HELP") != source.service->commands.end();
CommandInfo::map::const_iterator it = source.service->commands.end();
unsigned count = 0;
for (unsigned max = params.size(); it == source.service->commands.end() && max > 0; --max)
{
Anope::string full_command;
for (unsigned i = 0; i < max; ++i)
full_command += " " + params[i];
full_command.erase(full_command.begin());
++count;
it = source.service->commands.find(full_command);
}
if (it == source.service->commands.end())
{
if (has_help)
source.Reply(_("Unknown command \002{0}\002. \"{1}{2} HELP\" for help."), message, Config->StrictPrivmsg, source.service->nick);
else
source.Reply(_("Unknown command \002{0}\002."), message);
return;
}
const CommandInfo &info = it->second;
ServiceReference<Command> c(info.name);
if (!c)
{
if (has_help)
source.Reply(_("Unknown command \002{0}\002. \"{1}{2} HELP\" for help."), message, Config->StrictPrivmsg, source.service->nick);
else
source.Reply(_("Unknown command \002{0}\002."), message);
source.service->logger.Log("Command {0} exists on me, but its service {1} was not found!", it->first, info.name);
return;
}
for (unsigned i = 0, j = params.size() - (count - 1); i < j; ++i)
params.erase(params.begin());
while (c->max_params > 0 && params.size() > c->max_params)
{
params[c->max_params - 1] += " " + params[c->max_params];
params.erase(params.begin() + c->max_params);
}
c->Run(source, it->first, info, params);
}
void Command::Run(CommandSource &source, const Anope::string &cmdname, const CommandInfo &info, std::vector<Anope::string> ¶ms)
{
if (this->RequireUser() && !source.GetUser())
return;
// Command requires registered users only
if (!this->AllowUnregistered() && !source.nc)
{
source.Reply(_("Password authentication required for that command."));
if (source.GetUser())
Anope::Logger.User(source.service).Category("access_denied_unreg").Log(_("Access denied for unregistered user {0} with command {1}"), source.GetUser()->GetMask(), cmdname);
return;
}
source.SetCommandInfo(info);
EventReturn MOD_RESULT = EventManager::Get()->Dispatch(&Event::PreCommand::OnPreCommand, source, this, params);
if (MOD_RESULT == EVENT_STOP)
return;
if (params.size() < this->min_params)
{
this->OnSyntaxError(source, !params.empty() ? params[params.size() - 1] : "");
return;
}
// If the command requires a permission, and they aren't registered or don't have the required perm, DENIED
if (!info.permission.empty() && !source.HasCommand(info.permission))
{
if (!source.IsOper())
source.Reply(_("Access denied. You are not a Services Operator."));
else
source.Reply(_("Access denied. You do not have access to command \002{0}\002."), info.permission);
if (source.GetUser())
Anope::Logger.User(source.service).Category("access_denied").Log(_("Access denied for user {0} with command {1}"), source.GetUser()->GetMask(), cmdname);
return;
}
this->Execute(source, params);
EventManager::Get()->Dispatch(&Event::PostCommand::OnPostCommand, source, this, params);
}
bool Command::FindCommandFromService(const Anope::string &command_service, ServiceBot* &bot, Anope::string &name)
{
bot = NULL;
for (std::pair<Anope::string, User *> p : UserListByNick)
{
User *u = p.second;
if (u->type != UserType::BOT)
continue;
ServiceBot *bi = anope_dynamic_static_cast<ServiceBot *>(u);
for (CommandInfo::map::const_iterator cit = bi->commands.begin(), cit_end = bi->commands.end(); cit != cit_end; ++cit)
{
const Anope::string &c_name = cit->first;
const CommandInfo &info = cit->second;
if (info.name != command_service)
continue;
bot = bi;
name = c_name;
return true;
}
}
return false;
}
|