]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
66889364dab6e75abf4009cab118891a9a2ea191
[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         adding ? parameter = this->AddBan(source, parameter, channel, status) : parameter = this->DelBan(source, parameter, channel, status);
32         return MODEACTION_ALLOW;
33 }
34
35 std::string& ModeChannelBan::AddBan(userrec *user,std::string &dest,chanrec *chan,int status)
36 {
37         BanItem b;
38         int toomanyexclamation = 0;
39         int toomanyat = 0;
40
41         if ((!user) || (!chan))
42         {
43                 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
44                 dest = "";
45                 return dest;
46         }
47
48         for (std::string::iterator i = dest.begin(); i != dest.end(); i++)
49         {
50                 if ((*i < 32) || (*i > 126))
51                 {
52                         dest = "";
53                         return dest;
54                 }
55                 else if (*i == '!')
56                 {
57                         toomanyexclamation++;
58                 }
59                 else if (*i == '@')
60                 {
61                         toomanyat++;
62                 }
63         }
64
65         if (toomanyexclamation != 1 || toomanyat != 1)
66         {
67                 /*
68                  * this stops sillyness like n!u!u!u@h, though note that most
69                  * ircds don't actually verify banmask validity. --w00t
70                  */
71                 dest = "";
72                 return dest;
73         }
74
75         long maxbans = GetMaxBans(chan->name);
76         if ((unsigned)chan->bans.size() > (unsigned)maxbans)
77         {
78                 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);
79                 dest = "";
80                 return dest;
81         }
82
83         log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
84
85         int MOD_RESULT = 0;
86         FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
87         if (MOD_RESULT)
88         {
89                 dest = "";
90                 return dest;
91         }
92
93         //TidyBan(dest);
94         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
95         {
96                 if (!strcasecmp(i->data,dest.c_str()))
97                 {
98                         // dont allow a user to set the same ban twice
99                         dest = "";
100                         return dest;
101                 }
102         }
103
104         b.set_time = TIME;
105         strlcpy(b.data,dest.c_str(),MAXBUF);
106         if (*user->nick)
107         {
108                 strlcpy(b.set_by,user->nick,NICKMAX-1);
109         }
110         else
111         {
112                 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
113         }
114         chan->bans.push_back(b);
115         return dest;
116 }
117
118 std::string& ModeChannelBan::DelBan(userrec *user,std::string& dest,chanrec *chan,int status)
119 {
120         if ((!user) || (!chan)) {
121                 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
122                 dest = "";
123                 return dest;
124         }
125
126         log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
127         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
128         {
129                 if (!strcasecmp(i->data,dest.c_str()))
130                 {
131                         int MOD_RESULT = 0;
132                         FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
133                         if (MOD_RESULT)
134                         {
135                                 dest = "";
136                                 return dest;
137                         }
138                         chan->bans.erase(i);
139                         return dest;
140                 }
141         }
142         dest = "";
143         return dest;
144 }
145