]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
Fix desync with halfop and voice when opped (this happens because we were ORing the...
[user/henk/code/inspircd.git] / src / modes / cmode_b.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 <string>
15 #include <vector>
16 #include "inspircd_config.h"
17 #include "configreader.h"
18 #include "hash_map.h"
19 #include "inspircd.h"
20 #include "mode.h"
21 #include "channels.h"
22 #include "users.h"
23
24 #include "modules.h"
25 #include "inspstring.h"
26 #include "hashcomp.h"
27 #include "modes/cmode_b.h"
28
29 ModeChannelBan::ModeChannelBan(InspIRCd* Instance) : ModeHandler(Instance, 'b', 1, 1, true, MODETYPE_CHANNEL, false)
30 {
31 }
32
33 ModeAction ModeChannelBan::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
34 {
35         int status = channel->GetStatus(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(chanrec* channel)
54 {
55         BanList copy;
56         char moderemove[MAXBUF];
57         userrec* n = new userrec(ServerInstance);
58         n->SetFd(FD_MAGIC_NUMBER);
59
60         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
61         {
62                 copy.push_back(*i);
63         }
64         for (BanList::iterator i = copy.begin(); i != copy.end(); i++)
65         {
66                 sprintf(moderemove,"-%c",this->GetModeChar());
67                 const char* parameters[] = { channel->name, moderemove, i->data };
68                 ServerInstance->SendMode(parameters, 3, n);
69         }
70
71         delete n;
72 }
73
74 void ModeChannelBan::RemoveMode(userrec* user)
75 {
76 }
77
78 void ModeChannelBan::DisplayList(userrec* user, chanrec* channel)
79 {
80         /* Display the channel banlist */
81         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
82         {
83                 user->WriteServ("367 %s %s %s %s %d",user->nick, channel->name, i->data, i->set_by, i->set_time);
84         }
85         user->WriteServ("368 %s %s :End of channel ban list",user->nick, channel->name);
86         return;
87 }
88
89 std::string& ModeChannelBan::AddBan(userrec *user,std::string &dest,chanrec *chan,int status)
90 {
91         if ((!user) || (!chan))
92         {
93                 ServerInstance->Log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
94                 dest = "";
95                 return dest;
96         }
97
98         /* Attempt to tidy the mask */
99         ModeParser::CleanMask(dest);
100         /* If the mask was invalid, we exit */
101         if (dest == "")
102                 return dest;
103
104         long maxbans = chan->GetMaxBans();
105         if ((unsigned)chan->bans.size() > (unsigned)maxbans)
106         {
107                 user->WriteServ("478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
108                 dest = "";
109                 return dest;
110         }
111
112         int MOD_RESULT = 0;
113         FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
114         if (MOD_RESULT)
115         {
116                 dest = "";
117                 return dest;
118         }
119
120         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
121         {
122                 if (!strcasecmp(i->data,dest.c_str()))
123                 {
124                         /* dont allow a user to set the same ban twice */
125                         dest = "";
126                         return dest;
127                 }
128         }
129
130         b.set_time = ServerInstance->Time();
131         strlcpy(b.data,dest.c_str(),MAXBUF);
132         if (*user->nick)
133         {
134                 strlcpy(b.set_by,user->nick,NICKMAX-1);
135         }
136         else
137         {
138                 strlcpy(b.set_by,ServerInstance->Config->ServerName,NICKMAX-1);
139         }
140         chan->bans.push_back(b);
141         return dest;
142 }
143
144 ModePair ModeChannelBan::ModeSet(userrec* source, userrec* dest, chanrec* 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,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(userrec *user,std::string& dest,chanrec *chan,int status)
157 {
158         if ((!user) || (!chan))
159         {
160                 ServerInstance->Log(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,dest.c_str()))
171                 {
172                         int MOD_RESULT = 0;
173                         FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
174                         if (MOD_RESULT)
175                         {
176                                 dest = "";
177                                 return dest;
178                         }
179                         chan->bans.erase(i);
180                         return dest;
181                 }
182         }
183         dest = "";
184         return dest;
185 }
186