]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listmode.cpp
35964dfb3396d9f893f78e63aa2d682d0c39e53d
[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, MC_LIST),
24         listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy),
25         configtag(ctag)
26         , extItem("listbase_mode_" + name + "_list", ExtensionItem::EXT_CHANNEL, Creator)
27 {
28         list = true;
29 }
30
31 void ListModeBase::DisplayList(User* user, Channel* channel)
32 {
33         ChanData* cd = extItem.get(channel);
34         if (cd)
35         {
36                 for (ModeList::const_iterator it = cd->list.begin(); it != cd->list.end(); ++it)
37                 {
38                         user->WriteNumeric(listnumeric, "%s %s %s %lu", channel->name.c_str(), it->mask.c_str(), it->setter.c_str(), (unsigned long) it->time);
39                 }
40         }
41         user->WriteNumeric(endoflistnumeric, "%s :%s", channel->name.c_str(), endofliststring.c_str());
42 }
43
44 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
45 {
46         user->WriteNumeric(endoflistnumeric, "%s :%s", channel->name.c_str(), endofliststring.c_str());
47 }
48
49 void ListModeBase::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
50 {
51         ChanData* cd = extItem.get(channel);
52         if (cd)
53         {
54                 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
55                 {
56                         changelist.push_remove(this, it->mask);
57                 }
58         }
59 }
60
61 void ListModeBase::DoRehash()
62 {
63         ConfigTagList tags = ServerInstance->Config->ConfTags(configtag);
64
65         limitlist oldlimits = chanlimits;
66         chanlimits.clear();
67
68         for (ConfigIter i = tags.first; i != tags.second; i++)
69         {
70                 // For each <banlist> tag
71                 ConfigTag* c = i->second;
72                 ListLimit limit(c->getString("chan"), c->getInt("limit"));
73
74                 if (limit.mask.size() && limit.limit > 0)
75                         chanlimits.push_back(limit);
76         }
77
78         // Add the default entry. This is inserted last so if the user specifies a
79         // wildcard record in the config it will take precedence over this entry.
80         chanlimits.push_back(ListLimit("*", 64));
81
82         // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
83         if (oldlimits == chanlimits)
84                 return;
85
86         const chan_hash& chans = ServerInstance->GetChans();
87         for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
88         {
89                 ChanData* cd = extItem.get(i->second);
90                 if (cd)
91                         cd->maxitems = -1;
92         }
93 }
94
95 unsigned int ListModeBase::FindLimit(const std::string& channame)
96 {
97         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
98         {
99                 if (InspIRCd::Match(channame, it->mask))
100                 {
101                         // We have a pattern matching the channel
102                         return it->limit;
103                 }
104         }
105         return 64;
106 }
107
108 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
109 {
110         if (cd->maxitems < 0)
111                 cd->maxitems = FindLimit(channame);
112         return cd->maxitems;
113 }
114
115 unsigned int ListModeBase::GetLimit(Channel* channel)
116 {
117         ChanData* cd = extItem.get(channel);
118         if (!cd) // just find the limit
119                 return FindLimit(channel->name);
120
121         return GetLimitInternal(channel->name, cd);
122 }
123
124 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
125 {
126         // Try and grab the list
127         ChanData* cd = extItem.get(channel);
128
129         if (adding)
130         {
131                 if (tidy)
132                         ModeParser::CleanMask(parameter);
133
134                 if (parameter.length() > 250)
135                         return MODEACTION_DENY;
136
137                 // If there was no list
138                 if (!cd)
139                 {
140                         // Make one
141                         cd = new ChanData;
142                         extItem.set(channel, cd);
143                 }
144
145                 // Check if the item already exists in the list
146                 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
147                 {
148                         if (parameter == it->mask)
149                         {
150                                 /* Give a subclass a chance to error about this */
151                                 TellAlreadyOnList(source, channel, parameter);
152
153                                 // it does, deny the change
154                                 return MODEACTION_DENY;
155                         }
156                 }
157
158                 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
159                 {
160                         /* List is full, give subclass a chance to send a custom message */
161                         TellListTooLong(source, channel, parameter);
162                         return MODEACTION_DENY;
163                 }
164
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.
169                  *
170                  * eg, the subclass could:
171                  * 1) allow
172                  * 2) 'fix' parameter and then allow
173                  * 3) deny
174                  */
175                 if (ValidateParam(source, channel, parameter))
176                 {
177                         // And now add the mask onto the list...
178                         cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
179                         return MODEACTION_ALLOW;
180                 }
181                 else
182                 {
183                         /* If they deny it they have the job of giving an error message */
184                         return MODEACTION_DENY;
185                 }
186         }
187         else
188         {
189                 // We're taking the mode off
190                 if (cd)
191                 {
192                         for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
193                         {
194                                 if (parameter == it->mask)
195                                 {
196                                         stdalgo::vector::swaperase(cd->list, it);
197                                         return MODEACTION_ALLOW;
198                                 }
199                         }
200                 }
201
202                 /* Tried to remove something that wasn't set */
203                 TellNotSet(source, channel, parameter);
204                 return MODEACTION_DENY;
205         }
206 }
207
208 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
209 {
210         return true;
211 }
212
213 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
214 {
215         source->WriteNumeric(ERR_BANLISTFULL, "%s %s :Channel ban list is full", channel->name.c_str(), parameter.c_str());
216 }
217
218 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
219 {
220 }
221
222 void ListModeBase::TellNotSet(User*, Channel*, std::string&)
223 {
224 }