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 CommandObj->Handle(new_parameters,pcnt,user);
158 dupes[item.c_str()] = true;
164 int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere)
166 /* First check if we have more than one item in the list, if we don't we return zero here and the handler
167 * which called us just carries on as it was.
169 if (!strchr(parameters[splithere],','))
172 std::map<irc::string, bool> dupes;
174 /* Only one commasepstream here */
175 irc::commasepstream items1(parameters[splithere]);
176 std::string item = "*";
177 unsigned int max = 0;
179 /* Parse the commasepstream until there are no tokens remaining.
180 * Each token we parse out, call the command handler that called us
183 while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
185 if (dupes.find(item.c_str()) == dupes.end())
187 const char* new_parameters[127];
189 for (int t = 0; (t < pcnt) && (t < 127); t++)
190 new_parameters[t] = parameters[t];
192 new_parameters[splithere] = item.c_str();
194 parameters[splithere] = item.c_str();
196 /* Execute the command handler over and over. If someone pulls our user
197 * record out from under us (e.g. if we /kill a comma sep list, and we're
198 * in that list ourselves) abort if we're gone.
200 CommandObj->Handle(new_parameters,pcnt,user);
202 dupes[item.c_str()] = true;
205 /* By returning 1 we tell our caller that nothing is to be done,
206 * as all the previous calls handled the data. This makes the parent
207 * return without doing any processing.
212 bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, userrec * user)
214 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
216 if (n != cmdlist.end())
218 if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
220 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
222 if (n->second->flags_needed)
224 return ((user->HasPermission(commandname)) || (ServerInstance->ULine(user->server)));
233 command_t* CommandParser::GetHandler(const std::string &commandname)
235 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
236 if (n != cmdlist.end())
242 // calls a handler function for a command
244 CmdResult CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
246 nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
248 if (n != cmdlist.end())
250 if (pcnt >= n->second->min_params)
252 if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
254 if (n->second->flags_needed)
256 if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
258 return n->second->Handle(parameters,pcnt,user);
263 return n->second->Handle(parameters,pcnt,user);
271 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
273 const char *command_p[127];
275 irc::tokenstream tokens(cmd);
276 std::string command = tokens.GetToken();
278 while (((para[items] = tokens.GetToken()) != "") && (items < 127))
280 command_p[items] = para[items].c_str();
284 std::transform(command.begin(), command.end(), command.begin(), ::toupper);
287 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false,cmd));
288 if (MOD_RESULT == 1) {
292 nspace::hash_map<std::string,command_t*>::iterator cm = cmdlist.find(command);
294 if (cm != cmdlist.end())
298 /* activity resets the ping pending timer */
299 user->nping = ServerInstance->Time() + user->pingmax;
300 if (cm->second->flags_needed)
302 if (!user->IsModeSet(cm->second->flags_needed))
304 user->WriteServ("481 %s :Permission Denied- You do not have the required operator privileges",user->nick);
307 if (!user->HasPermission(command))
309 user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
313 if ((user->registered == REG_ALL) && (!*user->oper) && (cm->second->IsDisabled()))
315 /* command is disabled! */
316 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
319 if (items < cm->second->min_params)
321 user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
322 /* If syntax is given, display this as the 461 reply */
323 if ((ServerInstance->Config->SyntaxHints) && (cm->second->syntax.length()))
324 user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
327 if ((user->registered == REG_ALL) || (cm->second->WorksBeforeReg()))
329 /* ikky /stats counters */
330 cm->second->use_count++;
331 cm->second->total_bytes += cmd.length();
334 FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true,cmd));
339 * WARNING: nothing may come after the
340 * command handler call, as the handler
341 * may free the user structure!
343 CmdResult result = cm->second->Handle(command_p,items,user);
345 FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, items, user, result,cmd));
350 user->WriteServ("451 %s :You have not registered",command.c_str());
357 ServerInstance->stats->statsUnknown++;
358 user->WriteServ("421 %s %s :Unknown command",user->nick,command.c_str());
362 bool CommandParser::RemoveCommands(const char* source)
364 nspace::hash_map<std::string,command_t*>::iterator i,safei;
365 for (i = cmdlist.begin(); i != cmdlist.end(); i++)
369 if (safei != cmdlist.end())
371 RemoveCommand(safei, source);
374 safei = cmdlist.begin();
375 if (safei != cmdlist.end())
377 RemoveCommand(safei, source);
382 void CommandParser::RemoveCommand(nspace::hash_map<std::string,command_t*>::iterator safei, const char* source)
384 command_t* x = safei->second;
385 if (x->source == std::string(source))
387 cmdlist.erase(safei);
391 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
393 std::string::size_type a;
398 while ((a = buffer.rfind("\n")) != std::string::npos)
400 while ((a = buffer.rfind("\r")) != std::string::npos)
407 ServerInstance->Log(DEBUG,"-> :%s %s",user->nick,buffer.c_str());
408 this->ProcessCommand(user,buffer);
413 bool CommandParser::CreateCommand(command_t *f, void* so_handle)
417 if (RFCCommands.find(f->command) == RFCCommands.end())
418 RFCCommands[f->command] = so_handle;
421 ServerInstance->Log(DEFAULT,"ERK! Somehow, we loaded a cmd_*.so file twice! Only the first instance is being recorded.");
426 /* create the command and push it onto the table */
427 if (cmdlist.find(f->command) == cmdlist.end())
429 cmdlist[f->command] = f;
435 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
438 this->SetupCommandTable();
441 bool CommandParser::FindSym(void** v, void* h)
443 *v = dlsym(h, "init_command");
444 const char* err = dlerror();
447 ServerInstance->Log(SPARSE, "Error loading core command: %s\n", err);
453 bool CommandParser::ReloadCommand(const char* cmd)
455 char filename[MAXBUF];
456 char commandname[MAXBUF];
459 for (const char* x = cmd; *x; x++, y++)
460 commandname[y] = toupper(*x);
464 SharedObjectList::iterator command = RFCCommands.find(commandname);
466 if (command != RFCCommands.end())
468 command_t* cmdptr = cmdlist.find(commandname)->second;
469 cmdlist.erase(cmdlist.find(commandname));
471 for (char* x = commandname; *x; x++)
476 dlclose(command->second);
477 RFCCommands.erase(command);
479 snprintf(filename, MAXBUF, "cmd_%s.so", commandname);
480 this->LoadCommand(filename);
488 CmdResult cmd_reload::Handle(const char** parameters, int pcnt, userrec *user)
490 user->WriteServ("NOTICE %s :*** Reloading command '%s'",user->nick, parameters[0]);
491 if (ServerInstance->Parser->ReloadCommand(parameters[0]))
493 user->WriteServ("NOTICE %s :*** Successfully reloaded command '%s'", user->nick, parameters[0]);
494 ServerInstance->WriteOpers("*** RELOAD: %s reloaded the '%s' command.", user->nick, parameters[0]);
499 user->WriteServ("NOTICE %s :*** Could not reload command '%s'", user->nick, parameters[0]);
504 void CommandParser::LoadCommand(const char* name)
506 char filename[MAXBUF];
508 command_t* (*cmd_factory_func)(InspIRCd*);
510 snprintf(filename, MAXBUF, "%s/%s", LIBRARYDIR, name);
511 h = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
515 ServerInstance->Log(SPARSE, "Error loading core command: %s", dlerror());
519 if (this->FindSym((void **)&cmd_factory_func, h))
521 command_t* newcommand = cmd_factory_func(ServerInstance);
522 this->CreateCommand(newcommand, h);
526 void CommandParser::SetupCommandTable()
530 DIR* library = opendir(LIBRARYDIR);
533 dirent* entry = NULL;
534 while ((entry = readdir(library)))
536 if (match(entry->d_name, "cmd_*.so"))
538 this->LoadCommand(entry->d_name);
544 this->CreateCommand(new cmd_reload(ServerInstance));