]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_channel/core_channel.cpp
Fix some confusing logic in sanick.
[user/henk/code/inspircd.git] / src / coremods / core_channel / core_channel.cpp
index ccf4d1a6e1c64695b1ecf1a632054066036bbed5..db17925b0f8432db33d397b598984740a32998ac 100644 (file)
@@ -1,7 +1,11 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
- *   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
+ *   Copyright (C) 2017-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2014-2015, 2018 Attila Molnar <attilamolnar@hush.com>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
 #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<const ClientProtocol::Events::Join&>(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 +99,8 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
        CommandKick cmdkick;
        CommandNames cmdnames;
        CommandTopic cmdtopic;
+       Events::ModuleEventProvider evprov;
+       JoinHook joinhook;
 
        ModeChannelBan banmode;
        SimpleChannelModeHandler inviteonlymode;
@@ -55,13 +126,15 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
 
  public:
        CoreModChannel()
-               : CheckExemption::EventListener(this)
+               : CheckExemption::EventListener(this, UINT_MAX)
                , invapi(this)
                , cmdinvite(this, invapi)
                , cmdjoin(this)
                , cmdkick(this)
                , cmdnames(this)
                , cmdtopic(this)
+               , evprov(this, "event/channel")
+               , joinhook(this)
                , banmode(this)
                , inviteonlymode(this, "inviteonly", 'i')
                , keymode(this)
@@ -79,14 +152,6 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
        void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
                ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
-               Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
-               if (optionstag->getBool("invitebypassmodes", true))
-                       ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
-               else
-               {
-                       for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
-                               ServerInstance->Modules.Detach(events[i], this);
-               }
 
                std::string current;
                irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
@@ -103,51 +168,65 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
                        ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
                        exempts[restriction] = prefix;
                }
-               exemptions.swap(exempts);
 
                ConfigTag* securitytag = ServerInstance->Config->ConfValue("security");
-               const std::string announceinvites = securitytag->getString("announceinvites", "dynamic");
+               const std::string announceinvites = securitytag->getString("announceinvites", "dynamic", 1);
+               Invite::AnnounceState newannouncestate;
                if (stdalgo::string::equalsci(announceinvites, "none"))
-                       cmdinvite.announceinvites = Invite::ANNOUNCE_NONE;
+                       newannouncestate = Invite::ANNOUNCE_NONE;
                else if (stdalgo::string::equalsci(announceinvites, "all"))
-                       cmdinvite.announceinvites = Invite::ANNOUNCE_ALL;
+                       newannouncestate = Invite::ANNOUNCE_ALL;
                else if (stdalgo::string::equalsci(announceinvites, "ops"))
-                       cmdinvite.announceinvites = Invite::ANNOUNCE_OPS;
+                       newannouncestate = Invite::ANNOUNCE_OPS;
                else if (stdalgo::string::equalsci(announceinvites, "dynamic"))
-                       cmdinvite.announceinvites = Invite::ANNOUNCE_DYNAMIC;
+                       newannouncestate = Invite::ANNOUNCE_DYNAMIC;
                else
                        throw ModuleException(announceinvites + " is an invalid <security:announceinvites> value, at " + securitytag->getTagLocation());
 
+               // Config is valid, apply it
+
+               // Validates and applies <maxlist> tags, so do it first
+               banmode.DoRehash();
+
+               exemptions.swap(exempts);
                // 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;
+               limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1;;
+               cmdinvite.announceinvites = newannouncestate;
+               joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser");
 
-               banmode.DoRehash();
+               Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
+               if (optionstag->getBool("invitebypassmodes", true))
+                       ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
+               else
+               {
+                       for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
+                               ServerInstance->Modules.Detach(events[i], this);
+               }
        }
 
        void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
        {
                tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);
 
-               // Build a map of limits to their mode character.
-               insp::flat_map<int, std::string> limits;
+               std::vector<std::string> limits;
+               std::string vlist;
                const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
                for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
                {
-                       const unsigned int limit = (*iter)->GetLowerLimit();
-                       limits[limit].push_back((*iter)->GetModeChar());
+                       ListModeBase* lm = *iter;
+                       limits.push_back(InspIRCd::Format("%c:%u", lm->GetModeChar(), lm->GetLowerLimit()));
+                       if (lm->HasVariableLength())
+                               vlist.push_back(lm->GetModeChar());
                }
 
-               // Generate the MAXLIST token from the limits map.
-               std::string& buffer = tokens["MAXLIST"];
-               for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
-               {
-                       if (!buffer.empty())
-                               buffer.push_back(',');
+               std::sort(limits.begin(), limits.end());
+               tokens["MAXLIST"] = stdalgo::string::join(limits, ',');
 
-                       buffer.append(iter->second);
-                       buffer.push_back(':');
-                       buffer.append(ConvToStr(iter->first));
+               if (!vlist.empty())
+               {
+                       tokens["VBANLIST"]; // deprecated
+                       tokens["VLIST"] = vlist;
                }
        }
 
@@ -165,7 +244,7 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
                        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)");
+                               user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (incorrect channel key)");
                                return MOD_RES_DENY;
                        }
                }
@@ -177,7 +256,7 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
                        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)");
+                               user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (invite only)");
                                return MOD_RES_DENY;
                        }
                }
@@ -189,7 +268,7 @@ class CoreModChannel : public Module, public CheckExemption::EventListener
                        FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
                        if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
                        {
-                               user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (Channel is full)");
+                               user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (channel is full)");
                                return MOD_RES_DENY;
                        }
                }