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