]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_b.cpp
Document TimerManager class
[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 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::DisplayList(userrec* user, chanrec* channel)
41 {
42         /* Display the channel banlist */
43         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
44         {
45                 user->WriteServ("367 %s %s %s %s %d",user->nick, channel->name, i->data, i->set_by, i->set_time);
46         }
47         user->WriteServ("368 %s %s :End of channel ban list",user->nick, channel->name);
48         return;
49 }
50
51 std::string& ModeChannelBan::AddBan(userrec *user,std::string &dest,chanrec *chan,int status)
52 {
53         if ((!user) || (!chan))
54         {
55                 ServerInstance->Log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
56                 dest = "";
57                 return dest;
58         }
59
60         /* Attempt to tidy the mask */
61         ModeParser::CleanMask(dest);
62         /* If the mask was invalid, we exit */
63         if (dest == "")
64                 return dest;
65
66         long maxbans = chan->GetMaxBans();
67         if ((unsigned)chan->bans.size() > (unsigned)maxbans)
68         {
69                 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);
70                 dest = "";
71                 return dest;
72         }
73
74         int MOD_RESULT = 0;
75         FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
76         if (MOD_RESULT)
77         {
78                 dest = "";
79                 return dest;
80         }
81
82         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
83         {
84                 if (!strcasecmp(i->data,dest.c_str()))
85                 {
86                         /* dont allow a user to set the same ban twice */
87                         dest = "";
88                         return dest;
89                 }
90         }
91
92         b.set_time = ServerInstance->Time();
93         strlcpy(b.data,dest.c_str(),MAXBUF);
94         if (*user->nick)
95         {
96                 strlcpy(b.set_by,user->nick,NICKMAX-1);
97         }
98         else
99         {
100                 strlcpy(b.set_by,ServerInstance->Config->ServerName,NICKMAX-1);
101         }
102         chan->bans.push_back(b);
103         return dest;
104 }
105
106 ModePair ModeChannelBan::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
107 {
108         for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
109         {
110                 if (!strcasecmp(i->data,parameter.c_str()))
111                 {
112                         return std::make_pair(true, i->data);
113                 }
114         }
115         return std::make_pair(false, parameter);
116 }
117
118 std::string& ModeChannelBan::DelBan(userrec *user,std::string& dest,chanrec *chan,int status)
119 {
120         if ((!user) || (!chan))
121         {
122                 ServerInstance->Log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
123                 dest = "";
124                 return dest;
125         }
126
127         /* 'Clean' the mask, e.g. nick -> nick!*@* */
128         ModeParser::CleanMask(dest);
129
130         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
131         {
132                 if (!strcasecmp(i->data,dest.c_str()))
133                 {
134                         int MOD_RESULT = 0;
135                         FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
136                         if (MOD_RESULT)
137                         {
138                                 dest = "";
139                                 return dest;
140                         }
141                         chan->bans.erase(i);
142                         return dest;
143                 }
144         }
145         dest = "";
146         return dest;
147 }
148