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