]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_h.cpp
Convert to SimpleUserModeHandler and SimpleChannelModeHandler
[user/henk/code/inspircd.git] / src / modes / cmode_h.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "configreader.h"
16 #include "mode.h"
17 #include "channels.h"
18 #include "users.h"
19 #include "modules.h"
20 #include "modes/cmode_h.h"
21
22 ModeChannelHalfOp::ModeChannelHalfOp(InspIRCd* Instance) : ModeHandler(Instance, 'h', 1, 1, true, MODETYPE_CHANNEL, false, '%', '@')
23 {
24 }
25
26 unsigned int ModeChannelHalfOp::GetPrefixRank()
27 {
28         return HALFOP_VALUE;
29 }
30
31 ModePair ModeChannelHalfOp::ModeSet(User*, User*, Channel* channel, const std::string &parameter)
32 {
33         User* x = ServerInstance->FindNick(parameter);
34         if (x)
35         {
36                 if (channel->GetStatusFlags(x) & UCMODE_HOP)
37                 {
38                         return std::make_pair(true, x->nick);
39                 }
40                 else
41                 {
42                         return std::make_pair(false, parameter);
43                 }
44         }
45         return std::make_pair(false, parameter);
46 }
47
48 void ModeChannelHalfOp::RemoveMode(Channel* channel, irc::modestacker* stack)
49 {
50         CUList* clist = channel->GetHalfoppedUsers();
51         CUList copy;
52         char moderemove[MAXBUF];
53
54         for (CUList::iterator i = clist->begin(); i != clist->end(); i++)
55         {
56                 User* n = i->first;
57                 copy.insert(std::make_pair(n,n->nick));
58         }
59
60         for (CUList::iterator i = copy.begin(); i != copy.end(); i++)
61         {
62                 if (stack)
63                 {
64                         stack->Push(this->GetModeChar(), i->first->nick);
65                 }
66                 else
67                 {
68                         sprintf(moderemove,"-%c",this->GetModeChar());
69                         const char* parameters[] = { channel->name, moderemove, i->first->nick };
70                         ServerInstance->SendMode(parameters, 3, ServerInstance->FakeClient);
71                 }
72         }
73
74 }
75
76 void ModeChannelHalfOp::RemoveMode(User*, irc::modestacker* stack)
77 {
78 }
79
80 ModeAction ModeChannelHalfOp::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding, bool servermode)
81 {
82         /* If halfops are not enabled in the conf, we don't execute
83          * anything in this class at all.
84          */
85         if (!ServerInstance->Config->AllowHalfop)
86         {
87                 parameter = "";
88                 return MODEACTION_DENY;
89         }
90
91         int status = channel->GetStatus(source);
92
93         /* Call the correct method depending on wether we're adding or removing the mode */
94         if (adding)
95         {
96                 parameter = this->AddHalfOp(source, parameter.c_str(), channel, status);
97         }
98         else
99         {
100                 parameter = this->DelHalfOp(source, parameter.c_str(), channel, status);
101         }
102         /* If the method above 'ate' the parameter by reducing it to an empty string, then
103          * it won't matter wether we return ALLOW or DENY here, as an empty string overrides
104          * the return value and is always MODEACTION_DENY if the mode is supposed to have
105          * a parameter.
106          */
107         if (parameter.length())
108                 return MODEACTION_ALLOW;
109         else
110                 return MODEACTION_DENY;
111 }
112
113 std::string ModeChannelHalfOp::AddHalfOp(User *user,const char* dest,Channel *chan,int status)
114 {
115         User *d = ServerInstance->Modes->SanityChecks(user,dest,chan,status);
116
117         if (d)
118         {
119                 if (IS_LOCAL(user))
120                 {
121                         int MOD_RESULT = 0;
122                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
123
124                         if (MOD_RESULT == ACR_DENY)
125                                 return "";
126                         if (MOD_RESULT == ACR_DEFAULT)
127                         {
128                                 if ((status < STATUS_OP) && (!ServerInstance->ULine(user->server)))
129                                 {
130                                         user->WriteServ("482 %s %s :You're not a channel operator",user->nick, chan->name);
131                                         return "";
132                                 }
133                         }
134                 }
135
136                 return ServerInstance->Modes->Grant(d,chan,UCMODE_HOP);
137         }
138         return "";
139 }
140
141 std::string ModeChannelHalfOp::DelHalfOp(User *user,const char *dest,Channel *chan,int status)
142 {
143         User *d = ServerInstance->Modes->SanityChecks(user,dest,chan,status);
144
145         if (d)
146         {
147                 if (IS_LOCAL(user))
148                 {
149                         int MOD_RESULT = 0;
150                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
151
152                         if (MOD_RESULT == ACR_DENY)
153                                 return "";
154                         if (MOD_RESULT == ACR_DEFAULT)
155                         {
156                                 if ((user != d) && ((status < STATUS_OP) && (!ServerInstance->ULine(user->server))))
157                                 {
158                                         user->WriteServ("482 %s %s :You are not a channel operator",user->nick, chan->name);
159                                         return "";
160                                 }
161                         }
162                 }
163
164                 return ServerInstance->Modes->Revoke(d,chan,UCMODE_HOP);
165         }
166         return "";
167 }
168