]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
Add chmode +k, cut down includes in use in mode.cpp
[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 ServerConfig* Config;
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 std::string& ModeChannelBan::AddBan(userrec *user,std::string &dest,chanrec *chan,int status)
49 {
50         if ((!user) || (!chan))
51         {
52                 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
53                 dest = "";
54                 return dest;
55         }
56
57         /* Attempt to tidy the mask */
58         ModeParser::CleanMask(dest);
59         /* If the mask was invalid, we exit */
60         if (dest == "")
61                 return dest;
62
63         long maxbans = GetMaxBans(chan->name);
64         if ((unsigned)chan->bans.size() > (unsigned)maxbans)
65         {
66                 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
67                 dest = "";
68                 return dest;
69         }
70
71         int MOD_RESULT = 0;
72         FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
73         if (MOD_RESULT)
74         {
75                 dest = "";
76                 return dest;
77         }
78
79         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
80         {
81                 if (!strcasecmp(i->data,dest.c_str()))
82                 {
83                         /* dont allow a user to set the same ban twice */
84                         dest = "";
85                         return dest;
86                 }
87         }
88
89         b.set_time = TIME;
90         strlcpy(b.data,dest.c_str(),MAXBUF);
91         if (*user->nick)
92         {
93                 strlcpy(b.set_by,user->nick,NICKMAX-1);
94         }
95         else
96         {
97                 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
98         }
99         chan->bans.push_back(b);
100         return dest;
101 }
102
103 std::string& ModeChannelBan::DelBan(userrec *user,std::string& dest,chanrec *chan,int status)
104 {
105         if ((!user) || (!chan))
106         {
107                 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
108                 dest = "";
109                 return dest;
110         }
111
112         /* 'Clean' the mask, e.g. nick -> nick!*@* */
113         ModeParser::CleanMask(dest);
114
115         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
116         {
117                 if (!strcasecmp(i->data,dest.c_str()))
118                 {
119                         int MOD_RESULT = 0;
120                         FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
121                         if (MOD_RESULT)
122                         {
123                                 dest = "";
124                                 return dest;
125                         }
126                         chan->bans.erase(i);
127                         return dest;
128                 }
129         }
130         dest = "";
131         return dest;
132 }
133