X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmode.cpp;h=22173c189aa63ee5dcc63870f2cecbfb0d5464a9;hb=1f3b7f2455b0d73d8874daff0459f4bde46b9524;hp=335bb85ce5a065eb92edc1f7080a2ea59ca76dab;hpb=7010a92426d2c3ab0cea5ba0d36a04bc6b52349f;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/mode.cpp b/src/mode.cpp index 335bb85ce..22173c189 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -44,21 +44,21 @@ ModeHandler::~ModeHandler() { } -int ModeHandler::GetNumParams(bool adding) +bool ModeHandler::NeedsParam(bool adding) const { switch (parameters_taken) { case PARAM_ALWAYS: - return 1; + return true; case PARAM_SETONLY: - return adding ? 1 : 0; + return adding; case PARAM_NONE: break; } - return 0; + return false; } -std::string ModeHandler::GetUserParameter(User* user) +std::string ModeHandler::GetUserParameter(const User* user) const { return ""; } @@ -90,6 +90,12 @@ bool ModeHandler::ResolveModeConflict(std::string& theirs, const std::string& ou return (theirs < ours); } +void ModeHandler::RegisterService() +{ + ServerInstance->Modes.AddMode(this); + ServerInstance->Modules.AddReferent((GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + name, this); +} + ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) { /* We're either trying to add a mode we already have or @@ -138,11 +144,6 @@ ModeWatcher::~ModeWatcher() ServerInstance->Modes->DelModeWatcher(this); } -ModeType ModeWatcher::GetModeType() -{ - return m_type; -} - bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool) { return true; @@ -169,7 +170,7 @@ ModeAction PrefixMode::OnModeChange(User* source, User*, Channel* chan, std::str if (!target) { - source->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameter.c_str()); + source->WriteNumeric(Numerics::NoSuchNick(parameter)); return MODEACTION_DENY; } @@ -213,12 +214,12 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode ModeHandler* mh = mcitem.mh; bool adding = mcitem.adding; - int pcnt = mh->GetNumParams(adding); + const bool needs_param = mh->NeedsParam(adding); std::string& parameter = mcitem.param; // crop mode parameter size to 250 characters if (parameter.length() > 250 && adding) - parameter = parameter.substr(0, 250); + parameter.erase(250); ModResult MOD_RESULT; FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding)); @@ -245,11 +246,12 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode unsigned int ourrank = chan->GetPrefixValue(user); if (ourrank < neededrank) { - PrefixMode* neededmh = NULL; - for(char c='A'; c <= 'z'; c++) + const PrefixMode* neededmh = NULL; + const PrefixModeList& prefixmodes = GetPrefixModes(); + for (PrefixModeList::const_iterator i = prefixmodes.begin(); i != prefixmodes.end(); ++i) { - PrefixMode* privmh = FindPrefixMode(c); - if (privmh && privmh->GetPrefixRank() >= neededrank) + const PrefixMode* const privmh = *i; + if (privmh->GetPrefixRank() >= neededrank) { // this mode is sufficient to allow this action if (!neededmh || privmh->GetPrefixRank() < neededmh->GetPrefixRank()) @@ -257,11 +259,10 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode } } if (neededmh) - user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You must have channel %s access or above to %sset channel mode %c", - chan->name.c_str(), neededmh->name.c_str(), adding ? "" : "un", modechar); + user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You must have channel %s access or above to %sset channel mode %c", + neededmh->name.c_str(), adding ? "" : "un", modechar)); else - user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You cannot %sset channel mode %c", - chan->name.c_str(), adding ? "" : "un", modechar); + user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You cannot %sset channel mode %c", (adding ? "" : "un"), modechar)); return MODEACTION_DENY; } } @@ -278,7 +279,7 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode return MODEACTION_DENY; // A module whacked the parameter completely, and there was one. Abort. - if (pcnt && parameter.empty()) + if ((needs_param) && (parameter.empty())) return MODEACTION_DENY; } } @@ -288,24 +289,24 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes; if (disabled[modechar - 'A']) { - user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - %s mode %c has been locked by the administrator", - type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - %s mode %c has been locked by the administrator", + type == MODETYPE_CHANNEL ? "channel" : "user", modechar)); return MODEACTION_DENY; } } - if (adding && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(modechar, type)) + if ((adding) && (IS_LOCAL(user)) && (mh->NeedsOper()) && (!user->HasModePermission(mh))) { /* It's an oper only mode, and they don't have access to it. */ if (user->IsOper()) { - user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - Oper type %s does not have access to set %s mode %c", - user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Oper type %s does not have access to set %s mode %c", + user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar)); } else { - user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - Only operators may set %s mode %c", - type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Only operators may set %s mode %c", + type == MODETYPE_CHANNEL ? "channel" : "user", modechar)); } return MODEACTION_DENY; } @@ -313,7 +314,7 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode /* Call the handler for the mode */ ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding); - if (pcnt && parameter.empty()) + if ((needs_param) && (parameter.empty())) return MODEACTION_DENY; if (ma != MODEACTION_ALLOW) @@ -353,12 +354,12 @@ void ModeParser::ModeParamsToChangeList(User* user, ModeType type, const std::ve if (!mh) { /* No mode handler? Unknown mode character then. */ - user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, "%c :is unknown mode char to me", modechar); + user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, modechar, "is unknown mode char to me"); continue; } std::string parameter; - if (mh->GetNumParams(adding) && param_at < endindex) + if ((mh->NeedsParam(adding)) && (param_at < endindex)) parameter = parameters[param_at++]; changelist.push(mh, adding, parameter); @@ -427,7 +428,7 @@ unsigned int ModeParser::ProcessSingle(User* user, Channel* targetchannel, User* // If the mode is supposed to have a parameter then we first take a look at item.param // and, if we were asked to, also handle mode merges now - if (mh->GetNumParams(item.adding)) + if (mh->NeedsParam(item.adding)) { // Skip the mode if the parameter does not pass basic validation if (!IsModeParamValid(user, targetchannel, targetuser, item)) @@ -714,7 +715,7 @@ std::string ModeParser::CreateModeList(ModeType mt, bool needparam) for (unsigned char mode = 'A'; mode <= 'z'; mode++) { ModeHandler* mh = modehandlers[mt][mode-65]; - if ((mh) && ((!needparam) || (mh->GetNumParams(true)))) + if ((mh) && ((!needparam) || (mh->NeedsParam(true)))) modestr.push_back(mode); } @@ -751,7 +752,7 @@ std::string ModeParser::GiveModeList(ModeType mt) /* One parameter when adding */ if (mh) { - if (mh->GetNumParams(true)) + if (mh->NeedsParam(true)) { PrefixMode* pm = mh->IsPrefixMode(); if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0))) @@ -761,7 +762,7 @@ std::string ModeParser::GiveModeList(ModeType mt) else { /* ... and one parameter when removing */ - if (mh->GetNumParams(false)) + if (mh->NeedsParam(false)) { /* But not a list mode */ if (!pm) @@ -786,24 +787,33 @@ std::string ModeParser::GiveModeList(ModeType mt) return type1 + "," + type2 + "," + type3 + "," + type4; } +struct PrefixModeSorter +{ + bool operator()(PrefixMode* lhs, PrefixMode* rhs) + { + return lhs->GetPrefixRank() < rhs->GetPrefixRank(); + } +}; + std::string ModeParser::BuildPrefixes(bool lettersAndModes) { std::string mletters; std::string mprefixes; - insp::flat_map > prefixes; + std::vector prefixes; const PrefixModeList& list = GetPrefixModes(); for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i) { PrefixMode* pm = *i; if (pm->GetPrefix()) - prefixes[pm->GetPrefixRank()] = std::make_pair(pm->GetPrefix(), pm->GetModeChar()); + prefixes.push_back(pm); } - for (insp::flat_map >::reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); ++n) + std::sort(prefixes.begin(), prefixes.end(), PrefixModeSorter()); + for (std::vector::const_reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); ++n) { - mletters = mletters + n->second.first; - mprefixes = mprefixes + n->second.second; + mletters += (*n)->GetPrefix(); + mprefixes += (*n)->GetModeChar(); } return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters; @@ -844,7 +854,7 @@ void ModeHandler::RemoveMode(Channel* channel, Modes::ChangeList& changelist) { if (channel->IsModeSet(this)) { - if (this->GetNumParams(false)) + if (this->NeedsParam(false)) // Removing this mode requires a parameter changelist.push_remove(this, channel->GetModeParameter(this)); else @@ -857,7 +867,7 @@ void PrefixMode::RemoveMode(Channel* chan, Modes::ChangeList& changelist) const Channel::MemberMap& userlist = chan->GetUsers(); for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i) { - if (i->second->hasMode(this->GetModeChar())) + if (i->second->HasMode(this)) changelist.push_remove(this, i->first->nick); } }