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 class FounderProtectBase
39 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)
43 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
45 userrec* x = MyInstance->FindNick(parameter);
48 if (!channel->HasUser(x))
50 return std::make_pair(false, parameter);
54 std::string item = extend+std::string(channel->name);
55 if (x->GetExt(item,dummyptr))
57 return std::make_pair(true, x->nick);
61 return std::make_pair(false, parameter);
65 return std::make_pair(false, parameter);
68 void DisplayList(userrec* user, chanrec* channel)
70 CUList* cl = channel->GetUsers();
71 std::string item = extend+std::string(channel->name);
72 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
74 if (i->second->GetExt(item, dummyptr))
76 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
79 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
82 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
84 userrec* theuser = MyInstance->FindNick(parameter);
85 if ((!theuser) || (!channel->HasUser(theuser)))
93 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
95 std::string item = extend+std::string(channel->name);
99 if (!theuser->GetExt(item, dummyptr))
101 theuser->Extend(item, fakevalue);
102 parameter = theuser->nick;
103 return MODEACTION_ALLOW;
108 if (theuser->GetExt(item, dummyptr))
110 theuser->Shrink(item);
111 parameter = theuser->nick;
112 return MODEACTION_ALLOW;
115 return MODEACTION_DENY;
119 class ChanFounder : public ModeHandler, public FounderProtectBase
123 ChanFounder(InspIRCd* Instance, bool using_prefixes)
124 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
125 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
127 unsigned int GetPrefixRank()
129 return FOUNDER_VALUE;
132 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
134 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
137 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
139 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
143 return MODEACTION_DENY;
146 // source is a server, or ulined, we'll let them +-q the user.
147 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
149 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
153 // whoops, someones being naughty!
154 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
156 return MODEACTION_DENY;
160 void DisplayList(userrec* user, chanrec* channel)
162 FounderProtectBase::DisplayList(user,channel);
166 class ChanProtect : public ModeHandler, public FounderProtectBase
170 ChanProtect(InspIRCd* Instance, bool using_prefixes)
171 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
172 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
174 unsigned int GetPrefixRank()
176 return PROTECT_VALUE;
179 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
181 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
184 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
186 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
189 return MODEACTION_DENY;
191 std::string founder = "cm_founder_"+std::string(channel->name);
193 // source has +q, is a server, or ulined, we'll let them +-a the user.
194 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
196 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
200 // bzzzt, wrong answer!
201 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
202 return MODEACTION_DENY;
206 virtual void DisplayList(userrec* user, chanrec* channel)
208 FounderProtectBase::DisplayList(user, channel);
213 class ModuleChanProtect : public Module
216 bool FirstInGetsFounder;
224 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
226 /* Load config stuff */
229 /* Initialise module variables */
231 cp = new ChanProtect(ServerInstance,QAPrefixes);
232 cf = new ChanFounder(ServerInstance,QAPrefixes);
234 ServerInstance->AddMode(cp, 'a');
235 ServerInstance->AddMode(cf, 'q');
238 void Implements(char* List)
240 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
243 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
245 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
246 user->Shrink("cm_founder_"+std::string(chan->name));
247 user->Shrink("cm_protect_"+std::string(chan->name));
250 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
252 // FIX: when someone parts a channel we must remove their Extensibles!
253 user->Shrink("cm_founder_"+std::string(channel->name));
254 user->Shrink("cm_protect_"+std::string(channel->name));
257 virtual void OnRehash(const std::string ¶meter)
259 /* Create a configreader class and read our flag,
260 * in old versions this was heap-allocated and the
261 * object was kept between rehashes...now we just
262 * stack-allocate it locally.
264 ConfigReader Conf(ServerInstance);
266 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
267 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
270 virtual void OnUserJoin(userrec* user, chanrec* channel)
272 // if the user is the first user into the channel, mark them as the founder, but only if
273 // the config option for it is set
274 if (FirstInGetsFounder)
276 if (channel->GetUserCounter() == 1)
278 // we're using Extensible::Extend to add data into user objects.
279 // this way is best as it adds data thats accessible to other modules
280 // (so long as you document your code properly) without breaking anything
281 // because its encapsulated neatly in a map.
283 // Change requested by katsklaw... when the first in is set to get founder,
284 // to make it clearer that +q has been given, send that one user the +q notice
285 // so that their client's syncronization and their sanity are left intact.
286 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
287 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
289 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
295 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
297 // here we perform access checks, this is the important bit that actually stops kicking/deopping
298 // etc of protected users. There are many types of access check, we're going to handle
299 // a relatively small number of them relevent to our module using a switch statement.
301 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
302 // don't allow action if:
303 // (A) Theyre founder (no matter what)
304 // (B) Theyre protected, and you're not
305 // always allow the action if:
306 // (A) The source is ulined
309 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
310 // without any access checks, we're not worthy :p
311 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
316 std::string founder = "cm_founder_"+std::string(channel->name);
317 std::string protect = "cm_protect_"+std::string(channel->name);
321 // a user has been deopped. Do we let them? hmmm...
323 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
324 if (dest->GetExt(founder,dummyptr))
326 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
327 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
332 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
334 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
336 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
341 // a user is being kicked. do we chop off the end of the army boot?
343 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
344 if (dest->GetExt(founder,dummyptr))
346 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
349 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
351 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
356 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
358 if (dest->GetExt(founder,dummyptr))
360 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");
363 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
365 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
370 // same with devoice.
372 if (dest->GetExt(founder,dummyptr))
374 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
377 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
379 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
385 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
389 virtual ~ModuleChanProtect()
395 virtual Version GetVersion()
397 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
400 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
402 // this is called when the server is linking into a net and wants to sync channel data.
403 // we should send our mode changes for the channel here to ensure that other servers
404 // know whos +q/+a on the channel.
405 CUList* cl = chan->GetUsers();
406 string_list commands;
407 std::string founder = "cm_founder_"+std::string(chan->name);
408 std::string protect = "cm_protect_"+std::string(chan->name);
409 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
411 if (i->second->GetExt(founder,dummyptr))
413 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
415 if (i->second->GetExt(protect,dummyptr))
417 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
425 class ModuleChanProtectFactory : public ModuleFactory
428 ModuleChanProtectFactory()
432 ~ModuleChanProtectFactory()
436 virtual Module * CreateModule(InspIRCd* Me)
438 return new ModuleChanProtect(Me);
444 extern "C" void * init_module( void )
446 return new ModuleChanProtectFactory;