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, const std::string &ctag)
23 : ModeHandler(Creator, Name, modechar, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_LIST),
24 listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
26 , extItem("listbase_mode_" + name + "_list", ExtensionItem::EXT_CHANNEL, Creator)
31 void ListModeBase::DisplayList(User* user, Channel* channel)
33 ChanData* cd = extItem.get(channel);
36 for (ModeList::const_iterator it = cd->list.begin(); it != cd->list.end(); ++it)
38 user->WriteNumeric(listnumeric, channel->name, it->mask, it->setter, (unsigned long) it->time);
41 user->WriteNumeric(endoflistnumeric, channel->name, endofliststring);
44 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
46 user->WriteNumeric(endoflistnumeric, channel->name, endofliststring);
49 void ListModeBase::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
51 ChanData* cd = extItem.get(channel);
54 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
56 changelist.push_remove(this, it->mask);
61 void ListModeBase::DoRehash()
63 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
65 limitlist oldlimits = chanlimits;
68 for (ConfigIter i = tags.first; i != tags.second; i++)
70 // For each <banlist> tag
71 ConfigTag* c = i->second;
72 ListLimit limit(c->getString("chan"), c->getInt("limit"));
74 if (limit.mask.size() && limit.limit > 0)
75 chanlimits.push_back(limit);
78 // Add the default entry. This is inserted last so if the user specifies a
79 // wildcard record in the config it will take precedence over this entry.
80 chanlimits.push_back(ListLimit("*", DEFAULT_LIST_SIZE));
82 // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
83 if (oldlimits == chanlimits)
86 const chan_hash& chans = ServerInstance->GetChans();
87 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
89 ChanData* cd = extItem.get(i->second);
95 unsigned int ListModeBase::FindLimit(const std::string& channame)
97 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
99 if (InspIRCd::Match(channame, it->mask))
101 // We have a pattern matching the channel
105 return DEFAULT_LIST_SIZE;
108 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
110 if (cd->maxitems < 0)
111 cd->maxitems = FindLimit(channame);
115 unsigned int ListModeBase::GetLimit(Channel* channel)
117 ChanData* cd = extItem.get(channel);
118 if (!cd) // just find the limit
119 return FindLimit(channel->name);
121 return GetLimitInternal(channel->name, cd);
124 unsigned int ListModeBase::GetLowerLimit()
126 unsigned int limit = UINT_MAX;
127 for (limitlist::iterator iter = chanlimits.begin(); iter != chanlimits.end(); ++iter)
129 if (iter->limit < limit)
132 return limit == UINT_MAX ? DEFAULT_LIST_SIZE : limit;
135 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
137 // Try and grab the list
138 ChanData* cd = extItem.get(channel);
143 ModeParser::CleanMask(parameter);
145 if (parameter.length() > 250)
146 return MODEACTION_DENY;
148 // If there was no list
153 extItem.set(channel, cd);
156 // Check if the item already exists in the list
157 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
159 if (parameter == it->mask)
161 /* Give a subclass a chance to error about this */
162 TellAlreadyOnList(source, channel, parameter);
164 // it does, deny the change
165 return MODEACTION_DENY;
169 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
171 /* List is full, give subclass a chance to send a custom message */
172 TellListTooLong(source, channel, parameter);
173 return MODEACTION_DENY;
176 /* Ok, it *could* be allowed, now give someone subclassing us
177 * a chance to validate the parameter.
178 * The param is passed by reference, so they can both modify it
179 * and tell us if we allow it or not.
181 * eg, the subclass could:
183 * 2) 'fix' parameter and then allow
186 if (ValidateParam(source, channel, parameter))
188 // And now add the mask onto the list...
189 cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
190 return MODEACTION_ALLOW;
194 /* If they deny it they have the job of giving an error message */
195 return MODEACTION_DENY;
200 // We're taking the mode off
203 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
205 if (parameter == it->mask)
207 stdalgo::vector::swaperase(cd->list, it);
208 return MODEACTION_ALLOW;
213 /* Tried to remove something that wasn't set */
214 TellNotSet(source, channel, parameter);
215 return MODEACTION_DENY;
219 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
224 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
226 source->WriteNumeric(ERR_BANLISTFULL, channel->name, parameter, "Channel ban list is full");
229 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
233 void ListModeBase::TellNotSet(User*, Channel*, std::string&)