]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
Chain ValidateServerName onto ValidateHostname so that the servername gets hostname...
[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         char moderemove[MAXBUF];
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                         sprintf(moderemove,"-%c",this->GetModeChar());
71                         const char* parameters[] = { channel->name, moderemove, i->data };
72                         ServerInstance->SendMode(parameters, 3, ServerInstance->FakeClient);
73                 }
74         }
75 }
76
77 void ModeChannelBan::RemoveMode(User*, irc::modestacker* stack)
78 {
79 }
80
81 void ModeChannelBan::DisplayList(User* user, Channel* channel)
82 {
83         /* Display the channel banlist */
84         for (BanList::reverse_iterator i = channel->bans.rbegin(); i != channel->bans.rend(); ++i)
85         {
86                 user->WriteServ("367 %s %s %s %s %lu",user->nick, channel->name, i->data, i->set_by, (unsigned long)i->set_time);
87         }
88         user->WriteServ("368 %s %s :End of channel ban list",user->nick, channel->name);
89         return;
90 }
91
92 void ModeChannelBan::DisplayEmptyList(User* user, Channel* channel)
93 {
94         user->WriteServ("368 %s %s :End of channel ban list",user->nick, channel->name);
95 }
96
97 std::string& ModeChannelBan::AddBan(User *user, std::string &dest, Channel *chan, int, bool servermode)
98 {
99         if ((!user) || (!chan))
100         {
101                 ServerInstance->Logs->Log("MODE",DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
102                 dest = "";
103                 return dest;
104         }
105
106         /* Attempt to tidy the mask */
107         ModeParser::CleanMask(dest);
108         /* If the mask was invalid, we exit */
109         if (dest == "")
110                 return dest;
111
112         long maxbans = chan->GetMaxBans();
113         if ((unsigned)chan->bans.size() > (unsigned)maxbans)
114         {
115                 user->WriteServ("478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %ld)",user->nick, chan->name,chan->name,maxbans);
116                 dest = "";
117                 return dest;
118         }
119
120         int MOD_RESULT = 0;
121         FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
122         if (MOD_RESULT)
123         {
124                 dest = "";
125                 return dest;
126         }
127
128         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
129         {
130                 if (!strcasecmp(i->data, dest.c_str()))
131                 {
132                         /* dont allow a user to set the same ban twice */
133                         dest = "";
134                         return dest;
135                 }
136         }
137
138         b.set_time = ServerInstance->Time();
139         strlcpy(b.data, dest.c_str(), MAXBUF);
140         strlcpy(b.set_by, servermode ? ServerInstance->Config->ServerName : user->nick, 63);
141         chan->bans.push_back(b);
142         return dest;
143 }
144
145 ModePair ModeChannelBan::ModeSet(User*, User*, Channel* channel, const std::string &parameter)
146 {
147         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
148         {
149                 if (!strcasecmp(i->data,parameter.c_str()))
150                 {
151                         return std::make_pair(true, i->data);
152                 }
153         }
154         return std::make_pair(false, parameter);
155 }
156
157 std::string& ModeChannelBan::DelBan(User *user, std::string& dest, Channel *chan, int)
158 {
159         if ((!user) || (!chan))
160         {
161                 ServerInstance->Logs->Log("MODE",DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
162                 dest = "";
163                 return dest;
164         }
165
166         /* 'Clean' the mask, e.g. nick -> nick!*@* */
167         ModeParser::CleanMask(dest);
168
169         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
170         {
171                 if (!strcasecmp(i->data,dest.c_str()))
172                 {
173                         int MOD_RESULT = 0;
174                         FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
175                         if (MOD_RESULT)
176                         {
177                                 dest = "";
178                                 return dest;
179                         }
180                         chan->bans.erase(i);
181                         return dest;
182                 }
183         }
184         dest = "";
185         return dest;
186 }
187