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 ModeList* el = extItem.get(channel);
35 for (ModeList::reverse_iterator it = el->rbegin(); it != el->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 ModeList* el = extItem.get(channel);
53 irc::modestacker modestack(false);
55 for (ModeList::iterator it = el->begin(); it != el->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);
88 for (ConfigIter i = tags.first; i != tags.second; i++)
90 // For each <banlist> tag
91 ConfigTag* c = i->second;
92 ListLimit limit(c->getString("chan"), c->getInt("limit"));
94 if (limit.mask.size() && limit.limit > 0)
95 chanlimits.push_back(limit);
98 if (chanlimits.empty())
99 chanlimits.push_back(ListLimit("*", 64));
102 void ListModeBase::DoImplements(Module* m)
104 ServerInstance->Modules->AddService(extItem);
106 Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
107 ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
110 unsigned int ListModeBase::GetLimit(Channel* channel)
112 for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
114 if (InspIRCd::Match(channel->name, it->mask))
116 // We have a pattern matching the channel
123 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
125 // Try and grab the list
126 ModeList* el = extItem.get(channel);
131 ModeParser::CleanMask(parameter);
133 if (parameter.length() > 250)
134 return MODEACTION_DENY;
136 // If there was no list
141 extItem.set(channel, el);
144 // Check if the item already exists in the list
145 for (ModeList::iterator it = el->begin(); it != el->end(); it++)
147 if (parameter == it->mask)
149 /* Give a subclass a chance to error about this */
150 TellAlreadyOnList(source, channel, parameter);
152 // it does, deny the change
153 return MODEACTION_DENY;
157 if ((IS_LOCAL(source)) && (el->size() >= GetLimit(channel)))
159 /* List is full, give subclass a chance to send a custom message */
160 TellListTooLong(source, channel, parameter);
162 return MODEACTION_DENY;
165 /* Ok, it *could* be allowed, now give someone subclassing us
166 * a chance to validate the parameter.
167 * The param is passed by reference, so they can both modify it
168 * and tell us if we allow it or not.
170 * eg, the subclass could:
172 * 2) 'fix' parameter and then allow
175 if (ValidateParam(source, channel, parameter))
177 // And now add the mask onto the list...
178 el->push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
179 return MODEACTION_ALLOW;
183 /* If they deny it they have the job of giving an error message */
184 return MODEACTION_DENY;
189 // We're taking the mode off
192 for (ModeList::iterator it = el->begin(); it != el->end(); it++)
194 if (parameter == it->mask)
197 return MODEACTION_ALLOW;
202 /* Tried to remove something that wasn't set */
203 TellNotSet(source, channel, parameter);
205 return MODEACTION_DENY;
209 void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque)
211 ModeList* mlist = extItem.get(chan);
215 irc::modestacker modestack(true);
216 std::vector<std::string> stackresult;
217 std::vector<TranslateType> types;
218 types.push_back(TR_TEXT);
220 for (ModeList::iterator it = mlist->begin(); it != mlist->end(); it++)
221 modestack.Push(mode, it->mask);
223 while (modestack.GetStackedLine(stackresult))
225 types.assign(stackresult.size(), this->GetTranslateType());
226 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
231 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
236 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
238 source->WriteNumeric(478, "%s %s %s :Channel ban list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
241 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
245 void ListModeBase::TellNotSet(User*, Channel*, std::string&)