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