X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmode.cpp;h=3406393cf1bfcfb914b2ea8b6bc71a28d86d852f;hb=3593e33ce374caee9aec0575b49ec9b0001a8ec6;hp=fcec6096f6854d17ac0bd8a707db2c20e7141184;hpb=576399edeb653fcbe17202823ab9bd2a92406ff8;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/mode.cpp b/src/mode.cpp index fcec6096f..3406393cf 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -11,11 +11,13 @@ * --------------------------------------------------- */ +/* $Core: libIRCDmode */ +/* $ExtraDeps: $(RELCPPFILES) */ +/* $ExtraObjects: modes/modeclasses.a */ +/* $ExtraBuild: ${MAKE} -C "modes" DIRNAME="src/modes" CC="$(CC)" $(MAKEARGS) CPPFILES="$(CPPFILES)" */ + #include "inspircd.h" -#include "users.h" -#include "modules.h" #include "inspstring.h" -#include "mode.h" /* +s (secret) */ #include "modes/cmode_s.h" @@ -79,6 +81,7 @@ unsigned int ModeHandler::GetCount() void ModeHandler::ChangeCount(int modifier) { count += modifier; + ServerInstance->Log(DEBUG,"Change count for mode %c is now %d", mode, count); } ModeType ModeHandler::GetModeType() @@ -106,12 +109,12 @@ char ModeHandler::GetModeChar() return mode; } -ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding) +ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool) { return MODEACTION_DENY; } -ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter) +ModePair ModeHandler::ModeSet(User*, User* dest, Channel* channel, const std::string&) { if (dest) { @@ -123,11 +126,15 @@ ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel, } } -void ModeHandler::DisplayList(userrec* user, chanrec* channel) +void ModeHandler::DisplayList(User*, Channel*) +{ +} + +void ModeHandler::DisplayEmptyList(User*, Channel*) { } -bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel) +bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string&, const std::string&, Channel*) { return (ours < theirs); } @@ -150,18 +157,18 @@ ModeType ModeWatcher::GetModeType() return m_type; } -bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type) +bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool, ModeType) { return true; } -void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type) +void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool, ModeType) { } -userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status) +User* ModeParser::SanityChecks(User *user, const char *dest, Channel *chan, int) { - userrec *d; + User *d; if ((!user) || (!dest) || (!chan) || (!*dest)) { return NULL; @@ -175,7 +182,7 @@ userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,i return d; } -const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK) +const char* ModeParser::Grant(User *d,Channel *chan,int MASK) { if (!chan) return ""; @@ -185,7 +192,6 @@ const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK) { if (n->second & MASK) { - ServerInstance->Log(DEBUG,"User already has privilage %d (privset: %d)", MASK, n->second); return ""; } n->second = n->second | MASK; @@ -201,17 +207,12 @@ const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK) n->first->AddVoicedUser(d); break; } - ServerInstance->Log(DEBUG,"grant: %s %s",n->first->name,d->nick); return d->nick; } - else - { - ServerInstance->Log(DEBUG,"Channel %s not in users joined list", chan->name); - } return ""; } -const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK) +const char* ModeParser::Revoke(User *d,Channel *chan,int MASK) { if (!chan) return ""; @@ -236,13 +237,12 @@ const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK) n->first->DelVoicedUser(d); break; } - ServerInstance->Log(DEBUG,"revoke: %s %s",n->first->name,d->nick); return d->nick; } return ""; } -void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text) +void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text) { if (targetchannel) { @@ -253,11 +253,17 @@ void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec } else if (targetuser) { - if ((targetuser == user) || (*user->oper)) + if (targetuser->Visibility && !targetuser->Visibility->VisibleTo(user)) + { + user->WriteServ("401 %s %s :No such nick/channel",user->nick, text); + return; + } + + if ((targetuser == user) || (IS_OPER(user))) { /* Display user's current mode string */ user->WriteServ("221 %s :+%s",targetuser->nick,targetuser->FormatModes()); - if (*targetuser->oper) + if (IS_OPER(targetuser)) user->WriteServ("008 %s +%s :Server notice mask", targetuser->nick, targetuser->FormatNoticeMasks()); return; } @@ -273,40 +279,64 @@ void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec return; } -void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool servermode) +void ModeParser::Process(const char** parameters, int pcnt, User *user, bool servermode) { std::string target = parameters[0]; ModeType type = MODETYPE_USER; unsigned char mask = 0; - chanrec* targetchannel = ServerInstance->FindChan(parameters[0]); - userrec* targetuser = ServerInstance->FindNick(parameters[0]); - - ServerInstance->Log(DEBUG,"ModeParser::Process start: pcnt=%d",pcnt); + Channel* targetchannel = ServerInstance->FindChan(parameters[0]); + User* targetuser = ServerInstance->FindNick(parameters[0]); - LastParse = ""; + LastParse.clear(); /* Special case for displaying the list for listmodes, * e.g. MODE #chan b, or MODE #chan +b without a parameter */ if ((targetchannel) && (pcnt == 2)) { - ServerInstance->Log(DEBUG,"Spool list"); const char* mode = parameters[1]; + int nonlistmodes_found = 0; + bool sent[256]; mask = MASK_CHANNEL; + + memset(&sent, 0, 256); while (mode && *mode) { + unsigned char mletter = *mode; + if (*mode == '+') { mode++; continue; } + /* Ensure the user doesnt request the same mode twice, + * so they cant flood themselves off out of idiocy. + */ + if (!sent[mletter]) + { + sent[mletter] = true; + } + else + { + mode++; + continue; + } + ModeHandler *mh = this->FindMode(*mode, MODETYPE_CHANNEL); + bool display = true; if ((mh) && (mh->IsListMode())) { + if (ServerInstance->Config->HideModeLists[mletter] && (targetchannel->GetStatus(user) < STATUS_HOP)) + { + user->WriteServ("482 %s %s :Only half-operators and above may view the +%c list",user->nick, targetchannel->name, *mode++); + mh->DisplayEmptyList(user, targetchannel); + continue; + } + /** See below for a description of what craq this is :D */ unsigned char handler_id = (*mode - 65) | mask; @@ -315,28 +345,30 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool { std::string dummyparam; - if((*watchers)->BeforeMode(user, NULL, targetchannel, dummyparam, true, MODETYPE_CHANNEL) == MODEACTION_ALLOW) - { - mh->DisplayList(user, targetchannel); - } + if (!((*watchers)->BeforeMode(user, NULL, targetchannel, dummyparam, true, MODETYPE_CHANNEL))) + display = false; } + + if (display) + mh->DisplayList(user, targetchannel); } + else + nonlistmodes_found++; mode++; } - - return; + + /* We didnt have any modes that were non-list, we can return here */ + if (!nonlistmodes_found) + return; } if (pcnt == 1) { - ServerInstance->Log(DEBUG,"Mode list request"); this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]); } else if (pcnt > 1) { - ServerInstance->Log(DEBUG,"More than one parameter"); - if (targetchannel) { type = MODETYPE_CHANNEL; @@ -349,8 +381,6 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool if ((IS_LOCAL(user)) && (targetchannel->GetStatus(user) < STATUS_HOP)) { /* We don't have halfop */ - ServerInstance->Log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes"); - int MOD_RESULT = 0; FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, targetchannel, AC_GENERAL_MODE)); if (MOD_RESULT == ACR_DENY) @@ -365,7 +395,8 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool * NOT a uline and NOT a servermode, * OR, NOT halfop or above. */ - user->WriteServ("482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name); + user->WriteServ("482 %s %s :You're not a channel %soperator",user->nick, targetchannel->name, + ServerInstance->Config->AllowHalfop ? "(half)" : ""); return; } } @@ -389,9 +420,9 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool } std::string mode_sequence = parameters[1]; - std::string parameter = ""; + std::string parameter; std::ostringstream parameter_list; - std::string output_sequence = ""; + std::string output_sequence; bool adding = true, state_change = false; unsigned char handler_id = 0; int parameter_counter = 2; /* Index of first parameter */ @@ -458,12 +489,11 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool /* This mode expects a parameter, do we have any parameters left in our list to use? */ if (parameter_counter < pcnt) { - ServerInstance->Log(DEBUG,"parameter_counter = %d, pcnt = %d", parameter_counter, pcnt); parameter = parameters[parameter_counter++]; /* Yerk, invalid! */ if ((parameter.find(':') == 0) || (parameter.rfind(' ') != std::string::npos)) - parameter = ""; + parameter.clear(); } else { @@ -475,7 +505,7 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) { - if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY) + if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == false) { abort = true; break; @@ -491,13 +521,28 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool if (abort) continue; } + else + { + /* Fix by brain: mode watchers not being called for parameterless modes */ + for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++) + { + if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == false) + { + abort = true; + break; + } + } + + if (abort) + 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->oper)) + if ((IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!IS_OPER(user))) { - user->WriteServ("481 %s :Permission Denied- Only IRC operators may %sset %s mode %c", user->nick, + user->WriteServ("481 %s :Permission Denied - Only IRC operators may %sset %s mode %c", user->nick, adding ? "" : "un", type == MODETYPE_CHANNEL ? "channel" : "user", modehandlers[handler_id]->GetModeChar()); continue; @@ -506,7 +551,7 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool /* Call the handler for the mode */ ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding); - if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == "")) + 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, @@ -528,15 +573,17 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool /* 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 != "")) + 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) { - userrec* user_to_prefix = ServerInstance->FindNick(parameter); + 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); @@ -563,13 +610,14 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool else { /* No mode handler? Unknown mode character then. */ - user->WriteServ("472 %s %c :is unknown mode char to me",user->nick, modechar); + user->WriteServ("%d %s %c :is unknown mode char to me", type == MODETYPE_CHANNEL ? 472 : 501, user->nick, modechar); } break; } } + /* Was there at least one valid mode in the sequence? */ - if (output_sequence != "") + if (!output_sequence.empty()) { if (servermode) { @@ -588,7 +636,6 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool { if (type == MODETYPE_CHANNEL) { - ServerInstance->Log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str()); targetchannel->WriteChannel(user,"MODE %s %s%s",targetchannel->name,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; @@ -646,7 +693,7 @@ void ModeParser::CleanMask(std::string &mask) } } -bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter) +bool ModeParser::AddMode(ModeHandler* mh) { unsigned char mask = 0; unsigned char pos = 0; @@ -655,7 +702,7 @@ bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter) * If they do that, thats their problem, and if i ever EVER see an * official InspIRCd developer do that, i'll beat them with a paddle! */ - if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z')) + if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z') || (mh->GetPrefix() > 126)) return false; /* A mode prefix of ',' is not acceptable, it would fuck up server to server. @@ -672,7 +719,6 @@ bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter) return false; modehandlers[pos] = mh; - ServerInstance->Log(DEBUG,"ModeParser::AddMode: added mode %c",mh->GetModeChar()); return true; } @@ -748,6 +794,9 @@ 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]) @@ -764,6 +813,9 @@ 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))) @@ -787,7 +839,7 @@ ModeHandler* ModeParser::FindPrefix(unsigned const char pfxletter) return NULL; } -std::string ModeParser::ModeString(userrec* user, chanrec* channel) +std::string ModeParser::ModeString(User* user, Channel* channel) { std::string types; std::string pars; @@ -824,6 +876,9 @@ 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; /* One parameter when adding */ if (modehandlers[pos]) @@ -863,20 +918,23 @@ std::string ModeParser::ChanModes() return type1 + "," + type2 + "," + type3 + "," + type4; } -bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two) +bool ModeParser::PrefixComparison(prefixtype one, prefixtype two) { return one.second > two.second; } std::string ModeParser::BuildPrefixes() { - std::string mletters = ""; - std::string mprefixes = ""; + std::string mletters; + std::string mprefixes; pfxcontainer pfx; std::map prefix_to_mode; 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())) @@ -912,7 +970,6 @@ bool ModeParser::AddModeWatcher(ModeWatcher* mw) pos = (mw->GetModeChar()-65) | mask; modewatchers[pos].push_back(mw); - ServerInstance->Log(DEBUG,"ModeParser::AddModeWatcher: watching mode %c",mw->GetModeChar()); return true; } @@ -935,19 +992,17 @@ bool ModeParser::DelModeWatcher(ModeWatcher* mw) if (a == modewatchers[pos].end()) { - ServerInstance->Log(DEBUG, "ModeParser::DelModeWatcher: Couldn't find watcher for mode %c in list", mw->GetModeChar()); return false; } modewatchers[pos].erase(a); - ServerInstance->Log(DEBUG,"ModeParser::DelModeWatcher: stopped watching mode %c",mw->GetModeChar()); return true; } /** This default implementation can remove simple user modes */ -void ModeHandler::RemoveMode(userrec* user) +void ModeHandler::RemoveMode(User* user) { char moderemove[MAXBUF]; const char* parameters[] = { user->nick, moderemove }; @@ -962,57 +1017,50 @@ void ModeHandler::RemoveMode(userrec* user) /** This default implementation can remove simple channel modes * (no parameters) */ -void ModeHandler::RemoveMode(chanrec* channel) +void ModeHandler::RemoveMode(Channel* channel) { char moderemove[MAXBUF]; const char* parameters[] = { channel->name, moderemove }; if (channel->IsModeSet(this->GetModeChar())) { - userrec* n = new userrec(ServerInstance); - sprintf(moderemove,"-%c",this->GetModeChar()); - n->SetFd(FD_MAGIC_NUMBER); - - ServerInstance->SendMode(parameters, 2, n); - - delete n; + ServerInstance->SendMode(parameters, 2, ServerInstance->FakeClient); } } ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance) { + 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 ModeUserServerNotice(Instance), + new ModeUserWallops(Instance), + new ModeUserInvisible(Instance), + new ModeUserOperator(Instance), + new ModeUserServerNoticeMask(Instance), + NULL + }; + /* Clear mode list */ memset(modehandlers, 0, sizeof(modehandlers)); memset(modewatchers, 0, sizeof(modewatchers)); /* Last parse string */ - LastParse = ""; + LastParse.clear(); /* Initialise the RFC mode letters */ - - /* Start with channel simple modes, no params */ - this->AddMode(new ModeChannelSecret(Instance), 's'); - this->AddMode(new ModeChannelPrivate(Instance), 'p'); - this->AddMode(new ModeChannelModerated(Instance), 'm'); - this->AddMode(new ModeChannelTopicOps(Instance), 't'); - this->AddMode(new ModeChannelNoExternal(Instance), 'n'); - this->AddMode(new ModeChannelInviteOnly(Instance), 'i'); - - /* Cannel modes with params */ - this->AddMode(new ModeChannelKey(Instance), 'k'); - this->AddMode(new ModeChannelLimit(Instance), 'l'); - - /* Channel listmodes */ - this->AddMode(new ModeChannelBan(Instance), 'b'); - this->AddMode(new ModeChannelOp(Instance), 'o'); - this->AddMode(new ModeChannelHalfOp(Instance), 'h'); - this->AddMode(new ModeChannelVoice(Instance), 'v'); - - /* Now for usermodes */ - this->AddMode(new ModeUserServerNotice(Instance), 's'); - this->AddMode(new ModeUserWallops(Instance), 'w'); - this->AddMode(new ModeUserInvisible(Instance), 'i'); - this->AddMode(new ModeUserOperator(Instance), 'o'); - this->AddMode(new ModeUserServerNoticeMask(Instance), 'n'); + for (int index = 0; modes[index]; index++) + this->AddMode(modes[index]); }