1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "m_override.h"
17 /* $ModDesc: Provides channel modes +a and +q */
19 #define PROTECT_VALUE 40000
20 #define FOUNDER_VALUE 50000
22 /** Handles basic operation of +qa channel modes
24 class FounderProtectBase
27 const std::string type;
32 bool& remove_own_privs;
33 bool& remove_other_privs;
35 FounderProtectBase(char Mode, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
36 type(mtype), mode(Mode), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
40 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
42 User* x = ServerInstance->FindNick(parameter);
45 Membership* memb = channel->GetUser(x);
48 return std::make_pair(false, parameter);
52 if (memb->hasMode(mode))
54 return std::make_pair(true, x->nick);
58 return std::make_pair(false, parameter);
62 return std::make_pair(false, parameter);
65 void RemoveMode(Channel* channel, irc::modestacker* stack)
67 const UserMembList* cl = channel->GetUsers();
68 std::vector<std::string> mode_junk;
69 mode_junk.push_back(channel->name);
70 irc::modestacker modestack(ServerInstance, false);
71 std::deque<std::string> stackresult;
73 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
75 if (i->second->hasMode(mode))
78 stack->Push(mode, i->first->nick);
80 modestack.Push(mode, i->first->nick);
87 while (modestack.GetStackedLine(stackresult))
89 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
90 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
91 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
95 void DisplayList(User* user, Channel* channel)
97 const UserMembList* cl = channel->GetUsers();
98 for (UserMembCIter i = cl->begin(); i != cl->end(); ++i)
100 if (i->second->hasMode(mode))
102 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
105 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
108 bool CanRemoveOthers(User* u1, Channel* c)
110 Membership* m1 = c->GetUser(u1);
111 return (remove_other_privs && m1 && m1->hasMode(mode));
115 /** Abstraction of FounderProtectBase for channel mode +q
117 class ChanFounder : public ModeHandler, public FounderProtectBase
120 ChanFounder(Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
121 : ModeHandler(Creator, 'q', PARAM_ALWAYS, MODETYPE_CHANNEL),
122 FounderProtectBase('q', "founder", 386, 387, depriv_self, depriv_others)
124 ModeHandler::list = true;
126 levelrequired = FOUNDER_VALUE;
127 m_paramtype = TR_NICK;
130 unsigned int GetPrefixRank()
132 return FOUNDER_VALUE;
135 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
137 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
140 void RemoveMode(Channel* channel, irc::modestacker* stack)
142 FounderProtectBase::RemoveMode(channel, stack);
145 void RemoveMode(User* user, irc::modestacker* stack)
149 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
151 User* theuser = ServerInstance->FindNick(parameter);
153 if (source == theuser && !adding && FounderProtectBase::remove_own_privs)
154 return MOD_RES_ALLOW;
156 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
158 return MOD_RES_PASSTHRU;
162 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
167 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
169 User* theuser = ServerInstance->FindNick(parameter);
172 return MODEACTION_DENY;
173 return MODEACTION_ALLOW;
176 void DisplayList(User* user, Channel* channel)
178 FounderProtectBase::DisplayList(user,channel);
182 /** Abstraction of FounderProtectBase for channel mode +a
184 class ChanProtect : public ModeHandler, public FounderProtectBase
187 ChanProtect(Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
188 : ModeHandler(Creator, 'a', PARAM_ALWAYS, MODETYPE_CHANNEL),
189 FounderProtectBase('a',"protected user", 388, 389, depriv_self, depriv_others)
191 ModeHandler::list = true;
193 levelrequired = PROTECT_VALUE;
194 m_paramtype = TR_NICK;
197 unsigned int GetPrefixRank()
199 return PROTECT_VALUE;
202 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
204 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
207 void RemoveMode(Channel* channel, irc::modestacker* stack)
209 FounderProtectBase::RemoveMode(channel, stack);
212 void RemoveMode(User* user, irc::modestacker* stack)
216 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
218 User* theuser = ServerInstance->FindNick(parameter);
220 if (channel->GetPrefixValue(source) > PROTECT_VALUE)
221 return MOD_RES_ALLOW;
223 // removing own privs?
224 if (source == theuser && !adding && FounderProtectBase::remove_own_privs)
225 return MOD_RES_ALLOW;
227 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
229 return MOD_RES_PASSTHRU;
233 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
238 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
240 User* theuser = ServerInstance->FindNick(parameter);
243 return MODEACTION_DENY;
245 return MODEACTION_ALLOW;
248 void DisplayList(User* user, Channel* channel)
250 FounderProtectBase::DisplayList(user, channel);
255 class ModuleChanProtect : public Module
258 bool FirstInGetsFounder;
269 ModuleChanProtect(InspIRCd* Me)
270 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
272 /* Load config stuff */
276 /* Initialise module variables */
278 cp = new ChanProtect(this, APrefix, DeprivSelf, DeprivOthers);
279 cf = new ChanFounder(this, QPrefix, DeprivSelf, DeprivOthers);
281 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
285 throw ModuleException("Could not add new modes!");
288 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin };
289 ServerInstance->Modules->Attach(eventlist, this, 3);
294 ConfigReader Conf(ServerInstance);
296 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
298 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
299 QPrefix = qpre.empty() ? 0 : qpre[0];
301 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
302 APrefix = apre.empty() ? 0 : apre[0];
304 if ((APrefix && QPrefix) && APrefix == QPrefix)
305 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
307 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
308 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
310 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
311 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
313 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
314 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
317 ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
319 // if the user is the first user into the channel, mark them as the founder, but only if
320 // the config option for it is set
322 if (FirstInGetsFounder && !chan)
325 return MOD_RES_PASSTHRU;
330 ServerInstance->Modes->DelMode(cp);
331 ServerInstance->Modes->DelMode(cf);
338 return Version("Founder and Protect modes (+qa)", VF_COMMON | VF_VENDOR);
342 MODULE_INIT(ModuleChanProtect)