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 * ---------------------------------------------------
17 #include "inspircd_config.h"
19 #include "configreader.h"
22 #include <sys/errno.h>
23 #include <sys/ioctl.h>
24 #include <sys/utsname.h>
42 #include "inspstring.h"
44 #include "helperfuncs.h"
46 #include "socketengine.h"
47 #include "userprocess.h"
51 #include "command_parse.h"
54 #define nspace __gnu_cxx
56 extern InspIRCd* ServerInstance;
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<InspSocket*> module_sockets;
61 extern std::vector<userrec*> local_users;
64 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
67 // This table references users by file descriptor.
68 // its an array to make it VERY fast, as all lookups are referenced
69 // by an integer, meaning there is no need for a scan/search operation.
70 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
72 extern Server* MyServer;
73 extern ServerConfig *Config;
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
78 /* Special commands which may occur without registration of the user */
79 cmd_user* command_user;
80 cmd_nick* command_nick;
81 cmd_pass* command_pass;
83 /* This function pokes and hacks at a parameter list like the following:
85 * PART #winbot,#darkgalaxy :m00!
87 * to turn it into a series of individual calls like this:
90 * PART #darkgalaxy :m00!
92 * The seperate calls are sent to a callback function provided by the caller
93 * (the caller will usually call itself recursively). The callback function
94 * must be a command handler. Calling this function on a line with no list causes
95 * no action to be taken. You must provide a starting and ending parameter number
96 * where the range of the list can be found, useful if you have a terminating
97 * parameter as above which is actually not part of the list, or parameters
98 * before the actual list as well. This code is used by many functions which
99 * can function as "one to list" (see the RFC) */
101 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
103 /* Copy of the parameter list, because like strltok, we make a bit of
104 * a mess of the parameter string we're given, and we want to keep this
107 char paramlist[MAXBUF];
108 /* Temporary variable used to hold one split parameter
111 /* Parameter list, we can have up to 32 of these
114 /* Seperated items, e.g. holds the #one and #two from "#one,#two"
117 /* Seperated keys, holds the 'two' and 'three' of "two,three"
120 /* Misc. counters, the total values hold the total number of
121 * seperated items in sep_items (total) and the total number of
122 * seperated items in sep_keys (total2)
124 int j = 0, q = 0, total = 0, total2 = 0;
125 /* A temporary copy of the comma seperated key list (see the description
126 * of the paramlist variable)
128 char keylist[MAXBUF];
129 /* Exactly what it says. Its nothing. We point invalid parameters at this.
133 /* First, initialize our arrays */
134 for (int i = 0; i < 32; i++)
135 sep_items[i] = sep_keys[i] = NULL;
137 /* Now find all parameters that are NULL, maybe above pcnt,
138 * and for safety, point them at 'nothing'
140 for (int i = 0; i < 10; i++)
144 parameters[i] = ¬hing;
147 /* Check if we're doing JOIN handling. JOIN has two lists in
148 * it, potentially, if we have keys, so this is a special-case
149 * to handle the keys if they are provided.
153 if (pcnt > 1) /* we have a key to copy */
155 strlcpy(keylist,parameters[1],MAXBUF);
159 /* There's nothing to split! We don't do anything
161 if (!parameters[start] || (!strchr(parameters[start],',')))
166 /* This function can handle multiple comma seperated
167 * lists as one, which is a feature no actual commands
168 * have yet -- this is futureproofing in case we encounter
169 * a command that does.
173 for (int i = start; i <= end; i++)
177 strlcat(paramlist,parameters[i],MAXBUF);
181 /* Now we split off paramlist into seperate parameters using
182 * pointer voodoo, this parameter list goes into sep_items
187 for (char* i = paramlist; *i; i++)
193 sep_items[j++] = param;
194 /* Iterate along to next item, if there is one */
196 if ((unsigned int)j > Config->MaxTargets)
198 /* BZZT! Too many items */
199 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
204 sep_items[j++] = param;
207 /* We add this extra comma here just to ensure we get all the keys
208 * in the event the user gave a malformed key string (yes, you guessed
209 * it, this is a mirc-ism)
211 if ((joins) && (*keylist) && (total>0)) // more than one channel and is joining
213 charlcat(keylist,',',MAXBUF);
216 /* If we're doing JOIN handling (e.g. we've had to kludge for two
217 * lists being handled at once, real smart by the way JRO </sarcasm>)
218 * and we also have keys, we must seperate out this key list into
219 * our char** list sep_keys for later use.
221 if ((joins) && (*keylist))
223 /* There is more than one key */
224 if (strchr(keylist,','))
228 for (char* i = keylist; *i; i++)
233 sep_keys[j++] = param;
238 sep_keys[j++] = param;
243 /* Now the easier bit. We call the command class's Handle function
244 * X times, where x is the number of parameters we've 'collated'.
246 for (j = 0; j < total; j++)
250 pars[0] = sep_items[j];
253 for (q = end; q < pcnt-1; q++)
257 pars[q-end+1] = parameters[q+1];
261 if ((joins) && (parameters[1]))
265 pars[1] = sep_keys[j];
273 /* repeatedly call the function with the hacked parameter list */
274 if ((joins) && (pcnt > 1))
278 /* pars[1] already set up and containing key from sep_keys[j] */
279 fn->Handle(pars,2,u);
283 pars[1] = parameters[1];
284 fn->Handle(pars,2,u);
289 fn->Handle(pars,pcnt-(end-start),u);
296 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
298 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
300 if (n != cmdlist.end())
302 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
304 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
306 if (n->second->flags_needed)
308 return ((user->HasPermission(commandname)) || (is_uline(user->server)));
317 // calls a handler function for a command
319 bool CommandParser::CallHandler(const std::string &commandname,char **parameters, int pcnt, userrec *user)
321 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
323 if (n != cmdlist.end())
325 if (pcnt >= n->second->min_params)
327 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
329 if (n->second->flags_needed)
331 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
333 n->second->Handle(parameters,pcnt,user);
339 n->second->Handle(parameters,pcnt,user);
348 int CommandParser::ProcessParameters(char **command_p,char *parameters)
354 /* no parameters, command_p invalid! */
358 if (*parameters == ':')
360 command_p[0] = parameters+1;
366 char* n = strchr(parameters,' ');
367 if ((!n) || (*parameters == ':'))
369 /* only one parameter */
370 command_p[0] = parameters;
371 if (*parameters == ':')
383 command_p[j++] = parameters;
385 for (char* i = parameters; *i; i++)
389 command_p[j++] = i+1;
392 if (*command_p[j-1] == ':')
394 *command_p[j-1]++; /* remove dodgy ":" */
396 /* parameter like this marks end of the sequence */
400 return j; /* returns total number of items in the list */
403 void CommandParser::ProcessCommand(userrec *user, char* cmd)
407 char *command_p[127];
408 char p[MAXBUF], temp[MAXBUF];
409 int j, items, cmd_found;
410 int total_params = 0;
412 for (int i = 0; i < 127; i++)
415 if (!user || !cmd || !*cmd)
420 char* first_space = NULL;
422 /* If the command is > 2 characters (quick and dirty way to find out) */
423 if (*cmd && *(cmd+1) && *(cmd+2))
425 for (char* q = cmd; *q; q++)
433 // found a 'trailing', we dont count them after this.
442 // another phidjit bug...
443 if (total_params > 126)
446 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
450 strlcpy(temp,cmd,MAXBUF);
452 std::string tmp = cmd;
454 for (int i = 0; i <= MODCOUNT; i++)
456 std::string oldtmp = tmp;
457 modules[i]->OnServerRaw(tmp,true,user);
460 log(DEBUG,"A Module changed the input string!");
461 log(DEBUG,"New string: %s",tmp.c_str());
462 log(DEBUG,"Old string: %s",oldtmp.c_str());
467 strlcpy(cmd,tmp.c_str(),MAXBUF);
468 strlcpy(temp,cmd,MAXBUF);
470 char* has_space = strchr(cmd,' ');
475 * no parameters, lets skip the formalities and not chop up
478 log(DEBUG,"About to preprocess command with no params");
482 for (char* i = cmd; *i; i++,cm_length++)
491 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
492 for (char* i = temp; *i; i++)
494 if ((*i != 10) && (*i != 13) && (*i != 0) && (*i != 7))
501 /* split the full string into a command plus parameters */
510 for (char* i = cmd; *i; i++)
512 /* capitalise the command ONLY, leave params intact */
514 /* are we nearly there yet?! :P */
526 for (char* i = cmd; *i; i++,cm_length++)
533 if (cm_length > MAXCOMMAND)
535 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
539 for (char* x = command; *x; x++)
541 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
543 if (((*x < '0') || (*x> '9')) && (*x != '-'))
545 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
547 ServerInstance->stats->statsUnknown++;
548 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
555 std::string xcommand = command;
556 if ((user->registered != 7) && (xcommand == "SERVER"))
558 kill_link(user,"Server connection to non-server port");
562 /* Tweak by brain - why was this INSIDE the mainloop? */
567 items = this->ProcessParameters(command_p,parameters);
582 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
583 if (MOD_RESULT == 1) {
587 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
589 if (cm != cmdlist.end())
593 /* activity resets the ping pending timer */
594 user->nping = TIME + user->pingmax;
595 if ((items) < cm->second->min_params)
597 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
598 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
601 if (cm->second->flags_needed)
603 if (!user->modes[cm->second->flags_needed-65])
605 log(DEBUG,"permission denied: %s %s",user->nick,command);
606 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
611 if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
613 log(DEBUG,"permission denied: %s %s",user->nick,command);
614 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
616 WriteOpers("*** \2WARNING\2: Command '%s' not allowed for oper '%s', dropped.",command,user->nick);
620 /* if the command isnt USER, PASS, or NICK, and nick is empty,
622 if ((cm->second != command_user) && (cm->second != command_nick) && (cm->second != command_pass))
624 if ((!isnick(user->nick)) || (user->registered != 7))
626 log(DEBUG,"not registered: %s %s",user->nick,command);
627 WriteServ(user->fd,"451 %s :You have not registered",command);
631 if ((user->registered == 7) && (!*user->oper) && (*Config->DisabledCommands))
633 std::stringstream dcmds(Config->DisabledCommands);
635 while (dcmds >> thiscmd)
637 if (!strcasecmp(thiscmd.c_str(),command))
639 // command is disabled!
640 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
645 if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
647 /* ikky /stats counters */
650 cm->second->use_count++;
651 cm->second->total_bytes+=strlen(temp);
655 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
662 * WARNING: nothing may come after the
663 * command handler call, as the handler
664 * may free the user structure!
667 cm->second->Handle(command_p,items,user);
672 WriteServ(user->fd,"451 %s :You have not registered",command);
679 ServerInstance->stats->statsUnknown++;
680 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
684 bool CommandParser::RemoveCommands(const char* source)
686 bool go_again = true;
692 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
694 command_t* x = i->second;
695 if (x->source == std::string(source))
697 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
708 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
712 if (!user || !cmdbuf || !*cmdbuf)
714 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
718 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
723 strlcpy(cmd,cmdbuf,MAXBUF);
725 while (charremove(cmd,10));
726 while (charremove(cmd,13));
728 int sl = strlen(cmd)-1;
729 while (sl && (cmd[sl] == ' ')) // strip trailing spaces
737 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
743 this->ProcessCommand(user,cmd);
747 bool CommandParser::CreateCommand(command_t *f)
749 /* create the command and push it onto the table */
750 if (cmdlist.find(f->command) == cmdlist.end())
752 cmdlist[f->command] = f;
753 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
759 CommandParser::CommandParser()
761 this->SetupCommandTable();
764 void CommandParser::SetupCommandTable()
766 /* These three are special (can occur without
767 * full user registration) and so are saved
770 command_user = new cmd_user;
771 command_nick = new cmd_nick;
772 command_pass = new cmd_pass;
773 this->CreateCommand(command_user);
774 this->CreateCommand(command_nick);
775 this->CreateCommand(command_pass);
777 /* The rest of these arent special. boo hoo.
779 this->CreateCommand(new cmd_quit);
780 this->CreateCommand(new cmd_version);
781 this->CreateCommand(new cmd_ping);
782 this->CreateCommand(new cmd_pong);
783 this->CreateCommand(new cmd_admin);
784 this->CreateCommand(new cmd_privmsg);
785 this->CreateCommand(new cmd_info);
786 this->CreateCommand(new cmd_time);
787 this->CreateCommand(new cmd_whois);
788 this->CreateCommand(new cmd_wallops);
789 this->CreateCommand(new cmd_notice);
790 this->CreateCommand(new cmd_join);
791 this->CreateCommand(new cmd_names);
792 this->CreateCommand(new cmd_part);
793 this->CreateCommand(new cmd_kick);
794 this->CreateCommand(new cmd_mode);
795 this->CreateCommand(new cmd_topic);
796 this->CreateCommand(new cmd_who);
797 this->CreateCommand(new cmd_motd);
798 this->CreateCommand(new cmd_rules);
799 this->CreateCommand(new cmd_oper);
800 this->CreateCommand(new cmd_list);
801 this->CreateCommand(new cmd_die);
802 this->CreateCommand(new cmd_restart);
803 this->CreateCommand(new cmd_kill);
804 this->CreateCommand(new cmd_rehash);
805 this->CreateCommand(new cmd_lusers);
806 this->CreateCommand(new cmd_stats);
807 this->CreateCommand(new cmd_userhost);
808 this->CreateCommand(new cmd_away);
809 this->CreateCommand(new cmd_ison);
810 this->CreateCommand(new cmd_summon);
811 this->CreateCommand(new cmd_users);
812 this->CreateCommand(new cmd_invite);
813 this->CreateCommand(new cmd_trace);
814 this->CreateCommand(new cmd_whowas);
815 this->CreateCommand(new cmd_connect);
816 this->CreateCommand(new cmd_squit);
817 this->CreateCommand(new cmd_modules);
818 this->CreateCommand(new cmd_links);
819 this->CreateCommand(new cmd_map);
820 this->CreateCommand(new cmd_kline);
821 this->CreateCommand(new cmd_gline);
822 this->CreateCommand(new cmd_zline);
823 this->CreateCommand(new cmd_qline);
824 this->CreateCommand(new cmd_eline);
825 this->CreateCommand(new cmd_loadmodule);
826 this->CreateCommand(new cmd_unloadmodule);
827 this->CreateCommand(new cmd_server);
828 this->CreateCommand(new cmd_commands);