]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/command_parse.cpp
Set defaults for an enum
[user/henk/code/inspircd.git] / src / command_parse.cpp
index 68b8b0bda0994a8a142bf41147d12f2892945a1d..84850a880fb564d7e95895903f0e2c198593d71d 100644 (file)
@@ -1,4 +1,4 @@
-       /*       +------------------------------------+
+/*       +------------------------------------+
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
@@ -65,40 +65,44 @@ std::string InspIRCd::TimeString(time_t curtime)
 /** Refactored by Brain, Jun 2007. Much faster with some clever O(1) array
  * lookups and pointer maths.
  */
-long InspIRCd::Duration(const char* str)
+long InspIRCd::Duration(const std::string &str)
 {
-       char n_field[MAXBUF];
-       const char* maxsize = n_field + MAXBUF;
+       unsigned char multiplier = 0;
        long total = 0;
-       char* field_ptr = n_field;
-       *n_field = 0;
+       long times = 1;
+       long subtotal = 0;
 
        /* Iterate each item in the string, looking for number or multiplier */
-       for (const char* i = str; *i; i++)
+       for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
        {
                /* Found a number, queue it onto the current number */
-               if ((*i >= '0') && (*i <= '9') && (field_ptr < maxsize))
-                       *field_ptr++ = *i;
+               if ((*i >= '0') && (*i <= '9'))
+               {
+                       subtotal = subtotal + ((*i - '0') * times);
+                       times = times * 10;
+               }
                else
                {
                        /* Found something thats not a number, find out how much
                         * it multiplies the built up number by, multiply the total
                         * and reset the built up number.
                         */
-                       *field_ptr = 0;
-                       field_ptr = n_field;
-                       total += atoi(n_field) * duration_multi[(const unsigned char)*i];
-                       *n_field = 0;
+                       if (subtotal)
+                               total += subtotal * duration_multi[multiplier];
+
+                       /* Next subtotal please */
+                       subtotal = 0;
+                       multiplier = *i;
+                       times = 1;
                }
        }
-       /* Any trailing values built up are treated as raw seconds */
-       if (*n_field)
+       if (multiplier)
        {
-               *field_ptr = 0;
-               total += atoi(n_field);
+               total += subtotal * duration_multi[multiplier];
+               subtotal = 0;
        }
-
-       return total;
+       /* Any trailing values built up are treated as raw seconds */
+       return total + subtotal;
 }
 
 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
@@ -129,7 +133,7 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
         */
        irc::commasepstream items1(parameters[splithere]);
        irc::commasepstream items2(parameters[extra]);
-       std::string item = "*";
+       std::string item("*");
        unsigned int max = 0;
 
        /* Attempt to iterate these lists and call the command objech
@@ -140,9 +144,9 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
        {
                if (dupes.find(item.c_str()) == dupes.end())
                {
-                       const char* new_parameters[127];
+                       const char* new_parameters[MAXPARAMETERS];
 
-                       for (int t = 0; (t < pcnt) && (t < 127); t++)
+                       for (int t = 0; (t < pcnt) && (t < MAXPARAMETERS); t++)
                                new_parameters[t] = parameters[t];
 
                        std::string extrastuff = items2.GetToken();
@@ -170,7 +174,7 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
 
        /* Only one commasepstream here */
        irc::commasepstream items1(parameters[splithere]);
-       std::string item = "*";
+       std::string item("*");
        unsigned int max = 0;
 
        /* Parse the commasepstream until there are no tokens remaining.
@@ -181,9 +185,9 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
        {
                if (dupes.find(item.c_str()) == dupes.end())
                {
-                       const char* new_parameters[127];
+                       const char* new_parameters[MAXPARAMETERS];
 
-                       for (int t = 0; (t < pcnt) && (t < 127); t++)
+                       for (int t = 0; (t < pcnt) && (t < MAXPARAMETERS); t++)
                                new_parameters[t] = parameters[t];
 
                        new_parameters[splithere] = item.c_str();
@@ -214,7 +218,7 @@ bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, use
        {
                if ((pcnt>=n->second->min_params) && (n->second->source != "<core>"))
                {
-                       if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
+                       if ((!n->second->flags_needed) || (user->IsModeSet(n->second->flags_needed)))
                        {
                                if (n->second->flags_needed)
                                {
@@ -246,7 +250,7 @@ CmdResult CommandParser::CallHandler(const std::string &commandname,const char**
        {
                if (pcnt >= n->second->min_params)
                {
-                       if ((!n->second->flags_needed) || (user->modes[n->second->flags_needed-65]))
+                       if ((!n->second->flags_needed) || (user->IsModeSet(n->second->flags_needed)))
                        {
                                if (n->second->flags_needed)
                                {
@@ -267,7 +271,7 @@ CmdResult CommandParser::CallHandler(const std::string &commandname,const char**
 
 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
 {
-       const char *command_p[127];
+       const char *command_p[MAXPARAMETERS];
        int items = 0;
        irc::tokenstream tokens(cmd);
        std::string command;
@@ -281,7 +285,7 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
        if (*command.c_str() == ':')
                tokens.GetToken(command);
 
-       while (tokens.GetToken(para[items]) && (items < 127))
+       while (tokens.GetToken(para[items]) && (items < MAXPARAMETERS))
        {
                command_p[items] = para[items].c_str();
                items++;
@@ -325,7 +329,6 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
                        if (items < cm->second->min_params)
                        {
                                user->WriteServ("461 %s %s :Not enough parameters.", user->nick, command.c_str());
-                               /* If syntax is given, display this as the 461 reply */
                                if ((ServerInstance->Config->SyntaxHints) && (user->registered == REG_ALL) && (cm->second->syntax.length()))
                                        user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
                                return;
@@ -342,9 +345,8 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
                                        return;
 
                                /*
-                                * WARNING: nothing may come after the
-                                * command handler call, as the handler
-                                * may free the user structure!
+                                * WARNING: nothing should come after this, as the user may be on a cull list to
+                                * be nuked next loop iteration. be sensible.
                                 */
                                CmdResult result = cm->second->Handle(command_p,items,user);
 
@@ -391,6 +393,7 @@ void CommandParser::RemoveCommand(command_table::iterator safei, const char* sou
        if (x->source == std::string(source))
        {
                cmdlist.erase(safei);
+               delete x;
        }
 }