1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include "inspircd_config.h"
19 #include "configreader.h"
22 #include <sys/errno.h>
23 #include <sys/ioctl.h>
24 #include <sys/utsname.h>
38 #include "inspstring.h"
39 #include "helperfuncs.h"
41 #include "socketengine.h"
42 #include "userprocess.h"
46 #include "command_parse.h"
49 #define nspace __gnu_cxx
53 /* Special commands which may occur without registration of the user */
54 cmd_user* command_user;
55 cmd_nick* command_nick;
56 cmd_pass* command_pass;
59 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
60 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
61 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
62 * the channel names and their keys as follows:
63 * JOIN #chan1,#chan2,#chan3 key1,,key3
64 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
65 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
66 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
67 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
69 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
71 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
72 * which called us just carries on as it was.
74 if (!strchr(parameters[splithere],','))
77 /* Create two lists, one for channel names, one for keys
79 irc::commasepstream items1(parameters[splithere]);
80 irc::commasepstream items2(parameters[extra]);
81 std::string item = "";
84 /* Attempt to iterate these lists and call the command objech
85 * which called us, for every parameter pair until there are
86 * no more left to parse.
88 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
90 std::string extrastuff = items2.GetToken();
91 parameters[splithere] = item.c_str();
92 parameters[extra] = extrastuff.c_str();
93 CommandObj->Handle(parameters,pcnt,user);
98 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
100 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
101 * which called us just carries on as it was.
103 if (!strchr(parameters[splithere],','))
106 /* Only one commasepstream here */
107 irc::commasepstream items1(parameters[splithere]);
108 std::string item = "";
109 unsigned int max = 0;
111 /* Parse the commasepstream until there are no tokens remaining.
112 * Each token we parse out, call the command handler that called us
115 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
117 parameters[splithere] = item.c_str();
118 CommandObj->Handle(parameters,pcnt,user);
120 /* By returning 1 we tell our caller that nothing is to be done,
121 * as all the previous calls handled the data. This makes the parent
122 * return without doing any processing.
127 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
129 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
131 if (n != cmdlist.end())
133 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
135 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
137 if (n->second->flags_needed)
139 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
148 // calls a handler function for a command
150 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
152 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
154 if (n != cmdlist.end())
156 if (pcnt >= n->second->min_params)
158 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
160 if (n->second->flags_needed)
162 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
164 n->second->Handle(parameters,pcnt,user);
170 n->second->Handle(parameters,pcnt,user);
179 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
181 const char *command_p[127];
183 std::string para[127];
184 irc::tokenstream tokens(cmd);
185 std::string command = tokens.GetToken();
187 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
188 command_p[items] = para[items++].c_str();
190 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
193 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
194 if (MOD_RESULT == 1) {
198 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
200 if (cm != cmdlist.end())
204 /* activity resets the ping pending timer */
205 user->nping = TIME + user->pingmax;
206 if (cm->second->flags_needed)
208 if (!user->IsModeSet(cm->second->flags_needed))
210 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
213 if (!user->HasPermission(command))
215 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
219 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
221 /* command is disabled! */
222 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
225 if (items < cm->second->min_params)
227 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
228 /* If syntax is given, display this as the 461 reply */
229 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
230 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
233 if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
235 /* ikky /stats counters */
236 cm->second->use_count++;
237 cm->second->total_bytes += cmd.length();
240 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
245 * WARNING: nothing may come after the
246 * command handler call, as the handler
247 * may free the user structure!
250 cm->second->Handle(command_p,items,user);
255 user->WriteServ("451 %s :You have not registered",command.c_str());
262 ServerInstance->stats->statsUnknown++;
263 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
267 bool CommandParser::RemoveCommands(const char* source)
269 bool go_again = true;
275 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
277 command_t* x = i->second;
278 if (x->source == std::string(source))
280 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
291 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
293 std::string::size_type a;
298 while ((a = buffer.find("\n")) != std::string::npos)
300 while ((a = buffer.find("\r")) != std::string::npos)
305 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
306 this->ProcessCommand(user,buffer);
310 bool CommandParser::CreateCommand(command_t *f)
312 /* create the command and push it onto the table */
313 if (cmdlist.find(f->command) == cmdlist.end())
315 cmdlist[f->command] = f;
316 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
322 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
324 this->SetupCommandTable();
327 void CommandParser::SetupCommandTable()
329 /* These three are special (can occur without
330 * full user registration) and so are saved
333 command_user = new cmd_user;
334 command_nick = new cmd_nick;
335 command_pass = new cmd_pass;
336 this->CreateCommand(command_user);
337 this->CreateCommand(command_nick);
338 this->CreateCommand(command_pass);
340 /* The rest of these arent special. boo hoo.
342 this->CreateCommand(new cmd_quit);
343 this->CreateCommand(new cmd_version);
344 this->CreateCommand(new cmd_ping);
345 this->CreateCommand(new cmd_pong);
346 this->CreateCommand(new cmd_admin);
347 this->CreateCommand(new cmd_privmsg);
348 this->CreateCommand(new cmd_info);
349 this->CreateCommand(new cmd_time);
350 this->CreateCommand(new cmd_whois);
351 this->CreateCommand(new cmd_wallops);
352 this->CreateCommand(new cmd_notice);
353 this->CreateCommand(new cmd_join);
354 this->CreateCommand(new cmd_names);
355 this->CreateCommand(new cmd_part);
356 this->CreateCommand(new cmd_kick);
357 this->CreateCommand(new cmd_mode);
358 this->CreateCommand(new cmd_topic);
359 this->CreateCommand(new cmd_who);
360 this->CreateCommand(new cmd_motd);
361 this->CreateCommand(new cmd_rules);
362 this->CreateCommand(new cmd_oper);
363 this->CreateCommand(new cmd_list);
364 this->CreateCommand(new cmd_die);
365 this->CreateCommand(new cmd_restart);
366 this->CreateCommand(new cmd_kill);
367 this->CreateCommand(new cmd_rehash);
368 this->CreateCommand(new cmd_lusers);
369 this->CreateCommand(new cmd_stats);
370 this->CreateCommand(new cmd_userhost);
371 this->CreateCommand(new cmd_away);
372 this->CreateCommand(new cmd_ison);
373 this->CreateCommand(new cmd_summon);
374 this->CreateCommand(new cmd_users);
375 this->CreateCommand(new cmd_invite);
376 this->CreateCommand(new cmd_trace);
377 this->CreateCommand(new cmd_whowas);
378 this->CreateCommand(new cmd_connect);
379 this->CreateCommand(new cmd_squit);
380 this->CreateCommand(new cmd_modules);
381 this->CreateCommand(new cmd_links);
382 this->CreateCommand(new cmd_map);
383 this->CreateCommand(new cmd_kline);
384 this->CreateCommand(new cmd_gline);
385 this->CreateCommand(new cmd_zline);
386 this->CreateCommand(new cmd_qline);
387 this->CreateCommand(new cmd_eline);
388 this->CreateCommand(new cmd_loadmodule);
389 this->CreateCommand(new cmd_unloadmodule);
390 this->CreateCommand(new cmd_server);
391 this->CreateCommand(new cmd_commands);