]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/listmode.cpp
Make more modules rehash atomically (#1535)
[user/henk/code/inspircd.git] / src / listmode.cpp
index 2e6703df38bc62394fbc033b4e78f91c91fde59d..6b5fb13c59569e165940e04770b35ce5c788369e 100644 (file)
@@ -22,7 +22,8 @@
 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, MC_LIST),
        listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
-       configtag(ctag), extItem("listbase_mode_" + name + "_list", Creator)
+       configtag(ctag)
+       , extItem("listbase_mode_" + name + "_list", ExtensionItem::EXT_CHANNEL, Creator)
 {
        list = true;
 }
@@ -32,27 +33,27 @@ void ListModeBase::DisplayList(User* user, Channel* channel)
        ChanData* cd = extItem.get(channel);
        if (cd)
        {
-               for (ModeList::reverse_iterator it = cd->list.rbegin(); it != cd->list.rend(); ++it)
+               for (ModeList::const_iterator it = cd->list.begin(); it != cd->list.end(); ++it)
                {
-                       user->WriteNumeric(listnumeric, "%s %s %s %lu", 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", 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", 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)
 {
        ChanData* cd = extItem.get(channel);
        if (cd)
        {
                for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
                {
-                       stack.Push(this->GetModeChar(), it->mask);
+                       changelist.push_remove(this, it->mask);
                }
        }
 }
@@ -61,27 +62,35 @@ void ListModeBase::DoRehash()
 {
        ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
 
-       limitlist oldlimits = chanlimits;
-       chanlimits.clear();
+       limitlist newlimits;
 
        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"));
+               ListLimit limit(c->getString("chan"), c->getUInt("limit", 0));
 
-               if (limit.mask.size() && limit.limit > 0)
-                       chanlimits.push_back(limit);
+               if (limit.mask.empty())
+                       throw ModuleException(InspIRCd::Format("<%s:chan> is empty at %s", configtag.c_str(), c->getTagLocation().c_str()));
+
+               if (limit.limit <= 0)
+                       throw ModuleException(InspIRCd::Format("<%s:limit> must be greater than 0, at %s", configtag.c_str(), c->getTagLocation().c_str()));
+
+               newlimits.push_back(limit);
        }
 
-       if (chanlimits.empty())
-               chanlimits.push_back(ListLimit("*", 64));
+       // Add the default entry. This is inserted last so if the user specifies a
+       // wildcard record in the config it will take precedence over this entry.
+       newlimits.push_back(ListLimit("*", DEFAULT_LIST_SIZE));
 
        // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
-       if (oldlimits == chanlimits)
+       if (chanlimits == newlimits)
                return;
 
-       for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
+       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)
@@ -99,7 +108,7 @@ unsigned int ListModeBase::FindLimit(const std::string& channame)
                        return it->limit;
                }
        }
-       return 64;
+       return DEFAULT_LIST_SIZE;
 }
 
 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
@@ -118,6 +127,17 @@ unsigned int ListModeBase::GetLimit(Channel* channel)
        return GetLimitInternal(channel->name, cd);
 }
 
+unsigned int ListModeBase::GetLowerLimit()
+{
+       unsigned int limit = UINT_MAX;
+       for (limitlist::iterator iter = chanlimits.begin(); iter != chanlimits.end(); ++iter)
+       {
+               if (iter->limit < limit)
+                       limit = iter->limit;
+       }
+       return limit == UINT_MAX ? DEFAULT_LIST_SIZE : limit;
+}
+
 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
 {
        // Try and grab the list
@@ -190,7 +210,7 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std
                        {
                                if (parameter == it->mask)
                                {
-                                       cd->list.erase(it);
+                                       stdalgo::vector::swaperase(cd->list, it);
                                        return MODEACTION_ALLOW;
                                }
                        }
@@ -207,15 +227,22 @@ bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
        return true;
 }
 
+void ListModeBase::OnParameterMissing(User*, User*, Channel*)
+{
+       // Intentionally left blank.
+}
+
 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
 {
-       source->WriteNumeric(ERR_BANLISTFULL, "%s %s :Channel ban list is full", 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()));
 }