]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/listmode.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / listmode.cpp
index f45f1624f57da8b06b17411013a0d230adb75988..f3b2d5fc57937e93c339c3a077b6fb2aec7219cf 100644 (file)
@@ -1,7 +1,10 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
- *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2018 B00mX0r <b00mx0r@aureus.pw>
+ *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2013-2014, 2016 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 "inspircd.h"
 #include "listmode.h"
 
-ListModeBase::ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string &eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy, const std::string &ctag)
-       : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL),
-       listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
-       configtag(ctag), extItem("listbase_mode_" + name + "_list", Creator)
+ListModeBase::ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string& eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy)
+       : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_LIST)
+       , listnumeric(lnum)
+       , endoflistnumeric(eolnum)
+       , endofliststring(eolstr)
+       , tidy(autotidy)
+       , extItem(name + "_mode_list", ExtensionItem::EXT_CHANNEL, Creator)
 {
        list = true;
 }
 
 void ListModeBase::DisplayList(User* user, Channel* channel)
 {
-       ModeList* el = extItem.get(channel);
-       if (el)
+       ChanData* cd = extItem.get(channel);
+       if (cd)
        {
-               for (ModeList::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
+               for (ModeList::const_iterator it = cd->list.begin(); it != cd->list.end(); ++it)
                {
-                       user->WriteNumeric(listnumeric, "%s %s %s %s %lu", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), (!it->setter.empty() ? it->setter.c_str() : ServerInstance->Config->ServerName.c_str()), (unsigned long) it->time);
+                       user->WriteNumeric(listnumeric, channel->name, it->mask, it->setter, (unsigned long) it->time);
                }
        }
-       user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
+       user->WriteNumeric(endoflistnumeric, channel->name, endofliststring);
 }
 
 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
 {
-       user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
+       user->WriteNumeric(endoflistnumeric, channel->name, endofliststring);
 }
 
-void ListModeBase::RemoveMode(Channel* channel, irc::modestacker* stack)
+void ListModeBase::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
 {
-       ModeList* el = extItem.get(channel);
-       if (el)
+       ChanData* cd = extItem.get(channel);
+       if (cd)
        {
-               irc::modestacker modestack(false);
-
-               for (ModeList::iterator it = el->begin(); it != el->end(); it++)
+               for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
                {
-                       if (stack)
-                               stack->Push(this->GetModeChar(), it->mask);
-                       else
-                               modestack.Push(this->GetModeChar(), it->mask);
-               }
-
-               if (stack)
-                       return;
-
-               std::vector<std::string> stackresult;
-               stackresult.push_back(channel->name);
-               while (modestack.GetStackedLine(stackresult))
-               {
-                       ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
-                       stackresult.clear();
-                       stackresult.push_back(channel->name);
+                       changelist.push_remove(this, it->mask);
                }
        }
 }
 
-void ListModeBase::RemoveMode(User*, irc::modestacker* stack)
-{
-       /* Listmodes dont get set on users */
-}
-
 void ListModeBase::DoRehash()
 {
-       ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
-
-       chanlimits.clear();
-
+       ConfigTagList tags = ServerInstance->Config->ConfTags("maxlist");
+       limitlist newlimits;
+       bool seen_default = false;
        for (ConfigIter i = tags.first; i != tags.second; i++)
        {
-               // For each <banlist> tag
                ConfigTag* c = i->second;
-               ListLimit limit(c->getString("chan"), c->getInt("limit"));
 
-               if (limit.mask.size() && limit.limit > 0)
-                       chanlimits.push_back(limit);
+               const std::string mname = c->getString("mode");
+               if (!mname.empty() && !stdalgo::string::equalsci(mname, name) && !(mname.length() == 1 && GetModeChar() == mname[0]))
+                       continue;
+
+               ListLimit limit(c->getString("chan", "*", 1), c->getUInt("limit", DEFAULT_LIST_SIZE));
+
+               if (limit.mask.empty())
+                       throw ModuleException(InspIRCd::Format("<maxlist:chan> is empty, at %s", c->getTagLocation().c_str()));
+
+               if (limit.mask == "*" || limit.mask == "#*")
+                       seen_default = true;
+
+               newlimits.push_back(limit);
        }
 
-       if (chanlimits.empty())
-               chanlimits.push_back(ListLimit("*", 64));
-}
+       // If no default limit has been specified then insert one.
+       if (!seen_default)
+       {
+               ServerInstance->Logs->Log("MODE", LOG_DEBUG, "No default <maxlist> entry was found for the %s mode; defaulting to %u",
+                       name.c_str(), DEFAULT_LIST_SIZE);
+               newlimits.push_back(ListLimit("*", DEFAULT_LIST_SIZE));
+       }
 
-void ListModeBase::DoImplements(Module* m)
-{
-       ServerInstance->Modules->AddService(extItem);
-       this->DoRehash();
-       Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
-       ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
+       // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
+       if (chanlimits == newlimits)
+               return;
+
+       chanlimits.swap(newlimits);
+
+       const chan_hash& chans = ServerInstance->GetChans();
+       for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
+       {
+               ChanData* cd = extItem.get(i->second);
+               if (cd)
+                       cd->maxitems = -1;
+       }
 }
 
