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