]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
Re-order stuff so that users cant get syntax for commands they dont have access to
[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
84 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
85  * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
86  * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
87  * the channel names and their keys as follows:
88  * JOIN #chan1,#chan2,#chan3 key1,,key3
89  * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
90  * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
91  * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
92  * Both will only parse until they reach Config->MaxTargets number of targets, to stop abuse via spam.
93  */
94 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
95 {
96         /* First check if we have more than one item in the list, if we don't we return zero here and the handler
97          * which called us just carries on as it was.
98          */
99         if (!strchr(parameters[splithere],','))
100                 return 0;
101
102         /* Create two lists, one for channel names, one for keys
103          */
104         irc::commasepstream items1(parameters[splithere]);
105         irc::commasepstream items2(parameters[extra]);
106         std::string item = "";
107         unsigned int max = 0;
108
109         /* Attempt to iterate these lists and call the command objech
110          * which called us, for every parameter pair until there are
111          * no more left to parse.
112          */
113         while (((item = items1.GetToken()) != "") && (max++ < Config->MaxTargets))
114         {
115                 std::string extrastuff = items2.GetToken();
116                 parameters[splithere] = item.c_str();
117                 parameters[extra] = extrastuff.c_str();
118                 CommandObj->Handle(parameters,pcnt,user);
119         }
120         return 1;
121 }
122
123 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
124 {
125         /* First check if we have more than one item in the list, if we don't we return zero here and the handler
126          * which called us just carries on as it was.
127          */
128         if (!strchr(parameters[splithere],','))
129                 return 0;
130
131         /* Only one commasepstream here */
132         irc::commasepstream items1(parameters[splithere]);
133         std::string item = "";
134         unsigned int max = 0;
135
136         /* Parse the commasepstream until there are no tokens remaining.
137          * Each token we parse out, call the command handler that called us
138          * with it
139          */
140         while (((item = items1.GetToken()) != "") && (max++ < Config->MaxTargets))
141         {
142                 parameters[splithere] = item.c_str();
143                 CommandObj->Handle(parameters,pcnt,user);
144         }
145         /* By returning 1 we tell our caller that nothing is to be done,
146          * as all the previous calls handled the data. This makes the parent
147          * return without doing any processing.
148          */
149         return 1;
150 }
151
152 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
153 {
154         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
155
156         if (n != cmdlist.end())
157         {
158                 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
159                 {
160                         if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
161                         {
162                                 if (n->second->flags_needed)
163                                 {
164                                         return ((user->HasPermission(commandname)) || (is_uline(user->server)));
165                                 }
166                                 return true;
167                         }
168                 }
169         }
170         return false;
171 }
172
173 // calls a handler function for a command
174
175 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
176 {
177         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
178
179         if (n != cmdlist.end())
180         {
181                 if (pcnt >= n->second->min_params)
182                 {
183                         if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
184                         {
185                                 if (n->second->flags_needed)
186                                 {
187                                         if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
188                                         {
189                                                 n->second->Handle(parameters,pcnt,user);
190                                                 return true;
191                                         }
192                                 }
193                                 else
194                                 {
195                                         n->second->Handle(parameters,pcnt,user);
196                                         return true;
197                                 }
198                         }
199                 }
200         }
201         return false;
202 }
203
204 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
205 {
206         const char *command_p[127];
207         int items = 0;
208         std::string para[127];
209         irc::tokenstream tokens(cmd);
210         std::string command = tokens.GetToken();
211
212         while (((para[items] = tokens.GetToken()) != "") && (items < 127))
213                 command_p[items] = para[items++].c_str();
214
215         for (std::string::iterator makeupper = command.begin(); makeupper != command.end(); makeupper++)
216                 *makeupper = toupper(*makeupper);
217                 
218         int MOD_RESULT = 0;
219         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
220         if (MOD_RESULT == 1) {
221                 return;
222         }
223
224         nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
225         
226         if (cm != cmdlist.end())
227         {
228                 if (user)
229                 {
230                         /* activity resets the ping pending timer */
231                         user->nping = TIME + user->pingmax;
232                         if (cm->second->flags_needed)
233                         {
234                                 if (!user->IsModeSet(cm->second->flags_needed))
235                                 {
236                                         WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
237                                         return;
238                                 }
239                         }
240                         if ((cm->second->flags_needed) && (!user->HasPermission(command)))
241                         {
242                                 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
243                                 return;
244                         }
245                         if ((user->registered == 7) && (!*user->oper) && (cm->second->IsDisabled()))
246                         {
247                                 /* command is disabled! */
248                                 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command.c_str());
249                                 return;
250                         }
251                         if (items < cm->second->min_params)
252                         {
253                                 /* If syntax is given, display this as the 461 reply */
254                                 if (cm->second->syntax.length())
255                                         WriteServ(user->fd,"461 %s %s :Syntax: %s %s", user->nick, command.c_str(), cm->second->command.c_str(), cm->second->syntax.c_str());
256                                 else
257                                         WriteServ(user->fd,"461 %s %s :Not enough parameters", user->nick, command.c_str());
258                                 return;
259                         }
260                         if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
261                         {
262                                 /* ikky /stats counters */
263                                 cm->second->use_count++;
264                                 cm->second->total_bytes += cmd.length();
265
266                                 int MOD_RESULT = 0;
267                                 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
268                                 if (MOD_RESULT == 1)
269                                         return;
270
271                                 /*
272                                  * WARNING: nothing may come after the
273                                  * command handler call, as the handler
274                                  * may free the user structure!
275                                  */
276
277                                 cm->second->Handle(command_p,items,user);
278                                 return;
279                         }
280                         else
281                         {
282                                 WriteServ(user->fd,"451 %s :You have not registered",command.c_str());
283                                 return;
284                         }
285                 }
286         }
287         else if (user)
288         {
289                 ServerInstance->stats->statsUnknown++;
290                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command.c_str());
291         }
292 }
293
294 bool CommandParser::RemoveCommands(const char* source)
295 {
296         bool go_again = true;
297
298         while (go_again)
299         {
300                 go_again = false;
301
302                 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
303                 {
304                         command_t* x = i->second;
305                         if (x->source == std::string(source))
306                         {
307                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
308                                 cmdlist.erase(i);
309                                 go_again = true;
310                                 break;
311                         }
312                 }
313         }
314
315         return true;
316 }
317
318 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
319 {
320         std::string::size_type a;
321
322         if (!user)
323                 return;
324
325         while ((a = buffer.find("\n")) != std::string::npos)
326                 buffer.erase(a);
327         while ((a = buffer.find("\r")) != std::string::npos)
328                 buffer.erase(a);
329
330         if (buffer.length())
331         {
332                 log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
333                 this->ProcessCommand(user,buffer);
334         }
335 }
336
337 bool CommandParser::CreateCommand(command_t *f)
338 {
339         /* create the command and push it onto the table */
340         if (cmdlist.find(f->command) == cmdlist.end())
341         {
342                 cmdlist[f->command] = f;
343                 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
344                 return true;
345         }
346         else return false;
347 }
348
349 CommandParser::CommandParser()
350 {
351         this->SetupCommandTable();
352 }
353
354 void CommandParser::SetupCommandTable()
355 {
356         /* These three are special (can occur without
357          * full user registration) and so are saved
358          * for later use.
359          */
360         command_user = new cmd_user;
361         command_nick = new cmd_nick;
362         command_pass = new cmd_pass;
363         this->CreateCommand(command_user);
364         this->CreateCommand(command_nick);
365         this->CreateCommand(command_pass);
366
367         /* The rest of these arent special. boo hoo.
368          */
369         this->CreateCommand(new cmd_quit);
370         this->CreateCommand(new cmd_version);
371         this->CreateCommand(new cmd_ping);
372         this->CreateCommand(new cmd_pong);
373         this->CreateCommand(new cmd_admin);
374         this->CreateCommand(new cmd_privmsg);
375         this->CreateCommand(new cmd_info);
376         this->CreateCommand(new cmd_time);
377         this->CreateCommand(new cmd_whois);
378         this->CreateCommand(new cmd_wallops);
379         this->CreateCommand(new cmd_notice);
380         this->CreateCommand(new cmd_join);
381         this->CreateCommand(new cmd_names);
382         this->CreateCommand(new cmd_part);
383         this->CreateCommand(new cmd_kick);
384         this->CreateCommand(new cmd_mode);
385         this->CreateCommand(new cmd_topic);
386         this->CreateCommand(new cmd_who);
387         this->CreateCommand(new cmd_motd);
388         this->CreateCommand(new cmd_rules);
389         this->CreateCommand(new cmd_oper);
390         this->CreateCommand(new cmd_list);
391         this->CreateCommand(new cmd_die);
392         this->CreateCommand(new cmd_restart);
393         this->CreateCommand(new cmd_kill);
394         this->CreateCommand(new cmd_rehash);
395         this->CreateCommand(new cmd_lusers);
396         this->CreateCommand(new cmd_stats);
397         this->CreateCommand(new cmd_userhost);
398         this->CreateCommand(new cmd_away);
399         this->CreateCommand(new cmd_ison);
400         this->CreateCommand(new cmd_summon);
401         this->CreateCommand(new cmd_users);
402         this->CreateCommand(new cmd_invite);
403         this->CreateCommand(new cmd_trace);
404         this->CreateCommand(new cmd_whowas);
405         this->CreateCommand(new cmd_connect);
406         this->CreateCommand(new cmd_squit);
407         this->CreateCommand(new cmd_modules);
408         this->CreateCommand(new cmd_links);
409         this->CreateCommand(new cmd_map);
410         this->CreateCommand(new cmd_kline);
411         this->CreateCommand(new cmd_gline);
412         this->CreateCommand(new cmd_zline);
413         this->CreateCommand(new cmd_qline);
414         this->CreateCommand(new cmd_eline);
415         this->CreateCommand(new cmd_loadmodule);
416         this->CreateCommand(new cmd_unloadmodule);
417         this->CreateCommand(new cmd_server);
418         this->CreateCommand(new cmd_commands);
419 }