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;
53 extern std::vector<Module*> modules;
54 extern std::vector<ircd_module*> factory;
59 extern Server* MyServer;
61 extern chan_hash chanlist;
63 /* Special commands which may occur without registration of the user */
64 cmd_user* command_user;
65 cmd_nick* command_nick;
66 cmd_pass* command_pass;
69 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
70 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
71 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
72 * the channel names and their keys as follows:
73 * JOIN #chan1,#chan2,#chan3 key1,,key3
74 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
75 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
76 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
77 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
79 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
81 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
82 * which called us just carries on as it was.
84 if (!strchr(parameters[splithere],','))
87 /* Create two lists, one for channel names, one for keys
89 irc::commasepstream items1(parameters[splithere]);
90 irc::commasepstream items2(parameters[extra]);
91 std::string item = "";
94 /* Attempt to iterate these lists and call the command objech
95 * which called us, for every parameter pair until there are
96 * no more left to parse.
98 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
100 std::string extrastuff = items2.GetToken();
101 parameters[splithere] = item.c_str();
102 parameters[extra] = extrastuff.c_str();
103 CommandObj->Handle(parameters,pcnt,user);
108 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
110 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
111 * which called us just carries on as it was.
113 if (!strchr(parameters[splithere],','))
116 /* Only one commasepstream here */
117 irc::commasepstream items1(parameters[splithere]);
118 std::string item = "";
119 unsigned int max = 0;
121 /* Parse the commasepstream until there are no tokens remaining.
122 * Each token we parse out, call the command handler that called us
125 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
127 parameters[splithere] = item.c_str();
128 CommandObj->Handle(parameters,pcnt,user);
130 /* By returning 1 we tell our caller that nothing is to be done,
131 * as all the previous calls handled the data. This makes the parent
132 * return without doing any processing.
137 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
139 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
141 if (n != cmdlist.end())
143 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
145 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
147 if (n->second->flags_needed)
149 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
158 // calls a handler function for a command
160 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
162 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
164 if (n != cmdlist.end())
166 if (pcnt >= n->second->min_params)
168 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
170 if (n->second->flags_needed)
172 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
174 n->second->Handle(parameters,pcnt,user);
180 n->second->Handle(parameters,pcnt,user);
189 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
191 const char *command_p[127];
193 std::string para[127];
194 irc::tokenstream tokens(cmd);
195 std::string command = tokens.GetToken();
197 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
198 command_p[items] = para[items++].c_str();
200 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
203 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
204 if (MOD_RESULT == 1) {
208 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
210 if (cm != cmdlist.end())
214 /* activity resets the ping pending timer */
215 user->nping = TIME + user->pingmax;
216 if (cm->second->flags_needed)
218 if (!user->IsModeSet(cm->second->flags_needed))
220 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
223 if (!user->HasPermission(command))
225 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
229 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
231 /* command is disabled! */
232 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
235 if (items < cm->second->min_params)
237 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
238 /* If syntax is given, display this as the 461 reply */
239 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
240 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
243 if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
245 /* ikky /stats counters */
246 cm->second->use_count++;
247 cm->second->total_bytes += cmd.length();
250 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
255 * WARNING: nothing may come after the
256 * command handler call, as the handler
257 * may free the user structure!
260 cm->second->Handle(command_p,items,user);
265 user->WriteServ("451 %s :You have not registered",command.c_str());
272 ServerInstance->stats->statsUnknown++;
273 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
277 bool CommandParser::RemoveCommands(const char* source)
279 bool go_again = true;
285 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
287 command_t* x = i->second;
288 if (x->source == std::string(source))
290 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
301 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
303 std::string::size_type a;
308 while ((a = buffer.find("\n")) != std::string::npos)
310 while ((a = buffer.find("\r")) != std::string::npos)
315 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
316 this->ProcessCommand(user,buffer);
320 bool CommandParser::CreateCommand(command_t *f)
322 /* create the command and push it onto the table */
323 if (cmdlist.find(f->command) == cmdlist.end())
325 cmdlist[f->command] = f;
326 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
332 CommandParser::CommandParser()
334 this->SetupCommandTable();
337 void CommandParser::SetupCommandTable()
339 /* These three are special (can occur without
340 * full user registration) and so are saved
343 command_user = new cmd_user;
344 command_nick = new cmd_nick;
345 command_pass = new cmd_pass;
346 this->CreateCommand(command_user);
347 this->CreateCommand(command_nick);
348 this->CreateCommand(command_pass);
350 /* The rest of these arent special. boo hoo.
352 this->CreateCommand(new cmd_quit);
353 this->CreateCommand(new cmd_version);
354 this->CreateCommand(new cmd_ping);
355 this->CreateCommand(new cmd_pong);
356 this->CreateCommand(new cmd_admin);
357 this->CreateCommand(new cmd_privmsg);
358 this->CreateCommand(new cmd_info);
359 this->CreateCommand(new cmd_time);
360 this->CreateCommand(new cmd_whois);
361 this->CreateCommand(new cmd_wallops);
362 this->CreateCommand(new cmd_notice);
363 this->CreateCommand(new cmd_join);
364 this->CreateCommand(new cmd_names);
365 this->CreateCommand(new cmd_part);
366 this->CreateCommand(new cmd_kick);
367 this->CreateCommand(new cmd_mode);
368 this->CreateCommand(new cmd_topic);
369 this->CreateCommand(new cmd_who);
370 this->CreateCommand(new cmd_motd);
371 this->CreateCommand(new cmd_rules);
372 this->CreateCommand(new cmd_oper);
373 this->CreateCommand(new cmd_list);
374 this->CreateCommand(new cmd_die);
375 this->CreateCommand(new cmd_restart);
376 this->CreateCommand(new cmd_kill);
377 this->CreateCommand(new cmd_rehash);
378 this->CreateCommand(new cmd_lusers);
379 this->CreateCommand(new cmd_stats);
380 this->CreateCommand(new cmd_userhost);
381 this->CreateCommand(new cmd_away);
382 this->CreateCommand(new cmd_ison);
383 this->CreateCommand(new cmd_summon);
384 this->CreateCommand(new cmd_users);
385 this->CreateCommand(new cmd_invite);
386 this->CreateCommand(new cmd_trace);
387 this->CreateCommand(new cmd_whowas);
388 this->CreateCommand(new cmd_connect);
389 this->CreateCommand(new cmd_squit);
390 this->CreateCommand(new cmd_modules);
391 this->CreateCommand(new cmd_links);
392 this->CreateCommand(new cmd_map);
393 this->CreateCommand(new cmd_kline);
394 this->CreateCommand(new cmd_gline);
395 this->CreateCommand(new cmd_zline);
396 this->CreateCommand(new cmd_qline);
397 this->CreateCommand(new cmd_eline);
398 this->CreateCommand(new cmd_loadmodule);
399 this->CreateCommand(new cmd_unloadmodule);
400 this->CreateCommand(new cmd_server);
401 this->CreateCommand(new cmd_commands);