1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides channel modes +a and +q */
18 #define PROTECT_VALUE 40000
19 #define FOUNDER_VALUE 50000
21 /** Handles basic operation of +qa channel modes
23 class FounderProtectBase
32 bool& remove_own_privs;
33 bool& remove_other_privs;
35 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
36 MyInstance(Instance), extend(ext), type(mtype), 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 = MyInstance->FindNick(parameter);
45 if (!channel->HasUser(x))
47 return std::make_pair(false, parameter);
51 std::string item = extend+std::string(channel->name);
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, char mc, irc::modestacker* stack)
67 CUList* cl = channel->GetUsers();
68 std::string item = extend + std::string(channel->name);
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 (CUList::iterator i = cl->begin(); i != cl->end(); i++)
76 if (i->first->GetExt(item))
79 stack->Push(mc, i->first->nick);
81 modestack.Push(mc, i->first->nick);
88 while (modestack.GetStackedLine(stackresult))
90 for (size_t j = 0; j < stackresult.size(); j++)
92 mode_junk.push_back(stackresult[j]);
94 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
98 void DisplayList(User* user, Channel* channel)
100 CUList* cl = channel->GetUsers();
101 std::string item = extend+std::string(channel->name);
102 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
104 if (i->first->GetExt(item))
106 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
109 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
112 User* FindAndVerify(std::string ¶meter, Channel* channel)
114 User* theuser = MyInstance->FindNick(parameter);
115 if ((!theuser) || (!channel->HasUser(theuser)))
123 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
125 std::string item = extend+std::string(c->name);
126 return (remove_other_privs && u1->GetExt(item) && u2->GetExt(item));
129 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
131 std::string item = extend+std::string(channel->name);
135 if (!theuser->GetExt(item))
137 theuser->Extend(item);
138 parameter = theuser->nick;
139 return MODEACTION_ALLOW;
144 if (theuser->GetExt(item))
146 theuser->Shrink(item);
147 parameter = theuser->nick;
148 return MODEACTION_ALLOW;
151 return MODEACTION_DENY;
155 /** Abstraction of FounderProtectBase for channel mode +q
157 class ChanFounder : public ModeHandler, public FounderProtectBase
160 ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
161 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
162 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
164 unsigned int GetPrefixRank()
166 return FOUNDER_VALUE;
169 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
171 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
174 void RemoveMode(Channel* channel, irc::modestacker* stack)
176 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
179 void RemoveMode(User* user, irc::modestacker* stack)
183 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
185 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
189 return MODEACTION_DENY;
192 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
194 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
196 // source is a server, or ulined, we'll let them +-q the user.
197 if (source == ServerInstance->FakeClient ||
198 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
199 (ServerInstance->ULine(source->nick.c_str())) ||
200 (ServerInstance->ULine(source->server)) ||
201 (!*source->server) ||
204 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
208 // whoops, someones being naughty!
209 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
211 return MODEACTION_DENY;
215 void DisplayList(User* user, Channel* channel)
217 FounderProtectBase::DisplayList(user,channel);
221 /** Abstraction of FounderProtectBase for channel mode +a
223 class ChanProtect : public ModeHandler, public FounderProtectBase
226 ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
227 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
228 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
230 unsigned int GetPrefixRank()
232 return PROTECT_VALUE;
235 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
237 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
240 void RemoveMode(Channel* channel, irc::modestacker* stack)
242 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
245 void RemoveMode(User* user, irc::modestacker* stack)
249 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
251 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
254 return MODEACTION_DENY;
256 std::string founder = "cm_founder_"+std::string(channel->name);
258 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
260 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
262 // source has +q, is a server, or ulined, we'll let them +-a the user.
263 if (source == ServerInstance->FakeClient ||
264 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
265 (ServerInstance->ULine(source->nick.c_str())) ||
266 (ServerInstance->ULine(source->server)) ||
267 (!*source->server) ||
268 (source->GetExt(founder)) ||
271 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
275 // bzzzt, wrong answer!
276 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
277 return MODEACTION_DENY;
281 virtual void DisplayList(User* user, Channel* channel)
283 FounderProtectBase::DisplayList(user, channel);
288 class ModuleChanProtect : public Module
291 bool FirstInGetsFounder;
302 ModuleChanProtect(InspIRCd* Me)
303 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
305 /* Load config stuff */
309 /* Initialise module variables */
311 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
312 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
314 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
318 throw ModuleException("Could not add new modes!");
321 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
322 ServerInstance->Modules->Attach(eventlist, this, 5);
325 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
327 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
328 user->Shrink("cm_founder_"+std::string(chan->name));
329 user->Shrink("cm_protect_"+std::string(chan->name));
332 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
334 // FIX: when someone parts a channel we must remove their Extensibles!
335 user->Shrink("cm_founder_"+std::string(channel->name));
336 user->Shrink("cm_protect_"+std::string(channel->name));
341 ConfigReader Conf(ServerInstance);
343 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
345 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
346 QPrefix = qpre.empty() ? 0 : qpre[0];
348 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
349 APrefix = apre.empty() ? 0 : apre[0];
351 if ((APrefix && QPrefix) && APrefix == QPrefix)
352 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
354 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
355 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
357 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
358 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
360 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
361 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
364 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
366 // if the user is the first user into the channel, mark them as the founder, but only if
367 // the config option for it is set
369 if (FirstInGetsFounder && !chan)
370 privs = std::string(1, QPrefix) + "@";
375 virtual void OnPostJoin(User *user, Channel *channel)
377 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
378 // before the client is in the channel
380 // This notice was here originally because it was all done prior to the creation of
381 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
382 // not sure it really should be here - ops don't get shown, obviously, and the prefix
383 // will appear in the names list for the user.. remove if desired -Special
385 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
386 user->WriteServ("MODE %s +q %s", channel->name.c_str(), user->nick.c_str());
389 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
391 // here we perform access checks, this is the important bit that actually stops kicking/deopping
392 // etc of protected users. There are many types of access check, we're going to handle
393 // a relatively small number of them relevent to our module using a switch statement.
394 // don't allow action if:
395 // (A) Theyre founder (no matter what)
396 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
397 // always allow the action if:
398 // (A) The source is ulined
400 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
401 // without any access checks, we're not worthy :p
402 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
405 std::string founder("cm_founder_"+channel->name);
406 std::string protect("cm_protect_"+channel->name);
408 // Can do anything to yourself if deprotectself is enabled.
409 if (DeprivSelf && source == dest)
412 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
413 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
417 // a user has been deopped. Do we let them? hmmm...
419 if (dest->GetExt(founder) && !candepriv_founder)
421 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
424 if ((dest->GetExt(protect)) && !candepriv_protected)
426 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
431 // a user is being kicked. do we chop off the end of the army boot?
433 if (dest->GetExt(founder) && !candepriv_founder)
435 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
438 if ((dest->GetExt(protect)) && !candepriv_protected)
440 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
445 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
447 if (dest->GetExt(founder) && !candepriv_founder)
449 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
452 if ((dest->GetExt(protect)) && !candepriv_protected)
454 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
459 // same with devoice.
461 if (dest->GetExt(founder) && !candepriv_founder)
463 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
466 if ((dest->GetExt(protect)) && !candepriv_protected)
468 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
474 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
478 virtual ~ModuleChanProtect()
480 ServerInstance->Modes->DelMode(cp);
481 ServerInstance->Modes->DelMode(cf);
486 virtual Version GetVersion()
488 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
492 MODULE_INIT(ModuleChanProtect)