]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listmode.cpp
Migrate u_listmode.h into the core, change +b to use it
[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         ModeList* el = extItem.get(channel);
33         if (el)
34         {
35                 for (ModeList::reverse_iterator it = el->rbegin(); it != el->rend(); ++it)
36                 {
37                         user->WriteNumeric(listnumeric, "%s %s %s %s %s", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), (it->nick.length() ? it->nick.c_str() : ServerInstance->Config->ServerName.c_str()), it->time.c_str());
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         ModeList* el = extItem.get(channel);
51         if (el)
52         {
53                 irc::modestacker modestack(false);
54
55                 for (ModeList::iterator it = el->begin(); it != el->end(); it++)
56                 {
57                         if (stack)
58                                 stack->Push(this->GetModeChar(), it->mask);
59                         else
60                                 modestack.Push(this->GetModeChar(), it->mask);
61                 }
62
63                 if (stack)
64                         return;
65
66                 std::vector<std::string> stackresult;
67                 stackresult.push_back(channel->name);
68                 while (modestack.GetStackedLine(stackresult))
69                 {
70                         ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
71                         stackresult.clear();
72                         stackresult.push_back(channel->name);
73                 }
74         }
75 }
76
77 void ListModeBase::RemoveMode(User*, irc::modestacker* stack)
78 {
79         /* Listmodes dont get set on users */
80 }
81
82 void ListModeBase::DoRehash()
83 {
84         ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
85
86         chanlimits.clear();
87
88         for (ConfigIter i = tags.first; i != tags.second; i++)
89         {
90                 // For each <banlist> tag
91                 ConfigTag* c = i->second;
92                 ListLimit limit;
93                 limit.mask = c->getString("chan");
94                 limit.limit = c->getInt("limit");
95
96                 if (limit.mask.size() && limit.limit > 0)
97                         chanlimits.push_back(limit);
98         }
99         if (chanlimits.size() == 0)
100         {
101                 ListLimit limit;
102                 limit.mask = "*";
103                 limit.limit = 64;
104                 chanlimits.push_back(limit);
105         }
106 }
107
108 void ListModeBase::DoImplements(Module* m)
109 {
110         ServerInstance->Modules->AddService(extItem);
111         this->DoRehash();
112         Implementation eventlist[] = { I_OnSyncChannel, I_OnRehash };
113         ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation));
114 }
115
116 unsigned int ListModeBase::GetLimit(Channel* channel)
117 {
118         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
119         {
120                 if (InspIRCd::Match(channel->name, it->mask))
121                 {
122                         // We have a pattern matching the channel
123                         return it->limit;
124                 }
125         }
126         return 64;
127 }
128
129 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
130 {
131         // Try and grab the list
132         ModeList* el = extItem.get(channel);
133
134         if (adding)
135         {
136                 if (tidy)
137                         ModeParser::CleanMask(parameter);
138
139                 if (parameter.length() > 250)
140                         return MODEACTION_DENY;
141
142                 // If there was no list
143                 if (!el)
144                 {
145                         // Make one
146                         el = new ModeList;
147                         extItem.set(channel, el);
148                 }
149
150                 // Check if the item already exists in the list
151                 for (ModeList::iterator it = el->begin(); it != el->end(); it++)
152                 {
153                         if (parameter == it->mask)
154                         {
155                                 /* Give a subclass a chance to error about this */
156                                 TellAlreadyOnList(source, channel, parameter);
157
158                                 // it does, deny the change
159                                 return MODEACTION_DENY;
160                         }
161                 }
162
163                 if ((IS_LOCAL(source)) && (el->size() >= GetLimit(channel)))
164                 {
165                         /* List is full, give subclass a chance to send a custom message */
166                         TellListTooLong(source, channel, parameter);
167                         parameter.clear();
168                         return MODEACTION_DENY;
169                 }
170
171                 /* Ok, it *could* be allowed, now give someone subclassing us
172                  * a chance to validate the parameter.
173                  * The param is passed by reference, so they can both modify it
174                  * and tell us if we allow it or not.
175                  *
176                  * eg, the subclass could:
177                  * 1) allow
178                  * 2) 'fix' parameter and then allow
179                  * 3) deny
180                  */
181                 if (ValidateParam(source, channel, parameter))
182                 {
183                         // And now add the mask onto the list...
184                         ListItem e;
185                         e.mask = parameter;
186                         e.nick = source->nick;
187                         e.time = ConvToStr(ServerInstance->Time());
188
189                         el->push_back(e);
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 (el)
202                 {
203                         for (ModeList::iterator it = el->begin(); it != el->end(); it++)
204                         {
205                                 if (parameter == it->mask)
206                                 {
207                                         el->erase(it);
208                                         if (el->size() == 0)
209                                         {
210                                                 extItem.unset(channel);
211                                         }
212                                         return MODEACTION_ALLOW;
213                                 }
214                         }
215                 }
216
217                 /* Tried to remove something that wasn't set */
218                 TellNotSet(source, channel, parameter);
219                 parameter.clear();
220                 return MODEACTION_DENY;
221         }
222 }
223
224 void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque)
225 {
226         ModeList* mlist = extItem.get(chan);
227         irc::modestacker modestack(true);
228         std::vector<std::string> stackresult;
229         std::vector<TranslateType> types;
230         types.push_back(TR_TEXT);
231         if (mlist)
232         {
233                 for (ModeList::iterator it = mlist->begin(); it != mlist->end(); it++)
234                 {
235                         modestack.Push(std::string(1, mode)[0], it->mask);
236                 }
237         }
238         while (modestack.GetStackedLine(stackresult))
239         {
240                 types.assign(stackresult.size(), this->GetTranslateType());
241                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, stackresult, types);
242                 stackresult.clear();
243         }
244 }
245
246 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
247 {
248         return true;
249 }
250
251 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
252 {
253         source->WriteNumeric(478, "%s %s %s :Channel ban list is full", source->nick.c_str(), channel->name.c_str(), parameter.c_str());
254 }
255
256 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
257 {
258 }
259
260 void ListModeBase::TellNotSet(User*, Channel*, std::string&)
261 {
262 }