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