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