]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
8d07522060cee51b8f2a5c350c1ef20774920a20
[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 "ctables.h"
44 #include "globals.h"
45 #include "modules.h"
46 #include "dynamic.h"
47 #include "wildcard.h"
48 #include "message.h"
49 #include "mode.h"
50 #include "commands.h"
51 #include "xline.h"
52 #include "inspstring.h"
53 #include "dnsqueue.h"
54 #include "helperfuncs.h"
55 #include "hashcomp.h"
56 #include "socketengine.h"
57 #include "userprocess.h"
58 #include "socket.h"
59 #include "dns.h"
60 #include "typedefs.h"
61 #include "command_parse.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 Module* IOHookModule;
72 extern InspSocket* socket_ref[65535];
73 extern time_t TIME;
74
75 extern SocketEngine* SE;
76
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];
81
82 extern serverstats* stats;
83 extern Server* MyServer;
84 extern ServerConfig *Config;
85
86 extern user_hash clientlist;
87 extern chan_hash chanlist;
88 extern command_table cmdlist;
89
90 /* This function pokes and hacks at a parameter list like the following:
91  *
92  * PART #winbot,#darkgalaxy :m00!
93  *
94  * to turn it into a series of individual calls like this:
95  *
96  * PART #winbot :m00!
97  * PART #darkgalaxy :m00!
98  *
99  * The seperate calls are sent to a callback function provided by the caller
100  * (the caller will usually call itself recursively). The callback function
101  * must be a command handler. Calling this function on a line with no list causes
102  * no action to be taken. You must provide a starting and ending parameter number
103  * where the range of the list can be found, useful if you have a terminating
104  * parameter as above which is actually not part of the list, or parameters
105  * before the actual list as well. This code is used by many functions which
106  * can function as "one to list" (see the RFC) */
107
108 int CommandParser::LoopCall(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
109 {
110         char plist[MAXBUF];
111         char *param;
112         char *pars[32];
113         char blog[32][MAXBUF];
114         char blog2[32][MAXBUF];
115         int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
116         char keystr[MAXBUF];
117         char moo[MAXBUF];
118
119         for (int i = 0; i <32; i++)
120                 strcpy(blog[i],"");
121
122         for (int i = 0; i <32; i++)
123                 strcpy(blog2[i],"");
124
125         strcpy(moo,"");
126         for (int i = 0; i <10; i++)
127         {
128                 if (!parameters[i])
129                 {
130                         parameters[i] = moo;
131                 }
132         }
133         if (joins)
134         {
135                 if (pcnt > 1) /* we have a key to copy */
136                 {
137                         strlcpy(keystr,parameters[1],MAXBUF);
138                 }
139         }
140
141         if (!parameters[start])
142         {
143                 return 0;
144         }
145         if (!strchr(parameters[start],','))
146         {
147                 return 0;
148         }
149         strcpy(plist,"");
150         for (int i = start; i <= end; i++)
151         {
152                 if (parameters[i])
153                 {
154                         strlcat(plist,parameters[i],MAXBUF);
155                 }
156         }
157
158         j = 0;
159         param = plist;
160
161         t = strlen(plist);
162         for (int i = 0; i < t; i++)
163         {
164                 if (plist[i] == ',')
165                 {
166                         plist[i] = '\0';
167                         strlcpy(blog[j++],param,MAXBUF);
168                         param = plist+i+1;
169                         if (j>20)
170                         {
171                                 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
172                                 return 1;
173                         }
174                 }
175         }
176         strlcpy(blog[j++],param,MAXBUF);
177         total = j;
178
179         if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
180         {
181                 strcat(keystr,",");
182         }
183
184         if ((joins) && (keystr))
185         {
186                 if (strchr(keystr,','))
187                 {
188                         j = 0;
189                         param = keystr;
190                         t2 = strlen(keystr);
191                         for (int i = 0; i < t2; i++)
192                         {
193                                 if (keystr[i] == ',')
194                                 {
195                                         keystr[i] = '\0';
196                                         strlcpy(blog2[j++],param,MAXBUF);
197                                         param = keystr+i+1;
198                                 }
199                         }
200                         strlcpy(blog2[j++],param,MAXBUF);
201                         total2 = j;
202                 }
203         }
204
205         for (j = 0; j < total; j++)
206         {
207                 if (blog[j])
208                 {
209                         pars[0] = blog[j];
210                 }
211                 for (q = end; q < pcnt-1; q++)
212                 {
213                         if (parameters[q+1])
214                         {
215                                 pars[q-end+1] = parameters[q+1];
216                         }
217                 }
218                 if ((joins) && (parameters[1]))
219                 {
220                         if (pcnt > 1)
221                         {
222                                 pars[1] = blog2[j];
223                         }
224                         else
225                         {
226                                 pars[1] = NULL;
227                         }
228                 }
229                 /* repeatedly call the function with the hacked parameter list */
230                 if ((joins) && (pcnt > 1))
231                 {
232                         if (pars[1])
233                         {
234                                 // pars[1] already set up and containing key from blog2[j]
235                                 fn(pars,2,u);
236                         }
237                         else
238                         {
239                                 pars[1] = parameters[1];
240                                 fn(pars,2,u);
241                         }
242                 }
243                 else
244                 {
245                         fn(pars,pcnt-(end-start),u);
246                 }
247         }
248
249         return 1;
250 }
251
252 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
253 {
254         for (unsigned int i = 0; i < cmdlist.size(); i++)
255         {
256                 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
257                 {
258                         if (cmdlist[i].handler_function)
259                         {
260                                 if ((pcnt>=cmdlist[i].min_params) && (strcasecmp(cmdlist[i].source,"<core>")))
261                                 {
262                                         if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
263                                         {
264                                                 if (cmdlist[i].flags_needed)
265                                                 {
266                                                         if ((user->HasPermission(commandname)) || (is_uline(user->server)))
267                                                         {
268                                                                 return true;
269                                                         }
270                                                         else
271                                                         {
272                                                                 return false;
273                                                         }
274                                                 }
275                                                 return true;
276                                         }
277                                 }
278                         }
279                 }
280         }
281         return false;
282 }
283
284 // calls a handler function for a command
285
286 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
287 {
288         for (unsigned int i = 0; i < cmdlist.size(); i++)
289         {
290                 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
291                 {
292                         if (cmdlist[i].handler_function)
293                         {
294                                 if (pcnt>=cmdlist[i].min_params)
295                                 {
296                                         if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
297                                         {
298                                                 if (cmdlist[i].flags_needed)
299                                                 {
300                                                         if ((user->HasPermission(commandname)) || (is_uline(user->server)))
301                                                         {
302                                                                 cmdlist[i].handler_function(parameters,pcnt,user);
303                                                         }
304                                                 }
305                                                 else
306                                                 {
307                                                         cmdlist[i].handler_function(parameters,pcnt,user);
308                                                 }
309                                         }
310                                 }
311                         }
312                 }
313         }
314 }
315
316 int CommandParser::ProcessParameters(char **command_p,char *parameters)
317 {
318         int j = 0;
319         int q = strlen(parameters);
320         if (!q)
321         {
322                 /* no parameters, command_p invalid! */
323                 return 0;
324         }
325         if (parameters[0] == ':')
326         {
327                 command_p[0] = parameters+1;
328                 return 1;
329         }
330         if (q)
331         {
332                 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
333                 {
334                         /* only one parameter */
335                         command_p[0] = parameters;
336                         if (parameters[0] == ':')
337                         {
338                                 if (strchr(parameters,' ') != NULL)
339                                 {
340                                         command_p[0]++;
341                                 }
342                         }
343                         return 1;
344                 }
345         }
346         command_p[j++] = parameters;
347         for (int i = 0; i <= q; i++)
348         {
349                 if (parameters[i] == ' ')
350                 {
351                         command_p[j++] = parameters+i+1;
352                         parameters[i] = '\0';
353                         if (command_p[j-1][0] == ':')
354                         {
355                                 *command_p[j-1]++; /* remove dodgy ":" */
356                                 break;
357                                 /* parameter like this marks end of the sequence */
358                         }
359                 }
360         }
361         return j; /* returns total number of items in the list */
362 }
363
364 void CommandParser::ProcessCommand(userrec *user, char* cmd)
365 {
366         char *parameters;
367         char *command;
368         char *command_p[127];
369         char p[MAXBUF], temp[MAXBUF];
370         int j, items, cmd_found;
371
372         for (int i = 0; i < 127; i++)
373                 command_p[i] = NULL;
374
375         if (!user)
376         {
377                 return;
378         }
379         if (!cmd)
380         {
381                 return;
382         }
383         if (!cmd[0])
384         {
385                 return;
386         }
387
388         int total_params = 0;
389         if (strlen(cmd)>2)
390         {
391                 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
392                 {
393                         if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
394                         {
395                                 total_params++;
396                                 // found a 'trailing', we dont count them after this.
397                                 break;
398                         }
399                         if (cmd[q] == ' ')
400                                 total_params++;
401                 }
402         }
403
404         // another phidjit bug...
405         if (total_params > 126)
406         {
407                 *(strchr(cmd,' ')) = '\0';
408                 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
409                 return;
410         }
411
412         strlcpy(temp,cmd,MAXBUF);
413
414         std::string tmp = cmd;
415         for (int i = 0; i <= MODCOUNT; i++)
416         {
417                 std::string oldtmp = tmp;
418                 modules[i]->OnServerRaw(tmp,true,user);
419                 if (oldtmp != tmp)
420                 {
421                         log(DEBUG,"A Module changed the input string!");
422                         log(DEBUG,"New string: %s",tmp.c_str());
423                         log(DEBUG,"Old string: %s",oldtmp.c_str());
424                         break;
425                 }
426         }
427         strlcpy(cmd,tmp.c_str(),MAXBUF);
428         strlcpy(temp,cmd,MAXBUF);
429
430         if (!strchr(cmd,' '))
431         {
432                 /* no parameters, lets skip the formalities and not chop up
433                  * the string */
434                 log(DEBUG,"About to preprocess command with no params");
435                 items = 0;
436                 command_p[0] = NULL;
437                 parameters = NULL;
438                 for (unsigned int i = 0; i <= strlen(cmd); i++)
439                 {
440                         cmd[i] = toupper(cmd[i]);
441                 }
442                 command = cmd;
443         }
444         else
445         {
446                 strcpy(cmd,"");
447                 j = 0;
448                 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
449                 for (unsigned int i = 0; i < strlen(temp); i++)
450                 {
451                         if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
452                         {
453                                 cmd[j++] = temp[i];
454                                 cmd[j] = 0;
455                         }
456                 }
457                 /* split the full string into a command plus parameters */
458                 parameters = p;
459                 strcpy(p," ");
460                 command = cmd;
461                 if (strchr(cmd,' '))
462                 {
463                         for (unsigned int i = 0; i <= strlen(cmd); i++)
464                         {
465                                 /* capitalise the command ONLY, leave params intact */
466                                 cmd[i] = toupper(cmd[i]);
467                                 /* are we nearly there yet?! :P */
468                                 if (cmd[i] == ' ')
469                                 {
470                                         command = cmd;
471                                         parameters = cmd+i+1;
472                                         cmd[i] = '\0';
473                                         break;
474                                 }
475                         }
476                 }
477                 else
478                 {
479                         for (unsigned int i = 0; i <= strlen(cmd); i++)
480                         {
481                                 cmd[i] = toupper(cmd[i]);
482                         }
483                 }
484
485         }
486         cmd_found = 0;
487
488         if (strlen(command)>MAXCOMMAND)
489         {
490                 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
491                 return;
492         }
493
494         for (unsigned int x = 0; x < strlen(command); x++)
495         {
496                 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
497                 {
498                         if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
499                         {
500                                 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
501                                 {
502                                         stats->statsUnknown++;
503                                         WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
504                                         return;
505                                 }
506                         }
507                 }
508         }
509
510         std::string xcommand = command;
511         for (unsigned int i = 0; i != cmdlist.size(); i++)
512         {
513                 if (cmdlist[i].command[0])
514                 {
515                         if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
516                         {
517                                 if (parameters)
518                                 {
519                                         if (parameters[0])
520                                         {
521                                                 items = process_parameters(command_p,parameters);
522                                         }
523                                         else
524                                         {
525                                                 items = 0;
526                                                 command_p[0] = NULL;
527                                         }
528                                 }
529                                 else
530                                 {
531                                         items = 0;
532                                         command_p[0] = NULL;
533                                 }
534
535                                 if (user)
536                                 {
537                                         /* activity resets the ping pending timer */
538                                         user->nping = TIME + user->pingmax;
539                                         if ((items) < cmdlist[i].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,cmdlist[i].flags_needed)) && (cmdlist[i].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 ((cmdlist[i].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                                                 if (cmdlist[i].handler_function)
588                                                 {
589
590                                                         /* ikky /stats counters */
591                                                         if (temp)
592                                                         {
593                                                                 cmdlist[i].use_count++;
594                                                                 cmdlist[i].total_bytes+=strlen(temp);
595                                                         }
596
597                                                         int MOD_RESULT = 0;
598                                                         FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
599                                                         if (MOD_RESULT == 1) {
600                                                                 return;
601                                                         }
602
603                                                         /* WARNING: nothing may come after the
604                                                          * command handler call, as the handler
605                                                          * may free the user structure! */
606
607                                                         cmdlist[i].handler_function(command_p,items,user);
608                                                 }
609                                                 return;
610                                         }
611                                         else
612                                         {
613                                                 WriteServ(user->fd,"451 %s :You have not registered",command);
614                                                 return;
615                                         }
616                                 }
617                                 cmd_found = 1;
618                         }
619
620                 }
621         }
622         if ((!cmd_found) && (user))
623         {
624                 stats->statsUnknown++;
625                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
626         }
627 }
628
629 bool CommandParser::RemoveCommands(const char* source)
630 {
631         bool go_again = true;
632         while (go_again)
633         {
634                 go_again = false;
635                 for (std::deque<command_t>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
636                 {
637                         if (!strcmp(i->source,source))
638                         {
639                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",i->source,i->command);
640                                 cmdlist.erase(i);
641                                 go_again = true;
642                                 break;
643                         }
644                 }
645         }
646         return true;
647 }
648
649 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
650 {
651         if (!user)
652         {
653                 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
654                 return;
655         }
656         char cmd[MAXBUF];
657         if (!cmdbuf)
658         {
659                 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
660                 return;
661         }
662         if (!cmdbuf[0])
663         {
664                 return;
665         }
666         while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
667
668         strlcpy(cmd,cmdbuf,MAXBUF);
669         if (!cmd[0])
670         {
671                 return;
672         }
673         int sl = strlen(cmd)-1;
674         if ((cmd[sl] == 13) || (cmd[sl] == 10))
675         {
676                 cmd[sl] = '\0';
677         }
678         sl = strlen(cmd)-1;
679         if ((cmd[sl] == 13) || (cmd[sl] == 10))
680         {
681                 cmd[sl] = '\0';
682         }
683         sl = strlen(cmd)-1;
684         while (cmd[sl] == ' ') // strip trailing spaces
685         {
686                 cmd[sl] = '\0';
687                 sl = strlen(cmd)-1;
688         }
689
690         if (!cmd[0])
691         {
692                 return;
693         }
694         log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
695         tidystring(cmd);
696         if ((user) && (cmd))
697         {
698                 this->ProcessCommand(user,cmd);
699         }
700 }
701