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"
62 extern InspIRCd* ServerInstance;
64 extern std::vector<Module*> modules;
65 extern std::vector<ircd_module*> factory;
66 extern std::vector<InspSocket*> module_sockets;
67 extern std::vector<userrec*> local_users;
70 extern Module* IOHookModule;
71 extern InspSocket* socket_ref[65535];
74 extern SocketEngine* SE = NULL;
76 // This table references users by file descriptor.
77 // its an array to make it VERY fast, as all lookups are referenced
78 // by an integer, meaning there is no need for a scan/search operation.
79 extern userrec* fd_ref_table[65536];
81 extern serverstats* stats = new serverstats;
82 extern Server* MyServer = new Server;
83 extern ServerConfig *Config = new ServerConfig;
85 extern user_hash clientlist;
86 extern chan_hash chanlist;
87 extern command_table cmdlist;
89 /* This function pokes and hacks at a parameter list like the following:
91 * PART #winbot,#darkgalaxy :m00!
93 * to turn it into a series of individual calls like this:
96 * PART #darkgalaxy :m00!
98 * The seperate calls are sent to a callback function provided by the caller
99 * (the caller will usually call itself recursively). The callback function
100 * must be a command handler. Calling this function on a line with no list causes
101 * no action to be taken. You must provide a starting and ending parameter number
102 * where the range of the list can be found, useful if you have a terminating
103 * parameter as above which is actually not part of the list, or parameters
104 * before the actual list as well. This code is used by many functions which
105 * can function as "one to list" (see the RFC) */
107 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
112 char blog[32][MAXBUF];
113 char blog2[32][MAXBUF];
114 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
118 for (int i = 0; i <32; i++)
121 for (int i = 0; i <32; i++)
125 for (int i = 0; i <10; i++)
134 if (pcnt > 1) /* we have a key to copy */
136 strlcpy(keystr,parameters[1],MAXBUF);
140 if (!parameters[start])
144 if (!strchr(parameters[start],','))
149 for (int i = start; i <= end; i++)
153 strlcat(plist,parameters[i],MAXBUF);
161 for (int i = 0; i < t; i++)
166 strlcpy(blog[j++],param,MAXBUF);
170 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
175 strlcpy(blog[j++],param,MAXBUF);
178 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
183 if ((joins) && (keystr))
185 if (strchr(keystr,','))
190 for (int i = 0; i < t2; i++)
192 if (keystr[i] == ',')
195 strlcpy(blog2[j++],param,MAXBUF);
199 strlcpy(blog2[j++],param,MAXBUF);
204 for (j = 0; j < total; j++)
210 for (q = end; q < pcnt-1; q++)
214 pars[q-end+1] = parameters[q+1];
217 if ((joins) && (parameters[1]))
228 /* repeatedly call the function with the hacked parameter list */
229 if ((joins) && (pcnt > 1))
233 // pars[1] already set up and containing key from blog2[j]
238 pars[1] = parameters[1];
244 fn(pars,pcnt-(end-start),u);
251 bool is_valid_cmd(std::string &commandname, int pcnt, userrec * user)
253 for (unsigned int i = 0; i < cmdlist.size(); i++)
255 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
257 if (cmdlist[i].handler_function)
259 if ((pcnt>=cmdlist[i].min_params) && (strcasecmp(cmdlist[i].source,"<core>")))
261 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
263 if (cmdlist[i].flags_needed)
265 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
283 // calls a handler function for a command
285 void call_handler(std::string &commandname,char **parameters, int pcnt, userrec *user)
287 for (unsigned int i = 0; i < cmdlist.size(); i++)
289 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
291 if (cmdlist[i].handler_function)
293 if (pcnt>=cmdlist[i].min_params)
295 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
297 if (cmdlist[i].flags_needed)
299 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
301 cmdlist[i].handler_function(parameters,pcnt,user);
306 cmdlist[i].handler_function(parameters,pcnt,user);
315 int process_parameters(char **command_p,char *parameters)
318 int q = strlen(parameters);
321 /* no parameters, command_p invalid! */
324 if (parameters[0] == ':')
326 command_p[0] = parameters+1;
331 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
333 /* only one parameter */
334 command_p[0] = parameters;
335 if (parameters[0] == ':')
337 if (strchr(parameters,' ') != NULL)
345 command_p[j++] = parameters;
346 for (int i = 0; i <= q; i++)
348 if (parameters[i] == ' ')
350 command_p[j++] = parameters+i+1;
351 parameters[i] = '\0';
352 if (command_p[j-1][0] == ':')
354 *command_p[j-1]++; /* remove dodgy ":" */
356 /* parameter like this marks end of the sequence */
360 return j; /* returns total number of items in the list */
363 void process_command(userrec *user, char* cmd)
367 char *command_p[127];
368 char p[MAXBUF], temp[MAXBUF];
369 int j, items, cmd_found;
371 for (int i = 0; i < 127; i++)
387 int total_params = 0;
390 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
392 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
395 // found a 'trailing', we dont count them after this.
403 // another phidjit bug...
404 if (total_params > 126)
406 *(strchr(cmd,' ')) = '\0';
407 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
411 strlcpy(temp,cmd,MAXBUF);
413 std::string tmp = cmd;
414 for (int i = 0; i <= MODCOUNT; i++)
416 std::string oldtmp = tmp;
417 modules[i]->OnServerRaw(tmp,true,user);
420 log(DEBUG,"A Module changed the input string!");
421 log(DEBUG,"New string: %s",tmp.c_str());
422 log(DEBUG,"Old string: %s",oldtmp.c_str());
426 strlcpy(cmd,tmp.c_str(),MAXBUF);
427 strlcpy(temp,cmd,MAXBUF);
429 if (!strchr(cmd,' '))
431 /* no parameters, lets skip the formalities and not chop up
433 log(DEBUG,"About to preprocess command with no params");
437 for (unsigned int i = 0; i <= strlen(cmd); i++)
439 cmd[i] = toupper(cmd[i]);
447 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
448 for (unsigned int i = 0; i < strlen(temp); i++)
450 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
456 /* split the full string into a command plus parameters */
462 for (unsigned int i = 0; i <= strlen(cmd); i++)
464 /* capitalise the command ONLY, leave params intact */
465 cmd[i] = toupper(cmd[i]);
466 /* are we nearly there yet?! :P */
470 parameters = cmd+i+1;
478 for (unsigned int i = 0; i <= strlen(cmd); i++)
480 cmd[i] = toupper(cmd[i]);
487 if (strlen(command)>MAXCOMMAND)
489 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
493 for (unsigned int x = 0; x < strlen(command); x++)
495 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
497 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
499 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
501 stats->statsUnknown++;
502 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
509 std::string xcommand = command;
510 for (unsigned int i = 0; i != cmdlist.size(); i++)
512 if (cmdlist[i].command[0])
514 if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
520 items = process_parameters(command_p,parameters);
536 /* activity resets the ping pending timer */
537 user->nping = TIME + user->pingmax;
538 if ((items) < cmdlist[i].min_params)
540 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
541 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
544 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
546 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
547 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
551 if ((cmdlist[i].flags_needed) && (!user->HasPermission(xcommand)))
553 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
554 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
558 /* if the command isnt USER, PASS, or NICK, and nick is empty,
560 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
562 if ((!isnick(user->nick)) || (user->registered != 7))
564 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
565 WriteServ(user->fd,"451 %s :You have not registered",command);
569 if ((user->registered == 7) && (!strchr(user->modes,'o')))
571 std::stringstream dcmds(Config->DisabledCommands);
576 if (!strcasecmp(thiscmd.c_str(),command))
578 // command is disabled!
579 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
584 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
586 if (cmdlist[i].handler_function)
589 /* ikky /stats counters */
592 cmdlist[i].use_count++;
593 cmdlist[i].total_bytes+=strlen(temp);
597 FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
598 if (MOD_RESULT == 1) {
602 /* WARNING: nothing may come after the
603 * command handler call, as the handler
604 * may free the user structure! */
606 cmdlist[i].handler_function(command_p,items,user);
612 WriteServ(user->fd,"451 %s :You have not registered",command);
621 if ((!cmd_found) && (user))
623 stats->statsUnknown++;
624 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
628 bool remove_commands(const char* source)
630 bool go_again = true;
634 for (std::deque<command_t>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
636 if (!strcmp(i->source,source))
638 log(DEBUG,"removecommands(%s) Removing dependent command: %s",i->source,i->command);
648 void process_buffer(const char* cmdbuf,userrec *user)
652 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
658 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
665 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
667 strlcpy(cmd,cmdbuf,MAXBUF);
672 int sl = strlen(cmd)-1;
673 if ((cmd[sl] == 13) || (cmd[sl] == 10))
678 if ((cmd[sl] == 13) || (cmd[sl] == 10))
683 while (cmd[sl] == ' ') // strip trailing spaces
693 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
697 process_command(user,cmd);