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 for (ConfigIter i = tags.first; i != tags.second; i++)
69 ConfigTag* c = i->second;
71 const std::string mname = c->getString("mode");
72 if (!mname.empty() && !stdalgo::string::equalsci(mname, name) && !(mname.length() == 1 && GetModeChar() == mname[0]))
75 ListLimit limit(c->getString("chan", "*"), c->getUInt("limit", 0));
77 if (limit.mask.empty())
78 throw ModuleException(InspIRCd::Format("<maxlist:chan> is empty, at %s", c->getTagLocation().c_str()));
81 throw ModuleException(InspIRCd::Format("<maxlist:limit> must be non-zero, at %s", c->getTagLocation().c_str()));
83 newlimits.push_back(limit);
86 // Add the default entry. This is inserted last so if the user specifies a
87 // wildcard record in the config it will take precedence over this entry.
88 newlimits.push_back(ListLimit("*", DEFAULT_LIST_SIZE));
90 // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
91 if (chanlimits == newlimits)
94 chanlimits.swap(newlimits);
96 const chan_hash& chans = ServerInstance->GetChans();
97 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
99 ChanData* cd = extItem.get(i->second);
105 unsigned int ListModeBase::FindLimit(const std::string& channame)
107 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
109 if (InspIRCd::Match(channame, it->mask))
111 // We have a pattern matching the channel
115 return DEFAULT_LIST_SIZE;
118 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
120 if (cd->maxitems < 0)
121 cd->maxitems = FindLimit(channame);
125 unsigned int ListModeBase::GetLimit(Channel* channel)
127 ChanData* cd = extItem.get(channel);
128 if (!cd) // just find the limit
129 return FindLimit(channel->name);
131 return GetLimitInternal(channel->name, cd);
134 unsigned int ListModeBase::GetLowerLimit()
136 unsigned int limit = UINT_MAX;
137 for (limitlist::iterator iter = chanlimits.begin(); iter != chanlimits.end(); ++iter)
139 if (iter->limit < limit)
142 return limit == UINT_MAX ? DEFAULT_LIST_SIZE : limit;
145 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
147 // Try and grab the list
148 ChanData* cd = extItem.get(channel);
153 ModeParser::CleanMask(parameter);
155 if (parameter.length() > 250)
156 return MODEACTION_DENY;
158 // If there was no list
163 extItem.set(channel, cd);
166 // Check if the item already exists in the list
167 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
169 if (parameter == it->mask)
171 /* Give a subclass a chance to error about this */
172 TellAlreadyOnList(source, channel, parameter);
174 // it does, deny the change
175 return MODEACTION_DENY;
179 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
181 /* List is full, give subclass a chance to send a custom message */
182 TellListTooLong(source, channel, parameter);
183 return MODEACTION_DENY;
186 /* Ok, it *could* be allowed, now give someone subclassing us
187 * a chance to validate the parameter.
188 * The param is passed by reference, so they can both modify it
189 * and tell us if we allow it or not.
191 * eg, the subclass could:
193 * 2) 'fix' parameter and then allow
196 if (ValidateParam(source, channel, parameter))
198 // And now add the mask onto the list...
199 cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
200 return MODEACTION_ALLOW;
204 /* If they deny it they have the job of giving an error message */
205 return MODEACTION_DENY;
210 // We're taking the mode off
213 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
215 if (parameter == it->mask)
217 stdalgo::vector::swaperase(cd->list, it);
218 return MODEACTION_ALLOW;
223 /* Tried to remove something that wasn't set */
224 TellNotSet(source, channel, parameter);
225 return MODEACTION_DENY;
229 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
234 void ListModeBase::OnParameterMissing(User*, User*, Channel*)
236 // Intentionally left blank.
239 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
241 source->WriteNumeric(ERR_BANLISTFULL, channel->name, parameter, mode, InspIRCd::Format("Channel %s list is full", name.c_str()));
244 void ListModeBase::TellAlreadyOnList(User* source, Channel* channel, std::string& parameter)
246 source->WriteNumeric(ERR_LISTMODEALREADYSET, channel->name, parameter, mode, InspIRCd::Format("Channel %s list already contains %s", name.c_str(), parameter.c_str()));
249 void ListModeBase::TellNotSet(User* source, Channel* channel, std::string& parameter)
251 source->WriteNumeric(ERR_LISTMODENOTSET, channel->name, parameter, mode, InspIRCd::Format("Channel %s list does not contain %s", name.c_str(), parameter.c_str()));