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;
83 /* This function pokes and hacks at a parameter list like the following:
85 * PART #winbot,#darkgalaxy :m00!
87 * to turn it into a series of individual calls like this:
90 * PART #darkgalaxy :m00!
92 * The seperate calls are sent to a callback function provided by the caller
93 * (the caller will usually call itself recursively). The callback function
94 * must be a command handler. Calling this function on a line with no list causes
95 * no action to be taken. You must provide a starting and ending parameter number
96 * where the range of the list can be found, useful if you have a terminating
97 * parameter as above which is actually not part of the list, or parameters
98 * before the actual list as well. This code is used by many functions which
99 * can function as "one to list" (see the RFC) */
101 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
103 /*if ((unsigned int)j > Config->MaxTargets)
105 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
108 fn->Handle(pars,pcnt-(end-start),u);
110 if (!strchr(parameters[splithere],','))
113 irc::commasepstream items1(parameters[splithere]);
114 irc::commasepstream items2(parameters[extra]);
115 std::string item = "";
117 while ((item = items1.GetToken()) != "")
119 std::string extrastuff = items2.GetToken();
120 parameters[splithere] = item.c_str();
121 parameters[extra] = extrastuff.c_str();
122 CommandObj->Handle(parameters,pcnt,user);
127 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
129 if (!strchr(parameters[splithere],','))
132 irc::commasepstream items1(parameters[splithere]);
133 std::string item = "";
135 while ((item = items1.GetToken()) != "")
137 parameters[splithere] = item.c_str();
138 CommandObj->Handle(parameters,pcnt,user);
143 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
145 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
147 if (n != cmdlist.end())
149 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
151 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
153 if (n->second->flags_needed)
155 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
164 // calls a handler function for a command
166 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
168 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
170 if (n != cmdlist.end())
172 if (pcnt >= n->second->min_params)
174 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
176 if (n->second->flags_needed)
178 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
180 n->second->Handle(parameters,pcnt,user);
186 n->second->Handle(parameters,pcnt,user);
195 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
197 const char *command_p[127];
199 std::string para[127];
200 irc::tokenstream tokens(cmd);
201 std::string command = tokens.GetToken();
203 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
204 command_p[items] = para[items++].c_str();
206 for (std::string::iterator makeupper = command.begin(); makeupper != command.end(); makeupper++)
207 *makeupper = toupper(*makeupper);
210 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
211 if (MOD_RESULT == 1) {
215 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
217 if (cm != cmdlist.end())
221 /* activity resets the ping pending timer */
222 user->nping = TIME + user->pingmax;
223 if (items < cm->second->min_params)
225 log(DEBUG,"not enough parameters: %s %s",user->nick,command.c_str());
226 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command.c_str());
229 if (cm->second->flags_needed)
231 if (!user->IsModeSet(cm->second->flags_needed))
233 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
234 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
238 if ((cm->second->flags_needed) && (!user->HasPermission(command)))
240 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
241 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
244 if ((user->registered == 7) && (!*user->oper) && (cm->second->IsDisabled()))
246 /* command is disabled! */
247 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command.c_str());
250 if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
252 /* ikky /stats counters */
253 cm->second->use_count++;
254 cm->second->total_bytes += cmd.length();
257 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
262 * WARNING: nothing may come after the
263 * command handler call, as the handler
264 * may free the user structure!
267 cm->second->Handle(command_p,items,user);
272 WriteServ(user->fd,"451 %s :You have not registered",command.c_str());
279 ServerInstance->stats->statsUnknown++;
280 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command.c_str());
284 bool CommandParser::RemoveCommands(const char* source)
286 bool go_again = true;
292 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
294 command_t* x = i->second;
295 if (x->source == std::string(source))
297 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
308 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
310 std::string::size_type a;
315 while ((a = buffer.find("\n")) != std::string::npos)
317 while ((a = buffer.find("\r")) != std::string::npos)
320 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
322 this->ProcessCommand(user,buffer);
325 bool CommandParser::CreateCommand(command_t *f)
327 /* create the command and push it onto the table */
328 if (cmdlist.find(f->command) == cmdlist.end())
330 cmdlist[f->command] = f;
331 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
337 CommandParser::CommandParser()
339 this->SetupCommandTable();
342 void CommandParser::SetupCommandTable()
344 /* These three are special (can occur without
345 * full user registration) and so are saved
348 command_user = new cmd_user;
349 command_nick = new cmd_nick;
350 command_pass = new cmd_pass;
351 this->CreateCommand(command_user);
352 this->CreateCommand(command_nick);
353 this->CreateCommand(command_pass);
355 /* The rest of these arent special. boo hoo.
357 this->CreateCommand(new cmd_quit);
358 this->CreateCommand(new cmd_version);
359 this->CreateCommand(new cmd_ping);
360 this->CreateCommand(new cmd_pong);
361 this->CreateCommand(new cmd_admin);
362 this->CreateCommand(new cmd_privmsg);
363 this->CreateCommand(new cmd_info);
364 this->CreateCommand(new cmd_time);
365 this->CreateCommand(new cmd_whois);
366 this->CreateCommand(new cmd_wallops);
367 this->CreateCommand(new cmd_notice);
368 this->CreateCommand(new cmd_join);
369 this->CreateCommand(new cmd_names);
370 this->CreateCommand(new cmd_part);
371 this->CreateCommand(new cmd_kick);
372 this->CreateCommand(new cmd_mode);
373 this->CreateCommand(new cmd_topic);
374 this->CreateCommand(new cmd_who);
375 this->CreateCommand(new cmd_motd);
376 this->CreateCommand(new cmd_rules);
377 this->CreateCommand(new cmd_oper);
378 this->CreateCommand(new cmd_list);
379 this->CreateCommand(new cmd_die);
380 this->CreateCommand(new cmd_restart);
381 this->CreateCommand(new cmd_kill);
382 this->CreateCommand(new cmd_rehash);
383 this->CreateCommand(new cmd_lusers);
384 this->CreateCommand(new cmd_stats);
385 this->CreateCommand(new cmd_userhost);
386 this->CreateCommand(new cmd_away);
387 this->CreateCommand(new cmd_ison);
388 this->CreateCommand(new cmd_summon);
389 this->CreateCommand(new cmd_users);
390 this->CreateCommand(new cmd_invite);
391 this->CreateCommand(new cmd_trace);
392 this->CreateCommand(new cmd_whowas);
393 this->CreateCommand(new cmd_connect);
394 this->CreateCommand(new cmd_squit);
395 this->CreateCommand(new cmd_modules);
396 this->CreateCommand(new cmd_links);
397 this->CreateCommand(new cmd_map);
398 this->CreateCommand(new cmd_kline);
399 this->CreateCommand(new cmd_gline);
400 this->CreateCommand(new cmd_zline);
401 this->CreateCommand(new cmd_qline);
402 this->CreateCommand(new cmd_eline);
403 this->CreateCommand(new cmd_loadmodule);
404 this->CreateCommand(new cmd_unloadmodule);
405 this->CreateCommand(new cmd_server);
406 this->CreateCommand(new cmd_commands);