]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
Move whowas settings into cmd_whowas from ConfigReader
[user/henk/code/inspircd.git] / src / modes / cmode_b.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include <string>
23 #include <vector>
24 #include "inspircd.h"
25 #include "configreader.h"
26 #include "mode.h"
27 #include "channels.h"
28 #include "users.h"
29 #include "modules.h"
30 #include "inspstring.h"
31 #include "hashcomp.h"
32 #include "modes/cmode_b.h"
33
34 ModeChannelBan::ModeChannelBan() : ModeHandler(NULL, "ban", 'b', PARAM_ALWAYS, MODETYPE_CHANNEL)
35 {
36         list = true;
37 }
38
39 ModeAction ModeChannelBan::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
40 {
41         int status = channel->GetPrefixValue(source);
42         /* Call the correct method depending on wether we're adding or removing the mode */
43         if (adding)
44         {
45                 this->AddBan(source, parameter, channel, status);
46         }
47         else
48         {
49                 this->DelBan(source, parameter, channel, status);
50         }
51         /* If the method above 'ate' the parameter by reducing it to an empty string, then
52          * it won't matter wether we return ALLOW or DENY here, as an empty string overrides
53          * the return value and is always MODEACTION_DENY if the mode is supposed to have
54          * a parameter.
55          */
56         return MODEACTION_ALLOW;
57 }
58
59 void ModeChannelBan::RemoveMode(Channel* channel, irc::modestacker* stack)
60 {
61         BanList copy;
62
63         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
64         {
65                 copy.push_back(*i);
66         }
67
68         for (BanList::iterator i = copy.begin(); i != copy.end(); i++)
69         {
70                 if (stack)
71                 {
72                         stack->Push(this->GetModeChar(), i->data);
73                 }
74                 else
75                 {
76                         std::vector<std::string> parameters; parameters.push_back(channel->name); parameters.push_back("-b"); parameters.push_back(i->data);
77                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
78                 }
79         }
80 }
81
82 void ModeChannelBan::RemoveMode(User*, irc::modestacker* stack)
83 {
84 }
85
86 void ModeChannelBan::DisplayList(User* user, Channel* channel)
87 {
88         /* Display the channel banlist */
89         for (BanList::reverse_iterator i = channel->bans.rbegin(); i != channel->bans.rend(); ++i)
90         {
91                 user->WriteServ("367 %s %s %s %s %lu",user->nick.c_str(), channel->name.c_str(), i->data.c_str(), i->set_by.c_str(), (unsigned long)i->set_time);
92         }
93         user->WriteServ("368 %s %s :End of channel ban list",user->nick.c_str(), channel->name.c_str());
94         return;
95 }
96
97 void ModeChannelBan::DisplayEmptyList(User* user, Channel* channel)
98 {
99         user->WriteServ("368 %s %s :End of channel ban list",user->nick.c_str(), channel->name.c_str());
100 }
101
102 std::string& ModeChannelBan::AddBan(User *user, std::string &dest, Channel *chan, int)
103 {
104         if ((!user) || (!chan))
105         {
106                 ServerInstance->Logs->Log("MODE",DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
107                 dest.clear();
108                 return dest;
109         }
110
111         /* Attempt to tidy the mask */
112         ModeParser::CleanMask(dest);
113         /* If the mask was invalid, we exit */
114         if (dest.empty() || dest.length() > 250)
115                 return dest;
116
117         long maxbans = chan->GetMaxBans();
118         if (IS_LOCAL(user) && ((unsigned)chan->bans.size() >= (unsigned)maxbans))
119         {
120                 user->WriteServ("478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %ld)",user->nick.c_str(), chan->name.c_str(), chan->name.c_str(), maxbans);
121                 dest.clear();
122                 return dest;
123         }
124
125         ModResult MOD_RESULT;
126         FIRST_MOD_RESULT(OnAddBan, MOD_RESULT, (user,chan,dest));
127         if (MOD_RESULT == MOD_RES_DENY)
128         {
129                 dest.clear();
130                 return dest;
131         }
132
133         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
134         {
135                 if (i->data == dest)
136                 {
137                         /* dont allow a user to set the same ban twice */
138                         dest.clear();
139                         return dest;
140                 }
141         }
142
143         b.set_time = ServerInstance->Time();
144         b.data.assign(dest, 0, MAXBUF);
145         b.set_by.assign(user->nick, 0, 64);
146         chan->bans.push_back(b);
147         return dest;
148 }
149
150 std::string& ModeChannelBan::DelBan(User *user, std::string& dest, Channel *chan, int)
151 {
152         if ((!user) || (!chan))
153         {
154                 ServerInstance->Logs->Log("MODE",DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
155                 dest.clear();
156                 return dest;
157         }
158
159         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
160         {
161                 if (!strcasecmp(i->data.c_str(), dest.c_str()))
162                 {
163                         ModResult MOD_RESULT;
164                         FIRST_MOD_RESULT(OnDelBan, MOD_RESULT, (user, chan, dest));
165                         if (MOD_RESULT == MOD_RES_DENY)
166                         {
167                                 dest.clear();
168                                 return dest;
169                         }
170                         chan->bans.erase(i);
171                         return dest;
172                 }
173         }
174         dest.clear();
175         return dest;
176 }
177