]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listmode.cpp
Deduplicate RemoveMode() implementations
[user/henk/code/inspircd.git] / src / listmode.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19 #include "inspircd.h"
20 #include "listmode.h"
21
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)
26 {
27         list = true;
28 }
29
30 void ListModeBase::DisplayList(User* user, Channel* channel)
31 {
32         ChanData* cd = extItem.get(channel);
33         if (cd)
34         {
35                 for (ModeList::reverse_iterator it = cd->list.rbegin(); it != cd->list.rend(); ++it)
36                 {
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);
38                 }
39         }
40         user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
41 }
42
43 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
44 {
45         user->WriteNumeric(endoflistnumeric, "%s %s :%s", user->nick.c_str(), channel->name.c_str(), endofliststring.c_str());
46 }
47
48 void ListModeBase::RemoveMode(Channel* channel, irc::modestacker& stack)
49 {
50         ChanData* cd = extItem.get(channel);
51         if (cd)
52         {
53                 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
54                 {
55                         stack.Push(this->GetModeChar(), it->mask);
56                 }
57         }
58 }
59
60 void ListModeBase::RemoveMode(User*, irc::modestacker* stack)
61 {
62         /* Listmodes dont get set on users */
63 }
64
65 void ListModeBase::DoRehash()
66 {
67         ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
68
69         limitlist oldlimits = chanlimits;
70         chanlimits.clear();
71
72         for (ConfigIter i = tags.first; i != tags.second; i++)
73         {
74                 // For each <banlist> tag
75                 ConfigTag* c = i->second;
76                 ListLimit limit(c->getString("chan"), c->getInt("limit"));
77
78                 if (limit.mask.size() && limit.limit > 0)
79                         chanlimits.push_back(limit);
80         }
81
82         if (chanlimits.empty())
83                 chanlimits.push_back(ListLimit("*", 64));
84
85         // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
86         if (oldlimits == chanlimits)
87                 return;
88
89         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
90         {
91                 ChanData* cd = extItem.get(i->second);
92                 if (cd)
93                         cd->maxitems = -1;
94         }
95 }
96
97 void ListModeBase::DoImplements(Module* m)
98 {
99         ServerInstance->Modules->AddService(extItem);
100         this->DoRehash();
101         Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
102         ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
103 }
104
105 unsigned int ListModeBase::FindLimit(const std::string& channame)
106 {
107         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
108         {
109                 if (InspIRCd::Match(channame, it->mask))
110                 {
111                         // We have a pattern matching the channel
112                         return it->limit;
113                 }
114         }
115         return 64;
116 }
117
118 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
119 {
120         if (cd->maxitems < 0)
121                 cd->maxitems = FindLimit(channame);
122         return cd->maxitems;
123 }
124
125 unsigned int ListModeBase::GetLimit(Channel* channel)
126 {
127         ChanData* cd = extItem.get(channel);
128         if (!cd) // just find the limit
129                 return FindLimit(channel->name);
130
131         return GetLimitInternal(channel->name, cd);
132 }
133
134 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
135 {
136         // Try and grab the list
137         ChanData* cd = extItem.get(channel);
138
139         if (adding)
140         {
141                 if (tidy)
142                         ModeParser::CleanMask(parameter);
143
144                 if (parameter.length() > 250)
145                         return MODEACTION_DENY;
146
147                 // If there was no list
148                 if (!cd)
149                 {
150                         // Make one
151                         cd = new ChanData;
152                         extItem.set(channel, cd);
153                 }
154
155                 // Check if the item already exists in the list
156                 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
157                 {
158                         if (parameter == it->mask)
159                         {
160                                 /* Give a subclass a chance to error about this */
161                                 TellAlreadyOnList(source, channel, parameter);
162
163                                 // it does, deny the change
164                                 return MODEACTION_DENY;
165                         }
166                 }
167
168                 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
169                 {
170                         /* List is full, give subclass a chance to send a custom message */
171                         TellListTooLong(source, channel, parameter);
172                         parameter.clear();
173                         return MODEACTION_DENY;
174                 }
175
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.
180                  *
181                  * eg, the subclass could:
182                  * 1) allow
183                  * 2) 'fix' parameter and then allow
184                  * 3) deny
185                  */
186                 if (ValidateParam(source, channel, parameter))
187                 {
188                         // And now add the mask onto the list...
189                         cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
190                         return MODEACTION_ALLOW;
191                 }
192                 else
193                 {
194                         /* If they deny it they have the job of giving an error message */
195                         return MODEACTION_DENY;
196                 }
197         }
198         else
199         {
200                 // We're taking the mode off
201                 if (cd)
202                 {
203                         for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
204                         {
205                                 if (parameter == it->mask)
206                                 {
207                                         cd->list.erase(it);
208                                         return MODEACTION_ALLOW;
209                                 }
210                         }
211                 }
212
213                 /* Tried to remove something that wasn't set */
214                 TellNotSet(source, channel, parameter);
215                 parameter.clear();
216                 return MODEACTION_DENY;
217         }
218 }
219
220 void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque)
221 {
222         ChanData* cd = extItem.get(chan);
223         if (!cd)
224                 return;
225
226         irc::modestacker modestack(true);
227         std::vector<std::string> stackresult;
228         std::vector<TranslateType> types;
229         types.push_back(TR_TEXT);
230
231         for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
232                 modestack.Push(mode, it->mask);
233
234         while (modestack.GetStackedLine(stackresult))
235         {
236                 types.assign(stackresult.size(), this->GetTranslateType());
237                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
238                 stackresult.clear();
239         }
240 }
241
242 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
243 {
244         return true;
245 }
246
247 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
248 {
249         source->WriteNumeric(478, "%s %s %s :Channel ban list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
250 }
251
252 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
253 {
254 }
255
256 void ListModeBase::TellNotSet(User*, Channel*, std::string&)
257 {
258 }