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>
42 #include "inspstring.h"
44 #include "helperfuncs.h"
46 #include "socketengine.h"
47 #include "userprocess.h"
51 #include "command_parse.h"
54 #define nspace __gnu_cxx
56 extern InspIRCd* ServerInstance;
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<InspSocket*> module_sockets;
61 extern std::vector<userrec*> local_users;
64 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
67 // This table references users by file descriptor.
68 // its an array to make it VERY fast, as all lookups are referenced
69 // by an integer, meaning there is no need for a scan/search operation.
70 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
72 extern Server* MyServer;
73 extern ServerConfig *Config;
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
78 /* Special commands which may occur without registration of the user */
79 cmd_user* command_user;
80 cmd_nick* command_nick;
81 cmd_pass* command_pass;
84 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
85 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
86 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
87 * the channel names and their keys as follows:
88 * JOIN #chan1,#chan2,#chan3 key1,,key3
89 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
90 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
91 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
92 * Both will only parse until they reach Config->MaxTargets number of targets, to stop abuse via spam.
94 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
96 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
97 * which called us just carries on as it was.
99 if (!strchr(parameters[splithere],','))
102 /* Create two lists, one for channel names, one for keys
104 irc::commasepstream items1(parameters[splithere]);
105 irc::commasepstream items2(parameters[extra]);
106 std::string item = "";
107 unsigned int max = 0;
109 /* Attempt to iterate these lists and call the command objech
110 * which called us, for every parameter pair until there are
111 * no more left to parse.
113 while (((item = items1.GetToken()) != "") && (max++ < Config->MaxTargets))
115 std::string extrastuff = items2.GetToken();
116 parameters[splithere] = item.c_str();
117 parameters[extra] = extrastuff.c_str();
118 CommandObj->Handle(parameters,pcnt,user);
123 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
125 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
126 * which called us just carries on as it was.
128 if (!strchr(parameters[splithere],','))
131 /* Only one commasepstream here */
132 irc::commasepstream items1(parameters[splithere]);
133 std::string item = "";
134 unsigned int max = 0;
136 /* Parse the commasepstream until there are no tokens remaining.
137 * Each token we parse out, call the command handler that called us
140 while (((item = items1.GetToken()) != "") && (max++ < Config->MaxTargets))
142 parameters[splithere] = item.c_str();
143 CommandObj->Handle(parameters,pcnt,user);
145 /* By returning 1 we tell our caller that nothing is to be done,
146 * as all the previous calls handled the data. This makes the parent
147 * return without doing any processing.
152 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
154 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
156 if (n != cmdlist.end())
158 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
160 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
162 if (n->second->flags_needed)
164 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
173 // calls a handler function for a command
175 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
177 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
179 if (n != cmdlist.end())
181 if (pcnt >= n->second->min_params)
183 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
185 if (n->second->flags_needed)
187 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
189 n->second->Handle(parameters,pcnt,user);
195 n->second->Handle(parameters,pcnt,user);
204 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
206 const char *command_p[127];
208 std::string para[127];
209 irc::tokenstream tokens(cmd);
210 std::string command = tokens.GetToken();
212 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
213 command_p[items] = para[items++].c_str();
215 for (std::string::iterator makeupper = command.begin(); makeupper != command.end(); makeupper++)
216 *makeupper = toupper(*makeupper);
219 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
220 if (MOD_RESULT == 1) {
224 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
226 if (cm != cmdlist.end())
230 /* activity resets the ping pending timer */
231 user->nping = TIME + user->pingmax;
232 if (items < cm->second->min_params)
234 log(DEBUG,"not enough parameters: %s %s",user->nick,command.c_str());
235 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command.c_str());
238 if (cm->second->flags_needed)
240 if (!user->IsModeSet(cm->second->flags_needed))
242 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
243 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
247 if ((cm->second->flags_needed) && (!user->HasPermission(command)))
249 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
250 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
253 if ((user->registered == 7) && (!*user->oper) && (cm->second->IsDisabled()))
255 /* command is disabled! */
256 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command.c_str());
259 if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
261 /* ikky /stats counters */
262 cm->second->use_count++;
263 cm->second->total_bytes += cmd.length();
266 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
271 * WARNING: nothing may come after the
272 * command handler call, as the handler
273 * may free the user structure!
276 cm->second->Handle(command_p,items,user);
281 WriteServ(user->fd,"451 %s :You have not registered",command.c_str());
288 ServerInstance->stats->statsUnknown++;
289 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command.c_str());
293 bool CommandParser::RemoveCommands(const char* source)
295 bool go_again = true;
301 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
303 command_t* x = i->second;
304 if (x->source == std::string(source))
306 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
317 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
319 std::string::size_type a;
324 while ((a = buffer.find("\n")) != std::string::npos)
326 while ((a = buffer.find("\r")) != std::string::npos)
331 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
332 this->ProcessCommand(user,buffer);
336 bool CommandParser::CreateCommand(command_t *f)
338 /* create the command and push it onto the table */
339 if (cmdlist.find(f->command) == cmdlist.end())
341 cmdlist[f->command] = f;
342 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
348 CommandParser::CommandParser()
350 this->SetupCommandTable();
353 void CommandParser::SetupCommandTable()
355 /* These three are special (can occur without
356 * full user registration) and so are saved
359 command_user = new cmd_user;
360 command_nick = new cmd_nick;
361 command_pass = new cmd_pass;
362 this->CreateCommand(command_user);
363 this->CreateCommand(command_nick);
364 this->CreateCommand(command_pass);
366 /* The rest of these arent special. boo hoo.
368 this->CreateCommand(new cmd_quit);
369 this->CreateCommand(new cmd_version);
370 this->CreateCommand(new cmd_ping);
371 this->CreateCommand(new cmd_pong);
372 this->CreateCommand(new cmd_admin);
373 this->CreateCommand(new cmd_privmsg);
374 this->CreateCommand(new cmd_info);
375 this->CreateCommand(new cmd_time);
376 this->CreateCommand(new cmd_whois);
377 this->CreateCommand(new cmd_wallops);
378 this->CreateCommand(new cmd_notice);
379 this->CreateCommand(new cmd_join);
380 this->CreateCommand(new cmd_names);
381 this->CreateCommand(new cmd_part);
382 this->CreateCommand(new cmd_kick);
383 this->CreateCommand(new cmd_mode);
384 this->CreateCommand(new cmd_topic);
385 this->CreateCommand(new cmd_who);
386 this->CreateCommand(new cmd_motd);
387 this->CreateCommand(new cmd_rules);
388 this->CreateCommand(new cmd_oper);
389 this->CreateCommand(new cmd_list);
390 this->CreateCommand(new cmd_die);
391 this->CreateCommand(new cmd_restart);
392 this->CreateCommand(new cmd_kill);
393 this->CreateCommand(new cmd_rehash);
394 this->CreateCommand(new cmd_lusers);
395 this->CreateCommand(new cmd_stats);
396 this->CreateCommand(new cmd_userhost);
397 this->CreateCommand(new cmd_away);
398 this->CreateCommand(new cmd_ison);
399 this->CreateCommand(new cmd_summon);
400 this->CreateCommand(new cmd_users);
401 this->CreateCommand(new cmd_invite);
402 this->CreateCommand(new cmd_trace);
403 this->CreateCommand(new cmd_whowas);
404 this->CreateCommand(new cmd_connect);
405 this->CreateCommand(new cmd_squit);
406 this->CreateCommand(new cmd_modules);
407 this->CreateCommand(new cmd_links);
408 this->CreateCommand(new cmd_map);
409 this->CreateCommand(new cmd_kline);
410 this->CreateCommand(new cmd_gline);
411 this->CreateCommand(new cmd_zline);
412 this->CreateCommand(new cmd_qline);
413 this->CreateCommand(new cmd_eline);
414 this->CreateCommand(new cmd_loadmodule);
415 this->CreateCommand(new cmd_unloadmodule);
416 this->CreateCommand(new cmd_server);
417 this->CreateCommand(new cmd_commands);