-unsigned int ListModeBase::GetLimit(Channel* channel)
+unsigned int ListModeBase::FindLimit(const std::string& channame)
 {
        for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
        {
-               if (InspIRCd::Match(channel->name, it->mask))
+               if (InspIRCd::Match(channame, it->mask))
                {
                        // We have a pattern matching the channel
                        return it->limit;
                }
        }
-       return 64;
+       return 0;
+}
+
+unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
+{
+       if (cd->maxitems < 0)
+               cd->maxitems = FindLimit(channame);
+       return cd->maxitems;
+}
+
+unsigned int ListModeBase::GetLimit(Channel* channel)
+{
+       ChanData* cd = extItem.get(channel);
+       if (!cd) // just find the limit
+               return FindLimit(channel->name);
+
+       return GetLimitInternal(channel->name, cd);
+}
+
+unsigned int ListModeBase::GetLowerLimit()
+{
+       if (chanlimits.empty())
+               return DEFAULT_LIST_SIZE;
+
+       unsigned int limit = UINT_MAX;
+       for (limitlist::iterator iter = chanlimits.begin(); iter != chanlimits.end(); ++iter)
+       {
+               if (iter->limit < limit)
+                       limit = iter->limit;
+       }
+       return limit;
 }
 
 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
 {
        // Try and grab the list
-       ModeList* el = extItem.get(channel);
+       ChanData* cd = extItem.get(channel);
 
        if (adding)
        {
                if (tidy)
                        ModeParser::CleanMask(parameter);
 
-               if (parameter.length() > 250)
-                       return MODEACTION_DENY;
-
                // If there was no list
-               if (!el)
+               if (!cd)
                {
                        // Make one
-                       el = new ModeList;
-                       extItem.set(channel, el);
+                       cd = new ChanData;
+                       extItem.set(channel, cd);
                }
 
                // Check if the item already exists in the list
-               for (ModeList::iterator it = el->begin(); it != el->end(); it++)
+               for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
                {
                        if (parameter == it->mask)
                        {
@@ -154,11 +184,10 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std
                        }
                }
 
-               if ((IS_LOCAL(source)) && (el->size() >= GetLimit(channel)))
+               if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
                {
                        /* List is full, give subclass a chance to send a custom message */
                        TellListTooLong(source, channel, parameter);
-                       parameter.clear();
                        return MODEACTION_DENY;
                }
 
@@ -175,7 +204,7 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std
                if (ValidateParam(source, channel, parameter))
                {
                        // And now add the mask onto the list...
-                       el->push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
+                       cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
                        return MODEACTION_ALLOW;
                }
                else
@@ -187,13 +216,13 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std
        else
        {
                // We're taking the mode off
-               if (el)
+               if (cd)
                {
-                       for (ModeList::iterator it = el->begin(); it != el->end(); it++)
+                       for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
                        {
                                if (parameter == it->mask)
                                {
-                                       el->erase(it);
+                                       stdalgo::vector::swaperase(cd->list, it);
                                        return MODEACTION_ALLOW;
                                }
                        }
@@ -201,47 +230,31 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std
 
                /* Tried to remove something that wasn't set */
                TellNotSet(source, channel, parameter);
-               parameter.clear();
                return MODEACTION_DENY;
        }
 }
 
-void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque)
+bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
 {
-       ModeList* mlist = extItem.get(chan);
-       if (!mlist)
-               return;
-
-       irc::modestacker modestack(true);
-       std::vector<std::string> stackresult;
-       std::vector<TranslateType> types;
-       types.push_back(TR_TEXT);
-
-       for (ModeList::iterator it = mlist->begin(); it != mlist->end(); it++)
-               modestack.Push(mode, it->mask);
-
-       while (modestack.GetStackedLine(stackresult))
-       {
-               types.assign(stackresult.size(), this->GetTranslateType());
-               proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
-               stackresult.clear();
-       }
+       return true;
 }
 
-bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
+void ListModeBase::OnParameterMissing(User*, User*, Channel*)
 {
-       return true;
+       // Intentionally left blank.
 }
 
 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
 {
-       source->WriteNumeric(478, "%s %s %s :Channel ban list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
+       source->WriteNumeric(ERR_BANLISTFULL, channel->name, parameter, mode, InspIRCd::Format("Channel %s list is full", name.c_str()));
 }
 
-void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
+void ListModeBase::TellAlreadyOnList(User* source, Channel* channel, std::string& parameter)
 {
+       source->WriteNumeric(ERR_LISTMODEALREADYSET, channel->name, parameter, mode, InspIRCd::Format("Channel %s list already contains %s", name.c_str(), parameter.c_str()));
 }
 
-void ListModeBase::TellNotSet(User*, Channel*, std::string&)
+void ListModeBase::TellNotSet(User* source, Channel* channel, std::string& parameter)
 {
+       source->WriteNumeric(ERR_LISTMODENOTSET, channel->name, parameter, mode, InspIRCd::Format("Channel %s list does not contain %s", name.c_str(), parameter.c_str()));
 }