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++)
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] || (!strchr(parameters[start],',')))
150 for (int i = start; i <= end; i++)
154 strlcat(plist,parameters[i],MAXBUF);
163 for (int i = 0; i < t; i++)
168 strlcpy(blog[j++],param,MAXBUF);
170 if ((unsigned int)j > Config->MaxTargets)
172 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,','))
194 for (int i = 0; i < t2; i++)
196 if (keystr[i] == ',')
199 strlcpy(blog2[j++],param,MAXBUF);
204 strlcpy(blog2[j++],param,MAXBUF);
209 for (j = 0; j < total; j++)
216 for (q = end; q < pcnt-1; q++)
220 pars[q-end+1] = parameters[q+1];
224 if ((joins) && (parameters[1]))
236 /* repeatedly call the function with the hacked parameter list */
237 if ((joins) && (pcnt > 1))
241 // pars[1] already set up and containing key from blog2[j]
242 fn->Handle(pars,2,u);
246 pars[1] = parameters[1];
247 fn->Handle(pars,2,u);
252 fn->Handle(pars,pcnt-(end-start),u);
259 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
261 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
263 if (n != cmdlist.end())
265 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
267 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
269 if (n->second->flags_needed)
271 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
289 // calls a handler function for a command
291 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
293 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
295 if (n != cmdlist.end())
297 if (pcnt >= n->second->min_params)
299 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
301 if (n->second->flags_needed)
303 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
305 n->second->Handle(parameters,pcnt,user);
310 n->second->Handle(parameters,pcnt,user);
317 int CommandParser::ProcessParameters(char **command_p,char *parameters)
320 int q = strlen(parameters);
324 /* no parameters, command_p invalid! */
328 if (parameters[0] == ':')
330 command_p[0] = parameters+1;
336 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
338 /* only one parameter */
339 command_p[0] = parameters;
340 if (parameters[0] == ':')
342 if (strchr(parameters,' ') != NULL)
352 command_p[j++] = parameters;
354 for (int i = 0; i <= q; i++)
356 if (parameters[i] == ' ')
358 command_p[j++] = parameters+i+1;
359 parameters[i] = '\0';
361 if (command_p[j-1][0] == ':')
363 *command_p[j-1]++; /* remove dodgy ":" */
365 /* parameter like this marks end of the sequence */
370 return j; /* returns total number of items in the list */
373 void CommandParser::ProcessCommand(userrec *user, char* cmd)
377 char *command_p[127];
378 char p[MAXBUF], temp[MAXBUF];
379 int j, items, cmd_found;
380 int total_params = 0;
383 for (int i = 0; i < 127; i++)
386 if (!user || !cmd || !*cmd)
395 for (unsigned int q = 0; q < xl - 1; q++)
402 // found a 'trailing', we dont count them after this.
411 // another phidjit bug...
412 if (total_params > 126)
414 *(strchr(cmd,' ')) = '\0';
415 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
419 strlcpy(temp,cmd,MAXBUF);
421 std::string tmp = cmd;
423 for (int i = 0; i <= MODCOUNT; i++)
425 std::string oldtmp = tmp;
426 modules[i]->OnServerRaw(tmp,true,user);
429 log(DEBUG,"A Module changed the input string!");
430 log(DEBUG,"New string: %s",tmp.c_str());
431 log(DEBUG,"Old string: %s",oldtmp.c_str());
436 strlcpy(cmd,tmp.c_str(),MAXBUF);
437 strlcpy(temp,cmd,MAXBUF);
439 if (!strchr(cmd,' '))
442 * no parameters, lets skip the formalities and not chop up
445 log(DEBUG,"About to preprocess command with no params");
449 unsigned int tl = strlen(cmd);
450 for (unsigned int i = 0; i <= tl; i++)
452 cmd[i] = toupper(cmd[i]);
461 unsigned int vl = strlen(temp);
462 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
463 for (unsigned int i = 0; i < vl; i++)
465 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
472 /* split the full string into a command plus parameters */
481 unsigned int cl = strlen(cmd);
483 for (unsigned int i = 0; i <= cl; i++)
485 /* capitalise the command ONLY, leave params intact */
486 cmd[i] = toupper(cmd[i]);
487 /* are we nearly there yet?! :P */
491 parameters = cmd+i+1;
499 for (char* i = cmd; *i; i++)
506 if (strlen(command)>MAXCOMMAND)
508 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
512 for (char* x = command; *x; x++)
514 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
516 if (((*x < '0') || (*x> '9')) && (*x != '-'))
518 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
520 ServerInstance->stats->statsUnknown++;
521 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
528 std::string xcommand = command;
529 if ((user->registered != 7) && (xcommand == "SERVER"))
531 kill_link(user,"Server connection to non-server port");
535 /* Tweak by brain - why was this INSIDE the mainloop? */
540 items = this->ProcessParameters(command_p,parameters);
555 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
556 if (MOD_RESULT == 1) {
560 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
562 if (cm != cmdlist.end())
566 /* activity resets the ping pending timer */
567 user->nping = TIME + user->pingmax;
568 if ((items) < cm->second->min_params)
570 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
571 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
574 if ((!strchr(user->modes,cm->second->flags_needed)) && (cm->second->flags_needed))
576 log(DEBUG,"permission denied: %s %s",user->nick,command);
577 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
581 if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
583 log(DEBUG,"permission denied: %s %s",user->nick,command);
584 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
588 /* if the command isnt USER, PASS, or NICK, and nick is empty,
590 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
592 if ((!isnick(user->nick)) || (user->registered != 7))
594 log(DEBUG,"not registered: %s %s",user->nick,command);
595 WriteServ(user->fd,"451 %s :You have not registered",command);
599 if ((user->registered == 7) && (!*user->oper) && (*Config->DisabledCommands))
601 std::stringstream dcmds(Config->DisabledCommands);
603 while (dcmds >> thiscmd)
605 if (!strcasecmp(thiscmd.c_str(),command))
607 // command is disabled!
608 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
613 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
615 /* ikky /stats counters */
618 cm->second->use_count++;
619 cm->second->total_bytes+=strlen(temp);
623 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
630 * WARNING: nothing may come after the
631 * command handler call, as the handler
632 * may free the user structure!
635 cm->second->Handle(command_p,items,user);
640 WriteServ(user->fd,"451 %s :You have not registered",command);
647 ServerInstance->stats->statsUnknown++;
648 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
652 bool CommandParser::RemoveCommands(const char* source)
654 bool go_again = true;
660 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
662 command_t* x = i->second;
663 if (x->source == std::string(source))
665 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
676 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
682 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
687 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
695 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
697 strlcpy(cmd,cmdbuf,MAXBUF);
705 /* XXX - This is ugly as sin, and incomprehensible - why are we doing this twice, for instance? --w00t */
706 int sl = strlen(cmd)-1;
708 if ((cmd[sl] == 13) || (cmd[sl] == 10))
715 if ((cmd[sl] == 13) || (cmd[sl] == 10))
723 while (cmd[sl] == ' ') // strip trailing spaces
734 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
738 this->ProcessCommand(user,cmd);
742 bool CommandParser::CreateCommand(command_t *f)
744 /* create the command and push it onto the table */
745 if (cmdlist.find(f->command) == cmdlist.end())
747 cmdlist[f->command] = f;
748 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
754 CommandParser::CommandParser()
756 this->SetupCommandTable();
759 void CommandParser::SetupCommandTable()
761 this->CreateCommand(new cmd_user);
762 this->CreateCommand(new cmd_nick);
763 this->CreateCommand(new cmd_quit);
764 this->CreateCommand(new cmd_version);
765 this->CreateCommand(new cmd_ping);
766 this->CreateCommand(new cmd_pong);
767 this->CreateCommand(new cmd_admin);
768 this->CreateCommand(new cmd_privmsg);
769 this->CreateCommand(new cmd_info);
770 this->CreateCommand(new cmd_time);
771 this->CreateCommand(new cmd_whois);
772 this->CreateCommand(new cmd_wallops);
773 this->CreateCommand(new cmd_notice);
774 this->CreateCommand(new cmd_join);
775 this->CreateCommand(new cmd_names);
776 this->CreateCommand(new cmd_part);
777 this->CreateCommand(new cmd_kick);
778 this->CreateCommand(new cmd_mode);
779 this->CreateCommand(new cmd_topic);
780 this->CreateCommand(new cmd_who);
781 this->CreateCommand(new cmd_motd);
782 this->CreateCommand(new cmd_rules);
783 this->CreateCommand(new cmd_oper);
784 this->CreateCommand(new cmd_list);
785 this->CreateCommand(new cmd_die);
786 this->CreateCommand(new cmd_restart);
787 this->CreateCommand(new cmd_kill);
788 this->CreateCommand(new cmd_rehash);
789 this->CreateCommand(new cmd_lusers);
790 this->CreateCommand(new cmd_stats);
791 this->CreateCommand(new cmd_userhost);
792 this->CreateCommand(new cmd_away);
793 this->CreateCommand(new cmd_ison);
794 this->CreateCommand(new cmd_summon);
795 this->CreateCommand(new cmd_users);
796 this->CreateCommand(new cmd_invite);
797 this->CreateCommand(new cmd_pass);
798 this->CreateCommand(new cmd_trace);
799 this->CreateCommand(new cmd_whowas);
800 this->CreateCommand(new cmd_connect);
801 this->CreateCommand(new cmd_squit);
802 this->CreateCommand(new cmd_modules);
803 this->CreateCommand(new cmd_links);
804 this->CreateCommand(new cmd_map);
805 this->CreateCommand(new cmd_kline);
806 this->CreateCommand(new cmd_gline);
807 this->CreateCommand(new cmd_zline);
808 this->CreateCommand(new cmd_qline);
809 this->CreateCommand(new cmd_eline);
810 this->CreateCommand(new cmd_loadmodule);
811 this->CreateCommand(new cmd_unloadmodule);
812 this->CreateCommand(new cmd_server);
813 this->CreateCommand(new cmd_commands);