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(command_t* fn, const char** parameters, int pcnt, userrec *u, int start, int end, int joins)
103 /* Copy of the parameter list, because like strltok, we make a bit of
104 * a mess of the parameter string we're given, and we want to keep this
107 char paramlist[MAXBUF];
108 /* Temporary variable used to hold one split parameter
111 /* Parameter list, we can have up to 32 of these
113 const char *pars[32];
114 /* Seperated items, e.g. holds the #one and #two from "#one,#two"
116 const char *sep_items[32];
117 /* Seperated keys, holds the 'two' and 'three' of "two,three"
119 const char *sep_keys[32];
120 /* Misc. counters, the total values hold the total number of
121 * seperated items in sep_items (total) and the total number of
122 * seperated items in sep_keys (total2)
124 int j = 0, q = 0, total = 0, total2 = 0;
125 /* A temporary copy of the comma seperated key list (see the description
126 * of the paramlist variable)
128 char keylist[MAXBUF];
129 /* Exactly what it says. Its nothing. We point invalid parameters at this.
133 /* First, initialize our arrays */
134 for (int i = 0; i < 32; i++)
135 sep_items[i] = sep_keys[i] = NULL;
137 /* Now find all parameters that are NULL, maybe above pcnt,
138 * and for safety, point them at 'nothing'
140 for (int i = 0; i < 10; i++)
144 parameters[i] = ¬hing;
147 /* Check if we're doing JOIN handling. JOIN has two lists in
148 * it, potentially, if we have keys, so this is a special-case
149 * to handle the keys if they are provided.
153 if (pcnt > 1) /* we have a key to copy */
155 strlcpy(keylist,parameters[1],MAXBUF);
159 /* There's nothing to split! We don't do anything
161 if (!parameters[start] || (!strchr(parameters[start],',')))
166 /* This function can handle multiple comma seperated
167 * lists as one, which is a feature no actual commands
168 * have yet -- this is futureproofing in case we encounter
169 * a command that does.
173 for (int i = start; i <= end; i++)
177 strlcat(paramlist,parameters[i],MAXBUF);
181 /* Now we split off paramlist into seperate parameters using
182 * pointer voodoo, this parameter list goes into sep_items
187 for (char* i = paramlist; *i; i++)
193 sep_items[j++] = param;
194 /* Iterate along to next item, if there is one */
196 if ((unsigned int)j > Config->MaxTargets)
198 /* BZZT! Too many items */
199 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
204 sep_items[j++] = param;
207 /* We add this extra comma here just to ensure we get all the keys
208 * in the event the user gave a malformed key string (yes, you guessed
209 * it, this is a mirc-ism)
211 if ((joins) && (*keylist) && (total>0)) // more than one channel and is joining
213 charlcat(keylist,',',MAXBUF);
216 /* If we're doing JOIN handling (e.g. we've had to kludge for two
217 * lists being handled at once, real smart by the way JRO </sarcasm>)
218 * and we also have keys, we must seperate out this key list into
219 * our char** list sep_keys for later use.
221 if ((joins) && (*keylist))
223 /* There is more than one key */
224 if (strchr(keylist,','))
228 for (char* i = keylist; *i; i++)
233 sep_keys[j++] = param;
238 sep_keys[j++] = param;
243 /* Now the easier bit. We call the command class's Handle function
244 * X times, where x is the number of parameters we've 'collated'.
246 for (j = 0; j < total; j++)
250 pars[0] = sep_items[j];
253 for (q = end; q < pcnt-1; q++)
257 pars[q-end+1] = parameters[q+1];
261 if ((joins) && (parameters[1]))
265 pars[1] = sep_keys[j];
273 /* repeatedly call the function with the hacked parameter list */
274 if ((joins) && (pcnt > 1))
278 /* pars[1] already set up and containing key from sep_keys[j] */
279 fn->Handle(pars,2,u);
283 pars[1] = parameters[1];
284 fn->Handle(pars,2,u);
289 fn->Handle(pars,pcnt-(end-start),u);
296 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
298 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
300 if (n != cmdlist.end())
302 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
304 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
306 if (n->second->flags_needed)
308 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
317 // calls a handler function for a command
319 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
321 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
323 if (n != cmdlist.end())
325 if (pcnt >= n->second->min_params)
327 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
329 if (n->second->flags_needed)
331 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
333 n->second->Handle(parameters,pcnt,user);
339 n->second->Handle(parameters,pcnt,user);
348 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
350 const char *command_p[127];
352 std::string para[127];
353 irc::tokenstream tokens(cmd);
354 std::string command = tokens.GetToken();
356 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
357 command_p[items] = para[items++].c_str();
359 for (std::string::iterator makeupper = command.begin(); makeupper != command.end(); makeupper++)
360 *makeupper = toupper(*makeupper);
363 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
364 if (MOD_RESULT == 1) {
368 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
370 if (cm != cmdlist.end())
374 /* activity resets the ping pending timer */
375 user->nping = TIME + user->pingmax;
376 if (items < cm->second->min_params)
378 log(DEBUG,"not enough parameters: %s %s",user->nick,command.c_str());
379 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command.c_str());
382 if (cm->second->flags_needed)
384 if (!user->IsModeSet(cm->second->flags_needed))
386 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
387 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
391 if ((cm->second->flags_needed) && (!user->HasPermission(command)))
393 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
394 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
397 if ((user->registered == 7) && (!*user->oper) && (cm->second->IsDisabled()))
399 /* command is disabled! */
400 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command.c_str());
403 if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
405 /* ikky /stats counters */
406 cm->second->use_count++;
407 cm->second->total_bytes += cmd.length();
410 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
415 * WARNING: nothing may come after the
416 * command handler call, as the handler
417 * may free the user structure!
420 cm->second->Handle(command_p,items,user);
425 WriteServ(user->fd,"451 %s :You have not registered",command.c_str());
432 ServerInstance->stats->statsUnknown++;
433 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command.c_str());
437 bool CommandParser::RemoveCommands(const char* source)
439 bool go_again = true;
445 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
447 command_t* x = i->second;
448 if (x->source == std::string(source))
450 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
461 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
463 std::string::size_type a;
468 while ((a = buffer.find("\n")) != std::string::npos)
470 while ((a = buffer.find("\r")) != std::string::npos)
473 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
475 this->ProcessCommand(user,buffer);
478 bool CommandParser::CreateCommand(command_t *f)
480 /* create the command and push it onto the table */
481 if (cmdlist.find(f->command) == cmdlist.end())
483 cmdlist[f->command] = f;
484 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
490 CommandParser::CommandParser()
492 this->SetupCommandTable();
495 void CommandParser::SetupCommandTable()
497 /* These three are special (can occur without
498 * full user registration) and so are saved
501 command_user = new cmd_user;
502 command_nick = new cmd_nick;
503 command_pass = new cmd_pass;
504 this->CreateCommand(command_user);
505 this->CreateCommand(command_nick);
506 this->CreateCommand(command_pass);
508 /* The rest of these arent special. boo hoo.
510 this->CreateCommand(new cmd_quit);
511 this->CreateCommand(new cmd_version);
512 this->CreateCommand(new cmd_ping);
513 this->CreateCommand(new cmd_pong);
514 this->CreateCommand(new cmd_admin);
515 this->CreateCommand(new cmd_privmsg);
516 this->CreateCommand(new cmd_info);
517 this->CreateCommand(new cmd_time);
518 this->CreateCommand(new cmd_whois);
519 this->CreateCommand(new cmd_wallops);
520 this->CreateCommand(new cmd_notice);
521 this->CreateCommand(new cmd_join);
522 this->CreateCommand(new cmd_names);
523 this->CreateCommand(new cmd_part);
524 this->CreateCommand(new cmd_kick);
525 this->CreateCommand(new cmd_mode);
526 this->CreateCommand(new cmd_topic);
527 this->CreateCommand(new cmd_who);
528 this->CreateCommand(new cmd_motd);
529 this->CreateCommand(new cmd_rules);
530 this->CreateCommand(new cmd_oper);
531 this->CreateCommand(new cmd_list);
532 this->CreateCommand(new cmd_die);
533 this->CreateCommand(new cmd_restart);
534 this->CreateCommand(new cmd_kill);
535 this->CreateCommand(new cmd_rehash);
536 this->CreateCommand(new cmd_lusers);
537 this->CreateCommand(new cmd_stats);
538 this->CreateCommand(new cmd_userhost);
539 this->CreateCommand(new cmd_away);
540 this->CreateCommand(new cmd_ison);
541 this->CreateCommand(new cmd_summon);
542 this->CreateCommand(new cmd_users);
543 this->CreateCommand(new cmd_invite);
544 this->CreateCommand(new cmd_trace);
545 this->CreateCommand(new cmd_whowas);
546 this->CreateCommand(new cmd_connect);
547 this->CreateCommand(new cmd_squit);
548 this->CreateCommand(new cmd_modules);
549 this->CreateCommand(new cmd_links);
550 this->CreateCommand(new cmd_map);
551 this->CreateCommand(new cmd_kline);
552 this->CreateCommand(new cmd_gline);
553 this->CreateCommand(new cmd_zline);
554 this->CreateCommand(new cmd_qline);
555 this->CreateCommand(new cmd_eline);
556 this->CreateCommand(new cmd_loadmodule);
557 this->CreateCommand(new cmd_unloadmodule);
558 this->CreateCommand(new cmd_server);
559 this->CreateCommand(new cmd_commands);