1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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)
60 if (splithere >= parameters.size())
63 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
64 * which called us just carries on as it was.
66 if (parameters[splithere].find(',') == std::string::npos)
69 /** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
70 * By using std::set (thanks for the idea w00t) we can cut this down a ton.
73 std::set<irc::string> dupes;
75 /* Create two lists, one for channel names, one for keys
77 irc::commasepstream items1(parameters[splithere]);
78 irc::commasepstream items2(parameters[extra]);
79 std::string extrastuff;
83 /* Attempt to iterate these lists and call the command objech
84 * which called us, for every parameter pair until there are
85 * no more left to parse.
87 while (items1.GetToken(item) && (max++ < ServerInstance->Config->MaxTargets))
89 if (dupes.find(item.c_str()) == dupes.end())
91 std::vector<std::string> new_parameters;
93 for (unsigned int t = 0; (t < parameters.size()) && (t < MAXPARAMETERS); t++)
94 new_parameters.push_back(parameters[t]);
96 if (!items2.GetToken(extrastuff))
99 new_parameters[splithere] = item.c_str();
100 new_parameters[extra] = extrastuff.c_str();
102 CommandObj->Handle(new_parameters, user);
104 dupes.insert(item.c_str());
110 int CommandParser::LoopCall(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere)
112 if (splithere >= parameters.size())
115 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
116 * which called us just carries on as it was.
118 if (parameters[splithere].find(',') == std::string::npos)
121 std::set<irc::string> dupes;
123 /* Only one commasepstream here */
124 irc::commasepstream items1(parameters[splithere]);
126 unsigned int max = 0;
128 /* Parse the commasepstream until there are no tokens remaining.
129 * Each token we parse out, call the command handler that called us
132 while (items1.GetToken(item) && (max++ < ServerInstance->Config->MaxTargets))
134 if (dupes.find(item.c_str()) == dupes.end())
136 std::vector<std::string> new_parameters;
138 for (unsigned int t = 0; (t < parameters.size()) && (t < MAXPARAMETERS); t++)
139 new_parameters.push_back(parameters[t]);
141 new_parameters[splithere] = item.c_str();
143 /* Execute the command handler. */
144 CommandObj->Handle(new_parameters, user);
146 dupes.insert(item.c_str());
149 /* By returning 1 we tell our caller that nothing is to be done,
150 * as all the previous calls handled the data. This makes the parent
151 * return without doing any processing.
156 bool CommandParser::IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user)
158 Commandtable::iterator n = cmdlist.find(commandname);
160 if (n != cmdlist.end())
162 if ((pcnt >= n->second->min_params))
164 if (IS_LOCAL(user) && n->second->flags_needed)
166 if (user->IsModeSet(n->second->flags_needed))
168 return (user->HasPermission(commandname));
180 Command* CommandParser::GetHandler(const std::string &commandname)
182 Commandtable::iterator n = cmdlist.find(commandname);
183 if (n != cmdlist.end())
189 // calls a handler function for a command
191 CmdResult CommandParser::CallHandler(const std::string &commandname, const std::vector<std::string>& parameters, User *user)
193 Commandtable::iterator n = cmdlist.find(commandname);
195 if (n != cmdlist.end())
197 if (parameters.size() >= n->second->min_params)
201 if (IS_LOCAL(user) && n->second->flags_needed)
203 /* if user is local, and flags are needed .. */
205 if (user->IsModeSet(n->second->flags_needed))
207 /* if user has the flags, and now has the permissions, go ahead */
208 if (user->HasPermission(commandname))
214 /* remote or no flags required anyway */
220 return n->second->Handle(parameters,user);
227 bool CommandParser::ProcessCommand(User *user, std::string &cmd)
229 std::vector<std::string> command_p;
230 irc::tokenstream tokens(cmd);
231 std::string command, token;
232 tokens.GetToken(command);
234 /* A client sent a nick prefix on their command (ick)
235 * rhapsody and some braindead bouncers do this --
236 * the rfc says they shouldnt but also says the ircd should
237 * discard it if they do.
239 if (command[0] == ':')
240 tokens.GetToken(command);
242 while (tokens.GetToken(token) && (command_p.size() <= MAXPARAMETERS))
243 command_p.push_back(token);
245 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
247 /* find the command, check it exists */
248 Commandtable::iterator cm = cmdlist.find(command);
250 /* Modify the user's penalty regardless of whether or not the command exists */
252 if (IS_LOCAL(user) && !user->HasPrivPermission("users/flood/no-throttle"))
254 // If it *doesn't* exist, give it a slightly heftier penalty than normal to deter flooding us crap
255 IS_LOCAL(user)->Penalty += cm != cmdlist.end() ? cm->second->Penalty : 2;
259 if (cm == cmdlist.end())
261 ModResult MOD_RESULT;
262 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
263 if (MOD_RESULT == MOD_RES_DENY)
267 * This double lookup is in case a module (abbreviation) wishes to change a command.
268 * Sure, the double lookup is a bit painful, but bear in mind this only happens for unknowns anyway.
270 * Thanks dz for making me actually understand why this is necessary!
273 cm = cmdlist.find(command);
274 if (cm == cmdlist.end())
276 if (user->registered == REG_ALL)
277 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
278 ServerInstance->stats->statsUnknown++;
283 if (cm->second->max_params && command_p.size() > cm->second->max_params)
286 * command_p input (assuming max_params 1):
292 std::string lparam = "";
295 * The '-1' here is a clever trick, we'll go backwards throwing everything into a temporary param
296 * and then just toss that into the array.
299 while (command_p.size() > (cm->second->max_params - 1))
301 // BE CAREFUL: .end() returns past the end of the vector, hence decrement.
302 std::vector<std::string>::iterator it = --command_p.end();
304 lparam.insert(0, " " + *(it));
305 command_p.erase(it); // remove last element
308 /* we now have (each iteration):
311 * ' is a test' <-- final string
312 * ...now remove the ' ' at the start...
314 lparam.erase(lparam.begin());
316 /* param is now 'is a test', which is exactly what we wanted! */
317 command_p.push_back(lparam);
321 * We call OnPreCommand here seperately if the command exists, so the magic above can
322 * truncate to max_params if necessary. -- w00t
324 ModResult MOD_RESULT;
325 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
326 if (MOD_RESULT == MOD_RES_DENY)
329 /* activity resets the ping pending timer */
330 LocalUser* luser = IS_LOCAL(user);
332 luser->nping = ServerInstance->Time() + luser->MyClass->GetPingTime();
334 if (cm->second->flags_needed)
336 if (!user->IsModeSet(cm->second->flags_needed))
338 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges",user->nick.c_str());
341 if (!user->HasPermission(command))
343 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to command %s",
344 user->nick.c_str(), user->oper->NameStr(), command.c_str());
348 if ((user->registered == REG_ALL) && (!IS_OPER(user)) && (cm->second->IsDisabled()))
350 /* command is disabled! */
351 if (ServerInstance->Config->DisabledDontExist)
353 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
357 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :This command has been disabled.",
358 user->nick.c_str(), command.c_str());
361 ServerInstance->SNO->WriteToSnoMask('t', "%s denied for %s (%s@%s)",
362 command.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str());
365 if (command_p.size() < cm->second->min_params)
367 user->WriteNumeric(ERR_NEEDMOREPARAMS, "%s %s :Not enough parameters.", user->nick.c_str(), command.c_str());
368 if ((ServerInstance->Config->SyntaxHints) && (user->registered == REG_ALL) && (cm->second->syntax.length()))
369 user->WriteNumeric(RPL_SYNTAX, "%s :SYNTAX %s %s", user->nick.c_str(), cm->second->command.c_str(), cm->second->syntax.c_str());
372 if ((user->registered != REG_ALL) && (!cm->second->WorksBeforeReg()))
374 user->WriteNumeric(ERR_NOTREGISTERED, "%s :You have not registered",command.c_str());
379 /* passed all checks.. first, do the (ugly) stats counters. */
380 cm->second->use_count++;
381 cm->second->total_bytes += cmd.length();
383 /* module calls too */
384 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, true, cmd));
385 if (MOD_RESULT == MOD_RES_DENY)
389 * WARNING: be careful, the user may be deleted soon
391 CmdResult result = cm->second->Handle(command_p, user);
393 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, user, result,cmd));
398 void CommandParser::RemoveCommand(Command* x)
400 Commandtable::iterator n = cmdlist.find(x->command);
401 if (n != cmdlist.end() && n->second == x)
407 ServerInstance->Parser->RemoveCommand(this);
410 bool CommandParser::ProcessBuffer(std::string &buffer,User *user)
412 if (!user || buffer.empty())
415 ServerInstance->Logs->Log("USERINPUT", DEBUG, "C[%s] I :%s %s",
416 user->uuid.c_str(), user->nick.c_str(), buffer.c_str());
417 return ProcessCommand(user,buffer);
420 bool CommandParser::AddCommand(Command *f)
422 /* create the command and push it onto the table */
423 if (cmdlist.find(f->command) == cmdlist.end())
425 cmdlist[f->command] = f;
431 CommandParser::CommandParser()
436 int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final, Command* custom_translator)
438 std::vector<TranslateType>::const_iterator types = to.begin();
441 int translations = 0;
444 for(i=0; i < source.size(); i++)
447 std::string item = source[i];
449 if (types == to.end())
457 if (prefix_final && i == source.size() - 1)
463 /* Translate single nickname */
464 user = ServerInstance->FindNick(item);
467 dest.append(user->uuid);
474 if (custom_translator)
475 custom_translator->EncodeParameter(item, i);
485 if (i != source.size() - 1)
492 int CommandParser::TranslateUIDs(TranslateType to, const std::string &source, std::string &dest)
496 int translations = 0;
502 /* Translate single nickname */
503 user = ServerInstance->FindNick(source);