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);
143 return MODEACTION_DENY;
145 // source is a server, or ulined, we'll let them +-q the user.
146 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
148 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
152 // whoops, someones being naughty!
153 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
155 return MODEACTION_DENY;
159 void DisplayList(userrec* user, chanrec* channel)
161 FounderProtectBase::DisplayList(user,channel);
165 class ChanProtect : public ModeHandler, public FounderProtectBase
169 ChanProtect(InspIRCd* Instance, bool using_prefixes)
170 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
171 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
173 unsigned int GetPrefixRank()
175 return PROTECT_VALUE;
178 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
180 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
183 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
185 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
188 return MODEACTION_DENY;
190 std::string founder = "cm_founder_"+std::string(channel->name);
192 // source has +q, is a server, or ulined, we'll let them +-a the user.
193 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
195 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
199 // bzzzt, wrong answer!
200 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
201 return MODEACTION_DENY;
205 virtual void DisplayList(userrec* user, chanrec* channel)
207 FounderProtectBase::DisplayList(user, channel);
212 class ModuleChanProtect : public Module
215 bool FirstInGetsFounder;
223 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
225 /* Load config stuff */
228 /* Initialise module variables */
230 cp = new ChanProtect(ServerInstance,QAPrefixes);
231 cf = new ChanFounder(ServerInstance,QAPrefixes);
233 ServerInstance->AddMode(cp, 'a');
234 ServerInstance->AddMode(cf, 'q');
237 void Implements(char* List)
239 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
242 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
244 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
245 user->Shrink("cm_founder_"+std::string(chan->name));
246 user->Shrink("cm_protect_"+std::string(chan->name));
249 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
251 // FIX: when someone parts a channel we must remove their Extensibles!
252 user->Shrink("cm_founder_"+std::string(channel->name));
253 user->Shrink("cm_protect_"+std::string(channel->name));
256 virtual void OnRehash(const std::string ¶meter)
258 /* Create a configreader class and read our flag,
259 * in old versions this was heap-allocated and the
260 * object was kept between rehashes...now we just
261 * stack-allocate it locally.
263 ConfigReader Conf(ServerInstance);
265 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
266 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
269 virtual void OnUserJoin(userrec* user, chanrec* channel)
271 // if the user is the first user into the channel, mark them as the founder, but only if
272 // the config option for it is set
273 if (FirstInGetsFounder)
275 if (channel->GetUserCounter() == 1)
277 // we're using Extensible::Extend to add data into user objects.
278 // this way is best as it adds data thats accessible to other modules
279 // (so long as you document your code properly) without breaking anything
280 // because its encapsulated neatly in a map.
282 // Change requested by katsklaw... when the first in is set to get founder,
283 // to make it clearer that +q has been given, send that one user the +q notice
284 // so that their client's syncronization and their sanity are left intact.
285 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
286 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
288 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
294 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
296 // here we perform access checks, this is the important bit that actually stops kicking/deopping
297 // etc of protected users. There are many types of access check, we're going to handle
298 // a relatively small number of them relevent to our module using a switch statement.
300 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
301 // don't allow action if:
302 // (A) Theyre founder (no matter what)
303 // (B) Theyre protected, and you're not
304 // always allow the action if:
305 // (A) The source is ulined
308 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
309 // without any access checks, we're not worthy :p
310 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
315 std::string founder = "cm_founder_"+std::string(channel->name);
316 std::string protect = "cm_protect_"+std::string(channel->name);
320 // a user has been deopped. Do we let them? hmmm...
322 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
323 if (dest->GetExt(founder,dummyptr))
325 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
326 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
331 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
333 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
335 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
340 // a user is being kicked. do we chop off the end of the army boot?
342 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
343 if (dest->GetExt(founder,dummyptr))
345 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
348 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
350 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
355 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
357 if (dest->GetExt(founder,dummyptr))
359 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");
362 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
364 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
369 // same with devoice.
371 if (dest->GetExt(founder,dummyptr))
373 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
376 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
378 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
384 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
388 virtual ~ModuleChanProtect()
394 virtual Version GetVersion()
396 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
399 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
401 // this is called when the server is linking into a net and wants to sync channel data.
402 // we should send our mode changes for the channel here to ensure that other servers
403 // know whos +q/+a on the channel.
404 CUList* cl = chan->GetUsers();
405 string_list commands;
406 std::string founder = "cm_founder_"+std::string(chan->name);
407 std::string protect = "cm_protect_"+std::string(chan->name);
408 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
410 if (i->second->GetExt(founder,dummyptr))
412 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
414 if (i->second->GetExt(protect,dummyptr))
416 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
424 class ModuleChanProtectFactory : public ModuleFactory
427 ModuleChanProtectFactory()
431 ~ModuleChanProtectFactory()
435 virtual Module * CreateModule(InspIRCd* Me)
437 return new ModuleChanProtect(Me);
443 extern "C" void * init_module( void )
445 return new ModuleChanProtectFactory;