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