2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ListModeBase::ListModeBase(Module* Creator, const std::string& Name, char modechar, const std::string& eolstr, unsigned int lnum, unsigned int eolnum, bool autotidy)
23 : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_LIST)
25 , endoflistnumeric(eolnum)
26 , endofliststring(eolstr)
28 , extItem(name + "_mode_list", ExtensionItem::EXT_CHANNEL, Creator)
33 void ListModeBase::DisplayList(User* user, Channel* channel)
35 ChanData* cd = extItem.get(channel);
38 for (ModeList::const_iterator it = cd->list.begin(); it != cd->list.end(); ++it)
40 user->WriteNumeric(listnumeric, channel->name, it->mask, it->setter, (unsigned long) it->time);
43 user->WriteNumeric(endoflistnumeric, channel->name, endofliststring);
46 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
48 user->WriteNumeric(endoflistnumeric, channel->name, endofliststring);
51 void ListModeBase::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
53 ChanData* cd = extItem.get(channel);
56 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
58 changelist.push_remove(this, it->mask);
63 void ListModeBase::DoRehash()
65 ConfigTagList tags = ServerInstance->Config->ConfTags("maxlist");
67 bool seen_default = false;
68 for (ConfigIter i = tags.first; i != tags.second; i++)
70 ConfigTag* c = i->second;
72 const std::string mname = c->getString("mode");
73 if (!mname.empty() && !stdalgo::string::equalsci(mname, name) && !(mname.length() == 1 && GetModeChar() == mname[0]))
76 ListLimit limit(c->getString("chan", "*", 1), c->getUInt("limit", DEFAULT_LIST_SIZE));
78 if (limit.mask.empty())
79 throw ModuleException(InspIRCd::Format("<maxlist:chan> is empty, at %s", c->getTagLocation().c_str()));
81 if (limit.mask == "*" || limit.mask == "#*")
84 newlimits.push_back(limit);
87 // If no default limit has been specified then insert one.
90 ServerInstance->Logs->Log("MODE", LOG_DEBUG, "No default <maxlist> entry was found for the %s mode; defaulting to %u",
91 name.c_str(), DEFAULT_LIST_SIZE);
92 newlimits.push_back(ListLimit("*", DEFAULT_LIST_SIZE));
95 // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
96 if (chanlimits == newlimits)
99 chanlimits.swap(newlimits);
101 const chan_hash& chans = ServerInstance->GetChans();
102 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
104 ChanData* cd = extItem.get(i->second);
110 unsigned int ListModeBase::FindLimit(const std::string& channame)
112 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
114 if (InspIRCd::Match(channame, it->mask))
116 // We have a pattern matching the channel
123 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
125 if (cd->maxitems < 0)
126 cd->maxitems = FindLimit(channame);
130 unsigned int ListModeBase::GetLimit(Channel* channel)
132 ChanData* cd = extItem.get(channel);
133 if (!cd) // just find the limit
134 return FindLimit(channel->name);
136 return GetLimitInternal(channel->name, cd);
139 unsigned int ListModeBase::GetLowerLimit()
141 if (chanlimits.empty())
142 return DEFAULT_LIST_SIZE;
144 unsigned int limit = UINT_MAX;
145 for (limitlist::iterator iter = chanlimits.begin(); iter != chanlimits.end(); ++iter)
147 if (iter->limit < limit)
153 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
155 // Try and grab the list
156 ChanData* cd = extItem.get(channel);
161 ModeParser::CleanMask(parameter);
163 // If there was no list
168 extItem.set(channel, cd);
171 // Check if the item already exists in the list
172 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
174 if (parameter == it->mask)
176 /* Give a subclass a chance to error about this */
177 TellAlreadyOnList(source, channel, parameter);
179 // it does, deny the change
180 return MODEACTION_DENY;
184 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
186 /* List is full, give subclass a chance to send a custom message */
187 TellListTooLong(source, channel, parameter);
188 return MODEACTION_DENY;
191 /* Ok, it *could* be allowed, now give someone subclassing us
192 * a chance to validate the parameter.
193 * The param is passed by reference, so they can both modify it
194 * and tell us if we allow it or not.
196 * eg, the subclass could:
198 * 2) 'fix' parameter and then allow
201 if (ValidateParam(source, channel, parameter))
203 // And now add the mask onto the list...
204 cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
205 return MODEACTION_ALLOW;
209 /* If they deny it they have the job of giving an error message */
210 return MODEACTION_DENY;
215 // We're taking the mode off
218 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
220 if (parameter == it->mask)
222 stdalgo::vector::swaperase(cd->list, it);
223 return MODEACTION_ALLOW;
228 /* Tried to remove something that wasn't set */
229 TellNotSet(source, channel, parameter);
230 return MODEACTION_DENY;
234 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
239 void ListModeBase::OnParameterMissing(User*, User*, Channel*)
241 // Intentionally left blank.
244 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
246 source->WriteNumeric(ERR_BANLISTFULL, channel->name, parameter, mode, InspIRCd::Format("Channel %s list is full", name.c_str()));
249 void ListModeBase::TellAlreadyOnList(User* source, Channel* channel, std::string& parameter)
251 source->WriteNumeric(ERR_LISTMODEALREADYSET, channel->name, parameter, mode, InspIRCd::Format("Channel %s list already contains %s", name.c_str(), parameter.c_str()));
254 void ListModeBase::TellNotSet(User* source, Channel* channel, std::string& parameter)
256 source->WriteNumeric(ERR_LISTMODENOTSET, channel->name, parameter, mode, InspIRCd::Format("Channel %s list does not contain %s", name.c_str(), parameter.c_str()));