X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmode.cpp;h=bce2b20191493a115f6957470bbe4d1f71f06847;hb=58ecf39129ae6d1e5a15785c7bb8bf0fd5ee46b9;hp=34234586997320085ec5bf9362e6f0c2203695bf;hpb=a85a9b8c115762ce691e9cfb3c1175725a920c87;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/mode.cpp b/src/mode.cpp index 342345869..bce2b2019 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -149,6 +149,71 @@ bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string&, return (ours < theirs); } +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) +{ + /* Only opers can change other users modes */ + if (source != dest) + return MODEACTION_DENY; + + if (adding) + { + if (!dest->IsModeSet(this->GetModeChar())) + { + dest->SetMode(this->GetModeChar(),true); + return MODEACTION_ALLOW; + } + } + else + { + if (dest->IsModeSet(this->GetModeChar())) + { + dest->SetMode(this->GetModeChar(),false); + return MODEACTION_ALLOW; + } + } + + return MODEACTION_DENY; +} + + +ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool servermode) +{ + if (adding) + { + if (!channel->IsModeSet(this->GetModeChar())) + { + channel->SetMode(this->GetModeChar(),true); + return MODEACTION_ALLOW; + } + } + else + { + if (channel->IsModeSet(this->GetModeChar())) + { + channel->SetMode(this->GetModeChar(),false); + return MODEACTION_ALLOW; + } + } + + return MODEACTION_DENY; +} + ModeWatcher::ModeWatcher(InspIRCd* Instance, char modeletter, ModeType type) : ServerInstance(Instance), mode(modeletter), m_type(type) { } @@ -186,7 +251,7 @@ User* ModeParser::SanityChecks(User *user, const char *dest, Channel *chan, int) d = ServerInstance->FindNick(dest); if (!d) { - user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, dest); + user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), dest); return NULL; } return d; @@ -217,7 +282,7 @@ const char* ModeParser::Grant(User *d,Channel *chan,int MASK) n->first->AddVoicedUser(d); break; } - return d->nick; + return d->nick.c_str(); } return ""; } @@ -247,7 +312,7 @@ const char* ModeParser::Revoke(User *d,Channel *chan,int MASK) n->first->DelVoicedUser(d); break; } - return d->nick; + return d->nick.c_str(); } return ""; } @@ -257,39 +322,39 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ if (targetchannel) { /* Display channel's current mode string */ - user->WriteNumeric(324, "%s %s +%s",user->nick, targetchannel->name, targetchannel->ChanModes(targetchannel->HasUser(user))); - user->WriteNumeric(329, "%s %s %lu", user->nick, targetchannel->name, (unsigned long)targetchannel->age); + user->WriteNumeric(RPL_CHANNELMODEIS, "%s %s +%s",user->nick.c_str(), targetchannel->name.c_str(), targetchannel->ChanModes(targetchannel->HasUser(user))); + user->WriteNumeric(RPL_CHANNELCREATED, "%s %s %lu", user->nick.c_str(), targetchannel->name.c_str(), (unsigned long)targetchannel->age); return; } else if (targetuser) { if (targetuser->Visibility && !targetuser->Visibility->VisibleTo(user)) { - user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, text); + user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), text); return; } if ((targetuser == user) || (IS_OPER(user))) { /* Display user's current mode string */ - user->WriteNumeric(221, "%s :+%s",targetuser->nick,targetuser->FormatModes()); + user->WriteNumeric(RPL_UMODEIS, "%s :+%s",targetuser->nick.c_str(),targetuser->FormatModes()); if (IS_OPER(targetuser)) - user->WriteNumeric(8, "%s +%s :Server notice mask", targetuser->nick, targetuser->FormatNoticeMasks()); + user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks()); return; } else { - user->WriteNumeric(502, "%s :Can't change mode for other users", user->nick); + user->WriteNumeric(ERR_USERSDONTMATCH, "%s :Can't change mode for other users", user->nick.c_str()); return; } } /* No such nick/channel */ - user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, text); + user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), text); return; } -void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bool servermode) +void ModeParser::Process(const std::vector& parameters, User *user, bool servermode) { std::string target = parameters[0]; ModeType type = MODETYPE_USER; @@ -302,15 +367,14 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo /* Special case for displaying the list for listmodes, * e.g. MODE #chan b, or MODE #chan +b without a parameter */ - if ((targetchannel) && (pcnt == 2)) + if ((targetchannel) && (parameters.size() == 2)) { - const char* mode = parameters[1]; + const char* mode = parameters[1].c_str(); int nonlistmodes_found = 0; - bool sent[256]; - mask = MASK_CHANNEL; + seq++; - memset(&sent, 0, 256); + mask = MASK_CHANNEL; while (mode && *mode) { @@ -325,9 +389,9 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo /* Ensure the user doesnt request the same mode twice, * so they cant flood themselves off out of idiocy. */ - if (!sent[mletter]) + if (sent[mletter] != seq) { - sent[mletter] = true; + sent[mletter] = seq; } else { @@ -348,11 +412,14 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo continue; } - if (ServerInstance->Config->HideModeLists[mletter] && (targetchannel->GetStatus(user) < STATUS_HOP)) + if (!IS_OPER(user)) { - user->WriteNumeric(482, "%s %s :Only half-operators and above may view the +%c list",user->nick, targetchannel->name, *mode++); - mh->DisplayEmptyList(user, targetchannel); - continue; + 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; + } } /** See below for a description of what craq this is :D @@ -381,11 +448,11 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo return; } - if (pcnt == 1) + if (parameters.size() == 1) { - this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]); + this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0].c_str()); } - else if (pcnt > 1) + else if (parameters.size() > 1) { bool SkipAccessChecks = false; @@ -414,14 +481,14 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo mask = MASK_USER; if ((user != targetuser) && (!ServerInstance->ULine(user->server))) { - user->WriteNumeric(502, "%s :Can't change mode for other users", user->nick); + user->WriteNumeric(ERR_USERSDONTMATCH, "%s :Can't change mode for other users", user->nick.c_str()); return; } } else { /* No such nick/channel */ - user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, parameters[0]); + user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str()); return; } @@ -431,8 +498,8 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo std::string output_sequence; bool adding = true, state_change = false; unsigned char handler_id = 0; - int parameter_counter = 2; /* Index of first parameter */ - int parameter_count = 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) */ @@ -495,7 +562,7 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo 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 < pcnt) + if (parameter_counter < parameters.size()) { parameter = parameters[parameter_counter++]; @@ -547,8 +614,8 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo if (!ourmode || ourmode->GetPrefixRank() < neededrank) { /* Bog off */ - user->WriteNumeric(482, "%s %s :You must have channel privilege %c or above to %sset channel mode %c", - user->nick, targetchannel->name, needed, adding ? "" : "un", modechar); + 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; } } @@ -574,25 +641,34 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo 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 ((IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!user->HasModePermission(modehandlers[handler_id]->GetModeChar(), type))) + if (adding && (IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!user->HasModePermission(modehandlers[handler_id]->GetModeChar(), type))) { if (IS_OPER(user)) { - user->WriteNumeric(481, "%s :Permission Denied - Oper type %s does not have access to %sset %s mode %c", - user->nick, - user->oper, - adding ? "" : "un", + 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(481, "%s :Permission Denied - Only operators may %sset %s mode %c", - user->nick, - adding ? "" : "un", + 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()); } @@ -649,7 +725,7 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo state_change = false; if ((output_sequence.length() + parameter_list.str().length() > 450) || (output_sequence.length() > 100) - || (parameter_count > MAXMODES)) + || (parameter_count > ServerInstance->Config->Limits.MaxModes)) { /* We cant have a mode sequence this long */ letter = mode_sequence.end() - 1; @@ -661,7 +737,7 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo 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, modechar); + user->WriteServ("%d %s %c :is unknown mode char to me", type == MODETYPE_CHANNEL ? 472 : 501, user->nick.c_str(), modechar); } break; } @@ -674,12 +750,12 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo { if (type == MODETYPE_CHANNEL) { - targetchannel->WriteChannelWithServ(ServerInstance->Config->ServerName, "MODE %s %s%s", targetchannel->name, output_sequence.c_str(), parameter_list.str().c_str()); + 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,output_sequence.c_str(), parameter_list.str().c_str()); + targetuser->WriteServ("MODE %s %s%s",targetuser->nick.c_str(),output_sequence.c_str(), parameter_list.str().c_str()); this->LastParse = targetuser->nick; } } @@ -687,13 +763,13 @@ void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bo { if (type == MODETYPE_CHANNEL) { - targetchannel->WriteChannel(user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str()); + 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,output_sequence.c_str(), parameter_list.str().c_str()); + 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; } @@ -718,6 +794,9 @@ void ModeParser::CleanMask(std::string &mask) std::string::size_type pos_of_dot = mask.find_first_of('.'); std::string::size_type pos_of_colons = mask.find("::"); /* Because ipv6 addresses are colon delimited -- double so it treats extban as nick */ + if (mask.length() >= 2 && mask[1] == ':') + return; // if it's an extban, don't even try guess how it needs to be formed. + if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos)) { /* Just a nick, or just a host - or clearly ipv6 (starting with :) */ @@ -1065,7 +1144,7 @@ bool ModeParser::DelModeWatcher(ModeWatcher* mw) void ModeHandler::RemoveMode(User* user, irc::modestacker* stack) { char moderemove[MAXBUF]; - const char* parameters[] = { user->nick, moderemove }; + std::vector parameters; if (user->IsModeSet(this->GetModeChar())) { @@ -1076,7 +1155,9 @@ void ModeHandler::RemoveMode(User* user, irc::modestacker* stack) else { sprintf(moderemove,"-%c",this->GetModeChar()); - ServerInstance->Parser->CallHandler("MODE", parameters, 2, user); + parameters.push_back(user->nick); + parameters.push_back(moderemove); + ServerInstance->Parser->CallHandler("MODE", parameters, user); } } } @@ -1087,7 +1168,7 @@ void ModeHandler::RemoveMode(User* user, irc::modestacker* stack) void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack) { char moderemove[MAXBUF]; - const char* parameters[] = { channel->name, moderemove }; + std::vector parameters; if (channel->IsModeSet(this->GetModeChar())) { @@ -1098,7 +1179,9 @@ void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack) else { sprintf(moderemove,"-%c",this->GetModeChar()); - ServerInstance->SendMode(parameters, 2, ServerInstance->FakeClient); + parameters.push_back(channel->name); + parameters.push_back(moderemove); + ServerInstance->SendMode(parameters, ServerInstance->FakeClient); } } } @@ -1127,9 +1210,8 @@ ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance) NULL }; - /* Clear mode list */ + /* Clear mode handler list */ memset(modehandlers, 0, sizeof(modehandlers)); - memset(modewatchers, 0, sizeof(modewatchers)); /* Last parse string */ LastParse.clear(); @@ -1137,4 +1219,7 @@ ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance) /* Initialise the RFC mode letters */ for (int index = 0; modes[index]; index++) this->AddMode(modes[index]); + + seq = 0; + memset(&sent, 0, 256); }