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),
25 configtag(ctag), extItem("listbase_mode_" + name + "_list", Creator)
30 void ListModeBase::DisplayList(User* user, Channel* channel)
32 ChanData* cd = extItem.get(channel);
35 for (ModeList::reverse_iterator it = cd->list.rbegin(); it != cd->list.rend(); ++it)
37 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);
40 user->WriteNumeric(endoflistnumeric, "%s :%s", channel->name.c_str(), endofliststring.c_str());
43 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
45 user->WriteNumeric(endoflistnumeric, "%s :%s", channel->name.c_str(), endofliststring.c_str());
48 void ListModeBase::RemoveMode(Channel* channel, irc::modestacker& stack)
50 ChanData* cd = extItem.get(channel);
53 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
55 stack.Push(this->GetModeChar(), it->mask);
60 void ListModeBase::DoRehash()
62 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
64 limitlist oldlimits = chanlimits;
67 for (ConfigIter i = tags.first; i != tags.second; i++)
69 // For each <banlist> tag
70 ConfigTag* c = i->second;
71 ListLimit limit(c->getString("chan"), c->getInt("limit"));
73 if (limit.mask.size() && limit.limit > 0)
74 chanlimits.push_back(limit);
77 if (chanlimits.empty())
78 chanlimits.push_back(ListLimit("*", 64));
80 // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
81 if (oldlimits == chanlimits)
84 const chan_hash& chans = ServerInstance->GetChans();
85 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
87 ChanData* cd = extItem.get(i->second);
93 unsigned int ListModeBase::FindLimit(const std::string& channame)
95 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
97 if (InspIRCd::Match(channame, it->mask))
99 // We have a pattern matching the channel
106 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
108 if (cd->maxitems < 0)
109 cd->maxitems = FindLimit(channame);
113 unsigned int ListModeBase::GetLimit(Channel* channel)
115 ChanData* cd = extItem.get(channel);
116 if (!cd) // just find the limit
117 return FindLimit(channel->name);
119 return GetLimitInternal(channel->name, cd);
122 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
124 // Try and grab the list
125 ChanData* cd = extItem.get(channel);
130 ModeParser::CleanMask(parameter);
132 if (parameter.length() > 250)
133 return MODEACTION_DENY;
135 // If there was no list
140 extItem.set(channel, cd);
143 // Check if the item already exists in the list
144 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
146 if (parameter == it->mask)
148 /* Give a subclass a chance to error about this */
149 TellAlreadyOnList(source, channel, parameter);
151 // it does, deny the change
152 return MODEACTION_DENY;
156 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
158 /* List is full, give subclass a chance to send a custom message */
159 TellListTooLong(source, channel, parameter);
160 return MODEACTION_DENY;
163 /* Ok, it *could* be allowed, now give someone subclassing us
164 * a chance to validate the parameter.
165 * The param is passed by reference, so they can both modify it
166 * and tell us if we allow it or not.
168 * eg, the subclass could:
170 * 2) 'fix' parameter and then allow
173 if (ValidateParam(source, channel, parameter))
175 // And now add the mask onto the list...
176 cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
177 return MODEACTION_ALLOW;
181 /* If they deny it they have the job of giving an error message */
182 return MODEACTION_DENY;
187 // We're taking the mode off
190 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
192 if (parameter == it->mask)
195 return MODEACTION_ALLOW;
200 /* Tried to remove something that wasn't set */
201 TellNotSet(source, channel, parameter);
202 return MODEACTION_DENY;
206 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
211 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
213 source->WriteNumeric(ERR_BANLISTFULL, "%s %s :Channel ban list is full", channel->name.c_str(), parameter.c_str());
216 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
220 void ListModeBase::TellNotSet(User*, Channel*, std::string&)