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