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),
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 %s %lu", user->nick.c_str(), 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 :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
43 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
45 user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
48 void ListModeBase::RemoveMode(Channel* channel, irc::modestacker* stack)
50 ChanData* cd = extItem.get(channel);
53 irc::modestacker modestack(false);
55 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
58 stack->Push(this->GetModeChar(), it->mask);
60 modestack.Push(this->GetModeChar(), it->mask);
66 std::vector<std::string> stackresult;
67 stackresult.push_back(channel->name);
68 while (modestack.GetStackedLine(stackresult))
70 ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
72 stackresult.push_back(channel->name);
77 void ListModeBase::RemoveMode(User*, irc::modestacker* stack)
79 /* Listmodes dont get set on users */
82 void ListModeBase::DoRehash()
84 ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
86 limitlist oldlimits = chanlimits;
89 for (ConfigIter i = tags.first; i != tags.second; i++)
91 // For each <banlist> tag
92 ConfigTag* c = i->second;
93 ListLimit limit(c->getString("chan"), c->getInt("limit"));
95 if (limit.mask.size() && limit.limit > 0)
96 chanlimits.push_back(limit);
99 if (chanlimits.empty())
100 chanlimits.push_back(ListLimit("*", 64));
102 // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
103 if (oldlimits == chanlimits)
106 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
108 ChanData* cd = extItem.get(i->second);
114 void ListModeBase::DoImplements(Module* m)
116 ServerInstance->Modules->AddService(extItem);
118 Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
119 ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
122 unsigned int ListModeBase::FindLimit(const std::string& channame)
124 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
126 if (InspIRCd::Match(channame, it->mask))
128 // We have a pattern matching the channel
135 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
137 if (cd->maxitems < 0)
138 cd->maxitems = FindLimit(channame);
142 unsigned int ListModeBase::GetLimit(Channel* channel)
144 ChanData* cd = extItem.get(channel);
145 if (!cd) // just find the limit
146 return FindLimit(channel->name);
148 return GetLimitInternal(channel->name, cd);
151 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
153 // Try and grab the list
154 ChanData* cd = extItem.get(channel);
159 ModeParser::CleanMask(parameter);
161 if (parameter.length() > 250)
162 return MODEACTION_DENY;
164 // If there was no list
169 extItem.set(channel, cd);
172 // Check if the item already exists in the list
173 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
175 if (parameter == it->mask)
177 /* Give a subclass a chance to error about this */
178 TellAlreadyOnList(source, channel, parameter);
180 // it does, deny the change
181 return MODEACTION_DENY;
185 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
187 /* List is full, give subclass a chance to send a custom message */
188 TellListTooLong(source, channel, parameter);
190 return MODEACTION_DENY;
193 /* Ok, it *could* be allowed, now give someone subclassing us
194 * a chance to validate the parameter.
195 * The param is passed by reference, so they can both modify it
196 * and tell us if we allow it or not.
198 * eg, the subclass could:
200 * 2) 'fix' parameter and then allow
203 if (ValidateParam(source, channel, parameter))
205 // And now add the mask onto the list...
206 cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
207 return MODEACTION_ALLOW;
211 /* If they deny it they have the job of giving an error message */
212 return MODEACTION_DENY;
217 // We're taking the mode off
220 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
222 if (parameter == it->mask)
225 return MODEACTION_ALLOW;
230 /* Tried to remove something that wasn't set */
231 TellNotSet(source, channel, parameter);
233 return MODEACTION_DENY;
237 void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque)
239 ChanData* cd = extItem.get(chan);
243 irc::modestacker modestack(true);
244 std::vector<std::string> stackresult;
245 std::vector<TranslateType> types;
246 types.push_back(TR_TEXT);
248 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
249 modestack.Push(mode, it->mask);
251 while (modestack.GetStackedLine(stackresult))
253 types.assign(stackresult.size(), this->GetTranslateType());
254 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
259 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
264 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
266 source->WriteNumeric(478, "%s %s %s :Channel ban list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
269 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
273 void ListModeBase::TellNotSet(User*, Channel*, std::string&)