]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
02db6258f62bcd9faddedf63a2fd9697f6d25c64
[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 #ifdef GCC3
64 #define nspace __gnu_cxx
65 #else
66 #define nspace std
67 #endif
68
69
70 extern InspIRCd* ServerInstance;
71
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;
76
77 extern int MODCOUNT;
78 extern InspSocket* socket_ref[65535];
79 extern time_t TIME;
80
81 extern SocketEngine* SE;
82
83 // This table references users by file descriptor.
84 // its an array to make it VERY fast, as all lookups are referenced
85 // by an integer, meaning there is no need for a scan/search operation.
86 extern userrec* fd_ref_table[65536];
87
88 extern Server* MyServer;
89 extern ServerConfig *Config;
90
91 extern user_hash clientlist;
92 extern chan_hash chanlist;
93
94 /* This function pokes and hacks at a parameter list like the following:
95  *
96  * PART #winbot,#darkgalaxy :m00!
97  *
98  * to turn it into a series of individual calls like this:
99  *
100  * PART #winbot :m00!
101  * PART #darkgalaxy :m00!
102  *
103  * The seperate calls are sent to a callback function provided by the caller
104  * (the caller will usually call itself recursively). The callback function
105  * must be a command handler. Calling this function on a line with no list causes
106  * no action to be taken. You must provide a starting and ending parameter number
107  * where the range of the list can be found, useful if you have a terminating
108  * parameter as above which is actually not part of the list, or parameters
109  * before the actual list as well. This code is used by many functions which
110  * can function as "one to list" (see the RFC) */
111
112 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
113 {
114         char plist[MAXBUF];
115         char *param;
116         char *pars[32];
117         char blog[32][MAXBUF];
118         char blog2[32][MAXBUF];
119         int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
120         char keystr[MAXBUF];
121         char moo[MAXBUF];
122
123         for (int i = 0; i <32; i++)
124                 *blog[i] = 0;
125
126         for (int i = 0; i <32; i++)
127                 *blog2[i] = 0;
128
129         *moo = 0;
130         for (int i = 0; i <10; i++)
131         {
132                 if (!parameters[i])
133                 {
134                         parameters[i] = moo;
135                 }
136         }
137         if (joins)
138         {
139                 if (pcnt > 1) /* we have a key to copy */
140                 {
141                         strlcpy(keystr,parameters[1],MAXBUF);
142                 }
143         }
144
145         if (!parameters[start])
146         {
147                 return 0;
148         }
149         if (!strchr(parameters[start],','))
150         {
151                 return 0;
152         }
153         *plist = 0;
154         for (int i = start; i <= end; i++)
155         {
156                 if (parameters[i])
157                 {
158                         strlcat(plist,parameters[i],MAXBUF);
159                 }
160         }
161
162         j = 0;
163         param = plist;
164
165         t = strlen(plist);
166         for (int i = 0; i < t; i++)
167         {
168                 if (plist[i] == ',')
169                 {
170                         plist[i] = '\0';
171                         strlcpy(blog[j++],param,MAXBUF);
172                         param = plist+i+1;
173                         if (j>20)
174                         {
175                                 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
176                                 return 1;
177                         }
178                 }
179         }
180         strlcpy(blog[j++],param,MAXBUF);
181         total = j;
182
183         if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
184         {
185                 strcat(keystr,",");
186         }
187
188         if ((joins) && (keystr))
189         {
190                 if (strchr(keystr,','))
191                 {
192                         j = 0;
193                         param = keystr;
194                         t2 = strlen(keystr);
195                         for (int i = 0; i < t2; i++)
196                         {
197                                 if (keystr[i] == ',')
198                                 {
199                                         keystr[i] = '\0';
200                                         strlcpy(blog2[j++],param,MAXBUF);
201                                         param = keystr+i+1;
202                                 }
203                         }
204                         strlcpy(blog2[j++],param,MAXBUF);
205                         total2 = j;
206                 }
207         }
208
209         for (j = 0; j < total; j++)
210         {
211                 if (blog[j])
212                 {
213                         pars[0] = blog[j];
214                 }
215                 for (q = end; q < pcnt-1; q++)
216                 {
217                         if (parameters[q+1])
218                         {
219                                 pars[q-end+1] = parameters[q+1];
220                         }
221                 }
222                 if ((joins) && (parameters[1]))
223                 {
224                         if (pcnt > 1)
225                         {
226                                 pars[1] = blog2[j];
227                         }
228                         else
229                         {
230                                 pars[1] = NULL;
231                         }
232                 }
233                 /* repeatedly call the function with the hacked parameter list */
234                 if ((joins) && (pcnt > 1))
235                 {
236                         if (pars[1])
237                         {
238                                 // pars[1] already set up and containing key from blog2[j]
239                                 fn->Handle(pars,2,u);
240                         }
241                         else
242                         {
243                                 pars[1] = parameters[1];
244                                 fn->Handle(pars,2,u);
245                         }
246                 }
247                 else
248                 {
249                         fn->Handle(pars,pcnt-(end-start),u);
250                 }
251         }
252
253         return 1;
254 }
255
256 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
257 {
258         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
259         if (n != cmdlist.end())
260         {
261                         if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
262                         {
263                                 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
264                                 {
265                                         if (n->second->flags_needed)
266                                         {
267                                                 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
268                                                 {
269                                                         return true;
270                                                 }
271                                                 else
272                                                 {
273                                                         return false;
274                                                 }
275                                         }
276                                         return true;
277                                 }
278                         }
279         }
280         return false;
281 }
282
283 // calls a handler function for a command
284
285 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
286 {
287         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
288         if (n != cmdlist.end())
289         {
290                         if (pcnt >= n->second->min_params)
291                         {
292                                 if ((strchr(user->modes,n->second->flags_needed)) || (!n->second->flags_needed))
293                                 {
294                                         if (n->second->flags_needed)
295                                         {
296                                                 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
297                                                 {
298                                                         n->second->Handle(parameters,pcnt,user);
299                                                 }
300                                         }
301                                         else
302                                         {
303                                                 n->second->Handle(parameters,pcnt,user);
304                                         }
305                                 }
306                         }
307         }
308 }
309
310 int CommandParser::ProcessParameters(char **command_p,char *parameters)
311 {
312         int j = 0;
313         int q = strlen(parameters);
314         if (!q)
315         {
316                 /* no parameters, command_p invalid! */
317                 return 0;
318         }
319         if (parameters[0] == ':')
320         {
321                 command_p[0] = parameters+1;
322                 return 1;
323         }
324         if (q)
325         {
326                 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
327                 {
328                         /* only one parameter */
329                         command_p[0] = parameters;
330                         if (parameters[0] == ':')
331                         {
332                                 if (strchr(parameters,' ') != NULL)
333                                 {
334                                         command_p[0]++;
335                                 }
336                         }
337                         return 1;
338                 }
339         }
340         command_p[j++] = parameters;
341         for (int i = 0; i <= q; i++)
342         {
343                 if (parameters[i] == ' ')
344                 {
345                         command_p[j++] = parameters+i+1;
346                         parameters[i] = '\0';
347                         if (command_p[j-1][0] == ':')
348                         {
349                                 *command_p[j-1]++; /* remove dodgy ":" */
350                                 break;
351                                 /* parameter like this marks end of the sequence */
352                         }
353                 }
354         }
355         return j; /* returns total number of items in the list */
356 }
357
358 void CommandParser::ProcessCommand(userrec *user, char* cmd)
359 {
360         char *parameters;
361         char *command;
362         char *command_p[127];
363         char p[MAXBUF], temp[MAXBUF];
364         int j, items, cmd_found;
365
366         for (int i = 0; i < 127; i++)
367                 command_p[i] = NULL;
368
369         if (!user)
370         {
371                 return;
372         }
373         if (!cmd)
374         {
375                 return;
376         }
377         if (!cmd[0])
378         {
379                 return;
380         }
381
382         int total_params = 0;
383         unsigned int xl = strlen(cmd);
384         if (xl > 2)
385         {
386                 for (unsigned int q = 0; q < xl - 1; q++)
387                 {
388                         if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
389                         {
390                                 total_params++;
391                                 // found a 'trailing', we dont count them after this.
392                                 break;
393                         }
394                         if (cmd[q] == ' ')
395                                 total_params++;
396                 }
397         }
398
399         // another phidjit bug...
400         if (total_params > 126)
401         {
402                 *(strchr(cmd,' ')) = '\0';
403                 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
404                 return;
405         }
406
407         strlcpy(temp,cmd,MAXBUF);
408
409         std::string tmp = cmd;
410         for (int i = 0; i <= MODCOUNT; i++)
411         {
412                 std::string oldtmp = tmp;
413                 modules[i]->OnServerRaw(tmp,true,user);
414                 if (oldtmp != tmp)
415                 {
416                         log(DEBUG,"A Module changed the input string!");
417                         log(DEBUG,"New string: %s",tmp.c_str());
418                         log(DEBUG,"Old string: %s",oldtmp.c_str());
419                         break;
420                 }
421         }
422         strlcpy(cmd,tmp.c_str(),MAXBUF);
423         strlcpy(temp,cmd,MAXBUF);
424
425         if (!strchr(cmd,' '))
426         {
427                 /* no parameters, lets skip the formalities and not chop up
428                  * the string */
429                 log(DEBUG,"About to preprocess command with no params");
430                 items = 0;
431                 command_p[0] = NULL;
432                 parameters = NULL;
433                 unsigned int tl = strlen(cmd);
434                 for (unsigned int i = 0; i <= tl; i++)
435                 {
436                         cmd[i] = toupper(cmd[i]);
437                 }
438                 command = cmd;
439         }
440         else
441         {
442                 *cmd = 0;
443                 j = 0;
444
445                 unsigned int vl = strlen(temp);
446                 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
447                 for (unsigned int i = 0; i < vl; i++)
448                 {
449                         if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
450                         {
451                                 cmd[j++] = temp[i];
452                                 cmd[j] = 0;
453                         }
454                 }
455                 /* split the full string into a command plus parameters */
456                 parameters = p;
457                 p[0] = ' ';
458                 p[1] = 0;
459                 
460                 command = cmd;
461                 if (strchr(cmd,' '))
462                 {
463                         unsigned int cl = strlen(cmd);
464                         for (unsigned int i = 0; i <= cl; i++)
465                         {
466                                 /* capitalise the command ONLY, leave params intact */
467                                 cmd[i] = toupper(cmd[i]);
468                                 /* are we nearly there yet?! :P */
469                                 if (cmd[i] == ' ')
470                                 {
471                                         command = cmd;
472                                         parameters = cmd+i+1;
473                                         cmd[i] = '\0';
474                                         break;
475                                 }
476                         }
477                 }
478                 else
479                 {
480                         for (unsigned int i = 0; i <= strlen(cmd); i++)
481                         {
482                                 cmd[i] = toupper(cmd[i]);
483                         }
484                 }
485
486         }
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 (char* x = command; *x; x++)
495         {
496                 if (((*x < 'A') || (*x > 'Z')) && (*x != '.'))
497                 {
498                         if (((*x < '0') || (*x> '9')) && (*x != '-'))
499                         {
500                                 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",*x))
501                                 {
502                                         ServerInstance->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         
512         /* Tweak by brain - why was this INSIDE the mainloop? */
513         if (parameters)
514         {
515                  if (parameters[0])
516                  {
517                          items = this->ProcessParameters(command_p,parameters);
518                  }
519                  else
520                  {
521                          items = 0;
522                          command_p[0] = NULL;
523                  }
524         }
525         else
526         {
527                 items = 0;
528                 command_p[0] = NULL;
529         }
530
531         int MOD_RESULT = 0;
532         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
533         if (MOD_RESULT == 1) {
534                 return;
535         }
536         
537         nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(xcommand);
538         
539         if (cm != cmdlist.end())
540         {
541                         
542                                 if (user)
543                                 {
544                                         /* activity resets the ping pending timer */
545                                         user->nping = TIME + user->pingmax;
546                                         if ((items) < cm->second->min_params)
547                                         {
548                                                 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
549                                                 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
550                                                 return;
551                                         }
552                                         if ((!strchr(user->modes,cm->second->flags_needed)) && (cm->second->flags_needed))
553                                         {
554                                                 log(DEBUG,"permission denied: %s %s",user->nick,command);
555                                                 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
556                                                 cmd_found = 1;
557                                                 return;
558                                         }
559                                         if ((cm->second->flags_needed) && (!user->HasPermission(xcommand)))
560                                         {
561                                                 log(DEBUG,"permission denied: %s %s",user->nick,command);
562                                                 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
563                                                 cmd_found = 1;
564                                                 return;
565                                         }
566                                         /* if the command isnt USER, PASS, or NICK, and nick is empty,
567                                          * deny command! */
568                                         if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
569                                         {
570                                                 if ((!isnick(user->nick)) || (user->registered != 7))
571                                                 {
572                                                         log(DEBUG,"not registered: %s %s",user->nick,command);
573                                                         WriteServ(user->fd,"451 %s :You have not registered",command);
574                                                         return;
575                                                 }
576                                         }
577                                         if ((user->registered == 7) && (!strchr(user->modes,'o')))
578                                         {
579                                                 std::stringstream dcmds(Config->DisabledCommands);
580                                                 while (!dcmds.eof())
581                                                 {
582                                                         std::string thiscmd;
583                                                         dcmds >> thiscmd;
584                                                         if (!strcasecmp(thiscmd.c_str(),command))
585                                                         {
586                                                                 // command is disabled!
587                                                                 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
588                                                                 return;
589                                                         }
590                                                 }
591                                         }
592                                         if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
593                                         {
594                                                         /* ikky /stats counters */
595                                                         if (temp)
596                                                         {
597                                                                 cm->second->use_count++;
598                                                                 cm->second->total_bytes+=strlen(temp);
599                                                         }
600
601                                                         int MOD_RESULT = 0;
602                                                         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
603                                                         if (MOD_RESULT == 1) {
604                                                                 return;
605                                                         }
606
607                                                         /* WARNING: nothing may come after the
608                                                          * command handler call, as the handler
609                                                          * may free the user structure! */
610
611                                                         cm->second->Handle(command_p,items,user);
612                                                 return;
613                                         }
614                                         else
615                                         {
616                                                 WriteServ(user->fd,"451 %s :You have not registered",command);
617                                                 return;
618                                         }
619                                 }
620         }
621         else if (user)
622         {
623                 ServerInstance->stats->statsUnknown++;
624                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
625         }
626 }
627
628 bool CommandParser::RemoveCommands(const char* source)
629 {
630         bool go_again = true;
631         while (go_again)
632         {
633                 go_again = false;
634                 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
635                 {
636                         command_t* x = i->second;
637                         if (x->source == std::string(source))
638                         {
639                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
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
702 bool CommandParser::CreateCommand(command_t *f)
703 {
704         /* create the command and push it onto the table */
705         cmdlist[f->command] = f;
706         log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
707         return true;
708 }
709
710 CommandParser::CommandParser()
711 {
712         this->SetupCommandTable();
713 }
714
715 void CommandParser::SetupCommandTable()
716 {
717         this->CreateCommand(new cmd_user);
718         this->CreateCommand(new cmd_nick);
719         this->CreateCommand(new cmd_quit);
720         this->CreateCommand(new cmd_version);
721         this->CreateCommand(new cmd_ping);
722         this->CreateCommand(new cmd_pong);
723         this->CreateCommand(new cmd_admin);
724         this->CreateCommand(new cmd_privmsg);
725         this->CreateCommand(new cmd_info);
726         this->CreateCommand(new cmd_time);
727         this->CreateCommand(new cmd_whois);
728         this->CreateCommand(new cmd_wallops);
729         this->CreateCommand(new cmd_notice);
730         this->CreateCommand(new cmd_join);
731         this->CreateCommand(new cmd_names);
732         this->CreateCommand(new cmd_part);
733         this->CreateCommand(new cmd_kick);
734         this->CreateCommand(new cmd_mode);
735         this->CreateCommand(new cmd_topic);
736         this->CreateCommand(new cmd_who);
737         this->CreateCommand(new cmd_motd);
738         this->CreateCommand(new cmd_rules);
739         this->CreateCommand(new cmd_oper);
740         this->CreateCommand(new cmd_list);
741         this->CreateCommand(new cmd_die);
742         this->CreateCommand(new cmd_restart);
743         this->CreateCommand(new cmd_kill);
744         this->CreateCommand(new cmd_rehash);
745         this->CreateCommand(new cmd_lusers);
746         this->CreateCommand(new cmd_stats);
747         this->CreateCommand(new cmd_userhost);
748         this->CreateCommand(new cmd_away);
749         this->CreateCommand(new cmd_ison);
750         this->CreateCommand(new cmd_summon);
751         this->CreateCommand(new cmd_users);
752         this->CreateCommand(new cmd_invite);
753         this->CreateCommand(new cmd_pass);
754         this->CreateCommand(new cmd_trace);
755         this->CreateCommand(new cmd_whowas);
756         this->CreateCommand(new cmd_connect);
757         this->CreateCommand(new cmd_squit);
758         this->CreateCommand(new cmd_modules);
759         this->CreateCommand(new cmd_links);
760         this->CreateCommand(new cmd_map);
761         this->CreateCommand(new cmd_kline);
762         this->CreateCommand(new cmd_gline);
763         this->CreateCommand(new cmd_zline);
764         this->CreateCommand(new cmd_qline);
765         this->CreateCommand(new cmd_eline);
766         this->CreateCommand(new cmd_loadmodule);
767         this->CreateCommand(new cmd_unloadmodule);
768         this->CreateCommand(new cmd_server);
769         this->CreateCommand(new cmd_commands);
770 }
771