1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include "socketengine.h"
18 #include "command_parse.h"
19 #include "exitcodes.h"
21 /* Directory Searching for Unix-Only */
27 int InspIRCd::PassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
30 FIRST_MOD_RESULT(OnPassCompare, res, (ex, data, input, hashtype));
33 if (res == MOD_RES_ALLOW)
36 /* Module explicitly didnt match */
37 if (res == MOD_RES_DENY)
40 /* We dont handle any hash types except for plaintext - Thanks tra26 */
41 if (hashtype != "" && hashtype != "plaintext")
42 /* See below. 1 because they dont match */
45 return (data != input); // this seems back to front, but returns 0 if they *match*, 1 else
48 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
49 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
50 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
51 * the channel names and their keys as follows:
52 * JOIN #chan1,#chan2,#chan3 key1,,key3
53 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
54 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
55 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
56 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
58 int CommandParser::LoopCall(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere, unsigned int extra, bool usemax)
60 if (splithere >= parameters.size())
63 if (extra >= parameters.size())
66 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
67 * which called us just carries on as it was.
69 if (parameters[splithere].find(',') == std::string::npos)
72 /** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
73 * By using std::set (thanks for the idea w00t) we can cut this down a ton.
76 std::set<irc::string> dupes;
78 /* Create two lists, one for channel names, one for keys
80 irc::commasepstream items1(parameters[splithere]);
81 irc::commasepstream items2(extra >= 0 ? parameters[extra] : "");
82 std::string extrastuff;
86 /* Attempt to iterate these lists and call the command objech
87 * which called us, for every parameter pair until there are
88 * no more left to parse.
90 while (items1.GetToken(item) && (!usemax || max++ < ServerInstance->Config->MaxTargets))
92 if (dupes.find(item.c_str()) == dupes.end())
94 std::vector<std::string> new_parameters(parameters);
96 if (!items2.GetToken(extrastuff))
99 new_parameters[splithere] = item;
101 new_parameters[extra] = extrastuff;
103 CommandObj->Handle(new_parameters, user);
105 dupes.insert(item.c_str());
111 bool CommandParser::IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user)
113 Commandtable::iterator n = cmdlist.find(commandname);
115 if (n != cmdlist.end())
117 if ((pcnt >= n->second->min_params))
119 if (IS_LOCAL(user) && n->second->flags_needed)
121 if (user->IsModeSet(n->second->flags_needed))
123 return (user->HasPermission(commandname));
135 Command* CommandParser::GetHandler(const std::string &commandname)
137 Commandtable::iterator n = cmdlist.find(commandname);
138 if (n != cmdlist.end())
144 // calls a handler function for a command
146 CmdResult CommandParser::CallHandler(const std::string &commandname, const std::vector<std::string>& parameters, User *user)
148 Commandtable::iterator n = cmdlist.find(commandname);
150 if (n != cmdlist.end())
152 if (parameters.size() >= n->second->min_params)
156 if (IS_LOCAL(user) && n->second->flags_needed)
158 /* if user is local, and flags are needed .. */
160 if (user->IsModeSet(n->second->flags_needed))
162 /* if user has the flags, and now has the permissions, go ahead */
163 if (user->HasPermission(commandname))
169 /* remote or no flags required anyway */
175 return n->second->Handle(parameters,user);
182 bool CommandParser::ProcessCommand(User *user, std::string &cmd)
184 std::vector<std::string> command_p;
185 irc::tokenstream tokens(cmd);
186 std::string command, token;
187 tokens.GetToken(command);
189 /* A client sent a nick prefix on their command (ick)
190 * rhapsody and some braindead bouncers do this --
191 * the rfc says they shouldnt but also says the ircd should
192 * discard it if they do.
194 if (command[0] == ':')
195 tokens.GetToken(command);
197 while (tokens.GetToken(token) && (command_p.size() <= MAXPARAMETERS))
198 command_p.push_back(token);
200 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
202 /* find the command, check it exists */
203 Commandtable::iterator cm = cmdlist.find(command);
205 /* Modify the user's penalty regardless of whether or not the command exists */
207 if (IS_LOCAL(user) && !user->HasPrivPermission("users/flood/no-throttle"))
209 // If it *doesn't* exist, give it a slightly heftier penalty than normal to deter flooding us crap
210 IS_LOCAL(user)->CommandFloodPenalty += cm != cmdlist.end() ? cm->second->Penalty * 1000 : 2000;
214 if (cm == cmdlist.end())
216 ModResult MOD_RESULT;
217 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
218 if (MOD_RESULT == MOD_RES_DENY)
222 * This double lookup is in case a module (abbreviation) wishes to change a command.
223 * Sure, the double lookup is a bit painful, but bear in mind this only happens for unknowns anyway.
225 * Thanks dz for making me actually understand why this is necessary!
228 cm = cmdlist.find(command);
229 if (cm == cmdlist.end())
231 if (user->registered == REG_ALL)
232 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
233 ServerInstance->stats->statsUnknown++;
238 if (cm->second->max_params && command_p.size() > cm->second->max_params)
241 * command_p input (assuming max_params 1):
247 std::string lparam = "";
250 * The '-1' here is a clever trick, we'll go backwards throwing everything into a temporary param
251 * and then just toss that into the array.
254 while (command_p.size() > (cm->second->max_params - 1))
256 // BE CAREFUL: .end() returns past the end of the vector, hence decrement.
257 std::vector<std::string>::iterator it = --command_p.end();
259 lparam.insert(0, " " + *(it));
260 command_p.erase(it); // remove last element
263 /* we now have (each iteration):
266 * ' is a test' <-- final string
267 * ...now remove the ' ' at the start...
269 lparam.erase(lparam.begin());
271 /* param is now 'is a test', which is exactly what we wanted! */
272 command_p.push_back(lparam);
276 * We call OnPreCommand here seperately if the command exists, so the magic above can
277 * truncate to max_params if necessary. -- w00t
279 ModResult MOD_RESULT;
280 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
281 if (MOD_RESULT == MOD_RES_DENY)
284 /* activity resets the ping pending timer */
285 LocalUser* luser = IS_LOCAL(user);
287 luser->nping = ServerInstance->Time() + luser->MyClass->GetPingTime();
289 if (cm->second->flags_needed)
291 if (!user->IsModeSet(cm->second->flags_needed))
293 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges",user->nick.c_str());
296 if (!user->HasPermission(command))
298 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to command %s",
299 user->nick.c_str(), user->oper->NameStr(), command.c_str());
303 if ((user->registered == REG_ALL) && (!IS_OPER(user)) && (cm->second->IsDisabled()))
305 /* command is disabled! */
306 if (ServerInstance->Config->DisabledDontExist)
308 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
312 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :This command has been disabled.",
313 user->nick.c_str(), command.c_str());
316 ServerInstance->SNO->WriteToSnoMask('t', "%s denied for %s (%s@%s)",
317 command.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str());
320 if (command_p.size() < cm->second->min_params)
322 user->WriteNumeric(ERR_NEEDMOREPARAMS, "%s %s :Not enough parameters.", user->nick.c_str(), command.c_str());
323 if ((ServerInstance->Config->SyntaxHints) && (user->registered == REG_ALL) && (cm->second->syntax.length()))
324 user->WriteNumeric(RPL_SYNTAX, "%s :SYNTAX %s %s", user->nick.c_str(), cm->second->name.c_str(), cm->second->syntax.c_str());
327 if ((user->registered != REG_ALL) && (!cm->second->WorksBeforeReg()))
329 user->WriteNumeric(ERR_NOTREGISTERED, "%s :You have not registered",command.c_str());
334 /* passed all checks.. first, do the (ugly) stats counters. */
335 cm->second->use_count++;
336 cm->second->total_bytes += cmd.length();
338 /* module calls too */
339 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, true, cmd));
340 if (MOD_RESULT == MOD_RES_DENY)
344 * WARNING: be careful, the user may be deleted soon
346 CmdResult result = cm->second->Handle(command_p, user);
348 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, user, result,cmd));
353 void CommandParser::RemoveCommand(Command* x)
355 Commandtable::iterator n = cmdlist.find(x->name);
356 if (n != cmdlist.end() && n->second == x)
362 ServerInstance->Parser->RemoveCommand(this);
365 bool CommandParser::ProcessBuffer(std::string &buffer,User *user)
367 if (!user || buffer.empty())
370 ServerInstance->Logs->Log("USERINPUT", DEBUG, "C[%s] I :%s %s",
371 user->uuid.c_str(), user->nick.c_str(), buffer.c_str());
372 return ProcessCommand(user,buffer);
375 bool CommandParser::AddCommand(Command *f)
377 /* create the command and push it onto the table */
378 if (cmdlist.find(f->name) == cmdlist.end())
380 cmdlist[f->name] = f;
386 CommandParser::CommandParser()
391 int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final, Command* custom_translator)
393 std::vector<TranslateType>::const_iterator types = to.begin();
396 int translations = 0;
399 for(i=0; i < source.size(); i++)
402 std::string item = source[i];
404 if (types == to.end())
412 if (prefix_final && i == source.size() - 1)
418 /* Translate single nickname */
419 user = ServerInstance->FindNick(item);
422 dest.append(user->uuid);
429 if (custom_translator)
430 custom_translator->EncodeParameter(item, i);
440 if (i != source.size() - 1)
447 int CommandParser::TranslateUIDs(TranslateType to, const std::string &source, std::string &dest)
451 int translations = 0;
457 /* Translate single nickname */
458 user = ServerInstance->FindNick(source);