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