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