1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
22 /* $ModDesc: Provides channel modes +a and +q */
24 #define PROTECT_VALUE 40000
25 #define FOUNDER_VALUE 50000
27 const char* fakevalue = "on";
29 /* When this is set to true, no restrictions apply to setting or
30 * removal of +qa. This is used while unloading so that the server
31 * can freely clear all of its users of the modes.
33 bool unload_kludge = false;
35 /** Handles basic operation of +qa channel modes
37 class FounderProtectBase
47 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e) : MyInstance(Instance), extend(ext), type(mtype), list(l), end(e)
51 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
53 userrec* x = MyInstance->FindNick(parameter);
56 if (!channel->HasUser(x))
58 return std::make_pair(false, parameter);
62 std::string item = extend+std::string(channel->name);
63 if (x->GetExt(item,dummyptr))
65 return std::make_pair(true, x->nick);
69 return std::make_pair(false, parameter);
73 return std::make_pair(false, parameter);
76 void RemoveMode(chanrec* channel, char mc)
79 CUList* cl = channel->GetUsers();
80 std::string item = extend + std::string(channel->name);
81 const char* mode_junk[MAXMODES+1];
82 userrec* n = new userrec(MyInstance);
83 n->SetFd(FD_MAGIC_NUMBER);
84 mode_junk[0] = channel->name;
85 irc::modestacker modestack(false);
86 std::deque<std::string> stackresult;
87 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
89 if (i->second->GetExt(item, dummyptr))
91 modestack.Push(mc, i->second->nick);
95 while (modestack.GetStackedLine(stackresult))
97 for (size_t j = 0; j < stackresult.size(); j++)
99 mode_junk[j+1] = stackresult[j].c_str();
101 MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
105 unload_kludge = false;
108 void DisplayList(userrec* user, chanrec* channel)
110 CUList* cl = channel->GetUsers();
111 std::string item = extend+std::string(channel->name);
112 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
114 if (i->second->GetExt(item, dummyptr))
116 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
119 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
122 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
124 userrec* theuser = MyInstance->FindNick(parameter);
125 if ((!theuser) || (!channel->HasUser(theuser)))
133 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
135 std::string item = extend+std::string(channel->name);
139 if (!theuser->GetExt(item, dummyptr))
141 theuser->Extend(item, fakevalue);
142 parameter = theuser->nick;
143 return MODEACTION_ALLOW;
148 if (theuser->GetExt(item, dummyptr))
150 theuser->Shrink(item);
151 parameter = theuser->nick;
152 return MODEACTION_ALLOW;
155 return MODEACTION_DENY;
159 /** Abstraction of FounderProtectBase for channel mode +q
161 class ChanFounder : public ModeHandler, public FounderProtectBase
165 ChanFounder(InspIRCd* Instance, bool using_prefixes)
166 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
167 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
169 unsigned int GetPrefixRank()
171 return FOUNDER_VALUE;
174 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
176 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
179 void RemoveMode(chanrec* channel)
181 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
184 void RemoveMode(userrec* user)
188 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
190 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
194 return MODEACTION_DENY;
197 // source is a server, or ulined, we'll let them +-q the user.
198 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
200 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
204 // whoops, someones being naughty!
205 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
207 return MODEACTION_DENY;
211 void DisplayList(userrec* user, chanrec* channel)
213 FounderProtectBase::DisplayList(user,channel);
217 /** Abstraction of FounderProtectBase for channel mode +a
219 class ChanProtect : public ModeHandler, public FounderProtectBase
223 ChanProtect(InspIRCd* Instance, bool using_prefixes)
224 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
225 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
227 unsigned int GetPrefixRank()
229 return PROTECT_VALUE;
232 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
234 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
237 void RemoveMode(chanrec* channel)
239 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
242 void RemoveMode(userrec* user)
246 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
248 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
251 return MODEACTION_DENY;
253 std::string founder = "cm_founder_"+std::string(channel->name);
255 // source has +q, is a server, or ulined, we'll let them +-a the user.
256 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
258 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
262 // bzzzt, wrong answer!
263 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
264 return MODEACTION_DENY;
268 virtual void DisplayList(userrec* user, chanrec* channel)
270 FounderProtectBase::DisplayList(user, channel);
275 class ModuleChanProtect : public Module
278 bool FirstInGetsFounder;
286 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
288 /* Load config stuff */
291 /* Initialise module variables */
293 cp = new ChanProtect(ServerInstance,QAPrefixes);
294 cf = new ChanFounder(ServerInstance,QAPrefixes);
296 ServerInstance->AddMode(cp, 'a');
297 ServerInstance->AddMode(cf, 'q');
300 void Implements(char* List)
302 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
305 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
307 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
308 user->Shrink("cm_founder_"+std::string(chan->name));
309 user->Shrink("cm_protect_"+std::string(chan->name));
312 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
314 // FIX: when someone parts a channel we must remove their Extensibles!
315 user->Shrink("cm_founder_"+std::string(channel->name));
316 user->Shrink("cm_protect_"+std::string(channel->name));
319 virtual void OnRehash(const std::string ¶meter)
321 /* Create a configreader class and read our flag,
322 * in old versions this was heap-allocated and the
323 * object was kept between rehashes...now we just
324 * stack-allocate it locally.
326 ConfigReader Conf(ServerInstance);
328 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
329 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
332 virtual void OnUserJoin(userrec* user, chanrec* channel)
334 // if the user is the first user into the channel, mark them as the founder, but only if
335 // the config option for it is set
336 if (FirstInGetsFounder)
338 if (channel->GetUserCounter() == 1)
340 // we're using Extensible::Extend to add data into user objects.
341 // this way is best as it adds data thats accessible to other modules
342 // (so long as you document your code properly) without breaking anything
343 // because its encapsulated neatly in a map.
345 // Change requested by katsklaw... when the first in is set to get founder,
346 // to make it clearer that +q has been given, send that one user the +q notice
347 // so that their client's syncronization and their sanity are left intact.
348 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
349 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
351 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
357 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
359 // here we perform access checks, this is the important bit that actually stops kicking/deopping
360 // etc of protected users. There are many types of access check, we're going to handle
361 // a relatively small number of them relevent to our module using a switch statement.
363 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
364 // don't allow action if:
365 // (A) Theyre founder (no matter what)
366 // (B) Theyre protected, and you're not
367 // always allow the action if:
368 // (A) The source is ulined
371 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
372 // without any access checks, we're not worthy :p
373 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
375 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns ALLOW");
379 std::string founder = "cm_founder_"+std::string(channel->name);
380 std::string protect = "cm_protect_"+std::string(channel->name);
384 // a user has been deopped. Do we let them? hmmm...
386 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
387 if (dest->GetExt(founder,dummyptr))
389 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
390 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
395 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
397 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
399 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
404 // a user is being kicked. do we chop off the end of the army boot?
406 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
407 if (dest->GetExt(founder,dummyptr))
409 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
412 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
414 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
419 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
421 if (dest->GetExt(founder,dummyptr))
423 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
426 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
428 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
433 // same with devoice.
435 if (dest->GetExt(founder,dummyptr))
437 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
440 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
442 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
448 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
449 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns DEFAULT");
453 virtual ~ModuleChanProtect()
455 ServerInstance->Modes->DelMode(cp);
456 ServerInstance->Modes->DelMode(cf);
461 virtual Version GetVersion()
463 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
466 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
468 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
469 * so we dont need to propogate it manually
473 // this is called when the server is linking into a net and wants to sync channel data.
474 // we should send our mode changes for the channel here to ensure that other servers
475 // know whos +q/+a on the channel.
476 CUList* cl = chan->GetUsers();
477 string_list commands;
478 std::string founder = "cm_founder_"+std::string(chan->name);
479 std::string protect = "cm_protect_"+std::string(chan->name);
480 irc::modestacker modestack(true);
481 std::deque<std::string> stackresult;
482 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
484 if (i->second->GetExt(founder,dummyptr))
486 modestack.Push('q',i->second->nick);
488 if (i->second->GetExt(protect,dummyptr))
490 modestack.Push('a',i->second->nick);
493 while (modestack.GetStackedLine(stackresult))
495 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
496 std::string line = mode_join.GetJoined();
497 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
505 class ModuleChanProtectFactory : public ModuleFactory
508 ModuleChanProtectFactory()
512 ~ModuleChanProtectFactory()
516 virtual Module * CreateModule(InspIRCd* Me)
518 return new ModuleChanProtect(Me);
524 extern "C" void * init_module( void )
526 return new ModuleChanProtectFactory;