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)
115 char blog[32][MAXBUF];
116 char blog2[32][MAXBUF];
117 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
121 for (int i = 0; i <32; i++)
124 for (int i = 0; i <32; i++)
128 for (int i = 0; i <10; i++)
137 if (pcnt > 1) /* we have a key to copy */
139 strlcpy(keystr,parameters[1],MAXBUF);
143 if (!parameters[start])
147 if (!strchr(parameters[start],','))
152 for (int i = start; i <= end; i++)
156 strlcat(plist,parameters[i],MAXBUF);
164 for (int i = 0; i < t; i++)
169 strlcpy(blog[j++],param,MAXBUF);
171 if ((unsigned int)j > Config->MaxTargets)
173 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
178 strlcpy(blog[j++],param,MAXBUF);
181 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
186 if ((joins) && (keystr))
188 if (strchr(keystr,','))
193 for (int i = 0; i < t2; i++)
195 if (keystr[i] == ',')
198 strlcpy(blog2[j++],param,MAXBUF);
202 strlcpy(blog2[j++],param,MAXBUF);
207 for (j = 0; j < total; j++)
213 for (q = end; q < pcnt-1; q++)
217 pars[q-end+1] = parameters[q+1];
220 if ((joins) && (parameters[1]))
231 /* repeatedly call the function with the hacked parameter list */
232 if ((joins) && (pcnt > 1))
236 // pars[1] already set up and containing key from blog2[j]
237 fn->Handle(pars,2,u);
241 pars[1] = parameters[1];
242 fn->Handle(pars,2,u);
247 fn->Handle(pars,pcnt-(end-start),u);
254 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
256 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
257 if (n != cmdlist.end())
259 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
261 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
263 if (n->second->flags_needed)
265 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
281 // calls a handler function for a command
283 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
285 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
286 if (n != cmdlist.end())
288 if (pcnt >= n->second->min_params)
290 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
292 if (n->second->flags_needed)
294 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
296 n->second->Handle(parameters,pcnt,user);
301 n->second->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;
381 unsigned int xl = strlen(cmd);
384 for (unsigned int q = 0; q < xl - 1; q++)
386 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
389 // found a 'trailing', we dont count them after this.
397 // another phidjit bug...
398 if (total_params > 126)
400 *(strchr(cmd,' ')) = '\0';
401 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
405 strlcpy(temp,cmd,MAXBUF);
407 std::string tmp = cmd;
408 for (int i = 0; i <= MODCOUNT; i++)
410 std::string oldtmp = tmp;
411 modules[i]->OnServerRaw(tmp,true,user);
414 log(DEBUG,"A Module changed the input string!");
415 log(DEBUG,"New string: %s",tmp.c_str());
416 log(DEBUG,"Old string: %s",oldtmp.c_str());
420 strlcpy(cmd,tmp.c_str(),MAXBUF);
421 strlcpy(temp,cmd,MAXBUF);
423 if (!strchr(cmd,' '))
425 /* no parameters, lets skip the formalities and not chop up
427 log(DEBUG,"About to preprocess command with no params");
431 unsigned int tl = strlen(cmd);
432 for (unsigned int i = 0; i <= tl; i++)
434 cmd[i] = toupper(cmd[i]);
443 unsigned int vl = strlen(temp);
444 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
445 for (unsigned int i = 0; i < vl; i++)
447 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
453 /* split the full string into a command plus parameters */
461 unsigned int cl = strlen(cmd);
462 for (unsigned int i = 0; i <= cl; 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 (char* i = cmd; *i; i++)
486 if (strlen(command)>MAXCOMMAND)
488 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
492 for (char* x = command; *x; x++)
494 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
496 if (((*x < '0') || (*x> '9')) && (*x != '-'))
498 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
500 ServerInstance->stats->statsUnknown++;
501 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
508 std::string xcommand = command;
509 if ((user->registered != 7) && (xcommand == "SERVER"))
511 kill_link(user,"Server connection to non-server port");
515 /* Tweak by brain - why was this INSIDE the mainloop? */
520 items = this->ProcessParameters(command_p,parameters);
535 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
536 if (MOD_RESULT == 1) {
540 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
542 if (cm != cmdlist.end())
546 /* activity resets the ping pending timer */
547 user->nping = TIME + user->pingmax;
548 if ((items) < cm->second->min_params)
550 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
551 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
554 if ((!strchr(user->modes,cm->second->flags_needed)) && (cm->second->flags_needed))
556 log(DEBUG,"permission denied: %s %s",user->nick,command);
557 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
561 if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
563 log(DEBUG,"permission denied: %s %s",user->nick,command);
564 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
568 /* if the command isnt USER, PASS, or NICK, and nick is empty,
570 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
572 if ((!isnick(user->nick)) || (user->registered != 7))
574 log(DEBUG,"not registered: %s %s",user->nick,command);
575 WriteServ(user->fd,"451 %s :You have not registered",command);
579 if ((user->registered == 7) && (!strchr(user->modes,'o')))
581 std::stringstream dcmds(Config->DisabledCommands);
586 if (!strcasecmp(thiscmd.c_str(),command))
588 // command is disabled!
589 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
594 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
596 /* ikky /stats counters */
599 cm->second->use_count++;
600 cm->second->total_bytes+=strlen(temp);
604 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
605 if (MOD_RESULT == 1) {
609 /* WARNING: nothing may come after the
610 * command handler call, as the handler
611 * may free the user structure! */
613 cm->second->Handle(command_p,items,user);
618 WriteServ(user->fd,"451 %s :You have not registered",command);
625 ServerInstance->stats->statsUnknown++;
626 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
630 bool CommandParser::RemoveCommands(const char* source)
632 bool go_again = true;
636 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
638 command_t* x = i->second;
639 if (x->source == std::string(source))
641 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
651 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
655 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
661 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
668 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
670 strlcpy(cmd,cmdbuf,MAXBUF);
675 int sl = strlen(cmd)-1;
676 if ((cmd[sl] == 13) || (cmd[sl] == 10))
681 if ((cmd[sl] == 13) || (cmd[sl] == 10))
686 while (cmd[sl] == ' ') // strip trailing spaces
696 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
700 this->ProcessCommand(user,cmd);
704 bool CommandParser::CreateCommand(command_t *f)
706 /* create the command and push it onto the table */
707 cmdlist[f->command] = f;
708 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
712 CommandParser::CommandParser()
714 this->SetupCommandTable();
717 void CommandParser::SetupCommandTable()
719 this->CreateCommand(new cmd_user);
720 this->CreateCommand(new cmd_nick);
721 this->CreateCommand(new cmd_quit);
722 this->CreateCommand(new cmd_version);
723 this->CreateCommand(new cmd_ping);
724 this->CreateCommand(new cmd_pong);
725 this->CreateCommand(new cmd_admin);
726 this->CreateCommand(new cmd_privmsg);
727 this->CreateCommand(new cmd_info);
728 this->CreateCommand(new cmd_time);
729 this->CreateCommand(new cmd_whois);
730 this->CreateCommand(new cmd_wallops);
731 this->CreateCommand(new cmd_notice);
732 this->CreateCommand(new cmd_join);
733 this->CreateCommand(new cmd_names);
734 this->CreateCommand(new cmd_part);
735 this->CreateCommand(new cmd_kick);
736 this->CreateCommand(new cmd_mode);
737 this->CreateCommand(new cmd_topic);
738 this->CreateCommand(new cmd_who);
739 this->CreateCommand(new cmd_motd);
740 this->CreateCommand(new cmd_rules);
741 this->CreateCommand(new cmd_oper);
742 this->CreateCommand(new cmd_list);
743 this->CreateCommand(new cmd_die);
744 this->CreateCommand(new cmd_restart);
745 this->CreateCommand(new cmd_kill);
746 this->CreateCommand(new cmd_rehash);
747 this->CreateCommand(new cmd_lusers);
748 this->CreateCommand(new cmd_stats);
749 this->CreateCommand(new cmd_userhost);
750 this->CreateCommand(new cmd_away);
751 this->CreateCommand(new cmd_ison);
752 this->CreateCommand(new cmd_summon);
753 this->CreateCommand(new cmd_users);
754 this->CreateCommand(new cmd_invite);
755 this->CreateCommand(new cmd_pass);
756 this->CreateCommand(new cmd_trace);
757 this->CreateCommand(new cmd_whowas);
758 this->CreateCommand(new cmd_connect);
759 this->CreateCommand(new cmd_squit);
760 this->CreateCommand(new cmd_modules);
761 this->CreateCommand(new cmd_links);
762 this->CreateCommand(new cmd_map);
763 this->CreateCommand(new cmd_kline);
764 this->CreateCommand(new cmd_gline);
765 this->CreateCommand(new cmd_zline);
766 this->CreateCommand(new cmd_qline);
767 this->CreateCommand(new cmd_eline);
768 this->CreateCommand(new cmd_loadmodule);
769 this->CreateCommand(new cmd_unloadmodule);
770 this->CreateCommand(new cmd_server);
771 this->CreateCommand(new cmd_commands);