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>
39 #include "inspstring.h"
40 #include "helperfuncs.h"
42 #include "socketengine.h"
43 #include "userprocess.h"
47 #include "command_parse.h"
50 #define nspace __gnu_cxx
52 extern InspIRCd* ServerInstance;
54 extern std::vector<Module*> modules;
55 extern std::vector<ircd_module*> factory;
56 extern std::vector<userrec*> local_users;
61 extern Server* MyServer;
63 extern user_hash clientlist;
64 extern chan_hash chanlist;
66 /* Special commands which may occur without registration of the user */
67 cmd_user* command_user;
68 cmd_nick* command_nick;
69 cmd_pass* command_pass;
72 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
73 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
74 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
75 * the channel names and their keys as follows:
76 * JOIN #chan1,#chan2,#chan3 key1,,key3
77 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
78 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
79 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
80 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
82 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
84 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
85 * which called us just carries on as it was.
87 if (!strchr(parameters[splithere],','))
90 /* Create two lists, one for channel names, one for keys
92 irc::commasepstream items1(parameters[splithere]);
93 irc::commasepstream items2(parameters[extra]);
94 std::string item = "";
97 /* Attempt to iterate these lists and call the command objech
98 * which called us, for every parameter pair until there are
99 * no more left to parse.
101 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
103 std::string extrastuff = items2.GetToken();
104 parameters[splithere] = item.c_str();
105 parameters[extra] = extrastuff.c_str();
106 CommandObj->Handle(parameters,pcnt,user);
111 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
113 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
114 * which called us just carries on as it was.
116 if (!strchr(parameters[splithere],','))
119 /* Only one commasepstream here */
120 irc::commasepstream items1(parameters[splithere]);
121 std::string item = "";
122 unsigned int max = 0;
124 /* Parse the commasepstream until there are no tokens remaining.
125 * Each token we parse out, call the command handler that called us
128 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
130 parameters[splithere] = item.c_str();
131 CommandObj->Handle(parameters,pcnt,user);
133 /* By returning 1 we tell our caller that nothing is to be done,
134 * as all the previous calls handled the data. This makes the parent
135 * return without doing any processing.
140 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
142 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
144 if (n != cmdlist.end())
146 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
148 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
150 if (n->second->flags_needed)
152 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
161 // calls a handler function for a command
163 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
165 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
167 if (n != cmdlist.end())
169 if (pcnt >= n->second->min_params)
171 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
173 if (n->second->flags_needed)
175 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
177 n->second->Handle(parameters,pcnt,user);
183 n->second->Handle(parameters,pcnt,user);
192 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
194 const char *command_p[127];
196 std::string para[127];
197 irc::tokenstream tokens(cmd);
198 std::string command = tokens.GetToken();
200 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
201 command_p[items] = para[items++].c_str();
203 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
206 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
207 if (MOD_RESULT == 1) {
211 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
213 if (cm != cmdlist.end())
217 /* activity resets the ping pending timer */
218 user->nping = TIME + user->pingmax;
219 if (cm->second->flags_needed)
221 if (!user->IsModeSet(cm->second->flags_needed))
223 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
226 if (!user->HasPermission(command))
228 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
232 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
234 /* command is disabled! */
235 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
238 if (items < cm->second->min_params)
240 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
241 /* If syntax is given, display this as the 461 reply */
242 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
243 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
246 if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
248 /* ikky /stats counters */
249 cm->second->use_count++;
250 cm->second->total_bytes += cmd.length();
253 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
258 * WARNING: nothing may come after the
259 * command handler call, as the handler
260 * may free the user structure!
263 cm->second->Handle(command_p,items,user);
268 user->WriteServ("451 %s :You have not registered",command.c_str());
275 ServerInstance->stats->statsUnknown++;
276 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
280 bool CommandParser::RemoveCommands(const char* source)
282 bool go_again = true;
288 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
290 command_t* x = i->second;
291 if (x->source == std::string(source))
293 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
304 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
306 std::string::size_type a;
311 while ((a = buffer.find("\n")) != std::string::npos)
313 while ((a = buffer.find("\r")) != std::string::npos)
318 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
319 this->ProcessCommand(user,buffer);
323 bool CommandParser::CreateCommand(command_t *f)
325 /* create the command and push it onto the table */
326 if (cmdlist.find(f->command) == cmdlist.end())
328 cmdlist[f->command] = f;
329 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
335 CommandParser::CommandParser()
337 this->SetupCommandTable();
340 void CommandParser::SetupCommandTable()
342 /* These three are special (can occur without
343 * full user registration) and so are saved
346 command_user = new cmd_user;
347 command_nick = new cmd_nick;
348 command_pass = new cmd_pass;
349 this->CreateCommand(command_user);
350 this->CreateCommand(command_nick);
351 this->CreateCommand(command_pass);
353 /* The rest of these arent special. boo hoo.
355 this->CreateCommand(new cmd_quit);
356 this->CreateCommand(new cmd_version);
357 this->CreateCommand(new cmd_ping);
358 this->CreateCommand(new cmd_pong);
359 this->CreateCommand(new cmd_admin);
360 this->CreateCommand(new cmd_privmsg);
361 this->CreateCommand(new cmd_info);
362 this->CreateCommand(new cmd_time);
363 this->CreateCommand(new cmd_whois);
364 this->CreateCommand(new cmd_wallops);
365 this->CreateCommand(new cmd_notice);
366 this->CreateCommand(new cmd_join);
367 this->CreateCommand(new cmd_names);
368 this->CreateCommand(new cmd_part);
369 this->CreateCommand(new cmd_kick);
370 this->CreateCommand(new cmd_mode);
371 this->CreateCommand(new cmd_topic);
372 this->CreateCommand(new cmd_who);
373 this->CreateCommand(new cmd_motd);
374 this->CreateCommand(new cmd_rules);
375 this->CreateCommand(new cmd_oper);
376 this->CreateCommand(new cmd_list);
377 this->CreateCommand(new cmd_die);
378 this->CreateCommand(new cmd_restart);
379 this->CreateCommand(new cmd_kill);
380 this->CreateCommand(new cmd_rehash);
381 this->CreateCommand(new cmd_lusers);
382 this->CreateCommand(new cmd_stats);
383 this->CreateCommand(new cmd_userhost);
384 this->CreateCommand(new cmd_away);
385 this->CreateCommand(new cmd_ison);
386 this->CreateCommand(new cmd_summon);
387 this->CreateCommand(new cmd_users);
388 this->CreateCommand(new cmd_invite);
389 this->CreateCommand(new cmd_trace);
390 this->CreateCommand(new cmd_whowas);
391 this->CreateCommand(new cmd_connect);
392 this->CreateCommand(new cmd_squit);
393 this->CreateCommand(new cmd_modules);
394 this->CreateCommand(new cmd_links);
395 this->CreateCommand(new cmd_map);
396 this->CreateCommand(new cmd_kline);
397 this->CreateCommand(new cmd_gline);
398 this->CreateCommand(new cmd_zline);
399 this->CreateCommand(new cmd_qline);
400 this->CreateCommand(new cmd_eline);
401 this->CreateCommand(new cmd_loadmodule);
402 this->CreateCommand(new cmd_unloadmodule);
403 this->CreateCommand(new cmd_server);
404 this->CreateCommand(new cmd_commands);