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
51 extern InspIRCd* ServerInstance;
55 extern Server* MyServer;
57 /* Special commands which may occur without registration of the user */
58 cmd_user* command_user;
59 cmd_nick* command_nick;
60 cmd_pass* command_pass;
63 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
64 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
65 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
66 * the channel names and their keys as follows:
67 * JOIN #chan1,#chan2,#chan3 key1,,key3
68 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
69 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
70 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
71 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
73 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
75 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
76 * which called us just carries on as it was.
78 if (!strchr(parameters[splithere],','))
81 /* Create two lists, one for channel names, one for keys
83 irc::commasepstream items1(parameters[splithere]);
84 irc::commasepstream items2(parameters[extra]);
85 std::string item = "";
88 /* Attempt to iterate these lists and call the command objech
89 * which called us, for every parameter pair until there are
90 * no more left to parse.
92 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
94 std::string extrastuff = items2.GetToken();
95 parameters[splithere] = item.c_str();
96 parameters[extra] = extrastuff.c_str();
97 CommandObj->Handle(parameters,pcnt,user);
102 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
104 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
105 * which called us just carries on as it was.
107 if (!strchr(parameters[splithere],','))
110 /* Only one commasepstream here */
111 irc::commasepstream items1(parameters[splithere]);
112 std::string item = "";
113 unsigned int max = 0;
115 /* Parse the commasepstream until there are no tokens remaining.
116 * Each token we parse out, call the command handler that called us
119 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
121 parameters[splithere] = item.c_str();
122 CommandObj->Handle(parameters,pcnt,user);
124 /* By returning 1 we tell our caller that nothing is to be done,
125 * as all the previous calls handled the data. This makes the parent
126 * return without doing any processing.
131 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
133 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
135 if (n != cmdlist.end())
137 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
139 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
141 if (n->second->flags_needed)
143 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
152 // calls a handler function for a command
154 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
156 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
158 if (n != cmdlist.end())
160 if (pcnt >= n->second->min_params)
162 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
164 if (n->second->flags_needed)
166 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
168 n->second->Handle(parameters,pcnt,user);
174 n->second->Handle(parameters,pcnt,user);
183 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
185 const char *command_p[127];
187 std::string para[127];
188 irc::tokenstream tokens(cmd);
189 std::string command = tokens.GetToken();
191 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
192 command_p[items] = para[items++].c_str();
194 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
197 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
198 if (MOD_RESULT == 1) {
202 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
204 if (cm != cmdlist.end())
208 /* activity resets the ping pending timer */
209 user->nping = TIME + user->pingmax;
210 if (cm->second->flags_needed)
212 if (!user->IsModeSet(cm->second->flags_needed))
214 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
217 if (!user->HasPermission(command))
219 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
223 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
225 /* command is disabled! */
226 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
229 if (items < cm->second->min_params)
231 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
232 /* If syntax is given, display this as the 461 reply */
233 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
234 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
237 if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
239 /* ikky /stats counters */
240 cm->second->use_count++;
241 cm->second->total_bytes += cmd.length();
244 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
249 * WARNING: nothing may come after the
250 * command handler call, as the handler
251 * may free the user structure!
254 cm->second->Handle(command_p,items,user);
259 user->WriteServ("451 %s :You have not registered",command.c_str());
266 ServerInstance->stats->statsUnknown++;
267 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
271 bool CommandParser::RemoveCommands(const char* source)
273 bool go_again = true;
279 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
281 command_t* x = i->second;
282 if (x->source == std::string(source))
284 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
295 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
297 std::string::size_type a;
302 while ((a = buffer.find("\n")) != std::string::npos)
304 while ((a = buffer.find("\r")) != std::string::npos)
309 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
310 this->ProcessCommand(user,buffer);
314 bool CommandParser::CreateCommand(command_t *f)
316 /* create the command and push it onto the table */
317 if (cmdlist.find(f->command) == cmdlist.end())
319 cmdlist[f->command] = f;
320 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
326 CommandParser::CommandParser()
328 this->SetupCommandTable();
331 void CommandParser::SetupCommandTable()
333 /* These three are special (can occur without
334 * full user registration) and so are saved
337 command_user = new cmd_user;
338 command_nick = new cmd_nick;
339 command_pass = new cmd_pass;
340 this->CreateCommand(command_user);
341 this->CreateCommand(command_nick);
342 this->CreateCommand(command_pass);
344 /* The rest of these arent special. boo hoo.
346 this->CreateCommand(new cmd_quit);
347 this->CreateCommand(new cmd_version);
348 this->CreateCommand(new cmd_ping);
349 this->CreateCommand(new cmd_pong);
350 this->CreateCommand(new cmd_admin);
351 this->CreateCommand(new cmd_privmsg);
352 this->CreateCommand(new cmd_info);
353 this->CreateCommand(new cmd_time);
354 this->CreateCommand(new cmd_whois);
355 this->CreateCommand(new cmd_wallops);
356 this->CreateCommand(new cmd_notice);
357 this->CreateCommand(new cmd_join);
358 this->CreateCommand(new cmd_names);
359 this->CreateCommand(new cmd_part);
360 this->CreateCommand(new cmd_kick);
361 this->CreateCommand(new cmd_mode);
362 this->CreateCommand(new cmd_topic);
363 this->CreateCommand(new cmd_who);
364 this->CreateCommand(new cmd_motd);
365 this->CreateCommand(new cmd_rules);
366 this->CreateCommand(new cmd_oper);
367 this->CreateCommand(new cmd_list);
368 this->CreateCommand(new cmd_die);
369 this->CreateCommand(new cmd_restart);
370 this->CreateCommand(new cmd_kill);
371 this->CreateCommand(new cmd_rehash);
372 this->CreateCommand(new cmd_lusers);
373 this->CreateCommand(new cmd_stats);
374 this->CreateCommand(new cmd_userhost);
375 this->CreateCommand(new cmd_away);
376 this->CreateCommand(new cmd_ison);
377 this->CreateCommand(new cmd_summon);
378 this->CreateCommand(new cmd_users);
379 this->CreateCommand(new cmd_invite);
380 this->CreateCommand(new cmd_trace);
381 this->CreateCommand(new cmd_whowas);
382 this->CreateCommand(new cmd_connect);
383 this->CreateCommand(new cmd_squit);
384 this->CreateCommand(new cmd_modules);
385 this->CreateCommand(new cmd_links);
386 this->CreateCommand(new cmd_map);
387 this->CreateCommand(new cmd_kline);
388 this->CreateCommand(new cmd_gline);
389 this->CreateCommand(new cmd_zline);
390 this->CreateCommand(new cmd_qline);
391 this->CreateCommand(new cmd_eline);
392 this->CreateCommand(new cmd_loadmodule);
393 this->CreateCommand(new cmd_unloadmodule);
394 this->CreateCommand(new cmd_server);
395 this->CreateCommand(new cmd_commands);