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