]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/helperfuncs.cpp
GCC doesn't like characters being used in array subscripts
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
index ffa30fb840799e8f4cdc025f1cbfd8a1170a3290..779bc94cc1b48d7340a2a35a10444cd3d896e23b 100644 (file)
@@ -396,13 +396,71 @@ std::string InspIRCd::TimeString(time_t curtime)
 void InspIRCd::AddExtBanChar(char c)
 {
        std::string &tok = Config->data005;
-       std::string::size_type ebpos;
+       std::string::size_type ebpos = tok.find(" EXTBAN=,");
 
-       if ((ebpos = tok.find(" EXTBAN=,")) == std::string::npos)
+       if (ebpos == std::string::npos)
        {
                tok.append(" EXTBAN=,");
                tok.push_back(c);
        }
        else
-               tok.insert(ebpos + 9, 1, c);
+       {
+               ebpos += 9;
+               while (isalpha(tok[ebpos]) && tok[ebpos] < c)
+                       ebpos++;
+               tok.insert(ebpos, 1, c);
+       }
+}
+
+std::string InspIRCd::GenRandomStr(int length, bool printable)
+{
+       char* buf = new char[length];
+       GenRandom(buf, length);
+       std::string rv;
+       rv.resize(length);
+       for(int i=0; i < length; i++)
+               rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
+       delete[] buf;
+       return rv;
+}
+
+// NOTE: this has a slight bias for lower values if max is not a power of 2.
+// Don't use it if that matters.
+unsigned long InspIRCd::GenRandomInt(unsigned long max)
+{
+       unsigned long rv;
+       GenRandom((char*)&rv, sizeof(rv));
+       return rv % max;
+}
+
+// This is overridden by a higher-quality algorithm when SSL support is loaded
+void GenRandomHandler::Call(char *output, size_t max)
+{
+       for(unsigned int i=0; i < max; i++)
+               output[i] = random();
+}
+
+ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
+{
+       unsigned int mypfx = chan->GetPrefixValue(user);
+       char minmode = 0;
+       std::string current;
+
+       irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
+
+       while (defaultstream.GetToken(current))
+       {
+               std::string::size_type pos = current.find(':');
+               if (pos == std::string::npos)
+                       continue;
+               if (current.substr(0,pos) == restriction)
+                       minmode = current[pos+1];
+       }
+
+       ModeHandler* mh = ServerInstance->Modes->FindMode(minmode, MODETYPE_CHANNEL);
+       if (mh && mypfx >= mh->GetPrefixRank())
+               return MOD_RES_ALLOW;
+       if (mh || minmode == '*')
+               return MOD_RES_DENY;
+       return MOD_RES_PASSTHRU;
 }