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