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"
55 #define nspace __gnu_cxx
61 extern InspIRCd* ServerInstance;
63 extern std::vector<Module*> modules;
64 extern std::vector<ircd_module*> factory;
65 extern std::vector<InspSocket*> module_sockets;
66 extern std::vector<userrec*> local_users;
69 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
72 // This table references users by file descriptor.
73 // its an array to make it VERY fast, as all lookups are referenced
74 // by an integer, meaning there is no need for a scan/search operation.
75 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
77 extern Server* MyServer;
78 extern ServerConfig *Config;
80 extern user_hash clientlist;
81 extern chan_hash chanlist;
83 /* Special commands which may occur without registration of the user */
84 cmd_user* command_user;
85 cmd_nick* command_nick;
86 cmd_pass* command_pass;
88 /* This function pokes and hacks at a parameter list like the following:
90 * PART #winbot,#darkgalaxy :m00!
92 * to turn it into a series of individual calls like this:
95 * PART #darkgalaxy :m00!
97 * The seperate calls are sent to a callback function provided by the caller
98 * (the caller will usually call itself recursively). The callback function
99 * must be a command handler. Calling this function on a line with no list causes
100 * no action to be taken. You must provide a starting and ending parameter number
101 * where the range of the list can be found, useful if you have a terminating
102 * parameter as above which is actually not part of the list, or parameters
103 * before the actual list as well. This code is used by many functions which
104 * can function as "one to list" (see the RFC) */
106 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
108 /* Copy of the parameter list, because like strltok, we make a bit of
109 * a mess of the parameter string we're given, and we want to keep this
112 char paramlist[MAXBUF];
113 /* Temporary variable used to hold one split parameter
116 /* Parameter list, we can have up to 32 of these
119 /* Seperated items, e.g. holds the #one and #two from "#one,#two"
122 /* Seperated keys, holds the 'two' and 'three' of "two,three"
125 /* Misc. counters, the total values hold the total number of
126 * seperated items in sep_items (total) and the total number of
127 * seperated items in sep_keys (total2)
129 int j = 0, q = 0, total = 0, total2 = 0;
130 /* A temporary copy of the comma seperated key list (see the description
131 * of the paramlist variable)
133 char keylist[MAXBUF];
134 /* Exactly what it says. Its nothing. We point invalid parameters at this.
138 /* First, initialize our arrays */
139 for (int i = 0; i < 32; i++)
140 sep_items[i] = sep_keys[i] = NULL;
142 /* Now find all parameters that are NULL, maybe above pcnt,
143 * and for safety, point them at 'nothing'
145 for (int i = 0; i < 10; i++)
149 parameters[i] = ¬hing;
152 /* Check if we're doing JOIN handling. JOIN has two lists in
153 * it, potentially, if we have keys, so this is a special-case
154 * to handle the keys if they are provided.
158 if (pcnt > 1) /* we have a key to copy */
160 strlcpy(keylist,parameters[1],MAXBUF);
164 /* There's nothing to split! We don't do anything
166 if (!parameters[start] || (!strchr(parameters[start],',')))
171 /* This function can handle multiple comma seperated
172 * lists as one, which is a feature no actual commands
173 * have yet -- this is futureproofing in case we encounter
174 * a command that does.
178 for (int i = start; i <= end; i++)
182 strlcat(paramlist,parameters[i],MAXBUF);
186 /* Now we split off paramlist into seperate parameters using
187 * pointer voodoo, this parameter list goes into sep_items
192 for (char* i = paramlist; *i; i++)
198 sep_items[j++] = param;
199 /* Iterate along to next item, if there is one */
201 if ((unsigned int)j > Config->MaxTargets)
203 /* BZZT! Too many items */
204 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
209 sep_items[j++] = param;
212 /* We add this extra comma here just to ensure we get all the keys
213 * in the event the user gave a malformed key string (yes, you guessed
214 * it, this is a mirc-ism)
216 if ((joins) && (*keylist) && (total>0)) // more than one channel and is joining
218 charlcat(keylist,',',MAXBUF);
221 /* If we're doing JOIN handling (e.g. we've had to kludge for two
222 * lists being handled at once, real smart by the way JRO </sarcasm>)
223 * and we also have keys, we must seperate out this key list into
224 * our char** list sep_keys for later use.
226 if ((joins) && (*keylist))
228 /* There is more than one key */
229 if (strchr(keylist,','))
233 for (char* i = keylist; *i; i++)
238 sep_keys[j++] = param;
243 sep_keys[j++] = param;
248 /* Now the easier bit. We call the command class's Handle function
249 * X times, where x is the number of parameters we've 'collated'.
251 for (j = 0; j < total; j++)
255 pars[0] = sep_items[j];
258 for (q = end; q < pcnt-1; q++)
262 pars[q-end+1] = parameters[q+1];
266 if ((joins) && (parameters[1]))
270 pars[1] = sep_keys[j];
278 /* repeatedly call the function with the hacked parameter list */
279 if ((joins) && (pcnt > 1))
283 /* pars[1] already set up and containing key from sep_keys[j] */
284 fn->Handle(pars,2,u);
288 pars[1] = parameters[1];
289 fn->Handle(pars,2,u);
294 fn->Handle(pars,pcnt-(end-start),u);
301 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
303 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
305 if (n != cmdlist.end())
307 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
309 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
311 if (n->second->flags_needed)
313 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
322 // calls a handler function for a command
324 bool CommandParser::CallHandler(const std::string &commandname,char **parameters, int pcnt, userrec *user)
326 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
328 if (n != cmdlist.end())
330 if (pcnt >= n->second->min_params)
332 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
334 if (n->second->flags_needed)
336 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
338 n->second->Handle(parameters,pcnt,user);
344 n->second->Handle(parameters,pcnt,user);
353 int CommandParser::ProcessParameters(char **command_p,char *parameters)
359 /* no parameters, command_p invalid! */
363 if (*parameters == ':')
365 command_p[0] = parameters+1;
371 char* n = strchr(parameters,' ');
372 if ((!n) || (*parameters == ':'))
374 /* only one parameter */
375 command_p[0] = parameters;
376 if (*parameters == ':')
388 command_p[j++] = parameters;
390 for (char* i = parameters; *i; i++)
394 command_p[j++] = i+1;
397 if (*command_p[j-1] == ':')
399 *command_p[j-1]++; /* remove dodgy ":" */
401 /* parameter like this marks end of the sequence */
405 return j; /* returns total number of items in the list */
408 void CommandParser::ProcessCommand(userrec *user, char* cmd)
412 char *command_p[127];
413 char p[MAXBUF], temp[MAXBUF];
414 int j, items, cmd_found;
415 int total_params = 0;
417 for (int i = 0; i < 127; i++)
420 if (!user || !cmd || !*cmd)
425 char* first_space = NULL;
427 /* If the command is > 2 characters (quick and dirty way to find out) */
428 if (*cmd && *(cmd+1) && *(cmd+2))
430 for (char* q = cmd; *q; q++)
438 // found a 'trailing', we dont count them after this.
447 // another phidjit bug...
448 if (total_params > 126)
451 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
455 strlcpy(temp,cmd,MAXBUF);
457 std::string tmp = cmd;
459 for (int i = 0; i <= MODCOUNT; i++)
461 std::string oldtmp = tmp;
462 modules[i]->OnServerRaw(tmp,true,user);
465 log(DEBUG,"A Module changed the input string!");
466 log(DEBUG,"New string: %s",tmp.c_str());
467 log(DEBUG,"Old string: %s",oldtmp.c_str());
472 strlcpy(cmd,tmp.c_str(),MAXBUF);
473 strlcpy(temp,cmd,MAXBUF);
475 char* has_space = strchr(cmd,' ');
480 * no parameters, lets skip the formalities and not chop up
483 log(DEBUG,"About to preprocess command with no params");
487 for (char* i = cmd; *i; i++,cm_length++)
496 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
497 for (char* i = temp; *i; i++)
499 if ((*i != 10) && (*i != 13) && (*i != 0) && (*i != 7))
506 /* split the full string into a command plus parameters */
515 for (char* i = cmd; *i; i++)
517 /* capitalise the command ONLY, leave params intact */
519 /* are we nearly there yet?! :P */
531 for (char* i = cmd; *i; i++,cm_length++)
538 if (cm_length > MAXCOMMAND)
540 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
544 for (char* x = command; *x; x++)
546 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
548 if (((*x < '0') || (*x> '9')) && (*x != '-'))
550 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
552 ServerInstance->stats->statsUnknown++;
553 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
560 std::string xcommand = command;
561 if ((user->registered != 7) && (xcommand == "SERVER"))
563 kill_link(user,"Server connection to non-server port");
567 /* Tweak by brain - why was this INSIDE the mainloop? */
572 items = this->ProcessParameters(command_p,parameters);
587 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
588 if (MOD_RESULT == 1) {
592 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
594 if (cm != cmdlist.end())
598 /* activity resets the ping pending timer */
599 user->nping = TIME + user->pingmax;
600 if ((items) < cm->second->min_params)
602 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
603 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
606 if (cm->second->flags_needed)
608 if (!user->modes[cm->second->flags_needed-65])
610 log(DEBUG,"permission denied: %s %s",user->nick,command);
611 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
616 if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
618 log(DEBUG,"permission denied: %s %s",user->nick,command);
619 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
621 WriteOpers("*** \2WARNING\2: Command '%s' not allowed for oper '%s', dropped.",command,user->nick);
625 /* if the command isnt USER, PASS, or NICK, and nick is empty,
627 if ((cm->second != command_user) && (cm->second != command_nick) && (cm->second != command_pass))
629 if ((!isnick(user->nick)) || (user->registered != 7))
631 log(DEBUG,"not registered: %s %s",user->nick,command);
632 WriteServ(user->fd,"451 %s :You have not registered",command);
636 if ((user->registered == 7) && (!*user->oper) && (*Config->DisabledCommands))
638 std::stringstream dcmds(Config->DisabledCommands);
640 while (dcmds >> thiscmd)
642 if (!strcasecmp(thiscmd.c_str(),command))
644 // command is disabled!
645 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
650 if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
652 /* ikky /stats counters */
655 cm->second->use_count++;
656 cm->second->total_bytes+=strlen(temp);
660 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
667 * WARNING: nothing may come after the
668 * command handler call, as the handler
669 * may free the user structure!
672 cm->second->Handle(command_p,items,user);
677 WriteServ(user->fd,"451 %s :You have not registered",command);
684 ServerInstance->stats->statsUnknown++;
685 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
689 bool CommandParser::RemoveCommands(const char* source)
691 bool go_again = true;
697 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
699 command_t* x = i->second;
700 if (x->source == std::string(source))
702 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
713 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
717 if (!user || !cmdbuf || !*cmdbuf)
719 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
723 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
728 strlcpy(cmd,cmdbuf,MAXBUF);
730 while (charremove(cmd,10));
731 while (charremove(cmd,13));
733 int sl = strlen(cmd)-1;
734 while (sl && (cmd[sl] == ' ')) // strip trailing spaces
742 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
748 this->ProcessCommand(user,cmd);
752 bool CommandParser::CreateCommand(command_t *f)
754 /* create the command and push it onto the table */
755 if (cmdlist.find(f->command) == cmdlist.end())
757 cmdlist[f->command] = f;
758 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
764 CommandParser::CommandParser()
766 this->SetupCommandTable();
769 void CommandParser::SetupCommandTable()
771 /* These three are special (can occur without
772 * full user registration) and so are saved
775 command_user = new cmd_user;
776 command_nick = new cmd_nick;
777 command_pass = new cmd_pass;
778 this->CreateCommand(command_user);
779 this->CreateCommand(command_nick);
780 this->CreateCommand(command_pass);
782 /* The rest of these arent special. boo hoo.
784 this->CreateCommand(new cmd_quit);
785 this->CreateCommand(new cmd_version);
786 this->CreateCommand(new cmd_ping);
787 this->CreateCommand(new cmd_pong);
788 this->CreateCommand(new cmd_admin);
789 this->CreateCommand(new cmd_privmsg);
790 this->CreateCommand(new cmd_info);
791 this->CreateCommand(new cmd_time);
792 this->CreateCommand(new cmd_whois);
793 this->CreateCommand(new cmd_wallops);
794 this->CreateCommand(new cmd_notice);
795 this->CreateCommand(new cmd_join);
796 this->CreateCommand(new cmd_names);
797 this->CreateCommand(new cmd_part);
798 this->CreateCommand(new cmd_kick);
799 this->CreateCommand(new cmd_mode);
800 this->CreateCommand(new cmd_topic);
801 this->CreateCommand(new cmd_who);
802 this->CreateCommand(new cmd_motd);
803 this->CreateCommand(new cmd_rules);
804 this->CreateCommand(new cmd_oper);
805 this->CreateCommand(new cmd_list);
806 this->CreateCommand(new cmd_die);
807 this->CreateCommand(new cmd_restart);
808 this->CreateCommand(new cmd_kill);
809 this->CreateCommand(new cmd_rehash);
810 this->CreateCommand(new cmd_lusers);
811 this->CreateCommand(new cmd_stats);
812 this->CreateCommand(new cmd_userhost);
813 this->CreateCommand(new cmd_away);
814 this->CreateCommand(new cmd_ison);
815 this->CreateCommand(new cmd_summon);
816 this->CreateCommand(new cmd_users);
817 this->CreateCommand(new cmd_invite);
818 this->CreateCommand(new cmd_trace);
819 this->CreateCommand(new cmd_whowas);
820 this->CreateCommand(new cmd_connect);
821 this->CreateCommand(new cmd_squit);
822 this->CreateCommand(new cmd_modules);
823 this->CreateCommand(new cmd_links);
824 this->CreateCommand(new cmd_map);
825 this->CreateCommand(new cmd_kline);
826 this->CreateCommand(new cmd_gline);
827 this->CreateCommand(new cmd_zline);
828 this->CreateCommand(new cmd_qline);
829 this->CreateCommand(new cmd_eline);
830 this->CreateCommand(new cmd_loadmodule);
831 this->CreateCommand(new cmd_unloadmodule);
832 this->CreateCommand(new cmd_server);
833 this->CreateCommand(new cmd_commands);