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