]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
Add RAWIO log level which is more verbose than DEBUG
[user/henk/code/inspircd.git] / src / command_parse.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 int InspIRCd::PassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
17 {
18         ModResult res;
19         FIRST_MOD_RESULT(OnPassCompare, res, (ex, data, input, hashtype));
20
21         /* Module matched */
22         if (res == MOD_RES_ALLOW)
23                 return 0;
24
25         /* Module explicitly didnt match */
26         if (res == MOD_RES_DENY)
27                 return 1;
28
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 */
32                 return 1;
33
34         return (data != input); // this seems back to front, but returns 0 if they *match*, 1 else
35 }
36
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.
46  */
47 int CommandParser::LoopCall(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere, int extra, bool usemax)
48 {
49         if (splithere >= parameters.size())
50                 return 0;
51
52         if (extra >= (signed)parameters.size())
53                 extra = -1;
54
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.
57          */
58         if (parameters[splithere].find(',') == std::string::npos)
59                 return 0;
60
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.
63          * ...VOOODOOOO!
64          */
65         std::set<irc::string> dupes;
66
67         /* Create two lists, one for channel names, one for keys
68          */
69         irc::commasepstream items1(parameters[splithere]);
70         irc::commasepstream items2(extra >= 0 ? parameters[extra] : "");
71         std::string extrastuff;
72         std::string item;
73         unsigned int max = 0;
74
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.
78          */
79         while (items1.GetToken(item) && (!usemax || max++ < ServerInstance->Config->MaxTargets))
80         {
81                 if (dupes.find(item.c_str()) == dupes.end())
82                 {
83                         std::vector<std::string> new_parameters(parameters);
84
85                         if (!items2.GetToken(extrastuff))
86                                 extrastuff = "";
87
88                         new_parameters[splithere] = item;
89                         if (extra >= 0)
90                                 new_parameters[extra] = extrastuff;
91
92                         CommandObj->Handle(new_parameters, user);
93
94                         dupes.insert(item.c_str());
95                 }
96         }
97         return 1;
98 }
99
100 bool CommandParser::IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user)
101 {
102         Commandtable::iterator n = cmdlist.find(commandname);
103
104         if (n != cmdlist.end())
105         {
106                 if ((pcnt >= n->second->min_params))
107                 {
108                         if (IS_LOCAL(user) && n->second->flags_needed)
109                         {
110                                 if (user->IsModeSet(n->second->flags_needed))
111                                 {
112                                         return (user->HasPermission(commandname));
113                                 }
114                         }
115                         else
116                         {
117                                 return true;
118                         }
119                 }
120         }
121         return false;
122 }
123
124 Command* CommandParser::GetHandler(const std::string &commandname)
125 {
126         Commandtable::iterator n = cmdlist.find(commandname);
127         if (n != cmdlist.end())
128                 return n->second;
129
130         return NULL;
131 }
132
133 // calls a handler function for a command
134
135 CmdResult CommandParser::CallHandler(const std::string &commandname, const std::vector<std::string>& parameters, User *user)
136 {
137         Commandtable::iterator n = cmdlist.find(commandname);
138
139         if (n != cmdlist.end())
140         {
141                 if (parameters.size() >= n->second->min_params)
142                 {
143                         bool bOkay = false;
144
145                         if (IS_LOCAL(user) && n->second->flags_needed)
146                         {
147                                 /* if user is local, and flags are needed .. */
148
149                                 if (user->IsModeSet(n->second->flags_needed))
150                                 {
151                                         /* if user has the flags, and now has the permissions, go ahead */
152                                         if (user->HasPermission(commandname))
153                                                 bOkay = true;
154                                 }
155                         }
156                         else
157                         {
158                                 /* remote or no flags required anyway */
159                                 bOkay = true;
160                         }
161
162                         if (bOkay)
163                         {
164                                 return n->second->Handle(parameters,user);
165                         }
166                 }
167         }
168         return CMD_INVALID;
169 }
170
171 bool CommandParser::ProcessCommand(LocalUser *user, std::string &cmd)
172 {
173         std::vector<std::string> command_p;
174         irc::tokenstream tokens(cmd);
175         std::string command, token;
176         tokens.GetToken(command);
177
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.
182          */
183         if (command[0] == ':')
184                 tokens.GetToken(command);
185
186         while (tokens.GetToken(token) && (command_p.size() <= MAXPARAMETERS))
187                 command_p.push_back(token);
188
189         std::transform(command.begin(), command.end(), command.begin(), ::toupper);
190
191         /* find the command, check it exists */
192         Commandtable::iterator cm = cmdlist.find(command);
193
194         /* Modify the user's penalty regardless of whether or not the command exists */
195         bool do_more = true;
196         if (!user->HasPrivPermission("users/flood/no-throttle"))
197         {
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;
200         }
201
202
203         if (cm == cmdlist.end())
204         {
205                 ModResult MOD_RESULT;
206                 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
207                 if (MOD_RESULT == MOD_RES_DENY)
208                         return true;
209
210                 /*
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.
213                  *
214                  * Thanks dz for making me actually understand why this is necessary!
215                  * -- w00t
216                  */
217                 cm = cmdlist.find(command);
218                 if (cm == cmdlist.end())
219                 {
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++;
223                         return true;
224                 }
225         }
226
227         if (cm->second->max_params && command_p.size() > cm->second->max_params)
228         {
229                 /*
230                  * command_p input (assuming max_params 1):
231                  *      this
232                  *      is
233                  *      a
234                  *      test
235                  */
236                 std::string lparam = "";
237
238                 /*
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.
241                  * -- w00t
242                  */
243                 while (command_p.size() > (cm->second->max_params - 1))
244                 {
245                         // BE CAREFUL: .end() returns past the end of the vector, hence decrement.
246                         std::vector<std::string>::iterator it = --command_p.end();
247
248                         lparam.insert(0, " " + *(it));
249                         command_p.erase(it); // remove last element
250                 }
251
252                 /* we now have (each iteration):
253                  *      ' test'
254                  *      ' a test'
255                  *      ' is a test' <-- final string
256                  * ...now remove the ' ' at the start...
257                  */
258                 lparam.erase(lparam.begin());
259
260                 /* param is now 'is a test', which is exactly what we wanted! */
261                 command_p.push_back(lparam);
262         }
263
264         /*
265          * We call OnPreCommand here seperately if the command exists, so the magic above can
266          * truncate to max_params if necessary. -- w00t
267          */
268         ModResult MOD_RESULT;
269         FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, false, cmd));
270         if (MOD_RESULT == MOD_RES_DENY)
271                 return true;
272
273         /* activity resets the ping pending timer */
274         user->nping = ServerInstance->Time() + user->MyClass->GetPingTime();
275
276         if (cm->second->flags_needed)
277         {
278                 if (!user->IsModeSet(cm->second->flags_needed))
279                 {
280                         user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges",user->nick.c_str());
281                         return do_more;
282                 }
283                 if (!user->HasPermission(command))
284                 {
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());
287                         return do_more;
288                 }
289         }
290         if ((user->registered == REG_ALL) && (!IS_OPER(user)) && (cm->second->IsDisabled()))
291         {
292                 /* command is disabled! */
293                 if (ServerInstance->Config->DisabledDontExist)
294                 {
295                         user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :Unknown command",user->nick.c_str(),command.c_str());
296                 }
297                 else
298                 {
299                         user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s %s :This command has been disabled.",
300                                                                                 user->nick.c_str(), command.c_str());
301                 }
302
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());
305                 return do_more;
306         }
307         if (command_p.size() < cm->second->min_params)
308         {
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());
312                 return do_more;
313         }
314         if ((user->registered != REG_ALL) && (!cm->second->WorksBeforeReg()))
315         {
316                 user->WriteNumeric(ERR_NOTREGISTERED, "%s :You have not registered",command.c_str());
317                 return do_more;
318         }
319         else
320         {
321                 /* passed all checks.. first, do the (ugly) stats counters. */
322                 cm->second->use_count++;
323                 cm->second->total_bytes += cmd.length();
324
325                 /* module calls too */
326                 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, command_p, user, true, cmd));
327                 if (MOD_RESULT == MOD_RES_DENY)
328                         return do_more;
329
330                 /*
331                  * WARNING: be careful, the user may be deleted soon
332                  */
333                 CmdResult result = cm->second->Handle(command_p, user);
334
335                 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, user, result,cmd));
336                 return do_more;
337         }
338 }
339
340 void CommandParser::RemoveCommand(Command* x)
341 {
342         Commandtable::iterator n = cmdlist.find(x->name);
343         if (n != cmdlist.end() && n->second == x)
344                 cmdlist.erase(n);
345 }
346
347 Command::~Command()
348 {
349         ServerInstance->Parser->RemoveCommand(this);
350 }
351
352 bool CommandParser::ProcessBuffer(std::string &buffer,LocalUser *user)
353 {
354         if (!user || buffer.empty())
355                 return true;
356
357         ServerInstance->Logs->Log("USERINPUT", RAWIO, "C[%s] I :%s %s",
358                 user->uuid.c_str(), user->nick.c_str(), buffer.c_str());
359         return ProcessCommand(user,buffer);
360 }
361
362 bool CommandParser::AddCommand(Command *f)
363 {
364         /* create the command and push it onto the table */
365         if (cmdlist.find(f->name) == cmdlist.end())
366         {
367                 cmdlist[f->name] = f;
368                 return true;
369         }
370         return false;
371 }
372
373 CommandParser::CommandParser()
374 {
375         para.resize(128);
376 }
377
378 int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final, Command* custom_translator)
379 {
380         std::vector<TranslateType>::const_iterator types = to.begin();
381         User* user = NULL;
382         unsigned int i;
383         int translations = 0;
384         dest.clear();
385
386         for(i=0; i < source.size(); i++)
387         {
388                 TranslateType t;
389                 std::string item = source[i];
390
391                 if (types == to.end())
392                         t = TR_TEXT;
393                 else
394                 {
395                         t = *types;
396                         types++;
397                 }
398
399                 if (prefix_final && i == source.size() - 1)
400                         dest.append(":");
401
402                 switch (t)
403                 {
404                         case TR_NICK:
405                                 /* Translate single nickname */
406                                 user = ServerInstance->FindNick(item);
407                                 if (user)
408                                 {
409                                         dest.append(user->uuid);
410                                         translations++;
411                                 }
412                                 else
413                                         dest.append(item);
414                         break;
415                         case TR_CUSTOM:
416                                 if (custom_translator)
417                                         custom_translator->EncodeParameter(item, i);
418                                 dest.append(item);
419                         break;
420                         case TR_END:
421                         case TR_TEXT:
422                         default:
423                                 /* Do nothing */
424                                 dest.append(item);
425                         break;
426                 }
427                 if (i != source.size() - 1)
428                         dest.append(" ");
429         }
430
431         return translations;
432 }
433
434 int CommandParser::TranslateUIDs(TranslateType to, const std::string &source, std::string &dest)
435 {
436         User* user = NULL;
437         std::string item;
438         int translations = 0;
439         dest.clear();
440
441         switch (to)
442         {
443                 case TR_NICK:
444                         /* Translate single nickname */
445                         user = ServerInstance->FindNick(source);
446                         if (user)
447                         {
448                                 dest = user->uuid;
449                                 translations++;
450                         }
451                         else
452                                 dest = source;
453                 break;
454                 case TR_END:
455                 case TR_TEXT:
456                 default:
457                         /* Do nothing */
458                         dest = source;
459                 break;
460         }
461
462         return translations;
463 }