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 InspIRCd* const MyInstance;
28 const std::string type;
33 bool& remove_own_privs;
34 bool& remove_other_privs;
36 FounderProtectBase(InspIRCd* Instance, char Mode, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
37 MyInstance(Instance), type(mtype), mode(Mode), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
41 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
43 User* x = MyInstance->FindNick(parameter);
46 Membership* memb = channel->GetUser(x);
49 return std::make_pair(false, parameter);
53 if (memb->hasMode(mode))
55 return std::make_pair(true, x->nick);
59 return std::make_pair(false, parameter);
63 return std::make_pair(false, parameter);
66 void RemoveMode(Channel* channel, irc::modestacker* stack)
68 const UserMembList* cl = channel->GetUsers();
69 std::vector<std::string> mode_junk;
70 mode_junk.push_back(channel->name);
71 irc::modestacker modestack(MyInstance, false);
72 std::deque<std::string> stackresult;
74 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
76 if (i->second->hasMode(mode))
79 stack->Push(mode, i->first->nick);
81 modestack.Push(mode, i->first->nick);
88 while (modestack.GetStackedLine(stackresult))
90 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
91 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
92 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
96 void DisplayList(User* user, Channel* channel)
98 const UserMembList* cl = channel->GetUsers();
99 for (UserMembCIter i = cl->begin(); i != cl->end(); ++i)
101 if (i->second->hasMode(mode))
103 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
106 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
109 bool CanRemoveOthers(User* u1, Channel* c)
111 Membership* m1 = c->GetUser(u1);
112 return (remove_other_privs && m1 && m1->hasMode(mode));
116 /** Abstraction of FounderProtectBase for channel mode +q
118 class ChanFounder : public ModeHandler, public FounderProtectBase
121 ChanFounder(InspIRCd* Instance, Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
122 : ModeHandler(Creator, 'q', PARAM_ALWAYS, MODETYPE_CHANNEL),
123 FounderProtectBase(Instance, 'q', "founder", 386, 387, depriv_self, depriv_others)
125 ModeHandler::list = true;
127 levelrequired = FOUNDER_VALUE;
128 m_paramtype = TR_NICK;
131 unsigned int GetPrefixRank()
133 return FOUNDER_VALUE;
136 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
138 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
141 void RemoveMode(Channel* channel, irc::modestacker* stack)
143 FounderProtectBase::RemoveMode(channel, stack);
146 void RemoveMode(User* user, irc::modestacker* stack)
150 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
152 User* theuser = ServerInstance->FindNick(parameter);
156 return MODEACTION_DENY;
159 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, channel))
161 return MODEACTION_ALLOW;
165 Module *Override = ServerInstance->Modules->FindFeature("Override");
168 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
169 const char * tmp = ovr.Send();
172 // source is a server, or ulined, we'll let them +-q the user.
173 if (!IS_LOCAL(source) ||
174 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
175 (ServerInstance->ULine(source->nick.c_str())) ||
176 (ServerInstance->ULine(source->server)) ||
177 (!*source->server) ||
180 return MODEACTION_ALLOW;
184 // whoops, someones being naughty!
185 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
187 return MODEACTION_DENY;
191 void DisplayList(User* user, Channel* channel)
193 FounderProtectBase::DisplayList(user,channel);
197 /** Abstraction of FounderProtectBase for channel mode +a
199 class ChanProtect : public ModeHandler, public FounderProtectBase
202 ChanProtect(InspIRCd* Instance, Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
203 : ModeHandler(Creator, 'a', PARAM_ALWAYS, MODETYPE_CHANNEL),
204 FounderProtectBase(Instance,'a',"protected user", 388, 389, depriv_self, depriv_others)
206 ModeHandler::list = true;
208 levelrequired = PROTECT_VALUE;
209 m_paramtype = TR_NICK;
212 unsigned int GetPrefixRank()
214 return PROTECT_VALUE;
217 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
219 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
222 void RemoveMode(Channel* channel, irc::modestacker* stack)
224 FounderProtectBase::RemoveMode(channel, stack);
227 void RemoveMode(User* user, irc::modestacker* stack)
231 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
233 User* theuser = ServerInstance->FindNick(parameter);
236 return MODEACTION_DENY;
238 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, channel))
240 return MODEACTION_ALLOW;
244 Module *Override = ServerInstance->Modules->FindFeature("Override");
247 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
248 const char * tmp = ovr.Send();
251 // source has +q, is a server, or ulined, we'll let them +-a the user.
252 if (!IS_LOCAL(source) ||
253 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
254 (ServerInstance->ULine(source->nick.c_str())) ||
255 (ServerInstance->ULine(source->server)) ||
256 (!*source->server) ||
257 (channel->GetPrefixValue(source) > PROTECT_VALUE) ||
261 return MODEACTION_ALLOW;
265 // bzzzt, wrong answer!
266 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
267 return MODEACTION_DENY;
271 void DisplayList(User* user, Channel* channel)
273 FounderProtectBase::DisplayList(user, channel);
278 class ModuleChanProtect : public Module
281 bool FirstInGetsFounder;
292 ModuleChanProtect(InspIRCd* Me)
293 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
295 /* Load config stuff */
299 /* Initialise module variables */
301 cp = new ChanProtect(ServerInstance, this, APrefix, DeprivSelf, DeprivOthers);
302 cf = new ChanFounder(ServerInstance, this, QPrefix, DeprivSelf, DeprivOthers);
304 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
308 throw ModuleException("Could not add new modes!");
311 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin };
312 ServerInstance->Modules->Attach(eventlist, this, 3);
317 ConfigReader Conf(ServerInstance);
319 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
321 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
322 QPrefix = qpre.empty() ? 0 : qpre[0];
324 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
325 APrefix = apre.empty() ? 0 : apre[0];
327 if ((APrefix && QPrefix) && APrefix == QPrefix)
328 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
330 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
331 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
333 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
334 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
336 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
337 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
340 ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
342 // if the user is the first user into the channel, mark them as the founder, but only if
343 // the config option for it is set
345 if (FirstInGetsFounder && !chan)
348 return MOD_RES_PASSTHRU;
353 ServerInstance->Modes->DelMode(cp);
354 ServerInstance->Modes->DelMode(cf);
361 return Version("Founder and Protect modes (+qa)", VF_COMMON | VF_VENDOR);
365 MODULE_INIT(ModuleChanProtect)