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