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 * ---------------------------------------------------
23 /* $ModDesc: Provides channel modes +a and +q */
25 #define PROTECT_VALUE 40000
26 #define FOUNDER_VALUE 50000
28 const char* fakevalue = "on";
30 class FounderProtectBase
40 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)
44 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
46 userrec* x = MyInstance->FindNick(parameter);
49 if (!channel->HasUser(x))
51 return std::make_pair(false, parameter);
55 std::string item = extend+std::string(channel->name);
56 if (x->GetExt(item,dummyptr))
58 return std::make_pair(true, x->nick);
62 return std::make_pair(false, parameter);
66 return std::make_pair(false, parameter);
69 void DisplayList(userrec* user, chanrec* channel)
71 CUList* cl = channel->GetUsers();
72 std::string item = extend+std::string(channel->name);
73 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
75 if (i->second->GetExt(item, dummyptr))
77 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
80 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
83 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
85 userrec* theuser = MyInstance->FindNick(parameter);
86 if ((!theuser) || (!channel->HasUser(theuser)))
94 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
96 std::string item = extend+std::string(channel->name);
100 if (!theuser->GetExt(item, dummyptr))
102 theuser->Extend(item, fakevalue);
103 parameter = theuser->nick;
104 return MODEACTION_ALLOW;
109 if (theuser->GetExt(item, dummyptr))
111 theuser->Shrink(item);
112 parameter = theuser->nick;
113 return MODEACTION_ALLOW;
116 return MODEACTION_DENY;
120 class ChanFounder : public ModeHandler, public FounderProtectBase
124 ChanFounder(InspIRCd* Instance, bool using_prefixes)
125 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
126 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
128 unsigned int GetPrefixRank()
130 return FOUNDER_VALUE;
133 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
135 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
138 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
140 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
144 return MODEACTION_DENY;
147 // source is a server, or ulined, we'll let them +-q the user.
148 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
150 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
154 // whoops, someones being naughty!
155 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
157 return MODEACTION_DENY;
161 void DisplayList(userrec* user, chanrec* channel)
163 FounderProtectBase::DisplayList(user,channel);
167 class ChanProtect : public ModeHandler, public FounderProtectBase
171 ChanProtect(InspIRCd* Instance, bool using_prefixes)
172 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
173 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
175 unsigned int GetPrefixRank()
177 return PROTECT_VALUE;
180 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
182 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
185 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
187 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
190 return MODEACTION_DENY;
192 std::string founder = "cm_founder_"+std::string(channel->name);
194 // source has +q, is a server, or ulined, we'll let them +-a the user.
195 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
197 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
201 // bzzzt, wrong answer!
202 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
203 return MODEACTION_DENY;
207 virtual void DisplayList(userrec* user, chanrec* channel)
209 FounderProtectBase::DisplayList(user, channel);
214 class ModuleChanProtect : public Module
217 bool FirstInGetsFounder;
225 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
227 /* Load config stuff */
230 /* Initialise module variables */
232 cp = new ChanProtect(ServerInstance,QAPrefixes);
233 cf = new ChanFounder(ServerInstance,QAPrefixes);
235 ServerInstance->AddMode(cp, 'a');
236 ServerInstance->AddMode(cf, 'q');
239 void Implements(char* List)
241 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
244 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
246 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
247 user->Shrink("cm_founder_"+std::string(chan->name));
248 user->Shrink("cm_protect_"+std::string(chan->name));
251 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
253 // FIX: when someone parts a channel we must remove their Extensibles!
254 user->Shrink("cm_founder_"+std::string(channel->name));
255 user->Shrink("cm_protect_"+std::string(channel->name));
258 virtual void OnRehash(const std::string ¶meter)
260 /* Create a configreader class and read our flag,
261 * in old versions this was heap-allocated and the
262 * object was kept between rehashes...now we just
263 * stack-allocate it locally.
265 ConfigReader Conf(ServerInstance);
267 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
268 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
271 virtual void OnUserJoin(userrec* user, chanrec* channel)
273 // if the user is the first user into the channel, mark them as the founder, but only if
274 // the config option for it is set
275 if (FirstInGetsFounder)
277 if (channel->GetUserCounter() == 1)
279 // we're using Extensible::Extend to add data into user objects.
280 // this way is best as it adds data thats accessible to other modules
281 // (so long as you document your code properly) without breaking anything
282 // because its encapsulated neatly in a map.
284 // Change requested by katsklaw... when the first in is set to get founder,
285 // to make it clearer that +q has been given, send that one user the +q notice
286 // so that their client's syncronization and their sanity are left intact.
287 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
288 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
290 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
296 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
298 // here we perform access checks, this is the important bit that actually stops kicking/deopping
299 // etc of protected users. There are many types of access check, we're going to handle
300 // a relatively small number of them relevent to our module using a switch statement.
302 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
303 // don't allow action if:
304 // (A) Theyre founder (no matter what)
305 // (B) Theyre protected, and you're not
306 // always allow the action if:
307 // (A) The source is ulined
310 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
311 // without any access checks, we're not worthy :p
312 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
317 std::string founder = "cm_founder_"+std::string(channel->name);
318 std::string protect = "cm_protect_"+std::string(channel->name);
322 // a user has been deopped. Do we let them? hmmm...
324 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
325 if (dest->GetExt(founder,dummyptr))
327 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
328 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
333 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
335 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
337 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
342 // a user is being kicked. do we chop off the end of the army boot?
344 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
345 if (dest->GetExt(founder,dummyptr))
347 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
350 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
352 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
357 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
359 if (dest->GetExt(founder,dummyptr))
361 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");
364 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
366 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
371 // same with devoice.
373 if (dest->GetExt(founder,dummyptr))
375 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
378 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
380 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
386 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
390 virtual ~ModuleChanProtect()
396 virtual Version GetVersion()
398 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
401 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
403 // this is called when the server is linking into a net and wants to sync channel data.
404 // we should send our mode changes for the channel here to ensure that other servers
405 // know whos +q/+a on the channel.
406 CUList* cl = chan->GetUsers();
407 string_list commands;
408 std::string founder = "cm_founder_"+std::string(chan->name);
409 std::string protect = "cm_protect_"+std::string(chan->name);
410 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
412 if (i->second->GetExt(founder,dummyptr))
414 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
416 if (i->second->GetExt(protect,dummyptr))
418 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
426 class ModuleChanProtectFactory : public ModuleFactory
429 ModuleChanProtectFactory()
433 ~ModuleChanProtectFactory()
437 virtual Module * CreateModule(InspIRCd* Me)
439 return new ModuleChanProtect(Me);
445 extern "C" void * init_module( void )
447 return new ModuleChanProtectFactory;