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 char moderemove[MAXBUF];
82 userrec* n = new userrec(MyInstance);
83 n->SetFd(FD_MAGIC_NUMBER);
84 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
86 if (i->second->GetExt(item, dummyptr))
88 sprintf(moderemove,"-%c",mc);
89 const char* parameters[] = { channel->name, moderemove, i->second->nick };
90 MyInstance->SendMode(parameters, 3, n);
94 unload_kludge = false;
97 void DisplayList(userrec* user, chanrec* channel)
99 CUList* cl = channel->GetUsers();
100 std::string item = extend+std::string(channel->name);
101 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
103 if (i->second->GetExt(item, dummyptr))
105 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
108 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
111 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
113 userrec* theuser = MyInstance->FindNick(parameter);
114 if ((!theuser) || (!channel->HasUser(theuser)))
122 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
124 std::string item = extend+std::string(channel->name);
128 if (!theuser->GetExt(item, dummyptr))
130 theuser->Extend(item, fakevalue);
131 parameter = theuser->nick;
132 return MODEACTION_ALLOW;
137 if (theuser->GetExt(item, dummyptr))
139 theuser->Shrink(item);
140 parameter = theuser->nick;
141 return MODEACTION_ALLOW;
144 return MODEACTION_DENY;
148 /** Abstraction of FounderProtectBase for channel mode +q
150 class ChanFounder : public ModeHandler, public FounderProtectBase
154 ChanFounder(InspIRCd* Instance, bool using_prefixes)
155 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
156 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
158 unsigned int GetPrefixRank()
160 return FOUNDER_VALUE;
163 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
165 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
168 void RemoveMode(chanrec* channel)
170 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
173 void RemoveMode(userrec* user)
177 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
179 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
183 return MODEACTION_DENY;
186 // source is a server, or ulined, we'll let them +-q the user.
187 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
189 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
193 // whoops, someones being naughty!
194 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
196 return MODEACTION_DENY;
200 void DisplayList(userrec* user, chanrec* channel)
202 FounderProtectBase::DisplayList(user,channel);
206 /** Abstraction of FounderProtectBase for channel mode +a
208 class ChanProtect : public ModeHandler, public FounderProtectBase
212 ChanProtect(InspIRCd* Instance, bool using_prefixes)
213 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
214 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
216 unsigned int GetPrefixRank()
218 return PROTECT_VALUE;
221 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
223 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
226 void RemoveMode(chanrec* channel)
228 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
231 void RemoveMode(userrec* user)
235 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
237 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
240 return MODEACTION_DENY;
242 std::string founder = "cm_founder_"+std::string(channel->name);
244 // source has +q, is a server, or ulined, we'll let them +-a the user.
245 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
247 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
251 // bzzzt, wrong answer!
252 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
253 return MODEACTION_DENY;
257 virtual void DisplayList(userrec* user, chanrec* channel)
259 FounderProtectBase::DisplayList(user, channel);
264 class ModuleChanProtect : public Module
267 bool FirstInGetsFounder;
275 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
277 /* Load config stuff */
280 /* Initialise module variables */
282 cp = new ChanProtect(ServerInstance,QAPrefixes);
283 cf = new ChanFounder(ServerInstance,QAPrefixes);
285 ServerInstance->AddMode(cp, 'a');
286 ServerInstance->AddMode(cf, 'q');
289 void Implements(char* List)
291 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
294 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
296 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
297 user->Shrink("cm_founder_"+std::string(chan->name));
298 user->Shrink("cm_protect_"+std::string(chan->name));
301 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
303 // FIX: when someone parts a channel we must remove their Extensibles!
304 user->Shrink("cm_founder_"+std::string(channel->name));
305 user->Shrink("cm_protect_"+std::string(channel->name));
308 virtual void OnRehash(const std::string ¶meter)
310 /* Create a configreader class and read our flag,
311 * in old versions this was heap-allocated and the
312 * object was kept between rehashes...now we just
313 * stack-allocate it locally.
315 ConfigReader Conf(ServerInstance);
317 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
318 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
321 virtual void OnUserJoin(userrec* user, chanrec* channel)
323 // if the user is the first user into the channel, mark them as the founder, but only if
324 // the config option for it is set
325 if (FirstInGetsFounder)
327 if (channel->GetUserCounter() == 1)
329 // we're using Extensible::Extend to add data into user objects.
330 // this way is best as it adds data thats accessible to other modules
331 // (so long as you document your code properly) without breaking anything
332 // because its encapsulated neatly in a map.
334 // Change requested by katsklaw... when the first in is set to get founder,
335 // to make it clearer that +q has been given, send that one user the +q notice
336 // so that their client's syncronization and their sanity are left intact.
337 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
338 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
340 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
346 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
348 // here we perform access checks, this is the important bit that actually stops kicking/deopping
349 // etc of protected users. There are many types of access check, we're going to handle
350 // a relatively small number of them relevent to our module using a switch statement.
352 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
353 // don't allow action if:
354 // (A) Theyre founder (no matter what)
355 // (B) Theyre protected, and you're not
356 // always allow the action if:
357 // (A) The source is ulined
360 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
361 // without any access checks, we're not worthy :p
362 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
364 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns ALLOW");
368 std::string founder = "cm_founder_"+std::string(channel->name);
369 std::string protect = "cm_protect_"+std::string(channel->name);
373 // a user has been deopped. Do we let them? hmmm...
375 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
376 if (dest->GetExt(founder,dummyptr))
378 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
379 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
384 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
386 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
388 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
393 // a user is being kicked. do we chop off the end of the army boot?
395 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
396 if (dest->GetExt(founder,dummyptr))
398 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
401 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
403 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
408 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
410 if (dest->GetExt(founder,dummyptr))
412 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");
415 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
417 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
422 // same with devoice.
424 if (dest->GetExt(founder,dummyptr))
426 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
429 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
431 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
437 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
438 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns DEFAULT");
442 virtual ~ModuleChanProtect()
444 ServerInstance->Modes->DelMode(cp);
445 ServerInstance->Modes->DelMode(cf);
450 virtual Version GetVersion()
452 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
455 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
457 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
458 * so we dont need to propogate it manually
462 // this is called when the server is linking into a net and wants to sync channel data.
463 // we should send our mode changes for the channel here to ensure that other servers
464 // know whos +q/+a on the channel.
465 CUList* cl = chan->GetUsers();
466 string_list commands;
467 std::string founder = "cm_founder_"+std::string(chan->name);
468 std::string protect = "cm_protect_"+std::string(chan->name);
469 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
471 if (i->second->GetExt(founder,dummyptr))
473 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
475 if (i->second->GetExt(protect,dummyptr))
477 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
487 class ModuleChanProtectFactory : public ModuleFactory
490 ModuleChanProtectFactory()
494 ~ModuleChanProtectFactory()
498 virtual Module * CreateModule(InspIRCd* Me)
500 return new ModuleChanProtect(Me);
506 extern "C" void * init_module( void )
508 return new ModuleChanProtectFactory;