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 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
30 #include <ext/hash_map>
51 #include "inspstring.h"
53 #include "helperfuncs.h"
55 #include "socketengine.h"
56 #include "userprocess.h"
60 #include "command_parse.h"
64 #define nspace __gnu_cxx
70 extern InspIRCd* ServerInstance;
72 extern std::vector<Module*> modules;
73 extern std::vector<ircd_module*> factory;
74 extern std::vector<InspSocket*> module_sockets;
75 extern std::vector<userrec*> local_users;
78 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
81 // This table references users by file descriptor.
82 // its an array to make it VERY fast, as all lookups are referenced
83 // by an integer, meaning there is no need for a scan/search operation.
84 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
86 extern Server* MyServer;
87 extern ServerConfig *Config;
89 extern user_hash clientlist;
90 extern chan_hash chanlist;
92 /* This function pokes and hacks at a parameter list like the following:
94 * PART #winbot,#darkgalaxy :m00!
96 * to turn it into a series of individual calls like this:
99 * PART #darkgalaxy :m00!
101 * The seperate calls are sent to a callback function provided by the caller
102 * (the caller will usually call itself recursively). The callback function
103 * must be a command handler. Calling this function on a line with no list causes
104 * no action to be taken. You must provide a starting and ending parameter number
105 * where the range of the list can be found, useful if you have a terminating
106 * parameter as above which is actually not part of the list, or parameters
107 * before the actual list as well. This code is used by many functions which
108 * can function as "one to list" (see the RFC) */
110 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
112 /* Copy of the parameter list, because like strltok, we make a bit of
113 * a mess of the parameter string we're given, and we want to keep this
116 char paramlist[MAXBUF];
117 /* Temporary variable used to hold one split parameter
120 /* Parameter list, we can have up to 32 of these
123 /* Seperated items, e.g. holds the #one and #two from "#one,#two"
126 /* Seperated keys, holds the 'two' and 'three' of "two,three"
129 /* Misc. counters, the total values hold the total number of
130 * seperated items in sep_items (total) and the total number of
131 * seperated items in sep_keys (total2)
133 int j = 0, q = 0, total = 0, total2 = 0;
134 /* A temporary copy of the comma seperated key list (see the description
135 * of the paramlist variable)
137 char keylist[MAXBUF];
138 /* Exactly what it says. Its nothing. We point invalid parameters at this.
142 /* First, initialize our arrays */
143 for (int i = 0; i < 32; i++)
144 sep_items[i] = sep_keys[i] = NULL;
146 /* Now find all parameters that are NULL, maybe above pcnt,
147 * and for safety, point them at 'nothing'
149 for (int i = 0; i < 10; i++)
153 parameters[i] = ¬hing;
156 /* Check if we're doing JOIN handling. JOIN has two lists in
157 * it, potentially, if we have keys, so this is a special-case
158 * to handle the keys if they are provided.
162 if (pcnt > 1) /* we have a key to copy */
164 strlcpy(keylist,parameters[1],MAXBUF);
168 /* There's nothing to split! We don't do anything
170 if (!parameters[start] || (!strchr(parameters[start],',')))
175 /* This function can handle multiple comma seperated
176 * lists as one, which is a feature no actual commands
177 * have yet -- this is futureproofing in case we encounter
178 * a command that does.
182 for (int i = start; i <= end; i++)
186 strlcat(paramlist,parameters[i],MAXBUF);
190 /* Now we split off paramlist into seperate parameters using
191 * pointer voodoo, this parameter list goes into sep_items
196 for (char* i = paramlist; *i; i++)
202 sep_items[j++] = param;
203 /* Iterate along to next item, if there is one */
205 if ((unsigned int)j > Config->MaxTargets)
207 /* BZZT! Too many items */
208 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
213 sep_items[j++] = param;
216 /* We add this extra comma here just to ensure we get all the keys
217 * in the event the user gave a malformed key string (yes, you guessed
218 * it, this is a mirc-ism)
220 if ((joins) && (*keylist) && (total>0)) // more than one channel and is joining
222 charlcat(keylist,',',MAXBUF);
225 /* If we're doing JOIN handling (e.g. we've had to kludge for two
226 * lists being handled at once, real smart by the way JRO </sarcasm>)
227 * and we also have keys, we must seperate out this key list into
228 * our char** list sep_keys for later use.
230 if ((joins) && (*keylist))
232 /* There is more than one key */
233 if (strchr(keylist,','))
237 for (char* i = keylist; *i; i++)
242 sep_keys[j++] = param;
247 sep_keys[j++] = param;
252 /* Now the easier bit. We call the command class's Handle function
253 * X times, where x is the number of parameters we've 'collated'.
255 for (j = 0; j < total; j++)
259 pars[0] = sep_items[j];
262 for (q = end; q < pcnt-1; q++)
266 pars[q-end+1] = parameters[q+1];
270 if ((joins) && (parameters[1]))
274 pars[1] = sep_keys[j];
282 /* repeatedly call the function with the hacked parameter list */
283 if ((joins) && (pcnt > 1))
287 /* pars[1] already set up and containing key from sep_keys[j] */
288 fn->Handle(pars,2,u);
292 pars[1] = parameters[1];
293 fn->Handle(pars,2,u);
298 fn->Handle(pars,pcnt-(end-start),u);
305 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
307 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
309 if (n != cmdlist.end())
311 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
313 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
315 if (n->second->flags_needed)
317 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
335 // calls a handler function for a command
337 bool CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
339 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
341 if (n != cmdlist.end())
343 if (pcnt >= n->second->min_params)
345 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
347 if (n->second->flags_needed)
349 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
351 n->second->Handle(parameters,pcnt,user);
357 n->second->Handle(parameters,pcnt,user);
366 int CommandParser::ProcessParameters(char **command_p,char *parameters)
369 int q = strlen(parameters);
373 /* no parameters, command_p invalid! */
377 if (parameters[0] == ':')
379 command_p[0] = parameters+1;
385 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
387 /* only one parameter */
388 command_p[0] = parameters;
389 if (parameters[0] == ':')
391 if (strchr(parameters,' ') != NULL)
401 command_p[j++] = parameters;
403 for (int i = 0; i <= q; i++)
405 if (parameters[i] == ' ')
407 command_p[j++] = parameters+i+1;
408 parameters[i] = '\0';
410 if (command_p[j-1][0] == ':')
412 *command_p[j-1]++; /* remove dodgy ":" */
414 /* parameter like this marks end of the sequence */
419 return j; /* returns total number of items in the list */
422 void CommandParser::ProcessCommand(userrec *user, char* cmd)
426 char *command_p[127];
427 char p[MAXBUF], temp[MAXBUF];
428 int j, items, cmd_found;
429 int total_params = 0;
432 for (int i = 0; i < 127; i++)
435 if (!user || !cmd || !*cmd)
444 for (unsigned int q = 0; q < xl - 1; q++)
451 // found a 'trailing', we dont count them after this.
460 // another phidjit bug...
461 if (total_params > 126)
463 *(strchr(cmd,' ')) = '\0';
464 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
468 strlcpy(temp,cmd,MAXBUF);
470 std::string tmp = cmd;
472 for (int i = 0; i <= MODCOUNT; i++)
474 std::string oldtmp = tmp;
475 modules[i]->OnServerRaw(tmp,true,user);
478 log(DEBUG,"A Module changed the input string!");
479 log(DEBUG,"New string: %s",tmp.c_str());
480 log(DEBUG,"Old string: %s",oldtmp.c_str());
485 strlcpy(cmd,tmp.c_str(),MAXBUF);
486 strlcpy(temp,cmd,MAXBUF);
488 if (!strchr(cmd,' '))
491 * no parameters, lets skip the formalities and not chop up
494 log(DEBUG,"About to preprocess command with no params");
498 unsigned int tl = strlen(cmd);
499 for (unsigned int i = 0; i <= tl; i++)
501 cmd[i] = toupper(cmd[i]);
510 unsigned int vl = strlen(temp);
511 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
512 for (unsigned int i = 0; i < vl; i++)
514 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
521 /* split the full string into a command plus parameters */
530 unsigned int cl = strlen(cmd);
532 for (unsigned int i = 0; i <= cl; i++)
534 /* capitalise the command ONLY, leave params intact */
535 cmd[i] = toupper(cmd[i]);
536 /* are we nearly there yet?! :P */
540 parameters = cmd+i+1;
548 for (char* i = cmd; *i; i++)
555 if (strlen(command)>MAXCOMMAND)
557 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
561 for (char* x = command; *x; x++)
563 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
565 if (((*x < '0') || (*x> '9')) && (*x != '-'))
567 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
569 ServerInstance->stats->statsUnknown++;
570 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
577 std::string xcommand = command;
578 if ((user->registered != 7) && (xcommand == "SERVER"))
580 kill_link(user,"Server connection to non-server port");
584 /* Tweak by brain - why was this INSIDE the mainloop? */
589 items = this->ProcessParameters(command_p,parameters);
604 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
605 if (MOD_RESULT == 1) {
609 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
611 if (cm != cmdlist.end())
615 /* activity resets the ping pending timer */
616 user->nping = TIME + user->pingmax;
617 if ((items) < cm->second->min_params)
619 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
620 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
623 if ((!strchr(user->modes,cm->second->flags_needed)) && (cm->second->flags_needed))
625 log(DEBUG,"permission denied: %s %s",user->nick,command);
626 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
630 if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
632 log(DEBUG,"permission denied: %s %s",user->nick,command);
633 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
635 WriteOpers("*** \2WARNING\2: Command '%s' not allowed for oper '%s', dropped.",command,user->nick);
639 /* if the command isnt USER, PASS, or NICK, and nick is empty,
641 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
643 if ((!isnick(user->nick)) || (user->registered != 7))
645 log(DEBUG,"not registered: %s %s",user->nick,command);
646 WriteServ(user->fd,"451 %s :You have not registered",command);
650 if ((user->registered == 7) && (!*user->oper) && (*Config->DisabledCommands))
652 std::stringstream dcmds(Config->DisabledCommands);
654 while (dcmds >> thiscmd)
656 if (!strcasecmp(thiscmd.c_str(),command))
658 // command is disabled!
659 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
664 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
666 /* ikky /stats counters */
669 cm->second->use_count++;
670 cm->second->total_bytes+=strlen(temp);
674 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
681 * WARNING: nothing may come after the
682 * command handler call, as the handler
683 * may free the user structure!
686 cm->second->Handle(command_p,items,user);
691 WriteServ(user->fd,"451 %s :You have not registered",command);
698 ServerInstance->stats->statsUnknown++;
699 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
703 bool CommandParser::RemoveCommands(const char* source)
705 bool go_again = true;
711 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
713 command_t* x = i->second;
714 if (x->source == std::string(source))
716 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
727 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
733 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
738 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
746 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
748 strlcpy(cmd,cmdbuf,MAXBUF);
756 /* XXX - This is ugly as sin, and incomprehensible - why are we doing this twice, for instance? --w00t */
757 int sl = strlen(cmd)-1;
759 if ((cmd[sl] == 13) || (cmd[sl] == 10))
766 if ((cmd[sl] == 13) || (cmd[sl] == 10))
774 while (cmd[sl] == ' ') // strip trailing spaces
785 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
789 this->ProcessCommand(user,cmd);
793 bool CommandParser::CreateCommand(command_t *f)
795 /* create the command and push it onto the table */
796 if (cmdlist.find(f->command) == cmdlist.end())
798 cmdlist[f->command] = f;
799 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
805 CommandParser::CommandParser()
807 this->SetupCommandTable();
810 void CommandParser::SetupCommandTable()
812 this->CreateCommand(new cmd_user);
813 this->CreateCommand(new cmd_nick);
814 this->CreateCommand(new cmd_quit);
815 this->CreateCommand(new cmd_version);
816 this->CreateCommand(new cmd_ping);
817 this->CreateCommand(new cmd_pong);
818 this->CreateCommand(new cmd_admin);
819 this->CreateCommand(new cmd_privmsg);
820 this->CreateCommand(new cmd_info);
821 this->CreateCommand(new cmd_time);
822 this->CreateCommand(new cmd_whois);
823 this->CreateCommand(new cmd_wallops);
824 this->CreateCommand(new cmd_notice);
825 this->CreateCommand(new cmd_join);
826 this->CreateCommand(new cmd_names);
827 this->CreateCommand(new cmd_part);
828 this->CreateCommand(new cmd_kick);
829 this->CreateCommand(new cmd_mode);
830 this->CreateCommand(new cmd_topic);
831 this->CreateCommand(new cmd_who);
832 this->CreateCommand(new cmd_motd);
833 this->CreateCommand(new cmd_rules);
834 this->CreateCommand(new cmd_oper);
835 this->CreateCommand(new cmd_list);
836 this->CreateCommand(new cmd_die);
837 this->CreateCommand(new cmd_restart);
838 this->CreateCommand(new cmd_kill);
839 this->CreateCommand(new cmd_rehash);
840 this->CreateCommand(new cmd_lusers);
841 this->CreateCommand(new cmd_stats);
842 this->CreateCommand(new cmd_userhost);
843 this->CreateCommand(new cmd_away);
844 this->CreateCommand(new cmd_ison);
845 this->CreateCommand(new cmd_summon);
846 this->CreateCommand(new cmd_users);
847 this->CreateCommand(new cmd_invite);
848 this->CreateCommand(new cmd_pass);
849 this->CreateCommand(new cmd_trace);
850 this->CreateCommand(new cmd_whowas);
851 this->CreateCommand(new cmd_connect);
852 this->CreateCommand(new cmd_squit);
853 this->CreateCommand(new cmd_modules);
854 this->CreateCommand(new cmd_links);
855 this->CreateCommand(new cmd_map);
856 this->CreateCommand(new cmd_kline);
857 this->CreateCommand(new cmd_gline);
858 this->CreateCommand(new cmd_zline);
859 this->CreateCommand(new cmd_qline);
860 this->CreateCommand(new cmd_eline);
861 this->CreateCommand(new cmd_loadmodule);
862 this->CreateCommand(new cmd_unloadmodule);
863 this->CreateCommand(new cmd_server);
864 this->CreateCommand(new cmd_commands);