]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
EXPERIMENTAL: Command search function now uses std::map, should be faster (i hope)
[user/henk/code/inspircd.git] / src / command_parse.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <deque>
38 #include <sched.h>
39 #ifdef THREADED_DNS
40 #include <pthread.h>
41 #endif
42 #include "users.h"
43 #include "globals.h"
44 #include "modules.h"
45 #include "dynamic.h"
46 #include "wildcard.h"
47 #include "message.h"
48 #include "mode.h"
49 #include "commands.h"
50 #include "xline.h"
51 #include "inspstring.h"
52 #include "dnsqueue.h"
53 #include "helperfuncs.h"
54 #include "hashcomp.h"
55 #include "socketengine.h"
56 #include "userprocess.h"
57 #include "socket.h"
58 #include "dns.h"
59 #include "typedefs.h"
60 #include "command_parse.h"
61 #include "ctables.h"
62
63 extern InspIRCd* ServerInstance;
64
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;
69
70 extern int MODCOUNT;
71 extern InspSocket* socket_ref[65535];
72 extern time_t TIME;
73
74 extern SocketEngine* SE;
75
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];
80
81 extern Server* MyServer;
82 extern ServerConfig *Config;
83
84 extern user_hash clientlist;
85 extern chan_hash chanlist;
86
87 /* This function pokes and hacks at a parameter list like the following:
88  *
89  * PART #winbot,#darkgalaxy :m00!
90  *
91  * to turn it into a series of individual calls like this:
92  *
93  * PART #winbot :m00!
94  * PART #darkgalaxy :m00!
95  *
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) */
104
105 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
106 {
107         char plist[MAXBUF];
108         char *param;
109         char *pars[32];
110         char blog[32][MAXBUF];
111         char blog2[32][MAXBUF];
112         int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
113         char keystr[MAXBUF];
114         char moo[MAXBUF];
115
116         for (int i = 0; i <32; i++)
117                 *blog[i] = 0;
118
119         for (int i = 0; i <32; i++)
120                 *blog2[i] = 0;
121
122         *moo = 0;
123         for (int i = 0; i <10; i++)
124         {
125                 if (!parameters[i])
126                 {
127                         parameters[i] = moo;
128                 }
129         }
130         if (joins)
131         {
132                 if (pcnt > 1) /* we have a key to copy */
133                 {
134                         strlcpy(keystr,parameters[1],MAXBUF);
135                 }
136         }
137
138         if (!parameters[start])
139         {
140                 return 0;
141         }
142         if (!strchr(parameters[start],','))
143         {
144                 return 0;
145         }
146         *plist = 0;
147         for (int i = start; i <= end; i++)
148         {
149                 if (parameters[i])
150                 {
151                         strlcat(plist,parameters[i],MAXBUF);
152                 }
153         }
154
155         j = 0;
156         param = plist;
157
158         t = strlen(plist);
159         for (int i = 0; i < t; i++)
160         {
161                 if (plist[i] == ',')
162                 {
163                         plist[i] = '\0';
164                         strlcpy(blog[j++],param,MAXBUF);
165                         param = plist+i+1;
166                         if (j>20)
167                         {
168                                 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
169                                 return 1;
170                         }
171                 }
172         }
173         strlcpy(blog[j++],param,MAXBUF);
174         total = j;
175
176         if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
177         {
178                 strcat(keystr,",");
179         }
180
181         if ((joins) && (keystr))
182         {
183                 if (strchr(keystr,','))
184                 {
185                         j = 0;
186                         param = keystr;
187                         t2 = strlen(keystr);
188                         for (int i = 0; i < t2; i++)
189                         {
190                                 if (keystr[i] == ',')
191                                 {
192                                         keystr[i] = '\0';
193                                         strlcpy(blog2[j++],param,MAXBUF);
194                                         param = keystr+i+1;
195                                 }
196                         }
197                         strlcpy(blog2[j++],param,MAXBUF);
198                         total2 = j;
199                 }
200         }
201
202         for (j = 0; j < total; j++)
203         {
204                 if (blog[j])
205                 {
206                         pars[0] = blog[j];
207                 }
208                 for (q = end; q < pcnt-1; q++)
209                 {
210                         if (parameters[q+1])
211                         {
212                                 pars[q-end+1] = parameters[q+1];
213                         }
214                 }
215                 if ((joins) && (parameters[1]))
216                 {
217                         if (pcnt > 1)
218                         {
219                                 pars[1] = blog2[j];
220                         }
221                         else
222                         {
223                                 pars[1] = NULL;
224                         }
225                 }
226                 /* repeatedly call the function with the hacked parameter list */
227                 if ((joins) && (pcnt > 1))
228                 {
229                         if (pars[1])
230                         {
231                                 // pars[1] already set up and containing key from blog2[j]
232                                 fn->Handle(pars,2,u);
233                         }
234                         else
235                         {
236                                 pars[1] = parameters[1];
237                                 fn->Handle(pars,2,u);
238                         }
239                 }
240                 else
241                 {
242                         fn->Handle(pars,pcnt-(end-start),u);
243                 }
244         }
245
246         return 1;
247 }
248
249 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
250 {
251         std::map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
252         if (n != cmdlist.end())
253         {
254                         if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
255                         {
256                                 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
257                                 {
258                                         if (n->second->flags_needed)
259                                         {
260                                                 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
261                                                 {
262                                                         return true;
263                                                 }
264                                                 else
265                                                 {
266                                                         return false;
267                                                 }
268                                         }
269                                         return true;
270                                 }
271                         }
272         }
273         return false;
274 }
275
276 // calls a handler function for a command
277
278 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
279 {
280         std::map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
281         if (n != cmdlist.end())
282         {
283                         if (pcnt >= n->second->min_params)
284                         {
285                                 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
286                                 {
287                                         if (n->second->flags_needed)
288                                         {
289                                                 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
290                                                 {
291                                                         n->second->Handle(parameters,pcnt,user);
292                                                 }
293                                         }
294                                         else
295                                         {
296                                                 n->second->Handle(parameters,pcnt,user);
297                                         }
298                                 }
299                         }
300         }
301 }
302
303 int CommandParser::ProcessParameters(char **command_p,char *parameters)
304 {
305         int j = 0;
306         int q = strlen(parameters);
307         if (!q)
308         {
309                 /* no parameters, command_p invalid! */
310                 return 0;
311         }
312         if (parameters[0] == ':')
313         {
314                 command_p[0] = parameters+1;
315                 return 1;
316         }
317         if (q)
318         {
319                 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
320                 {
321                         /* only one parameter */
322                         command_p[0] = parameters;
323                         if (parameters[0] == ':')
324                         {
325                                 if (strchr(parameters,' ') != NULL)
326                                 {
327                                         command_p[0]++;
328                                 }
329                         }
330                         return 1;
331                 }
332         }
333         command_p[j++] = parameters;
334         for (int i = 0; i <= q; i++)
335         {
336                 if (parameters[i] == ' ')
337                 {
338                         command_p[j++] = parameters+i+1;
339                         parameters[i] = '\0';
340                         if (command_p[j-1][0] == ':')
341                         {
342                                 *command_p[j-1]++; /* remove dodgy ":" */
343                                 break;
344                                 /* parameter like this marks end of the sequence */
345                         }
346                 }
347         }
348         return j; /* returns total number of items in the list */
349 }
350
351 void CommandParser::ProcessCommand(userrec *user, char* cmd)
352 {
353         char *parameters;
354         char *command;
355         char *command_p[127];
356         char p[MAXBUF], temp[MAXBUF];
357         int j, items, cmd_found;
358
359         for (int i = 0; i < 127; i++)
360                 command_p[i] = NULL;
361
362         if (!user)
363         {
364                 return;
365         }
366         if (!cmd)
367         {
368                 return;
369         }
370         if (!cmd[0])
371         {
372                 return;
373         }
374
375         int total_params = 0;
376         unsigned int xl = strlen(cmd);
377         if (xl > 2)
378         {
379                 for (unsigned int q = 0; q < xl - 1; q++)
380                 {
381                         if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
382                         {
383                                 total_params++;
384                                 // found a 'trailing', we dont count them after this.
385                                 break;
386                         }
387                         if (cmd[q] == ' ')
388                                 total_params++;
389                 }
390         }
391
392         // another phidjit bug...
393         if (total_params > 126)
394         {
395                 *(strchr(cmd,' ')) = '\0';
396                 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
397                 return;
398         }
399
400         strlcpy(temp,cmd,MAXBUF);
401
402         std::string tmp = cmd;
403         for (int i = 0; i <= MODCOUNT; i++)
404         {
405                 std::string oldtmp = tmp;
406                 modules[i]->OnServerRaw(tmp,true,user);
407                 if (oldtmp != tmp)
408                 {
409                         log(DEBUG,"A Module changed the input string!");
410                         log(DEBUG,"New string: %s",tmp.c_str());
411                         log(DEBUG,"Old string: %s",oldtmp.c_str());
412                         break;
413                 }
414         }
415         strlcpy(cmd,tmp.c_str(),MAXBUF);
416         strlcpy(temp,cmd,MAXBUF);
417
418         if (!strchr(cmd,' '))
419         {
420                 /* no parameters, lets skip the formalities and not chop up
421                  * the string */
422                 log(DEBUG,"About to preprocess command with no params");
423                 items = 0;
424                 command_p[0] = NULL;
425                 parameters = NULL;
426                 unsigned int tl = strlen(cmd);
427                 for (unsigned int i = 0; i <= tl; i++)
428                 {
429                         cmd[i] = toupper(cmd[i]);
430                 }
431                 command = cmd;
432         }
433         else
434         {
435                 *cmd = 0;
436                 j = 0;
437
438                 unsigned int vl = strlen(temp);
439                 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
440                 for (unsigned int i = 0; i < vl; i++)
441                 {
442                         if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
443                         {
444                                 cmd[j++] = temp[i];
445                                 cmd[j] = 0;
446                         }
447                 }
448                 /* split the full string into a command plus parameters */
449                 parameters = p;
450                 p[0] = ' ';
451                 p[1] = 0;
452                 
453                 command = cmd;
454                 if (strchr(cmd,' '))
455                 {
456                         unsigned int cl = strlen(cmd);
457                         for (unsigned int i = 0; i <= cl; i++)
458                         {
459                                 /* capitalise the command ONLY, leave params intact */
460                                 cmd[i] = toupper(cmd[i]);
461                                 /* are we nearly there yet?! :P */
462                                 if (cmd[i] == ' ')
463                                 {
464                                         command = cmd;
465                                         parameters = cmd+i+1;
466                                         cmd[i] = '\0';
467                                         break;
468                                 }
469                         }
470                 }
471                 else
472                 {
473                         for (unsigned int i = 0; i <= strlen(cmd); i++)
474                         {
475                                 cmd[i] = toupper(cmd[i]);
476                         }
477                 }
478
479         }
480
481         if (strlen(command)>MAXCOMMAND)
482         {
483                 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
484                 return;
485         }
486
487         for (char* x = command; *x; x++)
488         {
489                 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
490                 {
491                         if (((*x < '0') || (*x> '9')) && (*x != '-'))
492                         {
493                                 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
494                                 {
495                                         ServerInstance->stats->statsUnknown++;
496                                         WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
497                                         return;
498                                 }
499                         }
500                 }
501         }
502
503         std::string xcommand = command;
504         
505         /* Tweak by brain - why was this INSIDE the mainloop? */
506         if (parameters)
507         {
508                  if (parameters[0])
509                  {
510                          items = this->ProcessParameters(command_p,parameters);
511                  }
512                  else
513                  {
514                          items = 0;
515                          command_p[0] = NULL;
516                  }
517         }
518         else
519         {
520                 items = 0;
521                 command_p[0] = NULL;
522         }
523
524         int MOD_RESULT = 0;
525         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
526         if (MOD_RESULT == 1) {
527                 return;
528         }
529         
530         std::map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
531         
532         if (cm != cmdlist.end())
533         {
534                         
535                                 if (user)
536                                 {
537                                         /* activity resets the ping pending timer */
538                                         user->nping = TIME + user->pingmax;
539                                         if ((items) < cm->second->min_params)
540                                         {
541                                                 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
542                                                 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
543                                                 return;
544                                         }
545                                         if ((!strchr(user->modes,cm->second->flags_needed)) && (cm->second->flags_needed))
546                                         {
547                                                 log(DEBUG,"permission denied: %s %s",user->nick,command);
548                                                 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
549                                                 cmd_found = 1;
550                                                 return;
551                                         }
552                                         if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
553                                         {
554                                                 log(DEBUG,"permission denied: %s %s",user->nick,command);
555                                                 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
556                                                 cmd_found = 1;
557                                                 return;
558                                         }
559                                         /* if the command isnt USER, PASS, or NICK, and nick is empty,
560                                          * deny command! */
561                                         if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
562                                         {
563                                                 if ((!isnick(user->nick)) || (user->registered != 7))
564                                                 {
565                                                         log(DEBUG,"not registered: %s %s",user->nick,command);
566                                                         WriteServ(user->fd,"451 %s :You have not registered",command);
567                                                         return;
568                                                 }
569                                         }
570                                         if ((user->registered == 7) && (!strchr(user->modes,'o')))
571                                         {
572                                                 std::stringstream dcmds(Config->DisabledCommands);
573                                                 while (!dcmds.eof())
574                                                 {
575                                                         std::string thiscmd;
576                                                         dcmds >> thiscmd;
577                                                         if (!strcasecmp(thiscmd.c_str(),command))
578                                                         {
579                                                                 // command is disabled!
580                                                                 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
581                                                                 return;
582                                                         }
583                                                 }
584                                         }
585                                         if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
586                                         {
587                                                         /* ikky /stats counters */
588                                                         if (temp)
589                                                         {
590                                                                 cm->second->use_count++;
591                                                                 cm->second->total_bytes+=strlen(temp);
592                                                         }
593
594                                                         int MOD_RESULT = 0;
595                                                         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
596                                                         if (MOD_RESULT == 1) {
597                                                                 return;
598                                                         }
599
600                                                         /* WARNING: nothing may come after the
601                                                          * command handler call, as the handler
602                                                          * may free the user structure! */
603
604                                                         cm->second->Handle(command_p,items,user);
605                                                 return;
606                                         }
607                                         else
608                                         {
609                                                 WriteServ(user->fd,"451 %s :You have not registered",command);
610                                                 return;
611                                         }
612                                 }
613         }
614         else if (user)
615         {
616                 ServerInstance->stats->statsUnknown++;
617                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
618         }
619 }
620
621 bool CommandParser::RemoveCommands(const char* source)
622 {
623         bool go_again = true;
624         while (go_again)
625         {
626                 go_again = false;
627                 for (std::map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
628                 {
629                         command_t* x = (command_t*)*i;
630                         if (x->source == std::string(source))
631                         {
632                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
633                                 cmdlist.erase(i);
634                                 go_again = true;
635                                 break;
636                         }
637                 }
638         }
639         return true;
640 }
641
642 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
643 {
644         if (!user)
645         {
646                 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
647                 return;
648         }
649         char cmd[MAXBUF];
650         if (!cmdbuf)
651         {
652                 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
653                 return;
654         }
655         if (!cmdbuf[0])
656         {
657                 return;
658         }
659         while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
660
661         strlcpy(cmd,cmdbuf,MAXBUF);
662         if (!cmd[0])
663         {
664                 return;
665         }
666         int sl = strlen(cmd)-1;
667         if ((cmd[sl] == 13) || (cmd[sl] == 10))
668         {
669                 cmd[sl] = '\0';
670         }
671         sl = strlen(cmd)-1;
672         if ((cmd[sl] == 13) || (cmd[sl] == 10))
673         {
674                 cmd[sl] = '\0';
675         }
676         sl = strlen(cmd)-1;
677         while (cmd[sl] == ' ') // strip trailing spaces
678         {
679                 cmd[sl] = '\0';
680                 sl = strlen(cmd)-1;
681         }
682
683         if (!cmd[0])
684         {
685                 return;
686         }
687         log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
688         tidystring(cmd);
689         if ((user) && (cmd))
690         {
691                 this->ProcessCommand(user,cmd);
692         }
693 }
694
695 bool CommandParser::CreateCommand(command_t *f)
696 {
697         /* create the command and push it onto the table */
698         cmdlist[f->command] = f;
699         log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
700         return true;
701 }
702
703 CommandParser::CommandParser()
704 {
705         this->SetupCommandTable();
706 }
707
708 void CommandParser::SetupCommandTable()
709 {
710         this->CreateCommand(new cmd_user);
711         this->CreateCommand(new cmd_nick);
712         this->CreateCommand(new cmd_quit);
713         this->CreateCommand(new cmd_version);
714         this->CreateCommand(new cmd_ping);
715         this->CreateCommand(new cmd_pong);
716         this->CreateCommand(new cmd_admin);
717         this->CreateCommand(new cmd_privmsg);
718         this->CreateCommand(new cmd_info);
719         this->CreateCommand(new cmd_time);
720         this->CreateCommand(new cmd_whois);
721         this->CreateCommand(new cmd_wallops);
722         this->CreateCommand(new cmd_notice);
723         this->CreateCommand(new cmd_join);
724         this->CreateCommand(new cmd_names);
725         this->CreateCommand(new cmd_part);
726         this->CreateCommand(new cmd_kick);
727         this->CreateCommand(new cmd_mode);
728         this->CreateCommand(new cmd_topic);
729         this->CreateCommand(new cmd_who);
730         this->CreateCommand(new cmd_motd);
731         this->CreateCommand(new cmd_rules);
732         this->CreateCommand(new cmd_oper);
733         this->CreateCommand(new cmd_list);
734         this->CreateCommand(new cmd_die);
735         this->CreateCommand(new cmd_restart);
736         this->CreateCommand(new cmd_kill);
737         this->CreateCommand(new cmd_rehash);
738         this->CreateCommand(new cmd_lusers);
739         this->CreateCommand(new cmd_stats);
740         this->CreateCommand(new cmd_userhost);
741         this->CreateCommand(new cmd_away);
742         this->CreateCommand(new cmd_ison);
743         this->CreateCommand(new cmd_summon);
744         this->CreateCommand(new cmd_users);
745         this->CreateCommand(new cmd_invite);
746         this->CreateCommand(new cmd_pass);
747         this->CreateCommand(new cmd_trace);
748         this->CreateCommand(new cmd_whowas);
749         this->CreateCommand(new cmd_connect);
750         this->CreateCommand(new cmd_squit);
751         this->CreateCommand(new cmd_modules);
752         this->CreateCommand(new cmd_links);
753         this->CreateCommand(new cmd_map);
754         this->CreateCommand(new cmd_kline);
755         this->CreateCommand(new cmd_gline);
756         this->CreateCommand(new cmd_zline);
757         this->CreateCommand(new cmd_qline);
758         this->CreateCommand(new cmd_eline);
759         this->CreateCommand(new cmd_loadmodule);
760         this->CreateCommand(new cmd_unloadmodule);
761         this->CreateCommand(new cmd_server);
762         this->CreateCommand(new cmd_commands);
763 }
764