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"
41 #include "socketengine.h"
42 #include "userprocess.h"
46 #include "command_parse.h"
49 #define nspace __gnu_cxx
51 /* Special commands which may occur without registration of the user */
52 cmd_user* command_user;
53 cmd_nick* command_nick;
54 cmd_pass* command_pass;
57 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
58 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
59 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
60 * the channel names and their keys as follows:
61 * JOIN #chan1,#chan2,#chan3 key1,,key3
62 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
63 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
64 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
65 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
67 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
69 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
70 * which called us just carries on as it was.
72 if (!strchr(parameters[splithere],','))
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 item = "";
82 /* Attempt to iterate these lists and call the command objech
83 * which called us, for every parameter pair until there are
84 * no more left to parse.
86 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
88 std::string extrastuff = items2.GetToken();
89 parameters[splithere] = item.c_str();
90 parameters[extra] = extrastuff.c_str();
91 CommandObj->Handle(parameters,pcnt,user);
96 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
98 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
99 * which called us just carries on as it was.
101 if (!strchr(parameters[splithere],','))
104 /* Only one commasepstream here */
105 irc::commasepstream items1(parameters[splithere]);
106 std::string item = "";
107 unsigned int max = 0;
109 /* Parse the commasepstream until there are no tokens remaining.
110 * Each token we parse out, call the command handler that called us
113 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
115 parameters[splithere] = item.c_str();
116 CommandObj->Handle(parameters,pcnt,user);
118 /* By returning 1 we tell our caller that nothing is to be done,
119 * as all the previous calls handled the data. This makes the parent
120 * return without doing any processing.
125 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
127 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
129 if (n != cmdlist.end())
131 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
133 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
135 if (n->second->flags_needed)
137 return ((user->HasPermission(commandname)) || (ServerInstance->ULine(user->server)));
146 // calls a handler function for a command
148 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
150 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
152 if (n != cmdlist.end())
154 if (pcnt >= n->second->min_params)
156 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
158 if (n->second->flags_needed)
160 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
162 n->second->Handle(parameters,pcnt,user);
168 n->second->Handle(parameters,pcnt,user);
177 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
179 const char *command_p[127];
181 std::string para[127];
182 irc::tokenstream tokens(cmd);
183 std::string command = tokens.GetToken();
185 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
186 command_p[items] = para[items++].c_str();
188 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
191 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
192 if (MOD_RESULT == 1) {
196 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
198 if (cm != cmdlist.end())
202 /* activity resets the ping pending timer */
203 user->nping = ServerInstance->Time() + user->pingmax;
204 if (cm->second->flags_needed)
206 if (!user->IsModeSet(cm->second->flags_needed))
208 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
211 if (!user->HasPermission(command))
213 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
217 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
219 /* command is disabled! */
220 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
223 if (items < cm->second->min_params)
225 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
226 /* If syntax is given, display this as the 461 reply */
227 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
228 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
231 if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
233 /* ikky /stats counters */
234 cm->second->use_count++;
235 cm->second->total_bytes += cmd.length();
238 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
243 * WARNING: nothing may come after the
244 * command handler call, as the handler
245 * may free the user structure!
248 cm->second->Handle(command_p,items,user);
253 user->WriteServ("451 %s :You have not registered",command.c_str());
260 ServerInstance->stats->statsUnknown++;
261 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
265 bool CommandParser::RemoveCommands(const char* source)
267 bool go_again = true;
273 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
275 command_t* x = i->second;
276 if (x->source == std::string(source))
278 ServerInstance->Log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
289 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
291 std::string::size_type a;
296 while ((a = buffer.find("\n")) != std::string::npos)
298 while ((a = buffer.find("\r")) != std::string::npos)
303 ServerInstance->Log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
304 this->ProcessCommand(user,buffer);
308 bool CommandParser::CreateCommand(command_t *f)
310 /* create the command and push it onto the table */
311 if (cmdlist.find(f->command) == cmdlist.end())
313 cmdlist[f->command] = f;
314 ServerInstance->Log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
320 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
322 this->SetupCommandTable();
325 void CommandParser::SetupCommandTable()
327 /* These three are special (can occur without
328 * full user registration) and so are saved
331 command_user = new cmd_user(ServerInstance);
332 command_nick = new cmd_nick(ServerInstance);
333 command_pass = new cmd_pass(ServerInstance);
334 this->CreateCommand(command_user);
335 this->CreateCommand(command_nick);
336 this->CreateCommand(command_pass);
338 /* The rest of these arent special. boo hoo.
340 this->CreateCommand(new cmd_quit(ServerInstance));
341 this->CreateCommand(new cmd_version(ServerInstance));
342 this->CreateCommand(new cmd_ping(ServerInstance));
343 this->CreateCommand(new cmd_pong(ServerInstance));
344 this->CreateCommand(new cmd_admin(ServerInstance));
345 this->CreateCommand(new cmd_privmsg(ServerInstance));
346 this->CreateCommand(new cmd_info(ServerInstance));
347 this->CreateCommand(new cmd_time(ServerInstance));
348 this->CreateCommand(new cmd_whois(ServerInstance));
349 this->CreateCommand(new cmd_wallops(ServerInstance));
350 this->CreateCommand(new cmd_notice(ServerInstance));
351 this->CreateCommand(new cmd_join(ServerInstance));
352 this->CreateCommand(new cmd_names(ServerInstance));
353 this->CreateCommand(new cmd_part(ServerInstance));
354 this->CreateCommand(new cmd_kick(ServerInstance));
355 this->CreateCommand(new cmd_mode(ServerInstance));
356 this->CreateCommand(new cmd_topic(ServerInstance));
357 this->CreateCommand(new cmd_who(ServerInstance));
358 this->CreateCommand(new cmd_motd(ServerInstance));
359 this->CreateCommand(new cmd_rules(ServerInstance));
360 this->CreateCommand(new cmd_oper(ServerInstance));
361 this->CreateCommand(new cmd_list(ServerInstance));
362 this->CreateCommand(new cmd_die(ServerInstance));
363 this->CreateCommand(new cmd_restart(ServerInstance));
364 this->CreateCommand(new cmd_kill(ServerInstance));
365 this->CreateCommand(new cmd_rehash(ServerInstance));
366 this->CreateCommand(new cmd_lusers(ServerInstance));
367 this->CreateCommand(new cmd_stats(ServerInstance));
368 this->CreateCommand(new cmd_userhost(ServerInstance));
369 this->CreateCommand(new cmd_away(ServerInstance));
370 this->CreateCommand(new cmd_ison(ServerInstance));
371 this->CreateCommand(new cmd_summon(ServerInstance));
372 this->CreateCommand(new cmd_users(ServerInstance));
373 this->CreateCommand(new cmd_invite(ServerInstance));
374 this->CreateCommand(new cmd_trace(ServerInstance));
375 this->CreateCommand(new cmd_whowas(ServerInstance));
376 this->CreateCommand(new cmd_connect(ServerInstance));
377 this->CreateCommand(new cmd_squit(ServerInstance));
378 this->CreateCommand(new cmd_modules(ServerInstance));
379 this->CreateCommand(new cmd_links(ServerInstance));
380 this->CreateCommand(new cmd_map(ServerInstance));
381 this->CreateCommand(new cmd_kline(ServerInstance));
382 this->CreateCommand(new cmd_gline(ServerInstance));
383 this->CreateCommand(new cmd_zline(ServerInstance));
384 this->CreateCommand(new cmd_qline(ServerInstance));
385 this->CreateCommand(new cmd_eline(ServerInstance));
386 this->CreateCommand(new cmd_loadmodule(ServerInstance));
387 this->CreateCommand(new cmd_unloadmodule(ServerInstance));
388 this->CreateCommand(new cmd_server(ServerInstance));
389 this->CreateCommand(new cmd_commands(ServerInstance));