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 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
92 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
93 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
97 void DisplayList(User* user, Channel* channel)
99 CUList* cl = channel->GetUsers();
100 std::string item = extend+std::string(channel->name);
101 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
103 if (i->first->GetExt(item))
105 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
108 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
111 User* FindAndVerify(std::string ¶meter, Channel* channel)
113 User* theuser = MyInstance->FindNick(parameter);
114 if ((!theuser) || (!channel->HasUser(theuser)))
122 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
124 std::string item = extend+std::string(c->name);
125 return (remove_other_privs && u1->GetExt(item) && u2->GetExt(item));
128 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
130 std::string item = extend+std::string(channel->name);
134 if (!theuser->GetExt(item))
136 theuser->Extend(item);
137 parameter = theuser->nick;
138 return MODEACTION_ALLOW;
143 if (theuser->GetExt(item))
145 theuser->Shrink(item);
146 parameter = theuser->nick;
147 return MODEACTION_ALLOW;
150 return MODEACTION_DENY;
154 /** Abstraction of FounderProtectBase for channel mode +q
156 class ChanFounder : public ModeHandler, public FounderProtectBase
159 ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
160 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
161 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
163 unsigned int GetPrefixRank()
165 return FOUNDER_VALUE;
168 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
170 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
173 void RemoveMode(Channel* channel, irc::modestacker* stack)
175 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
178 void RemoveMode(User* user, irc::modestacker* stack)
182 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
184 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
188 return MODEACTION_DENY;
191 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
193 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
197 Module *Override = ServerInstance->Modules->FindFeature("Override");
200 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
201 const char * tmp = ovr.Send();
204 // source is a server, or ulined, we'll let them +-q the user.
205 if (source == ServerInstance->FakeClient ||
206 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
207 (ServerInstance->ULine(source->nick.c_str())) ||
208 (ServerInstance->ULine(source->server)) ||
209 (!*source->server) ||
210 (!IS_LOCAL(source)) ||
213 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
217 // whoops, someones being naughty!
218 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
220 return MODEACTION_DENY;
224 void DisplayList(User* user, Channel* channel)
226 FounderProtectBase::DisplayList(user,channel);
230 /** Abstraction of FounderProtectBase for channel mode +a
232 class ChanProtect : public ModeHandler, public FounderProtectBase
235 ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
236 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
237 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
239 unsigned int GetPrefixRank()
241 return PROTECT_VALUE;
244 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
246 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
249 void RemoveMode(Channel* channel, irc::modestacker* stack)
251 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
254 void RemoveMode(User* user, irc::modestacker* stack)
258 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
260 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
263 return MODEACTION_DENY;
265 std::string founder = "cm_founder_"+std::string(channel->name);
267 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
269 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
273 Module *Override = ServerInstance->Modules->FindFeature("Override");
276 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
277 const char * tmp = ovr.Send();
280 // source has +q, is a server, or ulined, we'll let them +-a the user.
281 if (source == ServerInstance->FakeClient ||
282 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
283 (ServerInstance->ULine(source->nick.c_str())) ||
284 (ServerInstance->ULine(source->server)) ||
285 (!*source->server) ||
286 (source->GetExt(founder)) ||
287 (!IS_LOCAL(source)) ||
291 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
295 // bzzzt, wrong answer!
296 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
297 return MODEACTION_DENY;
301 virtual void DisplayList(User* user, Channel* channel)
303 FounderProtectBase::DisplayList(user, channel);
308 class ModuleChanProtect : public Module
311 bool FirstInGetsFounder;
322 ModuleChanProtect(InspIRCd* Me)
323 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
325 /* Load config stuff */
329 /* Initialise module variables */
331 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
332 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
334 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
338 throw ModuleException("Could not add new modes!");
341 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnAccessCheck };
342 ServerInstance->Modules->Attach(eventlist, this, 4);
345 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
347 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
348 user->Shrink("cm_founder_"+std::string(chan->name));
349 user->Shrink("cm_protect_"+std::string(chan->name));
352 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
354 // FIX: when someone parts a channel we must remove their Extensibles!
355 user->Shrink("cm_founder_"+std::string(channel->name));
356 user->Shrink("cm_protect_"+std::string(channel->name));
361 ConfigReader Conf(ServerInstance);
363 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
365 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
366 QPrefix = qpre.empty() ? 0 : qpre[0];
368 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
369 APrefix = apre.empty() ? 0 : apre[0];
371 if ((APrefix && QPrefix) && APrefix == QPrefix)
372 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
374 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
375 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
377 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
378 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
380 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
381 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
384 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
386 // if the user is the first user into the channel, mark them as the founder, but only if
387 // the config option for it is set
389 if (FirstInGetsFounder && !chan)
390 privs = std::string(1, QPrefix) + "@";
395 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
397 // here we perform access checks, this is the important bit that actually stops kicking/deopping
398 // etc of protected users. There are many types of access check, we're going to handle
399 // a relatively small number of them relevent to our module using a switch statement.
400 // don't allow action if:
401 // (A) Theyre founder (no matter what)
402 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
403 // always allow the action if:
404 // (A) The source is ulined
406 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
407 // without any access checks, we're not worthy :p
408 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
411 std::string founder("cm_founder_"+channel->name);
412 std::string protect("cm_protect_"+channel->name);
414 // Can do anything to yourself if deprotectself is enabled.
415 if (DeprivSelf && source == dest)
418 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
419 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
423 // a user has been deopped. Do we let them? hmmm...
425 if (dest->GetExt(founder) && !candepriv_founder)
427 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
430 if ((dest->GetExt(protect)) && !candepriv_protected)
432 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
437 // a user is being kicked. do we chop off the end of the army boot?
439 if (dest->GetExt(founder) && !candepriv_founder)
441 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
444 if ((dest->GetExt(protect)) && !candepriv_protected)
446 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
451 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
453 if (dest->GetExt(founder) && !candepriv_founder)
455 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
458 if ((dest->GetExt(protect)) && !candepriv_protected)
460 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
465 // same with devoice.
467 if (dest->GetExt(founder) && !candepriv_founder)
469 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
472 if ((dest->GetExt(protect)) && !candepriv_protected)
474 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
480 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
484 virtual ~ModuleChanProtect()
486 ServerInstance->Modes->DelMode(cp);
487 ServerInstance->Modes->DelMode(cf);
492 virtual Version GetVersion()
494 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
498 MODULE_INIT(ModuleChanProtect)