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 ChanFounder : public ModeHandler
34 ChanFounder(InspIRCd* Instance, bool using_prefixes) : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0) { }
36 unsigned int GetPrefixRank()
42 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
44 userrec* x = ServerInstance->FindNick(parameter);
47 if (!channel->HasUser(x))
49 return std::make_pair(false, parameter);
53 std::string founder = "cm_founder_"+std::string(channel->name);
54 if (x->GetExt(founder,dummyptr))
56 return std::make_pair(true, x->nick);
60 return std::make_pair(false, parameter);
64 return std::make_pair(false, parameter);
68 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
70 userrec* theuser = ServerInstance->FindNick(parameter);
72 ServerInstance->Log(DEBUG,"ChanFounder::OnModeChange");
74 // cant find the user given as the parameter, eat the mode change.
77 ServerInstance->Log(DEBUG,"No such user in ChanFounder");
79 return MODEACTION_DENY;
82 // given user isnt even on the channel, eat the mode change
83 if (!channel->HasUser(theuser))
85 ServerInstance->Log(DEBUG,"Channel doesn't have user in ChanFounder");
87 return MODEACTION_DENY;
90 std::string protect = "cm_protect_"+std::string(channel->name);
91 std::string founder = "cm_founder_"+std::string(channel->name);
93 // source is a server, or ulined, we'll let them +-q the user.
94 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
96 ServerInstance->Log(DEBUG,"Allowing remote mode change in ChanFounder");
99 if (!theuser->GetExt(founder,dummyptr))
101 ServerInstance->Log(DEBUG,"Does not have the ext item in ChanFounder");
102 if (!theuser->Extend(founder,fakevalue))
103 ServerInstance->Log(DEBUG,"COULD NOT EXTEND!!!");
104 // Tidy the nickname (make case match etc)
105 parameter = theuser->nick;
106 if (theuser->GetExt(founder, dummyptr))
107 ServerInstance->Log(DEBUG,"Extended!");
109 ServerInstance->Log(DEBUG,"Not extended :(");
110 return MODEACTION_ALLOW;
115 if (theuser->GetExt(founder, dummyptr))
117 theuser->Shrink(founder);
118 // Tidy the nickname (make case match etc)
119 parameter = theuser->nick;
120 return MODEACTION_ALLOW;
123 return MODEACTION_DENY;
127 // whoops, someones being naughty!
128 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
130 return MODEACTION_DENY;
134 void DisplayList(userrec* user, chanrec* channel)
136 CUList* cl = channel->GetUsers();
137 std::string founder = "cm_founder_"+std::string(channel->name);
138 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
140 if (i->second->GetExt(founder, dummyptr))
142 user->WriteServ("386 %s %s %s",user->nick, channel->name,i->second->nick);
145 user->WriteServ("387 %s %s :End of channel founder list",user->nick, channel->name);
150 class ChanProtect : public ModeHandler
154 ChanProtect(InspIRCd* Instance, bool using_prefixes) : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0) { }
156 unsigned int GetPrefixRank()
158 return PROTECT_VALUE;
162 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
164 userrec* x = ServerInstance->FindNick(parameter);
167 if (!channel->HasUser(x))
169 return std::make_pair(false, parameter);
173 std::string founder = "cm_protect_"+std::string(channel->name);
174 if (x->GetExt(founder,dummyptr))
176 return std::make_pair(true, x->nick);
180 return std::make_pair(false, parameter);
184 return std::make_pair(false, parameter);
187 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
189 userrec* theuser = ServerInstance->FindNick(parameter);
191 // cant find the user given as the parameter, eat the mode change.
195 return MODEACTION_DENY;
198 // given user isnt even on the channel, eat the mode change
199 if (!channel->HasUser(theuser))
202 return MODEACTION_DENY;
205 std::string protect = "cm_protect_"+std::string(channel->name);
206 std::string founder = "cm_founder_"+std::string(channel->name);
208 // source has +q, is a server, or ulined, we'll let them +-a the user.
209 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
213 if (!theuser->GetExt(protect,dummyptr))
215 theuser->Extend(protect,fakevalue);
216 // Tidy the nickname (make case match etc)
217 parameter = theuser->nick;
218 return MODEACTION_ALLOW;
223 if (theuser->GetExt(protect,dummyptr))
225 theuser->Shrink(protect);
226 // Tidy the nickname (make case match etc)
227 parameter = theuser->nick;
228 return MODEACTION_ALLOW;
231 return MODEACTION_DENY;
235 // bzzzt, wrong answer!
236 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
237 return MODEACTION_DENY;
241 virtual void DisplayList(userrec* user, chanrec* channel)
243 CUList* cl = channel->GetUsers();
244 std::string protect = "cm_protect_"+std::string(channel->name);
245 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
247 if (i->second->GetExt(protect,dummyptr))
249 user->WriteServ("388 %s %s %s",user->nick, channel->name,i->second->nick);
252 user->WriteServ("389 %s %s :End of channel protected user list",user->nick, channel->name);
257 class ModuleChanProtect : public Module
260 bool FirstInGetsFounder;
268 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
270 /* Load config stuff */
273 /* Initialise module variables */
275 cp = new ChanProtect(ServerInstance,QAPrefixes);
276 cf = new ChanFounder(ServerInstance,QAPrefixes);
278 ServerInstance->AddMode(cp, 'a');
279 ServerInstance->AddMode(cf, 'q');
282 void Implements(char* List)
284 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
287 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
289 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
290 user->Shrink("cm_founder_"+std::string(chan->name));
291 user->Shrink("cm_protect_"+std::string(chan->name));
294 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
296 // FIX: when someone parts a channel we must remove their Extensibles!
297 user->Shrink("cm_founder_"+std::string(channel->name));
298 user->Shrink("cm_protect_"+std::string(channel->name));
301 virtual void OnRehash(const std::string ¶meter)
303 /* Create a configreader class and read our flag,
304 * in old versions this was heap-allocated and the
305 * object was kept between rehashes...now we just
306 * stack-allocate it locally.
308 ConfigReader Conf(ServerInstance);
310 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
311 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
314 virtual void OnUserJoin(userrec* user, chanrec* channel)
316 // if the user is the first user into the channel, mark them as the founder, but only if
317 // the config option for it is set
318 if (FirstInGetsFounder)
320 if (channel->GetUserCounter() == 1)
322 // we're using Extensible::Extend to add data into user objects.
323 // this way is best as it adds data thats accessible to other modules
324 // (so long as you document your code properly) without breaking anything
325 // because its encapsulated neatly in a map.
327 // Change requested by katsklaw... when the first in is set to get founder,
328 // to make it clearer that +q has been given, send that one user the +q notice
329 // so that their client's syncronization and their sanity are left intact.
330 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
331 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
333 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
339 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
341 // here we perform access checks, this is the important bit that actually stops kicking/deopping
342 // etc of protected users. There are many types of access check, we're going to handle
343 // a relatively small number of them relevent to our module using a switch statement.
345 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
346 // don't allow action if:
347 // (A) Theyre founder (no matter what)
348 // (B) Theyre protected, and you're not
349 // always allow the action if:
350 // (A) The source is ulined
353 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
354 // without any access checks, we're not worthy :p
355 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
360 std::string founder = "cm_founder_"+std::string(channel->name);
361 std::string protect = "cm_protect_"+std::string(channel->name);
365 // a user has been deopped. Do we let them? hmmm...
367 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
368 if (dest->GetExt(founder,dummyptr))
370 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
371 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
376 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
378 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
380 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
385 // a user is being kicked. do we chop off the end of the army boot?
387 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
388 if (dest->GetExt(founder,dummyptr))
390 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
393 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
395 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
400 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
402 if (dest->GetExt(founder,dummyptr))
404 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");
407 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
409 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
414 // same with devoice.
416 if (dest->GetExt(founder,dummyptr))
418 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
421 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
423 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
429 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
433 virtual ~ModuleChanProtect()
439 virtual Version GetVersion()
441 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
444 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
446 // this is called when the server is linking into a net and wants to sync channel data.
447 // we should send our mode changes for the channel here to ensure that other servers
448 // know whos +q/+a on the channel.
449 CUList* cl = chan->GetUsers();
450 string_list commands;
451 std::string founder = "cm_founder_"+std::string(chan->name);
452 std::string protect = "cm_protect_"+std::string(chan->name);
453 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
455 if (i->second->GetExt(founder,dummyptr))
457 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
459 if (i->second->GetExt(protect,dummyptr))
461 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
469 class ModuleChanProtectFactory : public ModuleFactory
472 ModuleChanProtectFactory()
476 ~ModuleChanProtectFactory()
480 virtual Module * CreateModule(InspIRCd* Me)
482 return new ModuleChanProtect(Me);
488 extern "C" void * init_module( void )
490 return new ModuleChanProtectFactory;