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