1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "configreader.h"
23 #include "socketengine.h"
25 #include "command_parse.h"
27 bool InspIRCd::ULine(const char* server)
34 return (Config->ulines.find(server) != Config->ulines.end());
37 bool InspIRCd::SilentULine(const char* server)
39 std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
40 if (n != Config->ulines.end())
45 int InspIRCd::OperPassCompare(const char* data,const char* input, int tagnumber)
48 FOREACH_RESULT_I(this,I_OnOperCompare,OnOperCompare(data, input, tagnumber))
53 return strcmp(data,input);
56 long InspIRCd::Duration(const char* str)
62 if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
66 return Duration(n.c_str());
69 for (char* i = (char*)str; *i; i++)
71 // if we have digits, build up a string for the value in n_field,
72 // up to 10 digits in size.
73 if ((*i >= '0') && (*i <= '9'))
75 strlcat(n_field,i,10);
79 // we dont have a digit, check for numeric tokens
83 total += atoi(n_field);
87 total += (atoi(n_field)*duration_m);
91 total += (atoi(n_field)*duration_h);
95 total += (atoi(n_field)*duration_d);
99 total += (atoi(n_field)*duration_w);
103 total += (atoi(n_field)*duration_y);
109 // add trailing seconds
110 total += atoi(n_field);
115 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
116 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
117 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
118 * the channel names and their keys as follows:
119 * JOIN #chan1,#chan2,#chan3 key1,,key3
120 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
121 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
122 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
123 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
125 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
127 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
128 * which called us just carries on as it was.
130 if (!strchr(parameters[splithere],','))
133 /** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
134 * By using std::map (thanks for the idea w00t) we can cut this down a ton.
137 std::map<irc::string, bool> dupes;
139 /* Create two lists, one for channel names, one for keys
141 irc::commasepstream items1(parameters[splithere]);
142 irc::commasepstream items2(parameters[extra]);
143 std::string item = "*";
144 unsigned int max = 0;
146 /* Attempt to iterate these lists and call the command objech
147 * which called us, for every parameter pair until there are
148 * no more left to parse.
150 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
152 if (dupes.find(item.c_str()) == dupes.end())
154 const char* new_parameters[127];
156 for (int t = 0; (t < pcnt) && (t < 127); t++)
157 new_parameters[t] = parameters[t];
159 std::string extrastuff = items2.GetToken();
161 new_parameters[splithere] = item.c_str();
162 new_parameters[extra] = extrastuff.c_str();
164 CommandObj->Handle(new_parameters,pcnt,user);
166 dupes[item.c_str()] = true;
172 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
174 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
175 * which called us just carries on as it was.
177 if (!strchr(parameters[splithere],','))
180 std::map<irc::string, bool> dupes;
182 /* Only one commasepstream here */
183 irc::commasepstream items1(parameters[splithere]);
184 std::string item = "*";
185 unsigned int max = 0;
187 /* Parse the commasepstream until there are no tokens remaining.
188 * Each token we parse out, call the command handler that called us
191 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
193 if (dupes.find(item.c_str()) == dupes.end())
195 const char* new_parameters[127];
197 for (int t = 0; (t < pcnt) && (t < 127); t++)
198 new_parameters[t] = parameters[t];
200 new_parameters[splithere] = item.c_str();
202 parameters[splithere] = item.c_str();
204 /* Execute the command handler over and over. If someone pulls our user
205 * record out from under us (e.g. if we /kill a comma sep list, and we're
206 * in that list ourselves) abort if we're gone.
208 CommandObj->Handle(new_parameters,pcnt,user);
210 dupes[item.c_str()] = true;
213 /* By returning 1 we tell our caller that nothing is to be done,
214 * as all the previous calls handled the data. This makes the parent
215 * return without doing any processing.
220 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
222 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
224 if (n != cmdlist.end())
226 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
228 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
230 if (n->second->flags_needed)
232 return ((user->HasPermission(commandname)) || (ServerInstance->ULine(user->server)));
241 command_t* CommandParser::GetHandler(const std::string &commandname)
243 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
244 if (n != cmdlist.end())
250 // calls a handler function for a command
252 CmdResult CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
254 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
256 if (n != cmdlist.end())
258 if (pcnt >= n->second->min_params)
260 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
262 if (n->second->flags_needed)
264 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
266 return n->second->Handle(parameters,pcnt,user);
271 return n->second->Handle(parameters,pcnt,user);
279 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
281 const char *command_p[127];
283 irc::tokenstream tokens(cmd);
285 tokens.GetToken(command);
287 while (tokens.GetToken(para[items]) && (items < 127))
289 command_p[items] = para[items].c_str();
293 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
296 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false,cmd));
297 if (MOD_RESULT == 1) {
301 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
303 if (cm != cmdlist.end())
307 /* activity resets the ping pending timer */
308 user->nping = ServerInstance->Time() + user->pingmax;
309 if (cm->second->flags_needed)
311 if (!user->IsModeSet(cm->second->flags_needed))
313 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privileges",user->nick);
316 if (!user->HasPermission(command))
318 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
322 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
324 /* command is disabled! */
325 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
328 if (items < cm->second->min_params)
330 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
331 /* If syntax is given, display this as the 461 reply */
332 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
333 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
336 if ((user->registered == REG_ALL) || (cm->second->WorksBeforeReg()))
338 /* ikky /stats counters */
339 cm->second->use_count++;
340 cm->second->total_bytes += cmd.length();
343 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true,cmd));
348 * WARNING: nothing may come after the
349 * command handler call, as the handler
350 * may free the user structure!
352 CmdResult result = cm->second->Handle(command_p,items,user);
354 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, items, user, result,cmd));
359 user->WriteServ("451 %s :You have not registered",command.c_str());
366 ServerInstance->stats->statsUnknown++;
367 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
371 bool CommandParser::RemoveCommands(const char* source)
373 nspace::hash_map<std::string,command_t*>::iterator i,safei;
374 for (i = cmdlist.begin(); i != cmdlist.end(); i++)
378 if (safei != cmdlist.end())
380 RemoveCommand(safei, source);
383 safei = cmdlist.begin();
384 if (safei != cmdlist.end())
386 RemoveCommand(safei, source);
391 void CommandParser::RemoveCommand(nspace::hash_map<std::string,command_t*>::iterator safei, const char* source)
393 command_t* x = safei->second;
394 if (x->source == std::string(source))
396 cmdlist.erase(safei);
400 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
402 std::string::size_type a;
407 while ((a = buffer.rfind("\n")) != std::string::npos)
409 while ((a = buffer.rfind("\r")) != std::string::npos)
416 ServerInstance->Log(DEBUG,"-> :%s %s",user->nick,buffer.c_str());
417 this->ProcessCommand(user,buffer);
422 bool CommandParser::CreateCommand(command_t *f, void* so_handle)
426 if (RFCCommands.find(f->command) == RFCCommands.end())
427 RFCCommands[f->command] = so_handle;
430 ServerInstance->Log(DEFAULT,"ERK! Somehow, we loaded a cmd_*.so file twice! Only the first instance is being recorded.");
435 /* create the command and push it onto the table */
436 if (cmdlist.find(f->command) == cmdlist.end())
438 cmdlist[f->command] = f;
444 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
447 this->SetupCommandTable();
450 bool CommandParser::FindSym(void** v, void* h)
452 *v = dlsym(h, "init_command");
453 const char* err = dlerror();
456 ServerInstance->Log(SPARSE, "Error loading core command: %s\n", err);
462 bool CommandParser::ReloadCommand(const char* cmd)
464 char filename[MAXBUF];
465 char commandname[MAXBUF];
468 for (const char* x = cmd; *x; x++, y++)
469 commandname[y] = toupper(*x);
473 SharedObjectList::iterator command = RFCCommands.find(commandname);
475 if (command != RFCCommands.end())
477 command_t* cmdptr = cmdlist.find(commandname)->second;
478 cmdlist.erase(cmdlist.find(commandname));
480 for (char* x = commandname; *x; x++)
485 dlclose(command->second);
486 RFCCommands.erase(command);
488 snprintf(filename, MAXBUF, "cmd_%s.so", commandname);
489 this->LoadCommand(filename);
497 CmdResult cmd_reload::Handle(const char** parameters, int pcnt, userrec *user)
499 user->WriteServ("NOTICE %s :*** Reloading command '%s'",user->nick, parameters[0]);
500 if (ServerInstance->Parser->ReloadCommand(parameters[0]))
502 user->WriteServ("NOTICE %s :*** Successfully reloaded command '%s'", user->nick, parameters[0]);
503 ServerInstance->WriteOpers("*** RELOAD: %s reloaded the '%s' command.", user->nick, parameters[0]);
508 user->WriteServ("NOTICE %s :*** Could not reload command '%s'", user->nick, parameters[0]);
513 void CommandParser::LoadCommand(const char* name)
515 char filename[MAXBUF];
517 command_t* (*cmd_factory_func)(InspIRCd*);
519 snprintf(filename, MAXBUF, "%s/%s", LIBRARYDIR, name);
520 h = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
524 ServerInstance->Log(SPARSE, "Error loading core command: %s", dlerror());
528 if (this->FindSym((void **)&cmd_factory_func, h))
530 command_t* newcommand = cmd_factory_func(ServerInstance);
531 this->CreateCommand(newcommand, h);
535 void CommandParser::SetupCommandTable()
539 DIR* library = opendir(LIBRARYDIR);
542 dirent* entry = NULL;
543 while ((entry = readdir(library)))
545 if (match(entry->d_name, "cmd_*.so"))
547 this->LoadCommand(entry->d_name);
553 this->CreateCommand(new cmd_reload(ServerInstance));