diff options
Diffstat (limited to 'src/mode.cpp')
-rw-r--r-- | src/mode.cpp | 365 |
1 files changed, 135 insertions, 230 deletions
diff --git a/src/mode.cpp b/src/mode.cpp index e2b0c2f68..15f5c0d60 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -24,35 +24,12 @@ #include "inspircd.h" - -/* +s (secret) */ -/* +p (private) */ -/* +m (moderated) */ -/* +t (only (half) ops can change topic) */ -/* +n (no external messages) */ -/* +i (invite only) */ -/* +w (see wallops) */ -/* +i (invisible) */ -#include "modes/simplemodes.h" -/* +b (bans) */ -#include "modes/cmode_b.h" -/* +k (keyed channel) */ -#include "modes/cmode_k.h" -/* +l (channel user limit) */ -#include "modes/cmode_l.h" -/* +o (channel op) */ -#include "modes/cmode_o.h" -/* +v (channel voice) */ -#include "modes/cmode_v.h" -/* +o (operator) */ -#include "modes/umode_o.h" -/* +s (server notice masks) */ -#include "modes/umode_s.h" +#include "builtinmodes.h" ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type) : ServiceProvider(Creator, Name, SERVICE_MODE), m_paramtype(TR_TEXT), parameters_taken(Params), mode(modeletter), prefix(0), oper(false), - list(false), m_type(type), levelrequired(HALFOP_VALUE) + list(false), m_type(type), levelrequired(HALFOP_VALUE), prefixrank(0) { } @@ -67,16 +44,6 @@ ModeHandler::~ModeHandler() { } -bool ModeHandler::IsListMode() -{ - return list; -} - -unsigned int ModeHandler::GetPrefixRank() -{ - return 0; -} - int ModeHandler::GetNumParams(bool adding) { switch (parameters_taken) @@ -127,7 +94,7 @@ ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel { /* We're either trying to add a mode we already have or remove a mode we don't have, deny. */ - if (dest->IsModeSet(this->GetModeChar()) == adding) + if (dest->IsModeSet(this) == adding) return MODEACTION_DENY; /* adding will be either true or false, depending on if we @@ -136,7 +103,7 @@ ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel aren't removing a mode we don't have, we don't have to do any other checks here to see if it's true or false, just add or remove the mode */ - dest->SetMode(this->GetModeChar(), adding); + dest->SetMode(this, adding); return MODEACTION_ALLOW; } @@ -146,7 +113,7 @@ ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Chan { /* We're either trying to add a mode we already have or remove a mode we don't have, deny. */ - if (channel->IsModeSet(this->GetModeChar()) == adding) + if (channel->IsModeSet(this) == adding) return MODEACTION_DENY; /* adding will be either true or false, depending on if we @@ -155,7 +122,7 @@ ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Chan aren't removing a mode we don't have, we don't have to do any other checks here to see if it's true or false, just add or remove the mode */ - channel->SetMode(this->GetModeChar(), adding); + channel->SetMode(this, adding); return MODEACTION_ALLOW; } @@ -167,10 +134,6 @@ ModeAction ParamChannelModeHandler::OnModeChange(User* source, User* dest, Chann std::string now = channel->GetModeParameter(this); if (parameter == now) return MODEACTION_DENY; - if (adding) - channel->SetModeParam(this, parameter); - else - channel->SetModeParam(this, ""); return MODEACTION_ALLOW; } @@ -179,8 +142,8 @@ bool ParamChannelModeHandler::ParamValidate(std::string& parameter) return true; } -ModeWatcher::ModeWatcher(Module* Creator, char modeletter, ModeType type) - : mode(modeletter), m_type(type), creator(Creator) +ModeWatcher::ModeWatcher(Module* Creator, const std::string& modename, ModeType type) + : mode(modename), m_type(type), creator(Creator) { } @@ -188,39 +151,18 @@ ModeWatcher::~ModeWatcher() { } -char ModeWatcher::GetModeChar() -{ - return mode; -} - ModeType ModeWatcher::GetModeType() { return m_type; } -bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool, ModeType) +bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool) { return true; } -void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool, ModeType) -{ -} - -User* ModeParser::SanityChecks(User *user, const char *dest, Channel *chan, int) +void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool) { - User *d; - if ((!user) || (!dest) || (!chan) || (!*dest)) - { - return NULL; - } - d = ServerInstance->FindNick(dest); - if (!d) - { - user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), dest); - return NULL; - } - return d; } void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text) @@ -238,8 +180,11 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ { /* Display user's current mode string */ user->WriteNumeric(RPL_UMODEIS, "%s :+%s",targetuser->nick.c_str(),targetuser->FormatModes()); - if (IS_OPER(targetuser)) - user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks()); + if ((targetuser->IsOper())) + { + ModeHandler* snomask = FindMode('s', MODETYPE_USER); + user->WriteNumeric(RPL_SNOMASKIS, "%s %s :Server notice mask", targetuser->nick.c_str(), snomask->GetUserParameter(user).c_str()); + } return; } else @@ -254,7 +199,6 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool std::string ¶meter, bool SkipACL) { ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER; - unsigned char mask = chan ? MASK_CHANNEL : MASK_USER; ModeHandler *mh = FindMode(modechar, type); int pcnt = mh->GetNumParams(adding); @@ -308,18 +252,23 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool } } - unsigned char handler_id = (modechar - 'A') | mask; - - for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) + // Ask mode watchers whether this mode change is OK + std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mh->name); + for (ModeWatchIter i = itpair.first; i != itpair.second; ++i) { - if ((*watchers)->BeforeMode(user, targetuser, chan, parameter, adding, type) == false) - return MODEACTION_DENY; - /* A module whacked the parameter completely, and there was one. abort. */ - if (pcnt && parameter.empty()) - return MODEACTION_DENY; + ModeWatcher* mw = i->second; + if (mw->GetModeType() == type) + { + if (!mw->BeforeMode(user, targetuser, chan, parameter, adding)) + return MODEACTION_DENY; + + // A module whacked the parameter completely, and there was one. Abort. + if (pcnt && parameter.empty()) + return MODEACTION_DENY; + } } - if (IS_LOCAL(user) && !IS_OPER(user)) + if (IS_LOCAL(user) && !user->IsOper()) { char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes; if (disabled[modechar - 'A']) @@ -333,10 +282,10 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool if (adding && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(modechar, type)) { /* It's an oper only mode, and they don't have access to it. */ - if (IS_OPER(user)) + if (user->IsOper()) { user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to set %s mode %c", - user->nick.c_str(), user->oper->NameStr(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + user->nick.c_str(), user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar); } else { @@ -370,13 +319,21 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool if (ma != MODEACTION_ALLOW) return ma; - for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) - (*watchers)->AfterMode(user, targetuser, chan, parameter, adding, type); + if ((!mh->IsListMode()) && (mh->GetNumParams(true)) && (chan)) + chan->SetModeParam(mh, (adding ? parameter : "")); + + itpair = modewatchermap.equal_range(mh->name); + for (ModeWatchIter i = itpair.first; i != itpair.second; ++i) + { + ModeWatcher* mw = i->second; + if (mw->GetModeType() == type) + mw->AfterMode(user, targetuser, chan, parameter, adding); + } return MODEACTION_ALLOW; } -void ModeParser::Process(const std::vector<std::string>& parameters, User *user, bool merge) +void ModeParser::Process(const std::vector<std::string>& parameters, User* user, ModeProcessFlag flags) { std::string target = parameters[0]; Channel* targetchannel = ServerInstance->FindChan(target); @@ -456,9 +413,9 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User *user, /* Make sure the user isn't trying to slip in an invalid parameter */ if ((parameter.find(':') == 0) || (parameter.rfind(' ') != std::string::npos)) continue; - if (merge && targetchannel && targetchannel->IsModeSet(modechar) && !mh->IsListMode()) + if ((flags & MODE_MERGE) && targetchannel && targetchannel->IsModeSet(mh) && !mh->IsListMode()) { - std::string ours = targetchannel->GetModeParameter(modechar); + std::string ours = targetchannel->GetModeParameter(mh); if (!mh->ResolveModeConflict(parameter, ours, targetchannel)) /* we won the mode merge, don't apply this mode */ continue; @@ -510,16 +467,15 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User *user, LastParse.append(output_mode); LastParse.append(output_parameters.str()); + if (!(flags & MODE_LOCALONLY)) + ServerInstance->PI->SendMode(user, targetuser, targetchannel, LastParseParams, LastParseTranslate); + if (targetchannel) - { - targetchannel->WriteChannel(user, "MODE %s", LastParse.c_str()); - FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, LastParseParams, LastParseTranslate)); - } + targetchannel->WriteChannel(user, "MODE " + LastParse); else - { - targetuser->WriteFrom(user, "MODE %s", LastParse.c_str()); - FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, LastParseParams, LastParseTranslate)); - } + targetuser->WriteFrom(user, "MODE " + LastParse); + + FOREACH_MOD(OnMode, (user, targetuser, targetchannel, LastParseParams, LastParseTranslate)); } else if (targetchannel && parameters.size() == 2) { @@ -566,15 +522,24 @@ void ModeParser::DisplayListModes(User* user, Channel* chan, std::string &mode_s display = false; } - unsigned char handler_id = (mletter - 'A') | MASK_CHANNEL; - - for(ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) + // Ask mode watchers whether it's OK to show the list + std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mh->name); + for (ModeWatchIter i = itpair.first; i != itpair.second; ++i) { - std::string dummyparam; + ModeWatcher* mw = i->second; + if (mw->GetModeType() == MODETYPE_CHANNEL) + { + std::string dummyparam; - if (!((*watchers)->BeforeMode(user, NULL, chan, dummyparam, true, MODETYPE_CHANNEL))) - display = false; + if (!mw->BeforeMode(user, NULL, chan, dummyparam, true)) + { + // A mode watcher doesn't want us to show the list + display = false; + break; + } + } } + if (display) mh->DisplayList(user, chan); else @@ -582,11 +547,6 @@ void ModeParser::DisplayListModes(User* user, Channel* chan, std::string &mode_s } } -const std::string& ModeParser::GetLastParse() -{ - return LastParse; -} - void ModeParser::CleanMask(std::string &mask) { std::string::size_type pos_of_pling = mask.find_first_of('!'); @@ -652,6 +612,7 @@ bool ModeParser::AddMode(ModeHandler* mh) return false; modehandlers[pos] = mh; + RecreateModeListFor004Numeric(); return true; } @@ -688,12 +649,23 @@ bool ModeParser::DelMode(ModeHandler* mh) // The channel may not be in the hash after RemoveMode(), see m_permchannels Channel* chan = i->second; ++i; - mh->RemoveMode(chan); + + irc::modestacker stack(false); + mh->RemoveMode(chan, stack); + + std::vector<std::string> stackresult; + stackresult.push_back(chan->name); + while (stack.GetStackedLine(stackresult)) + { + this->Process(stackresult, ServerInstance->FakeClient, MODE_LOCALONLY); + stackresult.erase(stackresult.begin() + 1, stackresult.end()); + } } break; } modehandlers[pos] = NULL; + RecreateModeListFor004Numeric(); return true; } @@ -712,52 +684,25 @@ ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt) return modehandlers[pos]; } -std::string ModeParser::UserModeList() +std::string ModeParser::CreateModeList(ModeType mt, bool needparam) { - char modestr[256]; - int pointer = 0; + std::string modestr; + unsigned char mask = ((mt == MODETYPE_CHANNEL) ? MASK_CHANNEL : MASK_USER); for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - unsigned char pos = (mode-65) | MASK_USER; + unsigned char pos = (mode-65) | mask; - if (modehandlers[pos]) - modestr[pointer++] = mode; + if ((modehandlers[pos]) && ((!needparam) || (modehandlers[pos]->GetNumParams(true)))) + modestr.push_back(mode); } - modestr[pointer++] = 0; - return modestr; -} - -std::string ModeParser::ChannelModeList() -{ - char modestr[256]; - int pointer = 0; - - for (unsigned char mode = 'A'; mode <= 'z'; mode++) - { - unsigned char pos = (mode-65) | MASK_CHANNEL; - if (modehandlers[pos]) - modestr[pointer++] = mode; - } - modestr[pointer++] = 0; return modestr; } -std::string ModeParser::ParaModeList() +void ModeParser::RecreateModeListFor004Numeric() { - char modestr[256]; - int pointer = 0; - - for (unsigned char mode = 'A'; mode <= 'z'; mode++) - { - unsigned char pos = (mode-65) | MASK_CHANNEL; - - if ((modehandlers[pos]) && (modehandlers[pos]->GetNumParams(true))) - modestr[pointer++] = mode; - } - modestr[pointer++] = 0; - return modestr; + Cached004ModeList = CreateModeList(MODETYPE_USER) + " " + CreateModeList(MODETYPE_CHANNEL) + " " + CreateModeList(MODETYPE_CHANNEL, true); } ModeHandler* ModeParser::FindPrefix(unsigned const char pfxletter) @@ -847,91 +792,62 @@ std::string ModeParser::BuildPrefixes(bool lettersAndModes) return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters; } -bool ModeParser::AddModeWatcher(ModeWatcher* mw) +void ModeParser::AddModeWatcher(ModeWatcher* mw) { - unsigned char mask = 0; - unsigned char pos = 0; - - if (!mw) - return false; - - if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z')) - return false; - - mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL; - pos = (mw->GetModeChar()-65) | mask; - - modewatchers[pos].push_back(mw); - - return true; + modewatchermap.insert(std::make_pair(mw->GetModeName(), mw)); } bool ModeParser::DelModeWatcher(ModeWatcher* mw) { - unsigned char mask = 0; - unsigned char pos = 0; - - if (!mw) - return false; - - if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z')) - return false; - - mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL; - pos = (mw->GetModeChar()-65) | mask; - - ModeWatchIter a = find(modewatchers[pos].begin(),modewatchers[pos].end(),mw); - - if (a == modewatchers[pos].end()) + std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mw->GetModeName()); + for (ModeWatchIter i = itpair.first; i != itpair.second; ++i) { - return false; + if (i->second == mw) + { + modewatchermap.erase(i); + return true; + } } - modewatchers[pos].erase(a); - - return true; + return false; } -/** This default implementation can remove simple user modes - */ -void ModeHandler::RemoveMode(User* user, irc::modestacker* stack) +void ModeHandler::RemoveMode(User* user) { + // Remove the mode if it's set on the user if (user->IsModeSet(this->GetModeChar())) { - if (stack) - { - stack->Push(this->GetModeChar()); - } - else - { - std::vector<std::string> parameters; - parameters.push_back(user->nick); - parameters.push_back("-"); - parameters[1].push_back(this->GetModeChar()); - ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient); - } + std::vector<std::string> parameters; + parameters.push_back(user->nick); + parameters.push_back("-"); + parameters[1].push_back(this->GetModeChar()); + ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient, ModeParser::MODE_LOCALONLY); } } -/** This default implementation can remove simple channel modes - * (no parameters) - */ -void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack) +void ModeHandler::RemoveMode(Channel* channel, irc::modestacker& stack) { - if (channel->IsModeSet(this->GetModeChar())) + if (this->GetPrefixRank()) { - if (stack) - { - stack->Push(this->GetModeChar()); - } + RemovePrefixMode(channel, stack); + } + else if (channel->IsModeSet(this)) + { + if (this->GetNumParams(false)) + // Removing this mode requires a parameter + stack.Push(this->GetModeChar(), channel->GetModeParameter(this)); else - { - std::vector<std::string> parameters; - parameters.push_back(channel->name); - parameters.push_back("-"); - parameters[1].push_back(this->GetModeChar()); - ServerInstance->SendMode(parameters, ServerInstance->FakeClient); - } + stack.Push(this->GetModeChar()); + } +} + +void ModeHandler::RemovePrefixMode(Channel* chan, irc::modestacker& stack) +{ + const UserMembList* userlist = chan->GetUsers(); + for (UserMembCIter i = userlist->begin(); i != userlist->end(); ++i) + { + if (i->second->hasMode(this->GetModeChar())) + stack.Push(this->GetModeChar(), i->first->nick); } } @@ -956,40 +872,29 @@ struct builtin_modes ModeUserOperator uo; ModeUserServerNoticeMask us; - void init(ModeParser* modes) - { - modes->AddMode(&s); - modes->AddMode(&p); - modes->AddMode(&m); - modes->AddMode(&t); - modes->AddMode(&n); - modes->AddMode(&i); - modes->AddMode(&k); - modes->AddMode(&l); - modes->AddMode(&b); - modes->AddMode(&o); - modes->AddMode(&v); - modes->AddMode(&uw); - modes->AddMode(&ui); - modes->AddMode(&uo); - modes->AddMode(&us); + void init() + { + ServiceProvider* modes[] = { &s, &p, &m, &t, &n, &i, &k, &l, &b, &o, &v, + &uw, &ui, &uo, &us }; + ServerInstance->Modules->AddServices(modes, sizeof(modes)/sizeof(ServiceProvider*)); } }; static builtin_modes static_modes; +void ModeParser::InitBuiltinModes() +{ + static_modes.init(); + static_modes.b.DoRehash(); +} + ModeParser::ModeParser() { /* Clear mode handler list */ memset(modehandlers, 0, sizeof(modehandlers)); - /* Last parse string */ - LastParse.clear(); - seq = 0; memset(&sent, 0, sizeof(sent)); - - static_modes.init(this); } ModeParser::~ModeParser() |