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
33 bool& remove_own_privs;
34 bool& remove_other_privs;
36 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
37 MyInstance(Instance), extend(ext), type(mtype), 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 if (!channel->HasUser(x))
48 return std::make_pair(false, parameter);
52 std::string item = extend+std::string(channel->name);
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, char mc, irc::modestacker* stack)
68 CUList* cl = channel->GetUsers();
69 std::string item = extend + std::string(channel->name);
70 std::vector<std::string> mode_junk;
71 mode_junk.push_back(channel->name);
72 irc::modestacker modestack(MyInstance, false);
73 std::deque<std::string> stackresult;
75 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
77 if (i->first->GetExt(item))
80 stack->Push(mc, i->first->nick);
82 modestack.Push(mc, i->first->nick);
89 while (modestack.GetStackedLine(stackresult))
91 for (size_t j = 0; j < stackresult.size(); j++)
93 mode_junk.push_back(stackresult[j]);
95 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
99 void DisplayList(User* user, Channel* channel)
101 CUList* cl = channel->GetUsers();
102 std::string item = extend+std::string(channel->name);
103 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
105 if (i->first->GetExt(item))
107 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
110 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
113 User* FindAndVerify(std::string ¶meter, Channel* channel)
115 User* theuser = MyInstance->FindNick(parameter);
116 if ((!theuser) || (!channel->HasUser(theuser)))
124 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
126 std::string item = extend+std::string(c->name);
127 return (remove_other_privs && u1->GetExt(item) && u2->GetExt(item));
130 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
132 std::string item = extend+std::string(channel->name);
136 if (!theuser->GetExt(item))
138 theuser->Extend(item);
139 parameter = theuser->nick;
140 return MODEACTION_ALLOW;
145 if (theuser->GetExt(item))
147 theuser->Shrink(item);
148 parameter = theuser->nick;
149 return MODEACTION_ALLOW;
152 return MODEACTION_DENY;
156 /** Abstraction of FounderProtectBase for channel mode +q
158 class ChanFounder : public ModeHandler, public FounderProtectBase
161 ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
162 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
163 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
165 unsigned int GetPrefixRank()
167 return FOUNDER_VALUE;
170 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
172 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
175 void RemoveMode(Channel* channel, irc::modestacker* stack)
177 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
180 void RemoveMode(User* user, irc::modestacker* stack)
184 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
186 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
190 return MODEACTION_DENY;
193 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
195 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
199 Module *Override = ServerInstance->Modules->FindFeature("Override");
202 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
203 const char * tmp = ovr.Send();
206 // source is a server, or ulined, we'll let them +-q the user.
207 if (source == ServerInstance->FakeClient ||
208 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
209 (ServerInstance->ULine(source->nick.c_str())) ||
210 (ServerInstance->ULine(source->server)) ||
211 (!*source->server) ||
212 (!IS_LOCAL(source)) ||
215 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
219 // whoops, someones being naughty!
220 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
222 return MODEACTION_DENY;
226 void DisplayList(User* user, Channel* channel)
228 FounderProtectBase::DisplayList(user,channel);
232 /** Abstraction of FounderProtectBase for channel mode +a
234 class ChanProtect : public ModeHandler, public FounderProtectBase
237 ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
238 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
239 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
241 unsigned int GetPrefixRank()
243 return PROTECT_VALUE;
246 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
248 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
251 void RemoveMode(Channel* channel, irc::modestacker* stack)
253 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
256 void RemoveMode(User* user, irc::modestacker* stack)
260 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
262 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
265 return MODEACTION_DENY;
267 std::string founder = "cm_founder_"+std::string(channel->name);
269 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
271 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
275 Module *Override = ServerInstance->Modules->FindFeature("Override");
278 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
279 const char * tmp = ovr.Send();
282 // source has +q, is a server, or ulined, we'll let them +-a the user.
283 if (source == ServerInstance->FakeClient ||
284 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
285 (ServerInstance->ULine(source->nick.c_str())) ||
286 (ServerInstance->ULine(source->server)) ||
287 (!*source->server) ||
288 (source->GetExt(founder)) ||
289 (!IS_LOCAL(source)) ||
293 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
297 // bzzzt, wrong answer!
298 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
299 return MODEACTION_DENY;
303 virtual void DisplayList(User* user, Channel* channel)
305 FounderProtectBase::DisplayList(user, channel);
310 class ModuleChanProtect : public Module
313 bool FirstInGetsFounder;
324 ModuleChanProtect(InspIRCd* Me)
325 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
327 /* Load config stuff */
331 /* Initialise module variables */
333 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
334 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
336 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
340 throw ModuleException("Could not add new modes!");
343 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
344 ServerInstance->Modules->Attach(eventlist, this, 5);
347 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
349 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
350 user->Shrink("cm_founder_"+std::string(chan->name));
351 user->Shrink("cm_protect_"+std::string(chan->name));
354 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
356 // FIX: when someone parts a channel we must remove their Extensibles!
357 user->Shrink("cm_founder_"+std::string(channel->name));
358 user->Shrink("cm_protect_"+std::string(channel->name));
363 ConfigReader Conf(ServerInstance);
365 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
367 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
368 QPrefix = qpre.empty() ? 0 : qpre[0];
370 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
371 APrefix = apre.empty() ? 0 : apre[0];
373 if ((APrefix && QPrefix) && APrefix == QPrefix)
374 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
376 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
377 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
379 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
380 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
382 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
383 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
386 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
388 // if the user is the first user into the channel, mark them as the founder, but only if
389 // the config option for it is set
391 if (FirstInGetsFounder && !chan)
392 privs = std::string(1, QPrefix) + "@";
397 virtual void OnPostJoin(User *user, Channel *channel)
399 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
400 // before the client is in the channel
402 // This notice was here originally because it was all done prior to the creation of
403 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
404 // not sure it really should be here - ops don't get shown, obviously, and the prefix
405 // will appear in the names list for the user.. remove if desired -Special
407 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
408 user->WriteServ("MODE %s +q %s", channel->name.c_str(), user->nick.c_str());
411 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
413 // here we perform access checks, this is the important bit that actually stops kicking/deopping
414 // etc of protected users. There are many types of access check, we're going to handle
415 // a relatively small number of them relevent to our module using a switch statement.
416 // don't allow action if:
417 // (A) Theyre founder (no matter what)
418 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
419 // always allow the action if:
420 // (A) The source is ulined
422 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
423 // without any access checks, we're not worthy :p
424 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
427 std::string founder("cm_founder_"+channel->name);
428 std::string protect("cm_protect_"+channel->name);
430 // Can do anything to yourself if deprotectself is enabled.
431 if (DeprivSelf && source == dest)
434 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
435 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
439 // a user has been deopped. Do we let them? hmmm...
441 if (dest->GetExt(founder) && !candepriv_founder)
443 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
446 if ((dest->GetExt(protect)) && !candepriv_protected)
448 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
453 // a user is being kicked. do we chop off the end of the army boot?
455 if (dest->GetExt(founder) && !candepriv_founder)
457 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
460 if ((dest->GetExt(protect)) && !candepriv_protected)
462 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
467 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
469 if (dest->GetExt(founder) && !candepriv_founder)
471 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
474 if ((dest->GetExt(protect)) && !candepriv_protected)
476 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
481 // same with devoice.
483 if (dest->GetExt(founder) && !candepriv_founder)
485 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
488 if ((dest->GetExt(protect)) && !candepriv_protected)
490 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
496 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
500 virtual ~ModuleChanProtect()
502 ServerInstance->Modes->DelMode(cp);
503 ServerInstance->Modes->DelMode(cf);
508 virtual Version GetVersion()
510 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
514 MODULE_INIT(ModuleChanProtect)