]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listmode.cpp
Merge insp20
[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), 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 %lu", 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", channel->name.c_str(), endofliststring.c_str());
41 }
42
43 void ListModeBase::DisplayEmptyList(User* user, Channel* channel)
44 {
45         user->WriteNumeric(endoflistnumeric, "%s :%s", channel->name.c_str(), endofliststring.c_str());
46 }
47
48 void ListModeBase::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
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                         changelist.push_remove(this, 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         // Add the default entry. This is inserted last so if the user specifies a
78         // wildcard record in the config it will take precedence over this entry.
79         chanlimits.push_back(ListLimit("*", 64));
80
81         // Most of the time our settings are unchanged, so we can avoid iterating the chanlist
82         if (oldlimits == chanlimits)
83                 return;
84
85         const chan_hash& chans = ServerInstance->GetChans();
86         for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
87         {
88                 ChanData* cd = extItem.get(i->second);
89                 if (cd)
90                         cd->maxitems = -1;
91         }
92 }
93
94 unsigned int ListModeBase::FindLimit(const std::string& channame)
95 {
96         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it)
97         {
98                 if (InspIRCd::Match(channame, it->mask))
99                 {
100                         // We have a pattern matching the channel
101                         return it->limit;
102                 }
103         }
104         return 64;
105 }
106
107 unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd)
108 {
109         if (cd->maxitems < 0)
110                 cd->maxitems = FindLimit(channame);
111         return cd->maxitems;
112 }
113
114 unsigned int ListModeBase::GetLimit(Channel* channel)
115 {
116         ChanData* cd = extItem.get(channel);
117         if (!cd) // just find the limit
118                 return FindLimit(channel->name);
119
120         return GetLimitInternal(channel->name, cd);
121 }
122
123 ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
124 {
125         // Try and grab the list
126         ChanData* cd = extItem.get(channel);
127
128         if (adding)
129         {
130                 if (tidy)
131                         ModeParser::CleanMask(parameter);
132
133                 if (parameter.length() > 250)
134                         return MODEACTION_DENY;
135
136                 // If there was no list
137                 if (!cd)
138                 {
139                         // Make one
140                         cd = new ChanData;
141                         extItem.set(channel, cd);
142                 }
143
144                 // Check if the item already exists in the list
145                 for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++)
146                 {
147                         if (parameter == it->mask)
148                         {
149                                 /* Give a subclass a chance to error about this */
150                                 TellAlreadyOnList(source, channel, parameter);
151
152                                 // it does, deny the change
153                                 return MODEACTION_DENY;
154                         }
155                 }
156
157                 if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd)))
158                 {
159                         /* List is full, give subclass a chance to send a custom message */
160                         TellListTooLong(source, channel, parameter);
161                         return MODEACTION_DENY;
162                 }
163
164                 /* Ok, it *could* be allowed, now give someone subclassing us
165                  * a chance to validate the parameter.
166                  * The param is passed by reference, so they can both modify it
167                  * and tell us if we allow it or not.
168                  *
169                  * eg, the subclass could:
170                  * 1) allow
171                  * 2) 'fix' parameter and then allow
172                  * 3) deny
173                  */
174                 if (ValidateParam(source, channel, parameter))
175                 {
176                         // And now add the mask onto the list...
177                         cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time()));
178                         return MODEACTION_ALLOW;
179                 }
180                 else
181                 {
182                         /* If they deny it they have the job of giving an error message */
183                         return MODEACTION_DENY;
184                 }
185         }
186         else
187         {
188                 // We're taking the mode off
189                 if (cd)
190                 {
191                         for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it)
192                         {
193                                 if (parameter == it->mask)
194                                 {
195                                         cd->list.erase(it);
196                                         return MODEACTION_ALLOW;
197                                 }
198                         }
199                 }
200
201                 /* Tried to remove something that wasn't set */
202                 TellNotSet(source, channel, parameter);
203                 return MODEACTION_DENY;
204         }
205 }
206
207 bool ListModeBase::ValidateParam(User*, Channel*, std::string&)
208 {
209         return true;
210 }
211
212 void ListModeBase::TellListTooLong(User* source, Channel* channel, std::string& parameter)
213 {
214         source->WriteNumeric(ERR_BANLISTFULL, "%s %s :Channel ban list is full", channel->name.c_str(), parameter.c_str());
215 }
216
217 void ListModeBase::TellAlreadyOnList(User*, Channel*, std::string&)
218 {
219 }
220
221 void ListModeBase::TellNotSet(User*, Channel*, std::string&)
222 {
223 }