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 // This table references users by file descriptor.
62 // its an array to make it VERY fast, as all lookups are referenced
63 // by an integer, meaning there is no need for a scan/search operation.
64 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
66 extern Server* MyServer;
68 extern user_hash clientlist;
69 extern chan_hash chanlist;
71 /* Special commands which may occur without registration of the user */
72 cmd_user* command_user;
73 cmd_nick* command_nick;
74 cmd_pass* command_pass;
77 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
78 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
79 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
80 * the channel names and their keys as follows:
81 * JOIN #chan1,#chan2,#chan3 key1,,key3
82 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
83 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
84 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
85 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
87 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
89 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
90 * which called us just carries on as it was.
92 if (!strchr(parameters[splithere],','))
95 /* Create two lists, one for channel names, one for keys
97 irc::commasepstream items1(parameters[splithere]);
98 irc::commasepstream items2(parameters[extra]);
99 std::string item = "";
100 unsigned int max = 0;
102 /* Attempt to iterate these lists and call the command objech
103 * which called us, for every parameter pair until there are
104 * no more left to parse.
106 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
108 std::string extrastuff = items2.GetToken();
109 parameters[splithere] = item.c_str();
110 parameters[extra] = extrastuff.c_str();
111 CommandObj->Handle(parameters,pcnt,user);
116 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
118 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
119 * which called us just carries on as it was.
121 if (!strchr(parameters[splithere],','))
124 /* Only one commasepstream here */
125 irc::commasepstream items1(parameters[splithere]);
126 std::string item = "";
127 unsigned int max = 0;
129 /* Parse the commasepstream until there are no tokens remaining.
130 * Each token we parse out, call the command handler that called us
133 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
135 parameters[splithere] = item.c_str();
136 CommandObj->Handle(parameters,pcnt,user);
138 /* By returning 1 we tell our caller that nothing is to be done,
139 * as all the previous calls handled the data. This makes the parent
140 * return without doing any processing.
145 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
147 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
149 if (n != cmdlist.end())
151 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
153 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
155 if (n->second->flags_needed)
157 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
166 // calls a handler function for a command
168 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
170 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
172 if (n != cmdlist.end())
174 if (pcnt >= n->second->min_params)
176 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
178 if (n->second->flags_needed)
180 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
182 n->second->Handle(parameters,pcnt,user);
188 n->second->Handle(parameters,pcnt,user);
197 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
199 const char *command_p[127];
201 std::string para[127];
202 irc::tokenstream tokens(cmd);
203 std::string command = tokens.GetToken();
205 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
206 command_p[items] = para[items++].c_str();
208 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
211 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
212 if (MOD_RESULT == 1) {
216 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
218 if (cm != cmdlist.end())
222 /* activity resets the ping pending timer */
223 user->nping = TIME + user->pingmax;
224 if (cm->second->flags_needed)
226 if (!user->IsModeSet(cm->second->flags_needed))
228 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
231 if (!user->HasPermission(command))
233 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
237 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
239 /* command is disabled! */
240 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
243 if (items < cm->second->min_params)
245 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
246 /* If syntax is given, display this as the 461 reply */
247 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
248 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
251 if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
253 /* ikky /stats counters */
254 cm->second->use_count++;
255 cm->second->total_bytes += cmd.length();
258 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
263 * WARNING: nothing may come after the
264 * command handler call, as the handler
265 * may free the user structure!
268 cm->second->Handle(command_p,items,user);
273 user->WriteServ("451 %s :You have not registered",command.c_str());
280 ServerInstance->stats->statsUnknown++;
281 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
285 bool CommandParser::RemoveCommands(const char* source)
287 bool go_again = true;
293 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
295 command_t* x = i->second;
296 if (x->source == std::string(source))
298 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
309 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
311 std::string::size_type a;
316 while ((a = buffer.find("\n")) != std::string::npos)
318 while ((a = buffer.find("\r")) != std::string::npos)
323 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
324 this->ProcessCommand(user,buffer);
328 bool CommandParser::CreateCommand(command_t *f)
330 /* create the command and push it onto the table */
331 if (cmdlist.find(f->command) == cmdlist.end())
333 cmdlist[f->command] = f;
334 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
340 CommandParser::CommandParser()
342 this->SetupCommandTable();
345 void CommandParser::SetupCommandTable()
347 /* These three are special (can occur without
348 * full user registration) and so are saved
351 command_user = new cmd_user;
352 command_nick = new cmd_nick;
353 command_pass = new cmd_pass;
354 this->CreateCommand(command_user);
355 this->CreateCommand(command_nick);
356 this->CreateCommand(command_pass);
358 /* The rest of these arent special. boo hoo.
360 this->CreateCommand(new cmd_quit);
361 this->CreateCommand(new cmd_version);
362 this->CreateCommand(new cmd_ping);
363 this->CreateCommand(new cmd_pong);
364 this->CreateCommand(new cmd_admin);
365 this->CreateCommand(new cmd_privmsg);
366 this->CreateCommand(new cmd_info);
367 this->CreateCommand(new cmd_time);
368 this->CreateCommand(new cmd_whois);
369 this->CreateCommand(new cmd_wallops);
370 this->CreateCommand(new cmd_notice);
371 this->CreateCommand(new cmd_join);
372 this->CreateCommand(new cmd_names);
373 this->CreateCommand(new cmd_part);
374 this->CreateCommand(new cmd_kick);
375 this->CreateCommand(new cmd_mode);
376 this->CreateCommand(new cmd_topic);
377 this->CreateCommand(new cmd_who);
378 this->CreateCommand(new cmd_motd);
379 this->CreateCommand(new cmd_rules);
380 this->CreateCommand(new cmd_oper);
381 this->CreateCommand(new cmd_list);
382 this->CreateCommand(new cmd_die);
383 this->CreateCommand(new cmd_restart);
384 this->CreateCommand(new cmd_kill);
385 this->CreateCommand(new cmd_rehash);
386 this->CreateCommand(new cmd_lusers);
387 this->CreateCommand(new cmd_stats);
388 this->CreateCommand(new cmd_userhost);
389 this->CreateCommand(new cmd_away);
390 this->CreateCommand(new cmd_ison);
391 this->CreateCommand(new cmd_summon);
392 this->CreateCommand(new cmd_users);
393 this->CreateCommand(new cmd_invite);
394 this->CreateCommand(new cmd_trace);
395 this->CreateCommand(new cmd_whowas);
396 this->CreateCommand(new cmd_connect);
397 this->CreateCommand(new cmd_squit);
398 this->CreateCommand(new cmd_modules);
399 this->CreateCommand(new cmd_links);
400 this->CreateCommand(new cmd_map);
401 this->CreateCommand(new cmd_kline);
402 this->CreateCommand(new cmd_gline);
403 this->CreateCommand(new cmd_zline);
404 this->CreateCommand(new cmd_qline);
405 this->CreateCommand(new cmd_eline);
406 this->CreateCommand(new cmd_loadmodule);
407 this->CreateCommand(new cmd_unloadmodule);
408 this->CreateCommand(new cmd_server);
409 this->CreateCommand(new cmd_commands);