]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
af96deaa8743b614f169336705a65f83c35fd366
[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 <sched.h>
30 #ifdef THREADED_DNS
31 #include <pthread.h>
32 #endif
33 #include "users.h"
34 #include "globals.h"
35 #include "modules.h"
36 #include "dynamic.h"
37 #include "wildcard.h"
38 #include "message.h"
39 #include "mode.h"
40 #include "commands.h"
41 #include "xline.h"
42 #include "inspstring.h"
43 #include "dnsqueue.h"
44 #include "helperfuncs.h"
45 #include "hashcomp.h"
46 #include "socketengine.h"
47 #include "userprocess.h"
48 #include "socket.h"
49 #include "dns.h"
50 #include "typedefs.h"
51 #include "command_parse.h"
52 #include "ctables.h"
53
54 #define nspace __gnu_cxx
55
56 extern InspIRCd* ServerInstance;
57
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<InspSocket*> module_sockets;
61 extern std::vector<userrec*> local_users;
62
63 extern int MODCOUNT;
64 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
65 extern time_t TIME;
66
67 // This table references users by file descriptor.
68 // its an array to make it VERY fast, as all lookups are referenced
69 // by an integer, meaning there is no need for a scan/search operation.
70 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
71
72 extern Server* MyServer;
73 extern ServerConfig *Config;
74
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
77
78 /* Special commands which may occur without registration of the user */
79 cmd_user* command_user;
80 cmd_nick* command_nick;
81 cmd_pass* command_pass;
82
83 /* This function pokes and hacks at a parameter list like the following:
84  *
85  * PART #winbot,#darkgalaxy :m00!
86  *
87  * to turn it into a series of individual calls like this:
88  *
89  * PART #winbot :m00!
90  * PART #darkgalaxy :m00!
91  *
92  * The seperate calls are sent to a callback function provided by the caller
93  * (the caller will usually call itself recursively). The callback function
94  * must be a command handler. Calling this function on a line with no list causes
95  * no action to be taken. You must provide a starting and ending parameter number
96  * where the range of the list can be found, useful if you have a terminating
97  * parameter as above which is actually not part of the list, or parameters
98  * before the actual list as well. This code is used by many functions which
99  * can function as "one to list" (see the RFC) */
100
101 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
102 {
103         /*if ((unsigned int)j > Config->MaxTargets)
104         {
105                 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,sep_items[j-1]);
106                 return 1;
107         }
108         fn->Handle(pars,pcnt-(end-start),u);
109         return 1;*/
110         if (!strchr(parameters[splithere],','))
111                 return 0;
112
113         irc::commasepstream items1(parameters[splithere]);
114         irc::commasepstream items2(parameters[extra]);
115         std::string item = "";
116
117         while ((item = items1.GetToken()) != "")
118         {
119                 std::string extrastuff = items2.GetToken();
120                 parameters[splithere] = item.c_str();
121                 parameters[extra] = extrastuff.c_str();
122                 CommandObj->Handle(parameters,pcnt,user);
123         }
124         return 1;
125 }
126
127 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
128 {
129         if (!strchr(parameters[splithere],','))
130                 return 0;
131
132         irc::commasepstream items1(parameters[splithere]);
133         std::string item = "";
134
135         while ((item = items1.GetToken()) != "")
136         {
137                 parameters[splithere] = item.c_str();
138                 CommandObj->Handle(parameters,pcnt,user);
139         }
140         return 1;
141 }
142
143 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
144 {
145         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
146
147         if (n != cmdlist.end())
148         {
149                 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
150                 {
151                         if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
152                         {
153                                 if (n->second->flags_needed)
154                                 {
155                                         return ((user->HasPermission(commandname)) || (is_uline(user->server)));
156                                 }
157                                 return true;
158                         }
159                 }
160         }
161         return false;
162 }
163
164 // calls a handler function for a command
165
166 bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
167 {
168         nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
169
170         if (n != cmdlist.end())
171         {
172                 if (pcnt >= n->second->min_params)
173                 {
174                         if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
175                         {
176                                 if (n->second->flags_needed)
177                                 {
178                                         if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
179                                         {
180                                                 n->second->Handle(parameters,pcnt,user);
181                                                 return true;
182                                         }
183                                 }
184                                 else
185                                 {
186                                         n->second->Handle(parameters,pcnt,user);
187                                         return true;
188                                 }
189                         }
190                 }
191         }
192         return false;
193 }
194
195 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
196 {
197         const char *command_p[127];
198         int items = 0;
199         std::string para[127];
200         irc::tokenstream tokens(cmd);
201         std::string command = tokens.GetToken();
202
203         while (((para[items] = tokens.GetToken()) != "") && (items < 127))
204                 command_p[items] = para[items++].c_str();
205
206         for (std::string::iterator makeupper = command.begin(); makeupper != command.end(); makeupper++)
207                 *makeupper = toupper(*makeupper);
208                 
209         int MOD_RESULT = 0;
210         FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
211         if (MOD_RESULT == 1) {
212                 return;
213         }
214
215         nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
216         
217         if (cm != cmdlist.end())
218         {
219                 if (user)
220                 {
221                         /* activity resets the ping pending timer */
222                         user->nping = TIME + user->pingmax;
223                         if (items < cm->second->min_params)
224                         {
225                                 log(DEBUG,"not enough parameters: %s %s",user->nick,command.c_str());
226                                 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command.c_str());
227                                 return;
228                         }
229                         if (cm->second->flags_needed)
230                         {
231                                 if (!user->IsModeSet(cm->second->flags_needed))
232                                 {
233                                         log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
234                                         WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
235                                         return;
236                                 }
237                         }
238                         if ((cm->second->flags_needed) && (!user->HasPermission(command)))
239                         {
240                                 log(DEBUG,"permission denied: %s %s",user->nick,command.c_str());
241                                 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
242                                 return;
243                         }
244                         if ((user->registered == 7) && (!*user->oper) && (cm->second->IsDisabled()))
245                         {
246                                 /* command is disabled! */
247                                 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command.c_str());
248                                 return;
249                         }
250                         if ((user->registered == 7) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
251                         {
252                                 /* ikky /stats counters */
253                                 cm->second->use_count++;
254                                 cm->second->total_bytes += cmd.length();
255
256                                 int MOD_RESULT = 0;
257                                 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
258                                 if (MOD_RESULT == 1)
259                                         return;
260
261                                 /*
262                                  * WARNING: nothing may come after the
263                                  * command handler call, as the handler
264                                  * may free the user structure!
265                                  */
266
267                                 cm->second->Handle(command_p,items,user);
268                                 return;
269                         }
270                         else
271                         {
272                                 WriteServ(user->fd,"451 %s :You have not registered",command.c_str());
273                                 return;
274                         }
275                 }
276         }
277         else if (user)
278         {
279                 ServerInstance->stats->statsUnknown++;
280                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command.c_str());
281         }
282 }
283
284 bool CommandParser::RemoveCommands(const char* source)
285 {
286         bool go_again = true;
287
288         while (go_again)
289         {
290                 go_again = false;
291
292                 for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
293                 {
294                         command_t* x = i->second;
295                         if (x->source == std::string(source))
296                         {
297                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
298                                 cmdlist.erase(i);
299                                 go_again = true;
300                                 break;
301                         }
302                 }
303         }
304
305         return true;
306 }
307
308 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
309 {
310         std::string::size_type a;
311
312         if (!user)
313                 return;
314
315         while ((a = buffer.find("\n")) != std::string::npos)
316                 buffer.erase(a);
317         while ((a = buffer.find("\r")) != std::string::npos)
318                 buffer.erase(a);
319
320         log(DEBUG,"CMDIN: %s %s",user->nick,buffer.c_str());
321
322         this->ProcessCommand(user,buffer);
323 }
324
325 bool CommandParser::CreateCommand(command_t *f)
326 {
327         /* create the command and push it onto the table */
328         if (cmdlist.find(f->command) == cmdlist.end())
329         {
330                 cmdlist[f->command] = f;
331                 log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
332                 return true;
333         }
334         else return false;
335 }
336
337 CommandParser::CommandParser()
338 {
339         this->SetupCommandTable();
340 }
341
342 void CommandParser::SetupCommandTable()
343 {
344         /* These three are special (can occur without
345          * full user registration) and so are saved
346          * for later use.
347          */
348         command_user = new cmd_user;
349         command_nick = new cmd_nick;
350         command_pass = new cmd_pass;
351         this->CreateCommand(command_user);
352         this->CreateCommand(command_nick);
353         this->CreateCommand(command_pass);
354
355         /* The rest of these arent special. boo hoo.
356          */
357         this->CreateCommand(new cmd_quit);
358         this->CreateCommand(new cmd_version);
359         this->CreateCommand(new cmd_ping);
360         this->CreateCommand(new cmd_pong);
361         this->CreateCommand(new cmd_admin);
362         this->CreateCommand(new cmd_privmsg);
363         this->CreateCommand(new cmd_info);
364         this->CreateCommand(new cmd_time);
365         this->CreateCommand(new cmd_whois);
366         this->CreateCommand(new cmd_wallops);
367         this->CreateCommand(new cmd_notice);
368         this->CreateCommand(new cmd_join);
369         this->CreateCommand(new cmd_names);
370         this->CreateCommand(new cmd_part);
371         this->CreateCommand(new cmd_kick);
372         this->CreateCommand(new cmd_mode);
373         this->CreateCommand(new cmd_topic);
374         this->CreateCommand(new cmd_who);
375         this->CreateCommand(new cmd_motd);
376         this->CreateCommand(new cmd_rules);
377         this->CreateCommand(new cmd_oper);
378         this->CreateCommand(new cmd_list);
379         this->CreateCommand(new cmd_die);
380         this->CreateCommand(new cmd_restart);
381         this->CreateCommand(new cmd_kill);
382         this->CreateCommand(new cmd_rehash);
383         this->CreateCommand(new cmd_lusers);
384         this->CreateCommand(new cmd_stats);
385         this->CreateCommand(new cmd_userhost);
386         this->CreateCommand(new cmd_away);
387         this->CreateCommand(new cmd_ison);
388         this->CreateCommand(new cmd_summon);
389         this->CreateCommand(new cmd_users);
390         this->CreateCommand(new cmd_invite);
391         this->CreateCommand(new cmd_trace);
392         this->CreateCommand(new cmd_whowas);
393         this->CreateCommand(new cmd_connect);
394         this->CreateCommand(new cmd_squit);
395         this->CreateCommand(new cmd_modules);
396         this->CreateCommand(new cmd_links);
397         this->CreateCommand(new cmd_map);
398         this->CreateCommand(new cmd_kline);
399         this->CreateCommand(new cmd_gline);
400         this->CreateCommand(new cmd_zline);
401         this->CreateCommand(new cmd_qline);
402         this->CreateCommand(new cmd_eline);
403         this->CreateCommand(new cmd_loadmodule);
404         this->CreateCommand(new cmd_unloadmodule);
405         this->CreateCommand(new cmd_server);
406         this->CreateCommand(new cmd_commands);
407 }