]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
Release v2.0.18
[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_config.h"
25 #include "configreader.h"
26 #include "hash_map.h"
27 #include "mode.h"
28 #include "channels.h"
29 #include "users.h"
30 #include "modules.h"
31 #include "inspstring.h"
32 #include "hashcomp.h"
33 #include "modes/cmode_b.h"
34
35 ModeChannelBan::ModeChannelBan() : ModeHandler(NULL, "ban", 'b', PARAM_ALWAYS, MODETYPE_CHANNEL)
36 {
37         list = true;
38 }
39
40 ModeAction ModeChannelBan::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
41 {
42         int status = channel->GetPrefixValue(source);
43         /* Call the correct method depending on wether we're adding or removing the mode */
44         if (adding)
45         {
46                 this->AddBan(source, parameter, channel, status);
47         }
48         else
49         {
50                 this->DelBan(source, parameter, channel, status);
51         }
52         /* If the method above 'ate' the parameter by reducing it to an empty string, then
53          * it won't matter wether we return ALLOW or DENY here, as an empty string overrides
54          * the return value and is always MODEACTION_DENY if the mode is supposed to have
55          * a parameter.
56          */
57         return MODEACTION_ALLOW;
58 }
59
60 void ModeChannelBan::RemoveMode(Channel* channel, irc::modestacker* stack)
61 {
62         BanList copy;
63
64         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
65         {
66                 copy.push_back(*i);
67         }
68
69         for (BanList::iterator i = copy.begin(); i != copy.end(); i++)
70         {
71                 if (stack)
72                 {
73                         stack->Push(this->GetModeChar(), i->data);
74                 }
75                 else
76                 {
77                         std::vector<std::string> parameters; parameters.push_back(channel->name); parameters.push_back("-b"); parameters.push_back(i->data);
78                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
79                 }
80         }
81 }
82
83 void ModeChannelBan::RemoveMode(User*, irc::modestacker* stack)
84 {
85 }
86
87 void ModeChannelBan::DisplayList(User* user, Channel* channel)
88 {
89         /* Display the channel banlist */
90         for (BanList::reverse_iterator i = channel->bans.rbegin(); i != channel->bans.rend(); ++i)
91         {
92                 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);
93         }
94         user->WriteServ("368 %s %s :End of channel ban list",user->nick.c_str(), channel->name.c_str());
95         return;
96 }
97
98 void ModeChannelBan::DisplayEmptyList(User* user, Channel* channel)
99 {
100         user->WriteServ("368 %s %s :End of channel ban list",user->nick.c_str(), channel->name.c_str());
101 }
102
103 std::string& ModeChannelBan::AddBan(User *user, std::string &dest, Channel *chan, int)
104 {
105         if ((!user) || (!chan))
106         {
107                 ServerInstance->Logs->Log("MODE",DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
108                 dest.clear();
109                 return dest;
110         }
111
112         /* Attempt to tidy the mask */
113         ModeParser::CleanMask(dest);
114         /* If the mask was invalid, we exit */
115         if (dest.empty() || dest.length() > 250)
116                 return dest;
117
118         long maxbans = chan->GetMaxBans();
119         if (IS_LOCAL(user) && ((unsigned)chan->bans.size() >= (unsigned)maxbans))
120         {
121                 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);
122                 dest.clear();
123                 return dest;
124         }
125
126         ModResult MOD_RESULT;
127         FIRST_MOD_RESULT(OnAddBan, MOD_RESULT, (user,chan,dest));
128         if (MOD_RESULT == MOD_RES_DENY)
129         {
130                 dest.clear();
131                 return dest;
132         }
133
134         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
135         {
136                 if (i->data == dest)
137                 {
138                         /* dont allow a user to set the same ban twice */
139                         dest.clear();
140                         return dest;
141                 }
142         }
143
144         b.set_time = ServerInstance->Time();
145         b.data.assign(dest, 0, MAXBUF);
146         b.set_by.assign(user->nick, 0, 64);
147         chan->bans.push_back(b);
148         return dest;
149 }
150
151 std::string& ModeChannelBan::DelBan(User *user, std::string& dest, Channel *chan, int)
152 {
153         if ((!user) || (!chan))
154         {
155                 ServerInstance->Logs->Log("MODE",DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
156                 dest.clear();
157                 return dest;
158         }
159
160         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
161         {
162                 if (!strcasecmp(i->data.c_str(), dest.c_str()))
163                 {
164                         ModResult MOD_RESULT;
165                         FIRST_MOD_RESULT(OnDelBan, MOD_RESULT, (user, chan, dest));
166                         if (MOD_RESULT == MOD_RES_DENY)
167                         {
168                                 dest.clear();
169                                 return dest;
170                         }
171                         dest = i->data;
172                         chan->bans.erase(i);
173                         return dest;
174                 }
175         }
176         dest.clear();
177         return dest;
178 }
179