X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fusers.cpp;h=1e2554107b1905c1d1a9bb1f30918e2c85330ebb;hb=48a400f2e068527b338ceecf8ed1dde2da971ca9;hp=397a132678433e82a41f8f82826b618d53de9341;hpb=d23c030c9a8fd58807438245a004e4aa5b7288ba;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/users.cpp b/src/users.cpp index 397a13267..1e2554107 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -63,7 +63,7 @@ std::string User::GetModeLetters(bool includeparams) const return ret; } -User::User(const std::string& uid, Server* srv, int type) +User::User(const std::string& uid, Server* srv, UserType type) : age(ServerInstance->Time()) , signon(0) , uuid(uid) @@ -189,12 +189,7 @@ bool LocalUser::HasPermission(const std::string &command) return false; } - if (oper->AllowedOperCommands.find(command) != oper->AllowedOperCommands.end()) - return true; - else if (oper->AllowedOperCommands.find("*") != oper->AllowedOperCommands.end()) - return true; - - return false; + return oper->AllowedOperCommands.Contains(command); } bool User::HasPrivPermission(const std::string &privstr, bool noisy) @@ -211,14 +206,8 @@ bool LocalUser::HasPrivPermission(const std::string &privstr, bool noisy) return false; } - if (oper->AllowedPrivs.find(privstr) != oper->AllowedPrivs.end()) - { - return true; - } - else if (oper->AllowedPrivs.find("*") != oper->AllowedPrivs.end()) - { + if (oper->AllowedPrivs.Contains(privstr)) return true; - } if (noisy) this->WriteNotice("Oper type " + oper->name + " does not have access to priv " + privstr); @@ -238,37 +227,56 @@ void UserIOHandler::OnDataReady() user->nick.c_str(), (unsigned long)recvq.length(), user->MyClass->GetRecvqMax()); return; } + unsigned long sendqmax = ULONG_MAX; if (!user->HasPrivPermission("users/flood/increased-buffers")) sendqmax = user->MyClass->GetSendqSoftMax(); + unsigned long penaltymax = ULONG_MAX; if (!user->HasPrivPermission("users/flood/no-fakelag")) penaltymax = user->MyClass->GetPenaltyThreshold() * 1000; + // The maximum size of an IRC message minus the terminating CR+LF. + const size_t maxmessage = ServerInstance->Config->Limits.MaxLine - 2; + std::string line; + line.reserve(maxmessage); + + bool eol_found; + std::string::size_type qpos; + while (user->CommandFloodPenalty < penaltymax && getSendQSize() < sendqmax) { - std::string line; - line.reserve(ServerInstance->Config->Limits.MaxLine); - std::string::size_type qpos = 0; - while (qpos < recvq.length()) + qpos = 0; + eol_found = false; + + const size_t qlen = recvq.length(); + while (qpos < qlen) { char c = recvq[qpos++]; switch (c) { - case '\0': - c = ' '; - break; - case '\r': - continue; - case '\n': - goto eol_found; + case '\0': + c = ' '; + break; + case '\r': + continue; + case '\n': + eol_found = true; + break; } - if (line.length() < ServerInstance->Config->Limits.MaxLine - 2) + + if (eol_found) + break; + + if (line.length() < maxmessage) line.push_back(c); } - // if we got here, the recvq ran out before we found a newline - return; -eol_found: + + // if we return here, we haven't found a newline and make no modifications to recvq + // so we can wait for more data + if (!eol_found) + return; + // just found a newline. Terminate the string, and pull it out of recvq recvq.erase(0, qpos); @@ -280,7 +288,11 @@ eol_found: ServerInstance->Parser.ProcessBuffer(line, user); if (user->quitting) return; + + // clear() does not reclaim memory associated with the string, so our .reserve() call is safe + line.clear(); } + if (user->CommandFloodPenalty >= penaltymax && !user->MyClass->fakelag) ServerInstance->Users->QuitUser(user, "Excess Flood"); } @@ -375,8 +387,8 @@ void User::Oper(OperInfo* info) void OperInfo::init() { - AllowedOperCommands.clear(); - AllowedPrivs.clear(); + AllowedOperCommands.Clear(); + AllowedPrivs.Clear(); AllowedUserModes.reset(); AllowedChanModes.reset(); AllowedUserModes['o' - 'A'] = true; // Call me paranoid if you want. @@ -384,19 +396,9 @@ void OperInfo::init() for(std::vector >::iterator iter = class_blocks.begin(); iter != class_blocks.end(); ++iter) { ConfigTag* tag = *iter; - std::string mycmd, mypriv; - /* Process commands */ - irc::spacesepstream CommandList(tag->getString("commands")); - while (CommandList.GetToken(mycmd)) - { - AllowedOperCommands.insert(mycmd); - } - irc::spacesepstream PrivList(tag->getString("privs")); - while (PrivList.GetToken(mypriv)) - { - AllowedPrivs.insert(mypriv); - } + AllowedOperCommands.AddList(tag->getString("commands")); + AllowedPrivs.AddList(tag->getString("privs")); std::string modes = tag->getString("usermodes"); for (std::string::const_iterator c = modes.begin(); c != modes.end(); ++c) @@ -456,6 +458,7 @@ void User::UnOper() ModeHandler* opermh = ServerInstance->Modes->FindMode('o', MODETYPE_USER); this->SetMode(opermh, false); + FOREACH_MOD(OnPostDeoper, (this)); } /* @@ -643,8 +646,8 @@ bool User::ChangeNick(const std::string& newnick, time_t newts) } if (this->registered == REG_ALL) - this->WriteCommon("NICK %s",newnick.c_str()); - std::string oldnick = nick; + this->WriteCommon("NICK %s", newnick.c_str()); + const std::string oldnick = nick; nick = newnick; InvalidateCache(); @@ -669,14 +672,7 @@ void LocalUser::OverruleNick() int LocalUser::GetServerPort() { - switch (this->server_sa.sa.sa_family) - { - case AF_INET6: - return htons(this->server_sa.in6.sin6_port); - case AF_INET: - return htons(this->server_sa.in4.sin_port); - } - return 0; + return this->server_sa.port(); } const std::string& User::GetIPString() @@ -709,7 +705,7 @@ const std::string& User::GetRealHost() const irc::sockets::cidr_mask User::GetCIDRMask() { - int range = 0; + unsigned char range = 0; switch (client_sa.sa.sa_family) { case AF_INET6: @@ -772,10 +768,12 @@ void LocalUser::Write(const std::string& text) if (!SocketEngine::BoundsCheckFd(&eh)) return; - if (text.length() > ServerInstance->Config->Limits.MaxLine - 2) + // The maximum size of an IRC message minus the terminating CR+LF. + const size_t maxmessage = ServerInstance->Config->Limits.MaxLine - 2; + if (text.length() > maxmessage) { - // this should happen rarely or never. Crop the string at 512 and try again. - std::string try_again(text, 0, ServerInstance->Config->Limits.MaxLine - 2); + // This should happen rarely or never. Crop the string at MaxLine and try again. + std::string try_again(text, 0, maxmessage); Write(try_again); return; } @@ -785,8 +783,9 @@ void LocalUser::Write(const std::string& text) eh.AddWriteBuf(text); eh.AddWriteBuf(wide_newline); - ServerInstance->stats.Sent += text.length() + 2; - this->bytes_out += text.length() + 2; + const size_t bytessent = text.length() + 2; + ServerInstance->stats.Sent += bytessent; + this->bytes_out += bytessent; this->cmds_out++; } @@ -1039,15 +1038,27 @@ bool User::ChangeDisplayedHost(const std::string& shost) void User::ChangeRealHost(const std::string& host, bool resetdisplay) { - if (displayhost == host) + // If the real host is the new host and we are not resetting the + // display host then we have nothing to do. + const bool changehost = (realhost != host); + if (!changehost && !resetdisplay) return; + // If the displayhost is not set and we are not resetting it then + // we need to copy it to the displayhost field. if (displayhost.empty() && !resetdisplay) displayhost = realhost; + // If the displayhost is the new host or we are resetting it then + // we clear its contents to save memory. else if (displayhost == host || resetdisplay) displayhost.clear(); + // If we are just resetting the display host then we don't need to + // do anything else. + if (!changehost) + return; + realhost = host; this->InvalidateCache(); } @@ -1184,15 +1195,15 @@ void User::PurgeEmptyChannels() const std::string& FakeUser::GetFullHost() { - if (!ServerInstance->Config->HideWhoisServer.empty()) - return ServerInstance->Config->HideWhoisServer; + if (!ServerInstance->Config->HideServer.empty()) + return ServerInstance->Config->HideServer; return server->GetName(); } const std::string& FakeUser::GetFullRealHost() { - if (!ServerInstance->Config->HideWhoisServer.empty()) - return ServerInstance->Config->HideWhoisServer; + if (!ServerInstance->Config->HideServer.empty()) + return ServerInstance->Config->HideServer; return server->GetName(); } @@ -1209,7 +1220,32 @@ ConnectClass::ConnectClass(ConfigTag* tag, char t, const std::string& mask, cons Update(&parent); name = "unnamed"; type = t; - config = tag; + host = mask; + + // Connect classes can inherit from each other but this is problematic for modules which can't use + // ConnectClass::Update so we build a hybrid tag containing all of the values set on this class as + // well as the parent class. + ConfigItems* items = NULL; + config = ConfigTag::create(tag->tag, tag->src_name, tag->src_line, items); + + const ConfigItems& parentkeys = parent.config->getItems(); + for (ConfigItems::const_iterator piter = parentkeys.begin(); piter != parentkeys.end(); ++piter) + { + // The class name and parent name are not inherited + if (piter->first == "name" || piter->first == "parent") + continue; + + // Store the item in the config tag. If this item also + // exists in the child it will be overwritten. + (*items)[piter->first] = piter->second; + } + + const ConfigItems& childkeys = tag->getItems(); + for (ConfigItems::const_iterator citer = childkeys.begin(); citer != childkeys.end(); ++citer) + { + // This will overwrite the parent value if present. + (*items)[citer->first] = citer->second; + } } void ConnectClass::Update(const ConnectClass* src)