1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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)
67 CUList* cl = channel->GetUsers();
68 std::string item = extend + std::string(channel->name);
69 const char* mode_junk[MAXMODES+2];
70 mode_junk[0] = channel->name;
71 irc::modestacker modestack(false);
72 std::deque<std::string> stackresult;
74 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
76 if (i->first->GetExt(item))
78 modestack.Push(mc, i->first->nick);
82 while (modestack.GetStackedLine(stackresult))
84 for (size_t j = 0; j < stackresult.size(); j++)
86 mode_junk[j+1] = stackresult[j].c_str();
88 MyInstance->SendMode(mode_junk, stackresult.size() + 1, MyInstance->FakeClient);
92 void DisplayList(User* user, Channel* channel)
94 CUList* cl = channel->GetUsers();
95 std::string item = extend+std::string(channel->name);
96 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
98 if (i->first->GetExt(item))
100 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
103 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
106 User* FindAndVerify(std::string ¶meter, Channel* channel)
108 User* theuser = MyInstance->FindNick(parameter);
109 if ((!theuser) || (!channel->HasUser(theuser)))
117 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
119 std::string item = extend+std::string(c->name);
120 return (u1->GetExt(item) && u2->GetExt(item));
123 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
125 std::string item = extend+std::string(channel->name);
129 if (!theuser->GetExt(item))
131 theuser->Extend(item);
132 parameter = theuser->nick;
133 return MODEACTION_ALLOW;
138 if (theuser->GetExt(item))
140 theuser->Shrink(item);
141 parameter = theuser->nick;
142 return MODEACTION_ALLOW;
145 return MODEACTION_DENY;
149 /** Abstraction of FounderProtectBase for channel mode +q
151 class ChanFounder : public ModeHandler, public FounderProtectBase
154 ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
155 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
156 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
158 unsigned int GetPrefixRank()
160 return FOUNDER_VALUE;
163 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
165 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
168 void RemoveMode(Channel* channel)
170 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
173 void RemoveMode(User* user)
177 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
179 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
183 return MODEACTION_DENY;
186 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
188 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
190 // source is a server, or ulined, we'll let them +-q the user.
191 if (source == ServerInstance->FakeClient ||
192 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
193 (ServerInstance->ULine(source->nick)) ||
194 (ServerInstance->ULine(source->server)) ||
195 (!*source->server) ||
198 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
202 // whoops, someones being naughty!
203 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q",source->nick, channel->name);
205 return MODEACTION_DENY;
209 void DisplayList(User* user, Channel* channel)
211 FounderProtectBase::DisplayList(user,channel);
215 /** Abstraction of FounderProtectBase for channel mode +a
217 class ChanProtect : public ModeHandler, public FounderProtectBase
220 ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
221 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
222 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
224 unsigned int GetPrefixRank()
226 return PROTECT_VALUE;
229 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
231 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
234 void RemoveMode(Channel* channel)
236 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
239 void RemoveMode(User* user)
243 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
245 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
248 return MODEACTION_DENY;
250 std::string founder = "cm_founder_"+std::string(channel->name);
252 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
254 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
256 // source has +q, is a server, or ulined, we'll let them +-a the user.
257 if (source == ServerInstance->FakeClient ||
258 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
259 (ServerInstance->ULine(source->nick)) ||
260 (ServerInstance->ULine(source->server)) ||
261 (!*source->server) ||
262 (source->GetExt(founder)) ||
265 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
269 // bzzzt, wrong answer!
270 source->WriteNumeric(482, "%s %s :You are not a channel founder",source->nick, channel->name);
271 return MODEACTION_DENY;
275 virtual void DisplayList(User* user, Channel* channel)
277 FounderProtectBase::DisplayList(user, channel);
282 class ModuleChanProtect : public Module
285 bool FirstInGetsFounder;
296 ModuleChanProtect(InspIRCd* Me)
297 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true)
299 /* Load config stuff */
303 /* Initialise module variables */
305 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
306 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
308 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
312 throw ModuleException("Could not add new modes!");
315 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnRehash, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
316 ServerInstance->Modules->Attach(eventlist, this, 6);
319 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
321 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
322 user->Shrink("cm_founder_"+std::string(chan->name));
323 user->Shrink("cm_protect_"+std::string(chan->name));
326 virtual void OnUserPart(User* user, Channel* channel, const std::string &partreason, bool &silent)
328 // FIX: when someone parts a channel we must remove their Extensibles!
329 user->Shrink("cm_founder_"+std::string(channel->name));
330 user->Shrink("cm_protect_"+std::string(channel->name));
333 virtual void OnRehash(User* user, const std::string ¶meter)
335 /* Create a configreader class and read our flag,
336 * in old versions this was heap-allocated and the
337 * object was kept between rehashes...now we just
338 * stack-allocate it locally.
340 ConfigReader Conf(ServerInstance);
342 char old_q = QPrefix;
343 char old_a = APrefix;
345 FirstInGetsFounder = Conf.ReadFlag("options", "noservices", 0);
347 std::string qpre = Conf.ReadValue("options", "qprefix", 0);
348 QPrefix = qpre.empty() ? 0 : qpre[0];
350 std::string apre = Conf.ReadValue("options", "aprefix", 0);
351 APrefix = apre.empty() ? 0 : apre[0];
353 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
354 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
356 ServerInstance->Logs->Log("chanprotect", DEBUG, "qprefix is %c and aprefix is %c", QPrefix, APrefix);
358 /* Did the user change the QA prefixes on the fly?
359 * If so, remove all instances of the mode, and reinit
360 * the module with prefixes enabled.
362 if ((old_q != QPrefix) && (!booting))
364 ServerInstance->Modes->DelMode(cf);
366 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
367 /* These wont fail, we already owned the mode characters before */
368 ServerInstance->Modes->AddMode(cf);
369 ServerInstance->SNO->WriteToSnoMask('A', "WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
372 if ((old_a != APrefix) && (!booting))
374 ServerInstance->Modes->DelMode(cp);
376 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
377 ServerInstance->Modes->AddMode(cp);
378 ServerInstance->SNO->WriteToSnoMask('A', "WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
382 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs)
384 // if the user is the first user into the channel, mark them as the founder, but only if
385 // the config option for it is set
387 if (FirstInGetsFounder && !chan)
388 privs = QPrefix + "@";
393 virtual void OnPostJoin(User *user, Channel *channel)
395 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
396 // before the client is in the channel
398 // This notice was here originally because it was all done prior to the creation of
399 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
400 // not sure it really should be here - ops don't get shown, obviously, and the prefix
401 // will appear in the names list for the user.. remove if desired -Special
403 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
404 user->WriteServ("MODE %s +q %s", channel->name, user->nick);
407 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
409 // here we perform access checks, this is the important bit that actually stops kicking/deopping
410 // etc of protected users. There are many types of access check, we're going to handle
411 // a relatively small number of them relevent to our module using a switch statement.
412 // don't allow action if:
413 // (A) Theyre founder (no matter what)
414 // (B) Theyre protected, and you're not
415 // always allow the action if:
416 // (A) The source is ulined
419 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
420 // without any access checks, we're not worthy :p
421 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
424 std::string founder = "cm_founder_"+std::string(channel->name);
425 std::string protect = "cm_protect_"+std::string(channel->name);
429 // a user has been deopped. Do we let them? hmmm...
431 if (dest->GetExt(founder))
433 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
436 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
438 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
443 // a user is being kicked. do we chop off the end of the army boot?
445 if (dest->GetExt(founder))
447 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
450 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
452 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
457 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
459 if (dest->GetExt(founder))
461 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
464 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
466 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
471 // same with devoice.
473 if (dest->GetExt(founder))
475 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
478 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
480 source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
486 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
490 virtual ~ModuleChanProtect()
492 ServerInstance->Modes->DelMode(cp);
493 ServerInstance->Modes->DelMode(cf);
498 virtual Version GetVersion()
500 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
504 MODULE_INIT(ModuleChanProtect)