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 int InspIRCd::PassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
19 FIRST_MOD_RESULT(OnPassCompare, res, (ex, data, input, hashtype));
22 if (res == MOD_RES_ALLOW)
25 /* Module explicitly didnt match */
26 if (res == MOD_RES_DENY)
29 /* We dont handle any hash types except for plaintext - Thanks tra26 */
30 if (hashtype != "" && hashtype != "plaintext")
31 /* See below. 1 because they dont match */
34 return (data != input); // this seems back to front, but returns 0 if they *match*, 1 else
37 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
38 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
39 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
40 * the channel names and their keys as follows:
41 * JOIN #chan1,#chan2,#chan3 key1,,key3
42 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
43 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
44 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
45 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
47 int CommandParser::LoopCall(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere, int extra, bool usemax)
49 if (splithere >= parameters.size())
52 if (extra >= (signed)parameters.size())
55 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
56 * which called us just carries on as it was.
58 if (parameters[splithere].find(',') == std::string::npos)
61 /** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
62 * By using std::set (thanks for the idea w00t) we can cut this down a ton.
65 std::set<irc::string> dupes;
67 /* Create two lists, one for channel names, one for keys
69 irc::commasepstream items1(parameters[splithere]);
70 irc::commasepstream items2(extra >= 0 ? parameters[extra] : "");
71 std::string extrastuff;
75 /* Attempt to iterate these lists and call the command objech
76 * which called us, for every parameter pair until there are
77 * no more left to parse.
79 while (items1.GetToken(item) && (!usemax || max++ < ServerInstance->Config->MaxTargets))
81 if (dupes.find(item.c_str()) == dupes.end())
83 std::vector<std::string> new_parameters(parameters);
85 if (!items2.GetToken(extrastuff))
88 new_parameters[splithere] = item;
90 new_parameters[extra] = extrastuff;
92 CommandObj->Handle(new_parameters, user);
94 dupes.insert(item.c_str());
100 bool CommandParser::IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user)
102 Commandtable::iterator n = cmdlist.find(commandname);
104 if (n != cmdlist.end())
106 if ((pcnt >= n->second->min_params))
108 if (IS_LOCAL(user) && n->second->flags_needed)
110 if (user->IsModeSet(n->second->flags_needed))
112 return (user->HasPermission(commandname));
124 Command* CommandParser::GetHandler(const std::string &commandname)
126 Commandtable::iterator n = cmdlist.find(commandname);
127 if (n != cmdlist.end())
133 // calls a handler function for a command
135 CmdResult CommandParser::CallHandler(const std::string &commandname, const std::vector<std::string>& parameters, User *user)
137 Commandtable::iterator n = cmdlist.find(commandname);
139 if (n != cmdlist.end())
141 if (parameters.size() >= n->second->min_params)
145 if (IS_LOCAL(user) && n->second->flags_needed)
147 /* if user is local, and flags are needed .. */
149 if (user->IsModeSet(n->second->flags_needed))
151 /* if user has the flags, and now has the permissions, go ahead */
152 if (user->HasPermission(commandname))
158 /* remote or no flags required anyway */
164 return n->second->Handle(parameters,user);
171 bool CommandParser::ProcessCommand(LocalUser *user, std::string &cmd)
173 std::vector<std::string> command_p;
174 irc::tokenstream tokens(cmd);
175 std::string command, token;
176 tokens.GetToken(command);
178 /* A client sent a nick prefix on their command (ick)
179 * rhapsody and some braindead bouncers do this --
180 * the rfc says they shouldnt but also says the ircd should
181 * discard it if they do.
183 if (command[0] == ':')
184 tokens.GetToken(command);
186 while (tokens.GetToken(token) && (command_p.size() <= MAXPARAMETERS))
187 command_p.push_back(token);
189 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
191 /* find the command, check it exists */
192 Commandtable::iterator cm = cmdlist.find(command);
194 /* Modify the user's penalty regardless of whether or not the command exists */
196 if (!user->HasPrivPermission("users/flood/no-throttle"))
198 // If it *doesn't* exist, give it a slightly heftier penalty than normal to deter flooding us crap
199 user->CommandFloodPenalty += cm != cmdlist.end() ? cm->second->Penalty * 1000 : 2000;
203 if (cm == cmdlist.end())
205 ModResult MOD_RESULT;
206 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
207 if (MOD_RESULT == MOD_RES_DENY)
211 * This double lookup is in case a module (abbreviation) wishes to change a command.
212 * Sure, the double lookup is a bit painful, but bear in mind this only happens for unknowns anyway.
214 * Thanks dz for making me actually understand why this is necessary!
217 cm = cmdlist.find(command);
218 if (cm == cmdlist.end())
220 if (user->registered == REG_ALL)
221 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
222 ServerInstance->stats->statsUnknown++;
227 if (cm->second->max_params && command_p.size() > cm->second->max_params)
230 * command_p input (assuming max_params 1):
236 std::string lparam = "";
239 * The '-1' here is a clever trick, we'll go backwards throwing everything into a temporary param
240 * and then just toss that into the array.
243 while (command_p.size() > (cm->second->max_params - 1))
245 // BE CAREFUL: .end() returns past the end of the vector, hence decrement.
246 std::vector<std::string>::iterator it = --command_p.end();
248 lparam.insert(0, " " + *(it));
249 command_p.erase(it); // remove last element
252 /* we now have (each iteration):
255 * ' is a test' <-- final string
256 * ...now remove the ' ' at the start...
258 lparam.erase(lparam.begin());
260 /* param is now 'is a test', which is exactly what we wanted! */
261 command_p.push_back(lparam);
265 * We call OnPreCommand here seperately if the command exists, so the magic above can
266 * truncate to max_params if necessary. -- w00t
268 ModResult MOD_RESULT;
269 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
270 if (MOD_RESULT == MOD_RES_DENY)
273 /* activity resets the ping pending timer */
274 user->nping = ServerInstance->Time() + user->MyClass->GetPingTime();
276 if (cm->second->flags_needed)
278 if (!user->IsModeSet(cm->second->flags_needed))
280 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges",user->nick.c_str());
283 if (!user->HasPermission(command))
285 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to command %s",
286 user->nick.c_str(), user->oper->NameStr(), command.c_str());
290 if ((user->registered == REG_ALL) && (!IS_OPER(user)) && (cm->second->IsDisabled()))
292 /* command is disabled! */
293 if (ServerInstance->Config->DisabledDontExist)
295 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
299 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :This command has been disabled.",
300 user->nick.c_str(), command.c_str());
303 ServerInstance->SNO->WriteToSnoMask('t', "%s denied for %s (%s@%s)",
304 command.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str());
307 if (command_p.size() < cm->second->min_params)
309 user->WriteNumeric(ERR_NEEDMOREPARAMS, "%s %s :Not enough parameters.", user->nick.c_str(), command.c_str());
310 if ((ServerInstance->Config->SyntaxHints) && (user->registered == REG_ALL) && (cm->second->syntax.length()))
311 user->WriteNumeric(RPL_SYNTAX, "%s :SYNTAX %s %s", user->nick.c_str(), cm->second->name.c_str(), cm->second->syntax.c_str());
314 if ((user->registered != REG_ALL) && (!cm->second->WorksBeforeReg()))
316 user->WriteNumeric(ERR_NOTREGISTERED, "%s :You have not registered",command.c_str());
321 /* passed all checks.. first, do the (ugly) stats counters. */
322 cm->second->use_count++;
323 cm->second->total_bytes += cmd.length();
325 /* module calls too */
326 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, true, cmd));
327 if (MOD_RESULT == MOD_RES_DENY)
331 * WARNING: be careful, the user may be deleted soon
333 CmdResult result = cm->second->Handle(command_p, user);
335 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, user, result,cmd));
340 void CommandParser::RemoveCommand(Command* x)
342 Commandtable::iterator n = cmdlist.find(x->name);
343 if (n != cmdlist.end() && n->second == x)
349 ServerInstance->Parser->RemoveCommand(this);
352 bool CommandParser::ProcessBuffer(std::string &buffer,LocalUser *user)
354 if (!user || buffer.empty())
357 ServerInstance->Logs->Log("USERINPUT", DEBUG, "C[%s] I :%s %s",
358 user->uuid.c_str(), user->nick.c_str(), buffer.c_str());
359 return ProcessCommand(user,buffer);
362 bool CommandParser::AddCommand(Command *f)
364 /* create the command and push it onto the table */
365 if (cmdlist.find(f->name) == cmdlist.end())
367 cmdlist[f->name] = f;
373 CommandParser::CommandParser()
378 int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final, Command* custom_translator)
380 std::vector<TranslateType>::const_iterator types = to.begin();
383 int translations = 0;
386 for(i=0; i < source.size(); i++)
389 std::string item = source[i];
391 if (types == to.end())
399 if (prefix_final && i == source.size() - 1)
405 /* Translate single nickname */
406 user = ServerInstance->FindNick(item);
409 dest.append(user->uuid);
416 if (custom_translator)
417 custom_translator->EncodeParameter(item, i);
427 if (i != source.size() - 1)
434 int CommandParser::TranslateUIDs(TranslateType to, const std::string &source, std::string &dest)
438 int translations = 0;
444 /* Translate single nickname */
445 user = ServerInstance->FindNick(source);