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