1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 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>
52 #include "inspstring.h"
54 #include "helperfuncs.h"
56 #include "socketengine.h"
57 #include "userprocess.h"
61 #include "command_parse.h"
63 extern InspIRCd* ServerInstance;
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
67 extern std::vector<InspSocket*> module_sockets;
68 extern std::vector<userrec*> local_users;
71 extern Module* IOHookModule;
72 extern InspSocket* socket_ref[65535];
75 extern SocketEngine* SE;
77 // This table references users by file descriptor.
78 // its an array to make it VERY fast, as all lookups are referenced
79 // by an integer, meaning there is no need for a scan/search operation.
80 extern userrec* fd_ref_table[65536];
82 extern serverstats* stats;
83 extern Server* MyServer;
84 extern ServerConfig *Config;
86 extern user_hash clientlist;
87 extern chan_hash chanlist;
88 extern command_table cmdlist;
90 /* This function pokes and hacks at a parameter list like the following:
92 * PART #winbot,#darkgalaxy :m00!
94 * to turn it into a series of individual calls like this:
97 * PART #darkgalaxy :m00!
99 * The seperate calls are sent to a callback function provided by the caller
100 * (the caller will usually call itself recursively). The callback function
101 * must be a command handler. Calling this function on a line with no list causes
102 * no action to be taken. You must provide a starting and ending parameter number
103 * where the range of the list can be found, useful if you have a terminating
104 * parameter as above which is actually not part of the list, or parameters
105 * before the actual list as well. This code is used by many functions which
106 * can function as "one to list" (see the RFC) */
108 int CommandParser::LoopCall(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
113 char blog[32][MAXBUF];
114 char blog2[32][MAXBUF];
115 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
119 for (int i = 0; i <32; i++)
122 for (int i = 0; i <32; i++)
126 for (int i = 0; i <10; i++)
135 if (pcnt > 1) /* we have a key to copy */
137 strlcpy(keystr,parameters[1],MAXBUF);
141 if (!parameters[start])
145 if (!strchr(parameters[start],','))
150 for (int i = start; i <= end; i++)
154 strlcat(plist,parameters[i],MAXBUF);
162 for (int i = 0; i < t; i++)
167 strlcpy(blog[j++],param,MAXBUF);
171 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
176 strlcpy(blog[j++],param,MAXBUF);
179 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
184 if ((joins) && (keystr))
186 if (strchr(keystr,','))
191 for (int i = 0; i < t2; i++)
193 if (keystr[i] == ',')
196 strlcpy(blog2[j++],param,MAXBUF);
200 strlcpy(blog2[j++],param,MAXBUF);
205 for (j = 0; j < total; j++)
211 for (q = end; q < pcnt-1; q++)
215 pars[q-end+1] = parameters[q+1];
218 if ((joins) && (parameters[1]))
229 /* repeatedly call the function with the hacked parameter list */
230 if ((joins) && (pcnt > 1))
234 // pars[1] already set up and containing key from blog2[j]
239 pars[1] = parameters[1];
245 fn(pars,pcnt-(end-start),u);
252 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
254 for (unsigned int i = 0; i < cmdlist.size(); i++)
256 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
258 if (cmdlist[i].handler_function)
260 if ((pcnt>=cmdlist[i].min_params) && (strcasecmp(cmdlist[i].source,"<core>")))
262 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
264 if (cmdlist[i].flags_needed)
266 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
284 // calls a handler function for a command
286 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
288 for (unsigned int i = 0; i < cmdlist.size(); i++)
290 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
292 if (cmdlist[i].handler_function)
294 if (pcnt>=cmdlist[i].min_params)
296 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
298 if (cmdlist[i].flags_needed)
300 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
302 cmdlist[i].handler_function(parameters,pcnt,user);
307 cmdlist[i].handler_function(parameters,pcnt,user);
316 int CommandParser::ProcessParameters(char **command_p,char *parameters)
319 int q = strlen(parameters);
322 /* no parameters, command_p invalid! */
325 if (parameters[0] == ':')
327 command_p[0] = parameters+1;
332 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
334 /* only one parameter */
335 command_p[0] = parameters;
336 if (parameters[0] == ':')
338 if (strchr(parameters,' ') != NULL)
346 command_p[j++] = parameters;
347 for (int i = 0; i <= q; i++)
349 if (parameters[i] == ' ')
351 command_p[j++] = parameters+i+1;
352 parameters[i] = '\0';
353 if (command_p[j-1][0] == ':')
355 *command_p[j-1]++; /* remove dodgy ":" */
357 /* parameter like this marks end of the sequence */
361 return j; /* returns total number of items in the list */
364 void CommandParser::ProcessCommand(userrec *user, char* cmd)
368 char *command_p[127];
369 char p[MAXBUF], temp[MAXBUF];
370 int j, items, cmd_found;
372 for (int i = 0; i < 127; i++)
388 int total_params = 0;
391 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
393 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
396 // found a 'trailing', we dont count them after this.
404 // another phidjit bug...
405 if (total_params > 126)
407 *(strchr(cmd,' ')) = '\0';
408 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
412 strlcpy(temp,cmd,MAXBUF);
414 std::string tmp = cmd;
415 for (int i = 0; i <= MODCOUNT; i++)
417 std::string oldtmp = tmp;
418 modules[i]->OnServerRaw(tmp,true,user);
421 log(DEBUG,"A Module changed the input string!");
422 log(DEBUG,"New string: %s",tmp.c_str());
423 log(DEBUG,"Old string: %s",oldtmp.c_str());
427 strlcpy(cmd,tmp.c_str(),MAXBUF);
428 strlcpy(temp,cmd,MAXBUF);
430 if (!strchr(cmd,' '))
432 /* no parameters, lets skip the formalities and not chop up
434 log(DEBUG,"About to preprocess command with no params");
438 for (unsigned int i = 0; i <= strlen(cmd); i++)
440 cmd[i] = toupper(cmd[i]);
448 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
449 for (unsigned int i = 0; i < strlen(temp); i++)
451 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
457 /* split the full string into a command plus parameters */
463 for (unsigned int i = 0; i <= strlen(cmd); i++)
465 /* capitalise the command ONLY, leave params intact */
466 cmd[i] = toupper(cmd[i]);
467 /* are we nearly there yet?! :P */
471 parameters = cmd+i+1;
479 for (unsigned int i = 0; i <= strlen(cmd); i++)
481 cmd[i] = toupper(cmd[i]);
488 if (strlen(command)>MAXCOMMAND)
490 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
494 for (unsigned int x = 0; x < strlen(command); x++)
496 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
498 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
500 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
502 stats->statsUnknown++;
503 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
510 std::string xcommand = command;
511 for (unsigned int i = 0; i != cmdlist.size(); i++)
513 if (cmdlist[i].command[0])
515 if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
521 items = process_parameters(command_p,parameters);
537 /* activity resets the ping pending timer */
538 user->nping = TIME + user->pingmax;
539 if ((items) < cmdlist[i].min_params)
541 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
542 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
545 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
547 log(DEBUG,"permission denied: %s %s",user->nick,command);
548 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
552 if ((cmdlist[i].flags_needed) && (!user->HasPermission(xcommand)))
554 log(DEBUG,"permission denied: %s %s",user->nick,command);
555 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
559 /* if the command isnt USER, PASS, or NICK, and nick is empty,
561 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
563 if ((!isnick(user->nick)) || (user->registered != 7))
565 log(DEBUG,"not registered: %s %s",user->nick,command);
566 WriteServ(user->fd,"451 %s :You have not registered",command);
570 if ((user->registered == 7) && (!strchr(user->modes,'o')))
572 std::stringstream dcmds(Config->DisabledCommands);
577 if (!strcasecmp(thiscmd.c_str(),command))
579 // command is disabled!
580 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
585 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
587 if (cmdlist[i].handler_function)
590 /* ikky /stats counters */
593 cmdlist[i].use_count++;
594 cmdlist[i].total_bytes+=strlen(temp);
598 FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
599 if (MOD_RESULT == 1) {
603 /* WARNING: nothing may come after the
604 * command handler call, as the handler
605 * may free the user structure! */
607 cmdlist[i].handler_function(command_p,items,user);
613 WriteServ(user->fd,"451 %s :You have not registered",command);
622 if ((!cmd_found) && (user))
624 stats->statsUnknown++;
625 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
629 bool CommandParser::RemoveCommands(const char* source)
631 bool go_again = true;
635 for (std::deque<command_t>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
637 if (!strcmp(i->source,source))
639 log(DEBUG,"removecommands(%s) Removing dependent command: %s",i->source,i->command);
649 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
653 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
659 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
666 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
668 strlcpy(cmd,cmdbuf,MAXBUF);
673 int sl = strlen(cmd)-1;
674 if ((cmd[sl] == 13) || (cmd[sl] == 10))
679 if ((cmd[sl] == 13) || (cmd[sl] == 10))
684 while (cmd[sl] == ' ') // strip trailing spaces
694 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
698 this->ProcessCommand(user,cmd);