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