X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmode.cpp;h=4478dce73df3f09981da9f200a06b01182572679;hb=6d03943426dcce76ba66567a9b18425a5ebb4c0c;hp=bdec78737d87aa9ae0958d5993d09c3050b86997;hpb=2d52c375efbad941407fdc81846b6e11a9d31834;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/mode.cpp b/src/mode.cpp index bdec78737..4478dce73 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -2,8 +2,8 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd: (C) 2002-2008 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits * * This program is free but copyrighted software; see * the file COPYING for details. @@ -12,9 +12,6 @@ */ /* $Core */ -/* $ExtraDeps: $(RELCPPFILES) */ -/* $ExtraObjects: modes/modeclasses.a */ -/* $ExtraBuild: @${MAKE} -C "modes" DIRNAME="src/modes" CC="$(CC)" $(MAKEARGS) CPPFILES="$(CPPFILES)" */ #include "inspircd.h" #include "inspstring.h" @@ -52,8 +49,9 @@ /* +s (server notice masks) */ #include "modes/umode_s.h" -ModeHandler::ModeHandler(InspIRCd* Instance, char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly, char mprefix, char prefixrequired) - : ServerInstance(Instance), mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly), prefix(mprefix), count(0), prefixneeded(prefixrequired) +ModeHandler::ModeHandler(Module* Creator, char modeletter, ParamSpec Params, ModeType type) + : mode(modeletter), parameters_taken(Params), list(false), m_type(type), m_paramtype(TR_TEXT), + oper(false), prefix(0), count(0), levelrequired(HALFOP_VALUE), creator(Creator) { } @@ -66,16 +64,6 @@ bool ModeHandler::IsListMode() return list; } -char ModeHandler::GetNeededPrefix() -{ - return prefixneeded; -} - -void ModeHandler::SetNeededPrefix(char needsprefix) -{ - prefixneeded = needsprefix; -} - unsigned int ModeHandler::GetPrefixRank() { return 0; @@ -92,24 +80,18 @@ void ModeHandler::ChangeCount(int modifier) ServerInstance->Logs->Log("MODE", DEBUG,"Change count for mode %c is now %d", mode, count); } -ModeType ModeHandler::GetModeType() -{ - return m_type; -} - -bool ModeHandler::NeedsOper() -{ - return oper; -} - -char ModeHandler::GetPrefix() -{ - return prefix; -} - int ModeHandler::GetNumParams(bool adding) { - return adding ? n_params_on : n_params_off; + switch (parameters_taken) + { + case PARAM_ALWAYS: + return 1; + case PARAM_SETONLY: + return adding ? 1 : 0; + case PARAM_NONE: + break; + } + return 0; } char ModeHandler::GetModeChar() @@ -122,7 +104,12 @@ std::string ModeHandler::GetUserParameter(User* user) return ""; } -ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool, bool) +ModResult ModeHandler::AccessCheck(User*, Channel*, std::string &, bool) +{ + return MOD_RES_PASSTHRU; +} + +ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool) { return MODEACTION_DENY; } @@ -151,28 +138,12 @@ void ModeHandler::OnParameterMissing(User* user, User* dest, Channel* channel) { } -bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string&, const std::string&, Channel*) +bool ModeHandler::CheckTimeStamp(std::string& theirs, const std::string& ours, Channel*) { - return (ours < theirs); + return (theirs < ours); } -SimpleUserModeHandler::SimpleUserModeHandler(InspIRCd* Instance, char modeletter) : ModeHandler(Instance, modeletter, 0, 0, false, MODETYPE_USER, false) -{ -} - -SimpleUserModeHandler::~SimpleUserModeHandler() -{ -} - -SimpleChannelModeHandler::~SimpleChannelModeHandler() -{ -} - -SimpleChannelModeHandler::SimpleChannelModeHandler(InspIRCd* Instance, char modeletter) : ModeHandler(Instance, modeletter, 0, 0, false, MODETYPE_CHANNEL, false) -{ -} - -ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool servermode) +ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) { if (adding) { @@ -195,7 +166,7 @@ ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel } -ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool servermode) +ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) { if (adding) { @@ -217,7 +188,7 @@ ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Chan return MODEACTION_DENY; } -ModeWatcher::ModeWatcher(InspIRCd* Instance, char modeletter, ModeType type) : ServerInstance(Instance), mode(modeletter), m_type(type) +ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type) { } @@ -235,12 +206,12 @@ ModeType ModeWatcher::GetModeType() return m_type; } -bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool, ModeType, bool) +bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool, ModeType) { return true; } -void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool, ModeType, bool) +void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool, ModeType) { } @@ -260,66 +231,6 @@ User* ModeParser::SanityChecks(User *user, const char *dest, Channel *chan, int) return d; } -const char* ModeParser::Grant(User *d,Channel *chan,int MASK) -{ - if (!chan) - return ""; - - UCListIter n = d->chans.find(chan); - if (n != d->chans.end()) - { - if (n->second & MASK) - { - return ""; - } - n->second = n->second | MASK; - switch (MASK) - { - case UCMODE_OP: - n->first->AddOppedUser(d); - break; - case UCMODE_HOP: - n->first->AddHalfoppedUser(d); - break; - case UCMODE_VOICE: - n->first->AddVoicedUser(d); - break; - } - return d->nick.c_str(); - } - return ""; -} - -const char* ModeParser::Revoke(User *d,Channel *chan,int MASK) -{ - if (!chan) - return ""; - - UCListIter n = d->chans.find(chan); - if (n != d->chans.end()) - { - if ((n->second & MASK) == 0) - { - return ""; - } - n->second ^= MASK; - switch (MASK) - { - case UCMODE_OP: - n->first->DelOppedUser(d); - break; - case UCMODE_HOP: - n->first->DelHalfoppedUser(d); - break; - case UCMODE_VOICE: - n->first->DelVoicedUser(d); - break; - } - return d->nick.c_str(); - } - return ""; -} - void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text) { if (targetchannel) @@ -329,14 +240,8 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ user->WriteNumeric(RPL_CHANNELCREATED, "%s %s %lu", user->nick.c_str(), targetchannel->name.c_str(), (unsigned long)targetchannel->age); return; } - else if (targetuser) + else { - if (targetuser->Visibility && !targetuser->Visibility->VisibleTo(user)) - { - user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), text); - return; - } - if (targetuser == user || user->HasPrivPermission("users/auspex")) { /* Display user's current mode string */ @@ -347,442 +252,326 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ } else { - user->WriteNumeric(ERR_USERSDONTMATCH, "%s :Can't change mode for other users", user->nick.c_str()); + user->WriteNumeric(ERR_USERSDONTMATCH, "%s :Can't view modes for other users", user->nick.c_str()); return; } } - - /* No such nick/channel */ - user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), text); - return; } -void ModeParser::Process(const std::vector& parameters, User *user, bool servermode) +ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool adding, const unsigned char modechar, + std::string ¶meter, bool SkipACL) { - std::string target = parameters[0]; - ModeType type = MODETYPE_USER; - unsigned char mask = 0; - Channel* targetchannel = ServerInstance->FindChan(parameters[0]); - User* targetuser = ServerInstance->FindNick(parameters[0]); + ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER; + unsigned char mask = chan ? MASK_CHANNEL : MASK_USER; - LastParse.clear(); + ModeHandler *mh = FindMode(modechar, type); + int pcnt = mh->GetNumParams(adding); - /* Special case for displaying the list for listmodes, - * e.g. MODE #chan b, or MODE #chan +b without a parameter - */ - if ((targetchannel) && (parameters.size() == 2)) - { - const char* mode = parameters[1].c_str(); - int nonlistmodes_found = 0; + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, modechar, parameter, adding, pcnt)); - seq++; + if (IS_LOCAL(user) && (MOD_RESULT == MOD_RES_DENY)) + return MODEACTION_DENY; - mask = MASK_CHANNEL; - - while (mode && *mode) - { - unsigned char mletter = *mode; + if (chan && !SkipACL && (MOD_RESULT != MOD_RES_ALLOW)) + { + MOD_RESULT = mh->AccessCheck(user, chan, parameter, adding); - if (*mode == '+') - { - mode++; - continue; - } - - /* Ensure the user doesnt request the same mode twice, - * so they cant flood themselves off out of idiocy. + if (MOD_RESULT == MOD_RES_DENY) + return MODEACTION_DENY; + if (MOD_RESULT == MOD_RES_PASSTHRU) + { + unsigned int neededrank = mh->GetLevelRequired(); + /* Compare our rank on the channel against the rank of the required prefix, + * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown + * in NAMES(X) are not in rank order, we know the most powerful mode is listed + * first, so we don't need to iterate, we just look up the first instead. */ - if (sent[mletter] != seq) + unsigned int ourrank = chan->GetPrefixValue(user); + if (ourrank < neededrank) { - sent[mletter] = seq; - } - else - { - mode++; - continue; + /* Bog off */ + // TODO replace with a real search for the proper prefix + char needed = neededrank > HALFOP_VALUE ? '@' : '%'; + user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must have channel privilege %c or above to %sset channel mode %c", + user->nick.c_str(), chan->name.c_str(), needed, adding ? "" : "un", modechar); + return MODEACTION_DENY; } + } + } - ModeHandler *mh = this->FindMode(*mode, MODETYPE_CHANNEL); - bool display = true; + unsigned char handler_id = (modechar - 'A') | mask; - if ((mh) && (mh->IsListMode())) - { - int MOD_RESULT = 0; - FOREACH_RESULT(I_OnRawMode, OnRawMode(user, targetchannel, *mode, "", true, 0)); - if (MOD_RESULT == ACR_DENY) - { - mode++; - continue; - } + for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) + { + 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; + } - if (!user->HasPrivPermission("channels/auspex")) - { - if (ServerInstance->Config->HideModeLists[mletter] && (targetchannel->GetStatus(user) < STATUS_HOP)) - { - user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only half-operators and above may view the +%c list",user->nick.c_str(), targetchannel->name.c_str(), *mode++); - mh->DisplayEmptyList(user, targetchannel); - continue; - } - } + if (IS_LOCAL(user) && !IS_OPER(user)) + { + char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes; + if (disabled[modechar - 'A']) + { + user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - %s mode %c has been locked by the administrator", + user->nick.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + return MODEACTION_DENY; + } + } + + 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)) + { + user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to set %s mode %c", + user->nick.c_str(), irc::Spacify(user->oper.c_str()), type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + } + else + { + user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Only operators may set %s mode %c", + user->nick.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar); + } + return MODEACTION_DENY; + } - /** See below for a description of what craq this is :D - */ - unsigned char handler_id = (*mode - 65) | mask; + /* Call the handler for the mode */ + ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding); - for(ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) - { - std::string dummyparam; - - if (!((*watchers)->BeforeMode(user, NULL, targetchannel, dummyparam, true, MODETYPE_CHANNEL))) - display = false; - } + if (pcnt && parameter.empty()) + return MODEACTION_DENY; - if (display) - mh->DisplayList(user, targetchannel); - } - else - nonlistmodes_found++; + if (ma != MODEACTION_ALLOW) + return ma; - mode++; - } + mh->ChangeCount(adding ? 1 : -1); - /* We didnt have any modes that were non-list, we can return here */ - if (!nonlistmodes_found) - return; + if (mh->GetPrefixRank() && chan) + { + User* user_to_prefix = ServerInstance->FindNick(parameter); + if (user_to_prefix) + chan->SetPrefix(user_to_prefix, modechar, adding); } + for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) + (*watchers)->AfterMode(user, targetuser, chan, parameter, adding, type); + + return MODEACTION_ALLOW; +} + +void ModeParser::Process(const std::vector& parameters, User *user, bool merge) +{ + std::string target = parameters[0]; + Channel* targetchannel = ServerInstance->FindChan(target); + User* targetuser = ServerInstance->FindNick(target); + ModeType type = targetchannel ? MODETYPE_CHANNEL : MODETYPE_USER; + + LastParse.clear(); + LastParseParams.clear(); + LastParseTranslate.clear(); + + if (!targetchannel && !targetuser) + { + user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(),target.c_str()); + return; + } if (parameters.size() == 1) { - this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0].c_str()); + this->DisplayCurrentModes(user, targetuser, targetchannel, target.c_str()); + return; + } + + std::string mode_sequence = parameters[1]; + + bool SkipAccessChecks = false; + + if (!IS_LOCAL(user) || ServerInstance->ULine(user->server)) + { + SkipAccessChecks = true; } - else if (parameters.size() > 1) + else { - bool SkipAccessChecks = false; + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnPreMode, MOD_RESULT, (user, targetuser, targetchannel, parameters)); + if (MOD_RESULT == MOD_RES_DENY) + return; + SkipAccessChecks = (MOD_RESULT == MOD_RES_ALLOW); + } - if (targetchannel) + if (targetuser && !SkipAccessChecks && user != targetuser) + { + user->WriteNumeric(ERR_USERSDONTMATCH, "%s :Can't change mode for other users", user->nick.c_str()); + return; + } + + std::string output_mode; + std::ostringstream output_parameters; + LastParseParams.push_back(output_mode); + LastParseTranslate.push_back(TR_TEXT); + + bool adding = true; + char output_pm = '\0'; // current output state, '+' or '-' + unsigned int param_at = 2; + + for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++) + { + unsigned char modechar = *letter; + if (modechar == '+' || modechar == '-') { - type = MODETYPE_CHANNEL; - mask = MASK_CHANNEL; + adding = (modechar == '+'); + continue; + } - /* Extra security checks on channel modes - * (e.g. are they a (half)op? - */ + ModeHandler *mh = this->FindMode(modechar, type); + if (!mh) + { + /* No mode handler? Unknown mode character then. */ + user->WriteServ("%d %s %c :is unknown mode char to me", type == MODETYPE_CHANNEL ? 472 : 501, user->nick.c_str(), modechar); + continue; + } - if ((IS_LOCAL(user)) && (!ServerInstance->ULine(user->server)) && (!servermode)) + std::string parameter = ""; + int pcnt = mh->GetNumParams(adding); + if (pcnt && param_at == parameters.size()) + { + /* No parameter, continue to the next mode */ + mh->OnParameterMissing(user, targetuser, targetchannel); + continue; + } + else if (pcnt) + { + parameter = parameters[param_at++]; + /* 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()) { - /* We don't have halfop */ - int MOD_RESULT = 0; - FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, targetchannel, AC_GENERAL_MODE)); - if (MOD_RESULT == ACR_DENY) - return; - SkipAccessChecks = (MOD_RESULT == ACR_ALLOW); + std::string ours = targetchannel->GetModeParameter(modechar); + if (!mh->CheckTimeStamp(parameter, ours, targetchannel)) + /* we won the mode merge, don't apply this mode */ + continue; } } - else if (targetuser) + + ModeAction ma = TryMode(user, targetuser, targetchannel, adding, modechar, parameter, SkipAccessChecks); + + if (ma != MODEACTION_ALLOW) + continue; + + char needed_pm = adding ? '+' : '-'; + if (needed_pm != output_pm) + { + output_pm = needed_pm; + output_mode.append(1, output_pm); + } + output_mode.append(1, modechar); + + if (pcnt) { - type = MODETYPE_USER; - mask = MASK_USER; - if (user != targetuser && IS_LOCAL(user) && !ServerInstance->ULine(user->server)) + TranslateType tt = mh->GetTranslateType(); + if (tt == TR_NICK) { - user->WriteNumeric(ERR_USERSDONTMATCH, "%s :Can't change mode for other users", user->nick.c_str()); - return; + User* u = ServerInstance->FindNick(parameter); + if (u) + parameter = u->nick; } + output_parameters << " " << parameter; + LastParseParams.push_back(parameter); + LastParseTranslate.push_back(tt); } - else + + if ( (output_mode.length() + output_parameters.str().length() > 450) + || (output_mode.length() > 100) + || (LastParseParams.size() > ServerInstance->Config->Limits.MaxModes)) { - /* No such nick/channel */ - user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str()); - return; + /* mode sequence is getting too long */ + break; } + } + + LastParseParams[0] = output_mode; + + if (!output_mode.empty()) + { + LastParse = targetchannel ? targetchannel->name : targetuser->nick; + LastParse.append(" "); + LastParse.append(output_mode); + LastParse.append(output_parameters.str()); - std::string mode_sequence = parameters[1]; - std::string parameter; - std::ostringstream parameter_list; - std::string output_sequence; - bool adding = true, state_change = false; - unsigned char handler_id = 0; - unsigned int parameter_counter = 2; /* Index of first parameter */ - unsigned int parameter_count = 0; - bool last_successful_state_change = false; - - /* A mode sequence that doesnt start with + or -. Assume +. - Thanks for the suggestion spike (bug#132) */ - if ((*mode_sequence.begin() != '+') && (*mode_sequence.begin() != '-')) - mode_sequence.insert(0, "+"); - - for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++) + if (targetchannel) { - unsigned char modechar = *letter; + targetchannel->WriteChannel(user, "MODE %s", LastParse.c_str()); + FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, LastParseParams, LastParseTranslate)); + } + else + { + targetuser->WriteFrom(user, "MODE %s", LastParse.c_str()); + FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, LastParseParams, LastParseTranslate)); + } + } + else if (targetchannel && parameters.size() == 2) + { + /* Special case for displaying the list for listmodes, + * e.g. MODE #chan b, or MODE #chan +b without a parameter + */ + this->DisplayListModes(user, targetchannel, mode_sequence); + } +} - switch (modechar) - { - /* NB: - * For + and - mode characters, we don't just stick the character into the output sequence. - * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this - * appearing in the output sequence, we store a flag which says there was a state change, - * which is set on any + or -, however, the + or - that we finish on is only appended to - * the output stream in the event it is followed by a non "+ or -" character, such as o or v. - */ - case '+': - /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick, - * however, will allow the + if it is the first item in the sequence, regardless. - */ - if ((!adding) || (!output_sequence.length())) - state_change = true; - adding = true; - if (!output_sequence.length()) - last_successful_state_change = false; - continue; - break; - case '-': - if ((adding) || (!output_sequence.length())) - state_change = true; - adding = false; - if (!output_sequence.length()) - last_successful_state_change = true; - continue; - break; - default: - - /** - * Watch carefully for the sleight of hand trick. - * 65 is the ascii value of 'A'. We take this from - * the char we're looking at to get a number between - * 1 and 127. We then logic-or it to get the hashed - * position, dependent on wether its a channel or - * a user mode. This is a little stranger, but a lot - * faster, than using a map of pairs. - */ - handler_id = (modechar - 65) | mask; - - if (modehandlers[handler_id]) - { - bool abort = false; +void ModeParser::DisplayListModes(User* user, Channel* chan, std::string &mode_sequence) +{ + seq++; - if (modehandlers[handler_id]->GetModeType() == type) - { - int MOD_RESULT = 0; - - if (modehandlers[handler_id]->GetNumParams(adding)) - { - /* This mode expects a parameter, do we have any parameters left in our list to use? */ - if (parameter_counter < parameters.size()) - { - parameter = parameters[parameter_counter++]; - - /* Yerk, invalid! */ - if ((parameter.find(':') == 0) || (parameter.rfind(' ') != std::string::npos)) - parameter.clear(); - } - else - { - /* No parameter, continue to the next mode */ - modehandlers[handler_id]->OnParameterMissing(user, targetuser, targetchannel); - continue; - } - - FOREACH_RESULT(I_OnRawMode, OnRawMode(user, targetchannel, modechar, parameter, adding, 1, servermode)); - } - else - { - FOREACH_RESULT(I_OnRawMode, OnRawMode(user, targetchannel, modechar, "", adding, 0, servermode)); - } - - if (IS_LOCAL(user) && (MOD_RESULT == ACR_DENY)) - continue; - - if (!SkipAccessChecks && IS_LOCAL(user) && (MOD_RESULT != ACR_ALLOW)) - { - /* Check access to this mode character */ - if ((type == MODETYPE_CHANNEL) && (modehandlers[handler_id]->GetNeededPrefix())) - { - char needed = modehandlers[handler_id]->GetNeededPrefix(); - ModeHandler* prefixmode = FindPrefix(needed); - - /* If the mode defined by the handler is not '\0', but the handler for it - * cannot be found, they probably dont have the right module loaded to implement - * the prefix they want to compare the mode against, e.g. '&' for m_chanprotect. - * Revert to checking against the minimum core prefix, '%'. - */ - if (needed && !prefixmode) - prefixmode = FindPrefix('%'); - - unsigned int neededrank = prefixmode->GetPrefixRank(); - /* Compare our rank on the channel against the rank of the required prefix, - * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown - * in NAMES(X) are not in rank order, we know the most powerful mode is listed - * first, so we don't need to iterate, we just look up the first instead. - */ - std::string modestring = targetchannel->GetAllPrefixChars(user); - char ml = (modestring.empty() ? '\0' : modestring[0]); - ModeHandler* ourmode = FindPrefix(ml); - if (!ourmode || ourmode->GetPrefixRank() < neededrank) - { - /* Bog off */ - user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must have channel privilege %c or above to %sset channel mode %c", - user->nick.c_str(), targetchannel->name.c_str(), needed, adding ? "" : "un", modechar); - continue; - } - } - } - - bool had_parameter = !parameter.empty(); - - for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) - { - if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type, servermode) == false) - { - abort = true; - break; - } - /* A module whacked the parameter completely, and there was one. abort. */ - if ((had_parameter) && (parameter.empty())) - { - abort = true; - break; - } - } - - if (abort) - continue; - - /* If it's disabled, they have to be an oper. - */ - if (IS_LOCAL(user) && !IS_OPER(user) && ((type == MODETYPE_CHANNEL ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes)[modehandlers[handler_id]->GetModeChar() - 'A'])) - { - user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - %s mode %c has been locked by the administrator", - user->nick.c_str(), - type == MODETYPE_CHANNEL ? "channel" : "user", - modehandlers[handler_id]->GetModeChar()); - continue; - } - - /* It's an oper only mode, check if theyre an oper. If they arent, - * eat any parameter that came with the mode, and continue to next - */ - if (adding && (IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!user->HasModePermission(modehandlers[handler_id]->GetModeChar(), type))) - { - if (IS_OPER(user)) - { - 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.c_str(), - type == MODETYPE_CHANNEL ? "channel" : "user", - modehandlers[handler_id]->GetModeChar()); - } - else - { - user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Only operators may set %s mode %c", - user->nick.c_str(), - type == MODETYPE_CHANNEL ? "channel" : "user", - modehandlers[handler_id]->GetModeChar()); - } - continue; - } - - /* Call the handler for the mode */ - ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding, servermode); - - if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter.empty())) - { - /* The handler nuked the parameter and they are supposed to have one. - * We CANT continue now, even if they actually returned MODEACTION_ALLOW, - * so we bail to the next mode character. - */ - continue; - } - - if (ma == MODEACTION_ALLOW) - { - /* We're about to output a valid mode letter - was there previously a pending state-change? */ - if (state_change) - { - if (adding != last_successful_state_change) - output_sequence.append(adding ? "+" : "-"); - last_successful_state_change = adding; - } - - /* Add the mode letter */ - output_sequence.push_back(modechar); - - modehandlers[handler_id]->ChangeCount(adding ? 1 : -1); - - /* Is there a valid parameter for this mode? If so add it to the parameter list */ - if ((modehandlers[handler_id]->GetNumParams(adding)) && (!parameter.empty())) - { - parameter_list << " " << parameter; - parameter_count++; - /* Does this mode have a prefix? */ - if (modehandlers[handler_id]->GetPrefix() && targetchannel) - { - User* user_to_prefix = ServerInstance->FindNick(parameter); - if (user_to_prefix) - targetchannel->SetPrefix(user_to_prefix, modehandlers[handler_id]->GetPrefix(), - modehandlers[handler_id]->GetPrefixRank(), adding); - } - } - - /* Call all the AfterMode events in the mode watchers for this mode */ - for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) - (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type, servermode); - - /* Reset the state change flag */ - state_change = false; - - if ((output_sequence.length() + parameter_list.str().length() > 450) || (output_sequence.length() > 100) - || (parameter_count > ServerInstance->Config->Limits.MaxModes)) - { - /* We cant have a mode sequence this long */ - letter = mode_sequence.end() - 1; - continue; - } - } - } - } - else - { - /* No mode handler? Unknown mode character then. */ - user->WriteServ("%d %s %c :is unknown mode char to me", type == MODETYPE_CHANNEL ? 472 : 501, user->nick.c_str(), modechar); - } - break; - } + for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++) + { + unsigned char mletter = *letter; + if (mletter == '+') + continue; + + /* Ensure the user doesnt request the same mode twice, + * so they cant flood themselves off out of idiocy. + */ + if (sent[mletter] == seq) + continue; + + sent[mletter] = seq; + + ModeHandler *mh = this->FindMode(mletter, MODETYPE_CHANNEL); + + if (!mh || !mh->IsListMode()) + return; + + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mletter, "", true, 0)); + if (MOD_RESULT == MOD_RES_DENY) + continue; + + bool display = true; + if (!user->HasPrivPermission("channels/auspex") && ServerInstance->Config->HideModeLists[mletter] && (chan->GetPrefixValue(user) < HALFOP_VALUE)) + { + user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only half-operators and above may view the +%c list", + user->nick.c_str(), chan->name.c_str(), mletter); + display = false; } - /* Was there at least one valid mode in the sequence? */ - if (!output_sequence.empty()) + /** See below for a description of what craq this is :D + */ + unsigned char handler_id = (mletter - 'A') | MASK_CHANNEL; + + for(ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) { - if (servermode) - { - if (type == MODETYPE_CHANNEL) - { - targetchannel->WriteChannelWithServ(ServerInstance->Config->ServerName, "MODE %s %s%s", targetchannel->name.c_str(), output_sequence.c_str(), parameter_list.str().c_str()); - this->LastParse = targetchannel->name; - } - else - { - targetuser->WriteServ("MODE %s %s%s",targetuser->nick.c_str(),output_sequence.c_str(), parameter_list.str().c_str()); - this->LastParse = targetuser->nick; - } - } - else - { - if (type == MODETYPE_CHANNEL) - { - targetchannel->WriteChannel(user, "MODE %s %s%s", targetchannel->name.c_str(), output_sequence.c_str(), parameter_list.str().c_str()); - FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str())); - this->LastParse = targetchannel->name; - } - else - { - user->WriteTo(targetuser, "MODE %s %s%s", targetuser->nick.c_str(), output_sequence.c_str(), parameter_list.str().c_str()); - FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, output_sequence + parameter_list.str())); - this->LastParse = targetuser->nick; - } - } + std::string dummyparam; - LastParse.append(" "); - LastParse.append(output_sequence); - LastParse.append(parameter_list.str()); + if (!((*watchers)->BeforeMode(user, NULL, chan, dummyparam, true, MODETYPE_CHANNEL))) + display = false; } + if (display) + mh->DisplayList(user, chan); + else + mh->DisplayEmptyList(user, chan); } } @@ -931,9 +720,6 @@ std::string ModeParser::ChannelModeList() for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - if ((!ServerInstance->Config->AllowHalfop) && (mode == 'h')) - continue; - unsigned char pos = (mode-65) | MASK_CHANNEL; if (modehandlers[pos]) @@ -950,9 +736,6 @@ std::string ModeParser::ParaModeList() for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - if ((!ServerInstance->Config->AllowHalfop) && (mode == 'h')) - continue; - unsigned char pos = (mode-65) | MASK_CHANNEL; if ((modehandlers[pos]) && (modehandlers[pos]->GetNumParams(true))) @@ -1010,7 +793,7 @@ std::string ModeParser::ModeString(User* user, Channel* channel, bool nick_suffi return types; } -std::string ModeParser::ChanModes() +std::string ModeParser::GiveModeList(ModeMasks m) { std::string type1; /* Listmodes EXCEPT those with a prefix */ std::string type2; /* Modes that take a param when adding or removing */ @@ -1019,13 +802,10 @@ std::string ModeParser::ChanModes() for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - if ((!ServerInstance->Config->AllowHalfop) && (mode == 'h')) - continue; - - unsigned char pos = (mode-65) | MASK_CHANNEL; + unsigned char pos = (mode-65) | m; /* One parameter when adding */ if (modehandlers[pos]) - { + { if (modehandlers[pos]->GetNumParams(true)) { if ((modehandlers[pos]->IsListMode()) && (!modehandlers[pos]->GetPrefix())) @@ -1055,44 +835,32 @@ std::string ModeParser::ChanModes() type4 += modehandlers[pos]->GetModeChar(); } } - } return type1 + "," + type2 + "," + type3 + "," + type4; } -bool ModeParser::PrefixComparison(prefixtype one, prefixtype two) -{ - return one.second > two.second; -} - std::string ModeParser::BuildPrefixes() { std::string mletters; std::string mprefixes; - pfxcontainer pfx; - std::map prefix_to_mode; + std::map > prefixes; for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - if ((!ServerInstance->Config->AllowHalfop) && (mode == 'h')) - continue; - unsigned char pos = (mode-65) | MASK_CHANNEL; if ((modehandlers[pos]) && (modehandlers[pos]->GetPrefix())) { - pfx.push_back(std::make_pair(modehandlers[pos]->GetPrefix(), modehandlers[pos]->GetPrefixRank())); - prefix_to_mode[modehandlers[pos]->GetPrefix()] = modehandlers[pos]->GetModeChar(); + prefixes[modehandlers[pos]->GetPrefixRank()] = std::make_pair( + modehandlers[pos]->GetPrefix(), modehandlers[pos]->GetModeChar()); } } - sort(pfx.begin(), pfx.end(), ModeParser::PrefixComparison); - - for (pfxcontainer::iterator n = pfx.begin(); n != pfx.end(); n++) + for(std::map >::reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); n++) { - mletters = mletters + n->first; - mprefixes = mprefixes + prefix_to_mode.find(n->first)->second; + mletters = mletters + n->second.first; + mprefixes = mprefixes + n->second.second; } return "(" + mprefixes + ")" + mletters; @@ -1161,7 +929,7 @@ void ModeHandler::RemoveMode(User* user, irc::modestacker* stack) sprintf(moderemove,"-%c",this->GetModeChar()); parameters.push_back(user->nick); parameters.push_back(moderemove); - ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient, false); + ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient); } } } @@ -1190,27 +958,30 @@ void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack) } } -ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance) +ModeParser::ModeParser() { ModeHandler* modes[] = { - new ModeChannelSecret(Instance), - new ModeChannelPrivate(Instance), - new ModeChannelModerated(Instance), - new ModeChannelTopicOps(Instance), - new ModeChannelNoExternal(Instance), - new ModeChannelInviteOnly(Instance), - new ModeChannelKey(Instance), - new ModeChannelLimit(Instance), - new ModeChannelBan(Instance), - new ModeChannelOp(Instance), - new ModeChannelHalfOp(Instance), - new ModeChannelVoice(Instance), - new ModeUserWallops(Instance), - new ModeUserInvisible(Instance), - new ModeUserOperator(Instance), - new ModeUserServerNoticeMask(Instance), - NULL + new ModeChannelSecret, + new ModeChannelPrivate, + new ModeChannelModerated, + new ModeChannelTopicOps, + + new ModeChannelNoExternal, + new ModeChannelInviteOnly, + new ModeChannelKey, + new ModeChannelLimit, + + new ModeChannelBan, + new ModeChannelOp, + new ModeChannelHalfOp, + new ModeChannelVoice, + + new ModeUserWallops, + new ModeUserInvisible, + new ModeUserOperator, + new ModeUserServerNoticeMask, +#define BUILTIN_MODE_COUNT 16 }; /* Clear mode handler list */ @@ -1220,9 +991,25 @@ ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance) LastParse.clear(); /* Initialise the RFC mode letters */ - for (int index = 0; modes[index]; index++) + for (int index = 0; index < BUILTIN_MODE_COUNT; index++) this->AddMode(modes[index]); seq = 0; memset(&sent, 0, sizeof(sent)); } + +ModeParser::~ModeParser() +{ + int count = 0; + for(int i=0; i < 256; i++) + { + ModeHandler* mh = modehandlers[i]; + if (mh) + { + count++; + delete mh; + } + } + if (count != BUILTIN_MODE_COUNT) + throw CoreException("Mode handler found non-core modes remaining at deallocation"); +}