X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fcoremods%2Fcore_channel%2Fcore_channel.cpp;h=05bf113edc14a6b11e61ec8538de474c03ccc962;hb=28eeee38bd912445f376d0261580173889c2137a;hp=3af809645e8fbb4fa8110d4b4aa218ae18a0c27b;hpb=ff3b706b2506d7614bce5e54bc88657bd62ebd4d;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/coremods/core_channel/core_channel.cpp b/src/coremods/core_channel/core_channel.cpp index 3af809645..05bf113ed 100644 --- a/src/coremods/core_channel/core_channel.cpp +++ b/src/coremods/core_channel/core_channel.cpp @@ -22,6 +22,71 @@ #include "invite.h" #include "listmode.h" +namespace +{ +/** Hook that sends a MODE after a JOIN if the user in the JOIN has some modes prefix set. + * This happens e.g. when modules such as operprefix explicitly set prefix modes on the joining + * user, or when a member with prefix modes does a host cycle. + */ +class JoinHook : public ClientProtocol::EventHook +{ + ClientProtocol::Messages::Mode modemsg; + Modes::ChangeList modechangelist; + const User* joininguser; + + public: + /** If true, MODE changes after JOIN will be sourced from the user, rather than the server + */ + bool modefromuser; + + JoinHook(Module* mod) + : ClientProtocol::EventHook(mod, "JOIN") + { + } + + void OnEventInit(const ClientProtocol::Event& ev) CXX11_OVERRIDE + { + const ClientProtocol::Events::Join& join = static_cast(ev); + const Membership& memb = *join.GetMember(); + + modechangelist.clear(); + for (std::string::const_iterator i = memb.modes.begin(); i != memb.modes.end(); ++i) + { + PrefixMode* const pm = ServerInstance->Modes.FindPrefixMode(*i); + if (!pm) + continue; // Shouldn't happen + modechangelist.push_add(pm, memb.user->nick); + } + + if (modechangelist.empty()) + { + // Member got no modes on join + joininguser = NULL; + return; + } + + joininguser = memb.user; + + // Prepare a mode protocol event that we can append to the message list in OnPreEventSend() + modemsg.SetParams(memb.chan, NULL, modechangelist); + if (modefromuser) + modemsg.SetSource(join); + else + modemsg.SetSourceUser(ServerInstance->FakeClient); + } + + ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE + { + // If joininguser is NULL then they didn't get any modes on join, skip. + // Also don't show their own modes to them, they get that in the NAMES list not via MODE. + if ((joininguser) && (user != joininguser)) + messagelist.push_back(&modemsg); + return MOD_RES_PASSTHRU; + } +}; + +} + class CoreModChannel : public Module, public CheckExemption::EventListener { Invite::APIImpl invapi; @@ -30,6 +95,21 @@ class CoreModChannel : public Module, public CheckExemption::EventListener CommandKick cmdkick; CommandNames cmdnames; CommandTopic cmdtopic; + Events::ModuleEventProvider evprov; + JoinHook joinhook; + + ModeChannelBan banmode; + SimpleChannelModeHandler inviteonlymode; + ModeChannelKey keymode; + ModeChannelLimit limitmode; + SimpleChannelModeHandler moderatedmode; + SimpleChannelModeHandler noextmsgmode; + ModeChannelOp opmode; + SimpleChannelModeHandler privatemode; + SimpleChannelModeHandler secretmode; + SimpleChannelModeHandler topiclockmode; + ModeChannelVoice voicemode; + insp::flat_map exemptions; ModResult IsInvited(User* user, Channel* chan) @@ -49,6 +129,19 @@ class CoreModChannel : public Module, public CheckExemption::EventListener , cmdkick(this) , cmdnames(this) , cmdtopic(this) + , evprov(this, "event/channel") + , joinhook(this) + , banmode(this) + , inviteonlymode(this, "inviteonly", 'i') + , keymode(this) + , limitmode(this) + , moderatedmode(this, "moderated", 'm') + , noextmsgmode(this, "noextmsg", 'n') + , opmode(this) + , privatemode(this, "private", 'p') + , secretmode(this, "secret", 's') + , topiclockmode(this, "topiclock", 't') + , voicemode(this) { } @@ -64,6 +157,8 @@ class CoreModChannel : public Module, public CheckExemption::EventListener ServerInstance->Modules.Detach(events[i], this); } + joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser"); + std::string current; irc::spacesepstream defaultstream(optionstag->getString("exemptchanops")); insp::flat_map exempts; @@ -80,10 +175,31 @@ class CoreModChannel : public Module, public CheckExemption::EventListener exempts[restriction] = prefix; } exemptions.swap(exempts); + + ConfigTag* securitytag = ServerInstance->Config->ConfValue("security"); + const std::string announceinvites = securitytag->getString("announceinvites", "dynamic"); + if (stdalgo::string::equalsci(announceinvites, "none")) + cmdinvite.announceinvites = Invite::ANNOUNCE_NONE; + else if (stdalgo::string::equalsci(announceinvites, "all")) + cmdinvite.announceinvites = Invite::ANNOUNCE_ALL; + else if (stdalgo::string::equalsci(announceinvites, "ops")) + cmdinvite.announceinvites = Invite::ANNOUNCE_OPS; + else if (stdalgo::string::equalsci(announceinvites, "dynamic")) + cmdinvite.announceinvites = Invite::ANNOUNCE_DYNAMIC; + else + throw ModuleException(announceinvites + " is an invalid value, at " + securitytag->getTagLocation()); + + // In 2.0 we allowed limits of 0 to be set. This is non-standard behaviour + // and will be removed in the next major release. + limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1; + + banmode.DoRehash(); } void On005Numeric(std::map& tokens) CXX11_OVERRIDE { + tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen); + // Build a map of limits to their mode character. insp::flat_map limits; const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes(); @@ -100,12 +216,62 @@ class CoreModChannel : public Module, public CheckExemption::EventListener if (!buffer.empty()) buffer.push_back(','); - buffer.append(iter->second); + std::string modes(iter->second); + std::sort(modes.begin(), modes.end()); + + buffer.append(modes); buffer.push_back(':'); buffer.append(ConvToStr(iter->first)); } } + ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE + { + if (!chan) + return MOD_RES_PASSTHRU; + + // Check whether the channel key is correct. + const std::string ckey = chan->GetModeParameter(&keymode); + if (!ckey.empty()) + { + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven)); + if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven))) + { + // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled) + user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (Incorrect channel key)"); + return MOD_RES_DENY; + } + } + + // Check whether the invite only mode is set. + if (chan->IsModeSet(inviteonlymode)) + { + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan)); + if (MOD_RESULT != MOD_RES_ALLOW) + { + user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (Invite only)"); + return MOD_RES_DENY; + } + } + + // Check whether the limit would be exceeded by this user joining. + if (chan->IsModeSet(limitmode)) + { + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan)); + if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast(limitmode.ext.get(chan)))) + { + user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (Channel is full)"); + return MOD_RES_DENY; + } + } + + // Everything looks okay. + return MOD_RES_PASSTHRU; + } + void OnPostJoin(Membership* memb) CXX11_OVERRIDE { Channel* const chan = memb->chan; @@ -115,7 +281,7 @@ class CoreModChannel : public Module, public CheckExemption::EventListener // Remove existing invite, if any invapi.Remove(localuser, chan); - if (chan->topicset) + if (chan->topic.length()) Topic::ShowTopic(localuser, chan); // Show all members of the channel, including invisible (+i) users @@ -177,6 +343,7 @@ class CoreModChannel : public Module, public CheckExemption::EventListener void Prioritize() CXX11_OVERRIDE { ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST); + ServerInstance->Modules.SetPriority(this, I_OnUserPreJoin, PRIORITY_LAST); } Version GetVersion() CXX11_OVERRIDE