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