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