]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
9170e3941ffd10932d6f22927ad8b6a7ad582ec3
[user/henk/code/inspircd.git] / src / command_parse.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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 #include "inspircd_config.h"
18 #include "inspircd.h"
19 #include "configreader.h"
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/errno.h>
23 #include <sys/ioctl.h>
24 #include <sys/utsname.h>
25 #include <time.h>
26 #include <string>
27 #include <sstream>
28 #include <vector>
29 #include <sched.h>
30 #ifdef THREADED_DNS
31 #include <pthread.h>
32 #endif
33 #include "users.h"
34 #include "globals.h"
35 #include "modules.h"
36 #include "dynamic.h"
37 #include "wildcard.h"
38 #include "message.h"
39 #include "mode.h"
40 #include "commands.h"
41 #include "xline.h"
42 #include "inspstring.h"
43 #include "dnsqueue.h"
44 #include "helperfuncs.h"
45 #include "hashcomp.h"
46 #include "socketengine.h"
47 #include "userprocess.h"
48 #include "socket.h"
49 #include "dns.h"
50 #include "typedefs.h"
51 #include "command_parse.h"
52 #include "ctables.h"
53
54 #define nspace __gnu_cxx
55
56 extern InspIRCd* ServerInstance;
57
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<InspSocket*> module_sockets;
61 extern std::vector<userrec*> local_users;
62
63 extern int MODCOUNT;
64 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
65 extern time_t TIME;
66
67 // This table references users by file descriptor.
68 // its an array to make it VERY fast, as all lookups are referenced
69 // by an integer, meaning there is no need for a scan/search operation.
70 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
71
72 extern Server* MyServer;
73 extern ServerConfig *Config;
74
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
77
78 /* Special commands which may occur without registration of the user */
79 cmd_user* command_user;
80 cmd_nick* command_nick;
81 cmd_pass* command_pass;
82
83 /* This function pokes and hacks at a parameter list like the following:
84  *
85  * PART #winbot,#darkgalaxy :m00!
86  *
87  * to turn it into a series of individual calls like this:
88  *
89  * PART #winbot :m00!
90  * PART #darkgalaxy :m00!
91  *
92  * The seperate calls are sent to a callback function provided by the caller
93  * (the caller will usually call itself recursively). The callback function
94  * must be a command handler. Calling this function on a line with no list causes
95  * no action to be taken. You must provide a starting and ending parameter number
96  * where the range of the list can be found, useful if you have a terminating
97  * parameter as above which is actually not part of the list, or parameters
98  * before the actual list as well. This code is used by many functions which
99  * can function as "one to list" (see the RFC) */
100
101 int CommandParser::LoopCall(command_t* fn, const char** parameters, int pcnt, userrec *u, int start, int end, int joins)
102 {
103         /* Copy of the parameter list, because like strltok, we make a bit of
104          * a mess of the parameter string we're given, and we want to keep this
105          * private.
106          */
107         char paramlist[MAXBUF];
108         /* Temporary variable used to hold one split parameter
109          */
110         char *param;
111         /* Parameter list, we can have up to 32 of these
112          */
113         const char *pars[32];
114         /* Seperated items, e.g. holds the #one and #two from "#one,#two"
115          */
116         const char *sep_items[32];
117         /* Seperated keys, holds the 'two' and 'three' of "two,three"
118          */
119         const char *sep_keys[32];
120         /* Misc. counters, the total values hold the total number of
121          * seperated items in sep_items (total) and the total number of
122          * seperated items in sep_keys (total2)
123          */
124         int j = 0, q = 0, total = 0, total2 = 0;
125         /* A temporary copy of the comma seperated key list (see the description
126          * of the paramlist variable)
127          */
128         char keylist[MAXBUF];
129         /* Exactly what it says. Its nothing. We point invalid parameters at this.
130          */
131         char nothing = 0;
132
133         /* First, initialize our arrays */
134         for (int i = 0; i < 32; i++)
135                 sep_items[i] = sep_keys[i] = NULL;
136
137         /* Now find all parameters that are NULL, maybe above pcnt,
138          * and for safety, point them at 'nothing'
139          */
140         for (int i = 0; i < 10; i++)
141         {
142                 if (!parameters[i])
143                 {
144                         parameters[i] = &nothing;
145                 }
146         }
147         /* Check if we're doing JOIN handling. JOIN has two lists in
148          * it, potentially, if we have keys, so this is a special-case
149          * to handle the keys if they are provided.
150          */
151         if (joins)
152         {
153                 if (pcnt > 1) /* we have a key to copy */
154                 {
155                         strlcpy(keylist,parameters[1],MAXBUF);
156                 }
157         }
158
159         /* There's nothing to split! We don't do anything
160          */
161         if (!parameters[start] || (!strchr(parameters[start],',')))
162         {
163                 return 0;
164         }
165
166         /* This function can handle multiple comma seperated
167          * lists as one, which is a feature no actual commands
168          * have yet -- this is futureproofing in case we encounter
169          * a command that  does.
170          */
171         *paramlist = 0;
172
173         for (int i = start; i <= end; i++)
174         {
175                 if (parameters[i])
176                 {
177                         strlcat(paramlist,parameters[i],MAXBUF);
178                 }
179         }
180
181         /* Now we split off paramlist into seperate parameters using
182          * pointer voodoo, this parameter list goes into sep_items
183          */
184         j = 0;
185         param = paramlist;
186
187         for (char* i = paramlist; *i; i++)
188         {
189                 /* Found an item */
190                 if (*i == ',')
191                 {
192                         *i = '\0';
193                         sep_items[j++] = param;
194                         /* Iterate along to next item, if there is one */
195                         param = i+1;
196                         if ((unsigned int)j > Config->MaxTargets)
197                         {
198                                 /* BZZT! Too many items */
199                                 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
200                                 return 1;
201                         }
202                 }
203         }
204         sep_items[j++] = param;
205         total = j;
206
207         /* We add this extra comma here just to ensure we get all the keys
208          * in the event the user gave a malformed key string (yes, you guessed
209          * it, this is a mirc-ism)
210          */
211         if ((joins) && (*keylist) && (total>0)) // more than one channel and is joining
212         {
213                 charlcat(keylist,',',MAXBUF);
214         }
215
216         /* If we're doing JOIN handling (e.g. we've had to kludge for two
217          * lists being handled at once, real smart by the way JRO </sarcasm>)
218          * and we also have keys, we must seperate out this key list into
219          * our char** list sep_keys for later use.
220          */
221         if ((joins) && (*keylist))
222         {
223                 /* There is more than one key */
224                 if (strchr(keylist,','))
225                 {
226                         j = 0;
227                         param = keylist;
228                         for (char* i = keylist; *i; i++)
229                         {
230                                 if (*i == ',')
231                                 {
232                                         *i = '\0';
233                                         sep_keys[j++] = param;
234                                         param = i+1;
235                                 }
236                         }
237
238                         sep_keys[j++] = param;
239                         total2 = j;
240                 }
241         }
242
243         /* Now the easier bit. We call the command class's Handle function
244          * X times, where x is the number of parameters we've 'collated'.
245          */
246         for (j = 0; j < total; j++)
247         {
248                 if (sep_items[j])
249                 {
250                         pars[0] = sep_items[j];
251                 }
252
253                 for (q = end; q < pcnt-1; q++)
254                 {
255                         if (parameters[q+1])
256                         {
257                                 pars[q-end+1] = parameters[q+1];
258                         }
259                 }
260
261                 if ((joins) && (parameters[1]))
262                 {
263                         if (pcnt > 1)
264                         {
265                                 pars[1] = sep_keys[j];
266                         }
267                         else
268                         {
269                                 pars[1] = NULL;
270                         }
271                 }
272
273                 /* repeatedly call the function with the hacked parameter list */
274                 if ((joins) && (pcnt > 1))
275                 {
276                         if (pars[1])
277                         {
278                                 /* pars[1] already set up and containing key from sep_keys[j] */
279                                 fn->Handle(pars,2,u);
280                         }
281                         else
282                         {
283                                 pars[1] = parameters[1];
284                                 fn->Handle(pars,2,u);
285                         }
286                 }
287                 else
288                 {
289                         fn->Handle(pars,pcnt-(end-start),u);
290                 }
291         }
292
293         return 1;
294 }
295
296 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
297 {
298         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
299
300         if (n != cmdlist.end())
301         {
302                 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
303                 {
304                         if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
305                         {
306                                 if (n->second->flags_needed)
307                                 {
308                                         return ((user->HasPermission(commandname)) || (is_uline(user->server)));
309                                 }
310                                 return true;
311                         }
312                 }
313         }
314         return false;
315 }
316
317 // calls a handler function for a command
318
319 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
320 {
321         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
322
323         if (n != cmdlist.end())
324         {
325                 if (pcnt >= n->second->min_params)
326                 {
327                         if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
328                         {
329                                 if (n->second->flags_needed)
330                                 {
331                                         if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
332                                         {
333                                                 n->second->Handle(parameters,pcnt,user);
334                                                 return true;
335                                         }
336                                 }
337                                 else
338                                 {
339                                         n->second->Handle(parameters,pcnt,user);
340                                         return true;
341                                 }
342                         }
343                 }
344         }
345         return false;
346 }
347
348 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
349 {
350         const char *command_p[127];
351         int items = 0;
352         std::string para[127];
353         irc::tokenstream tokens(cmd);
354         std::string command = tokens.GetToken();
355
356         while (((para[items] = tokens.GetToken()) != "") && (items < 127))
357                 command_p[items] = para[items++].c_str();
358
359         for (std::string::iterator makeupper = command.begin(); makeupper != command.end(); makeupper++)
360                 *makeupper = toupper(*makeupper);
361                 
362         int MOD_RESULT = 0;
363         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
364         if (MOD_RESULT == 1) {
365                 return;
366         }
367
368         nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
369         
370         if (cm != cmdlist.end())
371         {
372                 if (user)
373                 {
374                         /* activity resets the ping pending timer */
375                         user->nping = TIME + user->pingmax;
376                         if (items < cm->second->min_params)
377                         {
378                                 log(DEBUG,"not enough parameters: %s %s",user->nick,command.c_str());
379                                 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command.c_str());
380                                 return;
381                         }
382                         if (cm->second->flags_needed)
383                         {
384                                 if (!user->IsModeSet(cm->second->flags_needed))
385                                 {
386                                         log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
387                                         WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
388                                         return;
389                                 }
390                         }
391                         if ((cm->second->flags_needed) && (!user->HasPermission(command)))
392                         {
393                                 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
394                                 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
395                                 return;
396                         }
397                         if ((user->registered == 7) && (!*user->oper) && (cm->second->IsDisabled()))
398                         {
399                                 /* command is disabled! */
400                                 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command.c_str());
401                                 return;
402                         }
403                         if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
404                         {
405                                 /* ikky /stats counters */
406                                 cm->second->use_count++;
407                                 cm->second->total_bytes += cmd.length();
408
409                                 int MOD_RESULT = 0;
410                                 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
411                                 if (MOD_RESULT == 1)
412                                         return;
413
414                                 /*
415                                  * WARNING: nothing may come after the
416                                  * command handler call, as the handler
417                                  * may free the user structure!
418                                  */
419
420                                 cm->second->Handle(command_p,items,user);
421                                 return;
422                         }
423                         else
424                         {
425                                 WriteServ(user->fd,"451 %s :You have not registered",command.c_str());
426                                 return;
427                         }
428                 }
429         }
430         else if (user)
431         {
432                 ServerInstance->stats->statsUnknown++;
433                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command.c_str());
434         }
435 }
436
437 bool CommandParser::RemoveCommands(const char* source)
438 {
439         bool go_again = true;
440
441         while (go_again)
442         {
443                 go_again = false;
444
445                 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
446                 {
447                         command_t* x = i->second;
448                         if (x->source == std::string(source))
449                         {
450                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
451                                 cmdlist.erase(i);
452                                 go_again = true;
453                                 break;
454                         }
455                 }
456         }
457
458         return true;
459 }
460
461 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
462 {
463         std::string::size_type a;
464
465         if (!user)
466                 return;
467
468         while ((a = buffer.find("\n")) != std::string::npos)
469                 buffer.erase(a);
470         while ((a = buffer.find("\r")) != std::string::npos)
471                 buffer.erase(a);
472
473         log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
474
475         this->ProcessCommand(user,buffer);
476 }
477
478 bool CommandParser::CreateCommand(command_t *f)
479 {
480         /* create the command and push it onto the table */
481         if (cmdlist.find(f->command) == cmdlist.end())
482         {
483                 cmdlist[f->command] = f;
484                 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
485                 return true;
486         }
487         else return false;
488 }
489
490 CommandParser::CommandParser()
491 {
492         this->SetupCommandTable();
493 }
494
495 void CommandParser::SetupCommandTable()
496 {
497         /* These three are special (can occur without
498          * full user registration) and so are saved
499          * for later use.
500          */
501         command_user = new cmd_user;
502         command_nick = new cmd_nick;
503         command_pass = new cmd_pass;
504         this->CreateCommand(command_user);
505         this->CreateCommand(command_nick);
506         this->CreateCommand(command_pass);
507
508         /* The rest of these arent special. boo hoo.
509          */
510         this->CreateCommand(new cmd_quit);
511         this->CreateCommand(new cmd_version);
512         this->CreateCommand(new cmd_ping);
513         this->CreateCommand(new cmd_pong);
514         this->CreateCommand(new cmd_admin);
515         this->CreateCommand(new cmd_privmsg);
516         this->CreateCommand(new cmd_info);
517         this->CreateCommand(new cmd_time);
518         this->CreateCommand(new cmd_whois);
519         this->CreateCommand(new cmd_wallops);
520         this->CreateCommand(new cmd_notice);
521         this->CreateCommand(new cmd_join);
522         this->CreateCommand(new cmd_names);
523         this->CreateCommand(new cmd_part);
524         this->CreateCommand(new cmd_kick);
525         this->CreateCommand(new cmd_mode);
526         this->CreateCommand(new cmd_topic);
527         this->CreateCommand(new cmd_who);
528         this->CreateCommand(new cmd_motd);
529         this->CreateCommand(new cmd_rules);
530         this->CreateCommand(new cmd_oper);
531         this->CreateCommand(new cmd_list);
532         this->CreateCommand(new cmd_die);
533         this->CreateCommand(new cmd_restart);
534         this->CreateCommand(new cmd_kill);
535         this->CreateCommand(new cmd_rehash);
536         this->CreateCommand(new cmd_lusers);
537         this->CreateCommand(new cmd_stats);
538         this->CreateCommand(new cmd_userhost);
539         this->CreateCommand(new cmd_away);
540         this->CreateCommand(new cmd_ison);
541         this->CreateCommand(new cmd_summon);
542         this->CreateCommand(new cmd_users);
543         this->CreateCommand(new cmd_invite);
544         this->CreateCommand(new cmd_trace);
545         this->CreateCommand(new cmd_whowas);
546         this->CreateCommand(new cmd_connect);
547         this->CreateCommand(new cmd_squit);
548         this->CreateCommand(new cmd_modules);
549         this->CreateCommand(new cmd_links);
550         this->CreateCommand(new cmd_map);
551         this->CreateCommand(new cmd_kline);
552         this->CreateCommand(new cmd_gline);
553         this->CreateCommand(new cmd_zline);
554         this->CreateCommand(new cmd_qline);
555         this->CreateCommand(new cmd_eline);
556         this->CreateCommand(new cmd_loadmodule);
557         this->CreateCommand(new cmd_unloadmodule);
558         this->CreateCommand(new cmd_server);
559         this->CreateCommand(new cmd_commands);
560 }