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 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 Server* MyServer;
83 extern ServerConfig *Config;
85 extern user_hash clientlist;
86 extern chan_hash chanlist;
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)
111 char blog[32][MAXBUF];
112 char blog2[32][MAXBUF];
113 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
117 for (int i = 0; i <32; i++)
120 for (int i = 0; i <32; i++)
124 for (int i = 0; i <10; i++)
133 if (pcnt > 1) /* we have a key to copy */
135 strlcpy(keystr,parameters[1],MAXBUF);
139 if (!parameters[start])
143 if (!strchr(parameters[start],','))
148 for (int i = start; i <= end; i++)
152 strlcat(plist,parameters[i],MAXBUF);
160 for (int i = 0; i < t; i++)
165 strlcpy(blog[j++],param,MAXBUF);
169 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
174 strlcpy(blog[j++],param,MAXBUF);
177 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
182 if ((joins) && (keystr))
184 if (strchr(keystr,','))
189 for (int i = 0; i < t2; i++)
191 if (keystr[i] == ',')
194 strlcpy(blog2[j++],param,MAXBUF);
198 strlcpy(blog2[j++],param,MAXBUF);
203 for (j = 0; j < total; j++)
209 for (q = end; q < pcnt-1; q++)
213 pars[q-end+1] = parameters[q+1];
216 if ((joins) && (parameters[1]))
227 /* repeatedly call the function with the hacked parameter list */
228 if ((joins) && (pcnt > 1))
232 // pars[1] already set up and containing key from blog2[j]
233 fn->Handle(pars,2,u);
237 pars[1] = parameters[1];
238 fn->Handle(pars,2,u);
243 fn->Handle(pars,pcnt-(end-start),u);
250 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
252 for (unsigned int i = 0; i < cmdlist.size(); i++)
254 if (cmdlist[i]->command == commandname)
256 if ((pcnt>=cmdlist[i]->min_params) && (cmdlist[i]->source != "<core>"))
258 if ((strchr(user->modes,cmdlist[i]->flags_needed)) || (!cmdlist[i]->flags_needed))
260 if (cmdlist[i]->flags_needed)
262 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
279 // calls a handler function for a command
281 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
283 for (unsigned int i = 0; i < cmdlist.size(); i++)
285 if (cmdlist[i]->command == commandname)
287 if (pcnt>=cmdlist[i]->min_params)
289 if ((strchr(user->modes,cmdlist[i]->flags_needed)) || (!cmdlist[i]->flags_needed))
291 if (cmdlist[i]->flags_needed)
293 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
295 cmdlist[i]->Handle(parameters,pcnt,user);
300 cmdlist[i]->Handle(parameters,pcnt,user);
308 int CommandParser::ProcessParameters(char **command_p,char *parameters)
311 int q = strlen(parameters);
314 /* no parameters, command_p invalid! */
317 if (parameters[0] == ':')
319 command_p[0] = parameters+1;
324 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
326 /* only one parameter */
327 command_p[0] = parameters;
328 if (parameters[0] == ':')
330 if (strchr(parameters,' ') != NULL)
338 command_p[j++] = parameters;
339 for (int i = 0; i <= q; i++)
341 if (parameters[i] == ' ')
343 command_p[j++] = parameters+i+1;
344 parameters[i] = '\0';
345 if (command_p[j-1][0] == ':')
347 *command_p[j-1]++; /* remove dodgy ":" */
349 /* parameter like this marks end of the sequence */
353 return j; /* returns total number of items in the list */
356 void CommandParser::ProcessCommand(userrec *user, char* cmd)
360 char *command_p[127];
361 char p[MAXBUF], temp[MAXBUF];
362 int j, items, cmd_found;
364 for (int i = 0; i < 127; i++)
380 int total_params = 0;
383 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
385 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
388 // found a 'trailing', we dont count them after this.
396 // another phidjit bug...
397 if (total_params > 126)
399 *(strchr(cmd,' ')) = '\0';
400 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
404 strlcpy(temp,cmd,MAXBUF);
406 std::string tmp = cmd;
407 for (int i = 0; i <= MODCOUNT; i++)
409 std::string oldtmp = tmp;
410 modules[i]->OnServerRaw(tmp,true,user);
413 log(DEBUG,"A Module changed the input string!");
414 log(DEBUG,"New string: %s",tmp.c_str());
415 log(DEBUG,"Old string: %s",oldtmp.c_str());
419 strlcpy(cmd,tmp.c_str(),MAXBUF);
420 strlcpy(temp,cmd,MAXBUF);
422 if (!strchr(cmd,' '))
424 /* no parameters, lets skip the formalities and not chop up
426 log(DEBUG,"About to preprocess command with no params");
430 for (unsigned int i = 0; i <= strlen(cmd); i++)
432 cmd[i] = toupper(cmd[i]);
440 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
441 for (unsigned int i = 0; i < strlen(temp); i++)
443 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
449 /* split the full string into a command plus parameters */
455 for (unsigned int i = 0; i <= strlen(cmd); i++)
457 /* capitalise the command ONLY, leave params intact */
458 cmd[i] = toupper(cmd[i]);
459 /* are we nearly there yet?! :P */
463 parameters = cmd+i+1;
471 for (unsigned int i = 0; i <= strlen(cmd); i++)
473 cmd[i] = toupper(cmd[i]);
480 if (strlen(command)>MAXCOMMAND)
482 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
486 for (unsigned int x = 0; x < strlen(command); x++)
488 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
490 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
492 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
494 ServerInstance->stats->statsUnknown++;
495 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
502 std::string xcommand = command;
503 for (unsigned int i = 0; i != cmdlist.size(); i++)
505 if ((xcommand.length() >= cmdlist[i]->command.length()) && (xcommand == cmdlist[i]->command))
511 items = this->ProcessParameters(command_p,parameters);
527 /* activity resets the ping pending timer */
528 user->nping = TIME + user->pingmax;
529 if ((items) < cmdlist[i]->min_params)
531 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
532 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
535 if ((!strchr(user->modes,cmdlist[i]->flags_needed)) && (cmdlist[i]->flags_needed))
537 log(DEBUG,"permission denied: %s %s",user->nick,command);
538 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
542 if ((cmdlist[i]->flags_needed) && (!user->HasPermission(xcommand)))
544 log(DEBUG,"permission denied: %s %s",user->nick,command);
545 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
549 /* if the command isnt USER, PASS, or NICK, and nick is empty,
551 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
553 if ((!isnick(user->nick)) || (user->registered != 7))
555 log(DEBUG,"not registered: %s %s",user->nick,command);
556 WriteServ(user->fd,"451 %s :You have not registered",command);
560 if ((user->registered == 7) && (!strchr(user->modes,'o')))
562 std::stringstream dcmds(Config->DisabledCommands);
567 if (!strcasecmp(thiscmd.c_str(),command))
569 // command is disabled!
570 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
575 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
577 /* ikky /stats counters */
580 cmdlist[i]->use_count++;
581 cmdlist[i]->total_bytes+=strlen(temp);
585 FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
586 if (MOD_RESULT == 1) {
590 /* WARNING: nothing may come after the
591 * command handler call, as the handler
592 * may free the user structure! */
594 cmdlist[i]->Handle(command_p,items,user);
599 WriteServ(user->fd,"451 %s :You have not registered",command);
606 if ((!cmd_found) && (user))
608 ServerInstance->stats->statsUnknown++;
609 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
613 bool CommandParser::RemoveCommands(const char* source)
615 bool go_again = true;
619 for (std::deque<command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
621 command_t* x = (command_t*)*i;
622 if (x->source == std::string(source))
624 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
634 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
638 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
644 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
651 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
653 strlcpy(cmd,cmdbuf,MAXBUF);
658 int sl = strlen(cmd)-1;
659 if ((cmd[sl] == 13) || (cmd[sl] == 10))
664 if ((cmd[sl] == 13) || (cmd[sl] == 10))
669 while (cmd[sl] == ' ') // strip trailing spaces
679 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
683 this->ProcessCommand(user,cmd);
687 bool CommandParser::CreateCommand(command_t *f)
689 /* create the command and push it onto the table */
690 cmdlist.push_back(f);
691 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
695 CommandParser::CommandParser()
697 this->SetupCommandTable();
700 void CommandParser::SetupCommandTable()
702 this->CreateCommand(new cmd_user);
703 this->CreateCommand(new cmd_nick);
704 this->CreateCommand(new cmd_quit);
705 this->CreateCommand(new cmd_version);
706 this->CreateCommand(new cmd_ping);
707 this->CreateCommand(new cmd_pong);
708 this->CreateCommand(new cmd_admin);
709 this->CreateCommand(new cmd_privmsg);
710 this->CreateCommand(new cmd_info);
711 this->CreateCommand(new cmd_time);
712 this->CreateCommand(new cmd_whois);
713 this->CreateCommand(new cmd_wallops);
714 this->CreateCommand(new cmd_notice);
715 this->CreateCommand(new cmd_join);
716 this->CreateCommand(new cmd_names);
717 this->CreateCommand(new cmd_part);
718 this->CreateCommand(new cmd_kick);
719 this->CreateCommand(new cmd_mode);
720 this->CreateCommand(new cmd_topic);
721 this->CreateCommand(new cmd_who);
722 this->CreateCommand(new cmd_motd);
723 this->CreateCommand(new cmd_rules);
724 this->CreateCommand(new cmd_oper);
725 this->CreateCommand(new cmd_list);
726 this->CreateCommand(new cmd_die);
727 this->CreateCommand(new cmd_restart);
728 this->CreateCommand(new cmd_kill);
729 this->CreateCommand(new cmd_rehash);
730 this->CreateCommand(new cmd_lusers);
731 this->CreateCommand(new cmd_stats);
732 this->CreateCommand(new cmd_userhost);
733 this->CreateCommand(new cmd_away);
734 this->CreateCommand(new cmd_ison);
735 this->CreateCommand(new cmd_summon);
736 this->CreateCommand(new cmd_users);
737 this->CreateCommand(new cmd_invite);
738 this->CreateCommand(new cmd_pass);
739 this->CreateCommand(new cmd_trace);
740 this->CreateCommand(new cmd_whowas);
741 this->CreateCommand(new cmd_connect);
742 this->CreateCommand(new cmd_squit);
743 this->CreateCommand(new cmd_modules);
744 this->CreateCommand(new cmd_links);
745 this->CreateCommand(new cmd_map);
746 this->CreateCommand(new cmd_kline);
747 this->CreateCommand(new cmd_gline);
748 this->CreateCommand(new cmd_zline);
749 this->CreateCommand(new cmd_qline);
750 this->CreateCommand(new cmd_eline);
751 this->CreateCommand(new cmd_loadmodule);
752 this->CreateCommand(new cmd_unloadmodule);
753 this->CreateCommand(new cmd_server);
754 this->CreateCommand(new cmd_commands);