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 (find(Config->ulines.begin(),Config->ulines.end(),server) != Config->ulines.end());
37 int InspIRCd::OperPassCompare(const char* data,const char* input, int tagnumber)
40 FOREACH_RESULT_I(this,I_OnOperCompare,OnOperCompare(data, input, tagnumber))
45 return strcmp(data,input);
48 long InspIRCd::Duration(const char* str)
54 if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
58 return Duration(n.c_str());
61 for (char* i = (char*)str; *i; i++)
63 // if we have digits, build up a string for the value in n_field,
64 // up to 10 digits in size.
65 if ((*i >= '0') && (*i <= '9'))
67 strlcat(n_field,i,10);
71 // we dont have a digit, check for numeric tokens
75 total += atoi(n_field);
79 total += (atoi(n_field)*duration_m);
83 total += (atoi(n_field)*duration_h);
87 total += (atoi(n_field)*duration_d);
91 total += (atoi(n_field)*duration_w);
95 total += (atoi(n_field)*duration_y);
101 // add trailing seconds
102 total += atoi(n_field);
107 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
108 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
109 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
110 * the channel names and their keys as follows:
111 * JOIN #chan1,#chan2,#chan3 key1,,key3
112 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
113 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
114 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
115 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
117 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra)
119 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
120 * which called us just carries on as it was.
122 if (!strchr(parameters[splithere],','))
125 /** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
126 * By using std::map (thanks for the idea w00t) we can cut this down a ton.
129 std::map<irc::string, bool> dupes;
131 /* Create two lists, one for channel names, one for keys
133 irc::commasepstream items1(parameters[splithere]);
134 irc::commasepstream items2(parameters[extra]);
135 std::string item = "*";
136 unsigned int max = 0;
138 /* Attempt to iterate these lists and call the command objech
139 * which called us, for every parameter pair until there are
140 * no more left to parse.
142 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
144 if (dupes.find(item.c_str()) == dupes.end())
146 const char* new_parameters[127];
148 for (int t = 0; (t < pcnt) && (t < 127); t++)
149 new_parameters[t] = parameters[t];
151 std::string extrastuff = items2.GetToken();
153 new_parameters[splithere] = item.c_str();
154 new_parameters[extra] = extrastuff.c_str();
156 if (CommandObj->Handle(new_parameters,pcnt,user) == CMD_USER_DELETED)
159 dupes[item.c_str()] = true;
165 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
167 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
168 * which called us just carries on as it was.
170 if (!strchr(parameters[splithere],','))
173 std::map<irc::string, bool> dupes;
175 /* Only one commasepstream here */
176 irc::commasepstream items1(parameters[splithere]);
177 std::string item = "*";
178 unsigned int max = 0;
180 /* Parse the commasepstream until there are no tokens remaining.
181 * Each token we parse out, call the command handler that called us
184 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
186 if (dupes.find(item.c_str()) == dupes.end())
188 const char* new_parameters[127];
190 for (int t = 0; (t < pcnt) && (t < 127); t++)
191 new_parameters[t] = parameters[t];
193 new_parameters[splithere] = item.c_str();
195 parameters[splithere] = item.c_str();
197 /* Execute the command handler over and over. If someone pulls our user
198 * record out from under us (e.g. if we /kill a comma sep list, and we're
199 * in that list ourselves) abort if we're gone.
201 if (CommandObj->Handle(new_parameters,pcnt,user) == CMD_USER_DELETED)
204 dupes[item.c_str()] = true;
207 /* By returning 1 we tell our caller that nothing is to be done,
208 * as all the previous calls handled the data. This makes the parent
209 * return without doing any processing.
214 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
216 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
218 if (n != cmdlist.end())
220 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
222 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
224 if (n->second->flags_needed)
226 return ((user->HasPermission(commandname)) || (ServerInstance->ULine(user->server)));
235 command_t* CommandParser::GetHandler(const std::string &commandname)
237 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
238 if (n != cmdlist.end())
244 // calls a handler function for a command
246 CmdResult CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
248 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
250 if (n != cmdlist.end())
252 if (pcnt >= n->second->min_params)
254 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
256 if (n->second->flags_needed)
258 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
260 return n->second->Handle(parameters,pcnt,user);
265 return n->second->Handle(parameters,pcnt,user);
273 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
275 const char *command_p[127];
277 irc::tokenstream tokens(cmd);
278 std::string command = tokens.GetToken();
280 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
282 command_p[items] = para[items].c_str();
286 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
289 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false,cmd));
290 if (MOD_RESULT == 1) {
294 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
296 if (cm != cmdlist.end())
300 /* activity resets the ping pending timer */
301 user->nping = ServerInstance->Time() + user->pingmax;
302 if (cm->second->flags_needed)
304 if (!user->IsModeSet(cm->second->flags_needed))
306 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privileges",user->nick);
309 if (!user->HasPermission(command))
311 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
315 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
317 /* command is disabled! */
318 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
321 if (items < cm->second->min_params)
323 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
324 /* If syntax is given, display this as the 461 reply */
325 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
326 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
329 if ((user->registered == REG_ALL) || (cm->second->WorksBeforeReg()))
331 /* ikky /stats counters */
332 cm->second->use_count++;
333 cm->second->total_bytes += cmd.length();
336 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true,cmd));
341 * WARNING: nothing may come after the
342 * command handler call, as the handler
343 * may free the user structure!
345 CmdResult result = cm->second->Handle(command_p,items,user);
347 if (result != CMD_USER_DELETED)
349 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, items, user, result,cmd));
355 user->WriteServ("451 %s :You have not registered",command.c_str());
362 ServerInstance->stats->statsUnknown++;
363 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
367 bool CommandParser::RemoveCommands(const char* source)
369 nspace::hash_map<std::string,command_t*>::iterator i,safei;
370 for (i = cmdlist.begin(); i != cmdlist.end(); i++)
374 if (safei != cmdlist.end())
376 RemoveCommand(safei, source);
379 safei = cmdlist.begin();
380 if (safei != cmdlist.end())
382 RemoveCommand(safei, source);
387 void CommandParser::RemoveCommand(nspace::hash_map<std::string,command_t*>::iterator safei, const char* source)
389 command_t* x = safei->second;
390 if (x->source == std::string(source))
392 cmdlist.erase(safei);
396 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
398 std::string::size_type a;
403 while ((a = buffer.rfind("\n")) != std::string::npos)
405 while ((a = buffer.rfind("\r")) != std::string::npos)
410 ServerInstance->Log(DEBUG,"-> :%s %s",user->nick,buffer.c_str());
411 this->ProcessCommand(user,buffer);
415 bool CommandParser::CreateCommand(command_t *f, void* so_handle)
419 if (RFCCommands.find(f->command) == RFCCommands.end())
420 RFCCommands[f->command] = so_handle;
423 ServerInstance->Log(DEFAULT,"ERK! Somehow, we loaded a cmd_*.so file twice! Only the first instance is being recorded.");
428 /* create the command and push it onto the table */
429 if (cmdlist.find(f->command) == cmdlist.end())
431 cmdlist[f->command] = f;
437 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
440 this->SetupCommandTable();
443 bool CommandParser::FindSym(void** v, void* h)
445 *v = dlsym(h, "init_command");
446 const char* err = dlerror();
449 ServerInstance->Log(SPARSE, "Error loading core command: %s\n", err);
455 bool CommandParser::ReloadCommand(const char* cmd)
457 char filename[MAXBUF];
458 char commandname[MAXBUF];
461 for (const char* x = cmd; *x; x++, y++)
462 commandname[y] = toupper(*x);
466 SharedObjectList::iterator command = RFCCommands.find(commandname);
468 if (command != RFCCommands.end())
470 command_t* cmdptr = cmdlist.find(commandname)->second;
471 cmdlist.erase(cmdlist.find(commandname));
473 for (char* x = commandname; *x; x++)
478 dlclose(command->second);
479 RFCCommands.erase(command);
481 snprintf(filename, MAXBUF, "cmd_%s.so", commandname);
482 this->LoadCommand(filename);
490 CmdResult cmd_reload::Handle(const char** parameters, int pcnt, userrec *user)
492 user->WriteServ("NOTICE %s :*** Reloading command '%s'",user->nick, parameters[0]);
493 if (ServerInstance->Parser->ReloadCommand(parameters[0]))
495 user->WriteServ("NOTICE %s :*** Successfully reloaded command '%s'", user->nick, parameters[0]);
496 ServerInstance->WriteOpers("*** RELOAD: %s reloaded the '%s' command.", user->nick, parameters[0]);
501 user->WriteServ("NOTICE %s :*** Could not reload command '%s'", user->nick, parameters[0]);
506 void CommandParser::LoadCommand(const char* name)
508 char filename[MAXBUF];
510 command_t* (*cmd_factory_func)(InspIRCd*);
512 snprintf(filename, MAXBUF, "%s/%s", LIBRARYDIR, name);
513 h = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
517 ServerInstance->Log(SPARSE, "Error loading core command: %s", dlerror());
521 if (this->FindSym((void **)&cmd_factory_func, h))
523 command_t* newcommand = cmd_factory_func(ServerInstance);
524 this->CreateCommand(newcommand, h);
528 void CommandParser::SetupCommandTable()
532 DIR* library = opendir(LIBRARYDIR);
535 dirent* entry = NULL;
536 while ((entry = readdir(library)))
538 if (match(entry->d_name, "cmd_*.so"))
540 this->LoadCommand(entry->d_name);
546 this->CreateCommand(new cmd_reload(ServerInstance));