]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listmode.cpp
fb76beab210cc4c8a00379e9083d644078d54b81
[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::DoRehash()
61 {
62         ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
63
64         limitlist oldlimits = chanlimits;
65         chanlimits.clear();
66
67         for (ConfigIter i = tags.first; i != tags.second; i++)
68         {
69                 // For each <banlist> tag
70                 ConfigTag* c = i->second;
71                 ListLimit limit(c->getString("chan"), c->getInt("limit"));
72
73                 if (limit.mask.size() && limit.limit > 0)
74                         chanlimits.push_back(limit);
75         }
76
77         if (chanlimits.empty())
78                 chanlimits.push_back(ListLimit("*", 64));
79
80         // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
81         if (oldlimits == chanlimits)
82                 return;
83
84         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
85         {
86                 ChanData* cd = extItem.get(i->second);
87                 if (cd)
88                         cd->maxitems = -1;
89         }
90 }
91
92 void ListModeBase::DoImplements(Module* m)
93 {
94         ServerInstance->Modules->AddService(extItem);
95         this->DoRehash();
96 }
97
98 unsigned int ListModeBase::FindLimit(const std::string& channame)
99 {
100         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
101         {
102                 if (InspIRCd::Match(channame, it->mask))
103                 {
104                         // We have a pattern matching the channel
105                         return it->limit;
106                 }
107         }
108         return 64;
109 }
110
111 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
112 {
113         if (cd->maxitems < 0)
114                 cd->maxitems = FindLimit(channame);
115         return cd->maxitems;
116 }
117
118 unsigned int ListModeBase::GetLimit(Channel* channel)
119 {
120         ChanData* cd = extItem.get(channel);
121         if (!cd) // just find the limit
122                 return FindLimit(channel->name);
123
124         return GetLimitInternal(channel->name, cd);
125 }
126
127 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
128 {
129         // Try and grab the list
130         ChanData* cd = extItem.get(channel);
131
132         if (adding)
133         {
134                 if (tidy)
135                         ModeParser::CleanMask(parameter);
136
137                 if (parameter.length() > 250)
138                         return MODEACTION_DENY;
139
140                 // If there was no list
141                 if (!cd)
142                 {
143                         // Make one
144                         cd = new ChanData;
145                         extItem.set(channel, cd);
146                 }
147
148                 // Check if the item already exists in the list
149                 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
150                 {
151                         if (parameter == it->mask)
152                         {
153                                 /* Give a subclass a chance to error about this */
154                                 TellAlreadyOnList(source, channel, parameter);
155
156                                 // it does, deny the change
157                                 return MODEACTION_DENY;
158                         }
159                 }
160
161                 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
162                 {
163                         /* List is full, give subclass a chance to send a custom message */
164                         TellListTooLong(source, channel, parameter);
165                         parameter.clear();
166                         return MODEACTION_DENY;
167                 }
168
169                 /* Ok, it *could* be allowed, now give someone subclassing us
170                  * a chance to validate the parameter.
171                  * The param is passed by reference, so they can both modify it
172                  * and tell us if we allow it or not.
173                  *
174                  * eg, the subclass could:
175                  * 1) allow
176                  * 2) 'fix' parameter and then allow
177                  * 3) deny
178                  */
179                 if (ValidateParam(source, channel, parameter))
180                 {
181                         // And now add the mask onto the list...
182                         cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
183                         return MODEACTION_ALLOW;
184                 }
185                 else
186                 {
187                         /* If they deny it they have the job of giving an error message */
188                         return MODEACTION_DENY;
189                 }
190         }
191         else
192         {
193                 // We're taking the mode off
194                 if (cd)
195                 {
196                         for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
197                         {
198                                 if (parameter == it->mask)
199                                 {
200                                         cd->list.erase(it);
201                                         return MODEACTION_ALLOW;
202                                 }
203                         }
204                 }
205
206                 /* Tried to remove something that wasn't set */
207                 TellNotSet(source, channel, parameter);
208                 parameter.clear();
209                 return MODEACTION_DENY;
210         }
211 }
212
213 void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque)
214 {
215         ChanData* cd = extItem.get(chan);
216         if (!cd)
217                 return;
218
219         irc::modestacker modestack(true);
220         std::vector<std::string> stackresult;
221         std::vector<TranslateType> types;
222         types.push_back(TR_TEXT);
223
224         for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
225                 modestack.Push(mode, it->mask);
226
227         while (modestack.GetStackedLine(stackresult))
228         {
229                 types.assign(stackresult.size(), this->GetTranslateType());
230                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
231                 stackresult.clear();
232         }
233 }
234
235 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
236 {
237         return true;
238 }
239
240 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
241 {
242         source->WriteNumeric(478, "%s %s %s :Channel ban list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
243 }
244
245 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
246 {
247 }
248
249 void ListModeBase::TellNotSet(User*, Channel*, std::string&)
250 {
251 }