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>
51 #include "inspstring.h"
53 #include "helperfuncs.h"
55 #include "socketengine.h"
56 #include "userprocess.h"
60 #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 InspSocket* socket_ref[65535];
74 extern SocketEngine* SE;
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 Server* MyServer;
82 extern ServerConfig *Config;
84 extern user_hash clientlist;
85 extern chan_hash chanlist;
87 /* This function pokes and hacks at a parameter list like the following:
89 * PART #winbot,#darkgalaxy :m00!
91 * to turn it into a series of individual calls like this:
94 * PART #darkgalaxy :m00!
96 * The seperate calls are sent to a callback function provided by the caller
97 * (the caller will usually call itself recursively). The callback function
98 * must be a command handler. Calling this function on a line with no list causes
99 * no action to be taken. You must provide a starting and ending parameter number
100 * where the range of the list can be found, useful if you have a terminating
101 * parameter as above which is actually not part of the list, or parameters
102 * before the actual list as well. This code is used by many functions which
103 * can function as "one to list" (see the RFC) */
105 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
110 char blog[32][MAXBUF];
111 char blog2[32][MAXBUF];
112 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
116 for (int i = 0; i <32; i++)
119 for (int i = 0; i <32; i++)
123 for (int i = 0; i <10; i++)
132 if (pcnt > 1) /* we have a key to copy */
134 strlcpy(keystr,parameters[1],MAXBUF);
138 if (!parameters[start])
142 if (!strchr(parameters[start],','))
147 for (int i = start; i <= end; i++)
151 strlcat(plist,parameters[i],MAXBUF);
159 for (int i = 0; i < t; i++)
164 strlcpy(blog[j++],param,MAXBUF);
168 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
173 strlcpy(blog[j++],param,MAXBUF);
176 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
181 if ((joins) && (keystr))
183 if (strchr(keystr,','))
188 for (int i = 0; i < t2; i++)
190 if (keystr[i] == ',')
193 strlcpy(blog2[j++],param,MAXBUF);
197 strlcpy(blog2[j++],param,MAXBUF);
202 for (j = 0; j < total; j++)
208 for (q = end; q < pcnt-1; q++)
212 pars[q-end+1] = parameters[q+1];
215 if ((joins) && (parameters[1]))
226 /* repeatedly call the function with the hacked parameter list */
227 if ((joins) && (pcnt > 1))
231 // pars[1] already set up and containing key from blog2[j]
232 fn->Handle(pars,2,u);
236 pars[1] = parameters[1];
237 fn->Handle(pars,2,u);
242 fn->Handle(pars,pcnt-(end-start),u);
249 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
251 for (unsigned int i = 0; i < cmdlist.size(); i++)
253 if (cmdlist[i]->command == commandname)
255 if ((pcnt>=cmdlist[i]->min_params) && (cmdlist[i]->source != "<core>"))
257 if ((strchr(user->modes,cmdlist[i]->flags_needed)) || (!cmdlist[i]->flags_needed))
259 if (cmdlist[i]->flags_needed)
261 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
278 // calls a handler function for a command
280 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
282 for (unsigned int i = 0; i < cmdlist.size(); i++)
284 if (cmdlist[i]->command == commandname)
286 if (pcnt>=cmdlist[i]->min_params)
288 if ((strchr(user->modes,cmdlist[i]->flags_needed)) || (!cmdlist[i]->flags_needed))
290 if (cmdlist[i]->flags_needed)
292 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
294 cmdlist[i]->Handle(parameters,pcnt,user);
299 cmdlist[i]->Handle(parameters,pcnt,user);
307 int CommandParser::ProcessParameters(char **command_p,char *parameters)
310 int q = strlen(parameters);
313 /* no parameters, command_p invalid! */
316 if (parameters[0] == ':')
318 command_p[0] = parameters+1;
323 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
325 /* only one parameter */
326 command_p[0] = parameters;
327 if (parameters[0] == ':')
329 if (strchr(parameters,' ') != NULL)
337 command_p[j++] = parameters;
338 for (int i = 0; i <= q; i++)
340 if (parameters[i] == ' ')
342 command_p[j++] = parameters+i+1;
343 parameters[i] = '\0';
344 if (command_p[j-1][0] == ':')
346 *command_p[j-1]++; /* remove dodgy ":" */
348 /* parameter like this marks end of the sequence */
352 return j; /* returns total number of items in the list */
355 void CommandParser::ProcessCommand(userrec *user, char* cmd)
359 char *command_p[127];
360 char p[MAXBUF], temp[MAXBUF];
361 int j, items, cmd_found;
363 for (int i = 0; i < 127; i++)
379 int total_params = 0;
382 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
384 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
387 // found a 'trailing', we dont count them after this.
395 // another phidjit bug...
396 if (total_params > 126)
398 *(strchr(cmd,' ')) = '\0';
399 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
403 strlcpy(temp,cmd,MAXBUF);
405 std::string tmp = cmd;
406 for (int i = 0; i <= MODCOUNT; i++)
408 std::string oldtmp = tmp;
409 modules[i]->OnServerRaw(tmp,true,user);
412 log(DEBUG,"A Module changed the input string!");
413 log(DEBUG,"New string: %s",tmp.c_str());
414 log(DEBUG,"Old string: %s",oldtmp.c_str());
418 strlcpy(cmd,tmp.c_str(),MAXBUF);
419 strlcpy(temp,cmd,MAXBUF);
421 if (!strchr(cmd,' '))
423 /* no parameters, lets skip the formalities and not chop up
425 log(DEBUG,"About to preprocess command with no params");
429 for (unsigned int i = 0; i <= strlen(cmd); i++)
431 cmd[i] = toupper(cmd[i]);
439 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
440 for (unsigned int i = 0; i < strlen(temp); i++)
442 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
448 /* split the full string into a command plus parameters */
454 for (unsigned int i = 0; i <= strlen(cmd); i++)
456 /* capitalise the command ONLY, leave params intact */
457 cmd[i] = toupper(cmd[i]);
458 /* are we nearly there yet?! :P */
462 parameters = cmd+i+1;
470 for (unsigned int i = 0; i <= strlen(cmd); i++)
472 cmd[i] = toupper(cmd[i]);
479 if (strlen(command)>MAXCOMMAND)
481 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
485 for (unsigned int x = 0; x < strlen(command); x++)
487 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
489 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
491 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
493 ServerInstance->stats->statsUnknown++;
494 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
501 std::string xcommand = command;
502 for (unsigned int i = 0; i != cmdlist.size(); i++)
504 if ((xcommand.length() >= cmdlist[i]->command.length()) && (xcommand == cmdlist[i]->command))
510 items = this->ProcessParameters(command_p,parameters);
526 /* activity resets the ping pending timer */
527 user->nping = TIME + user->pingmax;
529 FOREACH_RESULT(OnPreCommand(command,command_p,items,user,false));
530 if (MOD_RESULT == 1) {
533 if ((items) < cmdlist[i]->min_params)
535 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
536 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
539 if ((!strchr(user->modes,cmdlist[i]->flags_needed)) && (cmdlist[i]->flags_needed))
541 log(DEBUG,"permission denied: %s %s",user->nick,command);
542 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
546 if ((cmdlist[i]->flags_needed) && (!user->HasPermission(xcommand)))
548 log(DEBUG,"permission denied: %s %s",user->nick,command);
549 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
553 /* if the command isnt USER, PASS, or NICK, and nick is empty,
555 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
557 if ((!isnick(user->nick)) || (user->registered != 7))
559 log(DEBUG,"not registered: %s %s",user->nick,command);
560 WriteServ(user->fd,"451 %s :You have not registered",command);
564 if ((user->registered == 7) && (!strchr(user->modes,'o')))
566 std::stringstream dcmds(Config->DisabledCommands);
571 if (!strcasecmp(thiscmd.c_str(),command))
573 // command is disabled!
574 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
579 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
581 /* ikky /stats counters */
584 cmdlist[i]->use_count++;
585 cmdlist[i]->total_bytes+=strlen(temp);
589 FOREACH_RESULT(OnPreCommand(command,command_p,items,user,true));
590 if (MOD_RESULT == 1) {
594 /* WARNING: nothing may come after the
595 * command handler call, as the handler
596 * may free the user structure! */
598 cmdlist[i]->Handle(command_p,items,user);
603 WriteServ(user->fd,"451 %s :You have not registered",command);
610 if ((!cmd_found) && (user))
612 ServerInstance->stats->statsUnknown++;
613 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
617 bool CommandParser::RemoveCommands(const char* source)
619 bool go_again = true;
623 for (std::deque<command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
625 command_t* x = (command_t*)*i;
626 if (x->source == std::string(source))
628 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
638 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
642 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
648 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
655 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
657 strlcpy(cmd,cmdbuf,MAXBUF);
662 int sl = strlen(cmd)-1;
663 if ((cmd[sl] == 13) || (cmd[sl] == 10))
668 if ((cmd[sl] == 13) || (cmd[sl] == 10))
673 while (cmd[sl] == ' ') // strip trailing spaces
683 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
687 this->ProcessCommand(user,cmd);
691 bool CommandParser::CreateCommand(command_t *f)
693 /* create the command and push it onto the table */
694 cmdlist.push_back(f);
695 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
699 CommandParser::CommandParser()
701 this->SetupCommandTable();
704 void CommandParser::SetupCommandTable()
706 this->CreateCommand(new cmd_user);
707 this->CreateCommand(new cmd_nick);
708 this->CreateCommand(new cmd_quit);
709 this->CreateCommand(new cmd_version);
710 this->CreateCommand(new cmd_ping);
711 this->CreateCommand(new cmd_pong);
712 this->CreateCommand(new cmd_admin);
713 this->CreateCommand(new cmd_privmsg);
714 this->CreateCommand(new cmd_info);
715 this->CreateCommand(new cmd_time);
716 this->CreateCommand(new cmd_whois);
717 this->CreateCommand(new cmd_wallops);
718 this->CreateCommand(new cmd_notice);
719 this->CreateCommand(new cmd_join);
720 this->CreateCommand(new cmd_names);
721 this->CreateCommand(new cmd_part);
722 this->CreateCommand(new cmd_kick);
723 this->CreateCommand(new cmd_mode);
724 this->CreateCommand(new cmd_topic);
725 this->CreateCommand(new cmd_who);
726 this->CreateCommand(new cmd_motd);
727 this->CreateCommand(new cmd_rules);
728 this->CreateCommand(new cmd_oper);
729 this->CreateCommand(new cmd_list);
730 this->CreateCommand(new cmd_die);
731 this->CreateCommand(new cmd_restart);
732 this->CreateCommand(new cmd_kill);
733 this->CreateCommand(new cmd_rehash);
734 this->CreateCommand(new cmd_lusers);
735 this->CreateCommand(new cmd_stats);
736 this->CreateCommand(new cmd_userhost);
737 this->CreateCommand(new cmd_away);
738 this->CreateCommand(new cmd_ison);
739 this->CreateCommand(new cmd_summon);
740 this->CreateCommand(new cmd_users);
741 this->CreateCommand(new cmd_invite);
742 this->CreateCommand(new cmd_pass);
743 this->CreateCommand(new cmd_trace);
744 this->CreateCommand(new cmd_whowas);
745 this->CreateCommand(new cmd_connect);
746 this->CreateCommand(new cmd_squit);
747 this->CreateCommand(new cmd_modules);
748 this->CreateCommand(new cmd_links);
749 this->CreateCommand(new cmd_map);
750 this->CreateCommand(new cmd_kline);
751 this->CreateCommand(new cmd_gline);
752 this->CreateCommand(new cmd_zline);
753 this->CreateCommand(new cmd_qline);
754 this->CreateCommand(new cmd_eline);
755 this->CreateCommand(new cmd_loadmodule);
756 this->CreateCommand(new cmd_unloadmodule);
757 this->CreateCommand(new cmd_server);
758 this->CreateCommand(new cmd_commands);