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 * ---------------------------------------------------
20 #include "helperfuncs.h"
22 /* $ModDesc: Provides channel modes +a and +q */
24 const char* fakevalue = "on";
26 class ChanFounder : public ModeHandler
32 ChanFounder(Server* s) : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
34 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
36 userrec* x = Find(parameter);
39 if (!channel->HasUser(x))
41 return std::make_pair(false, parameter);
45 std::string founder = "cm_founder_"+std::string(channel->name);
46 if (x->GetExt(founder,dummyptr))
48 return std::make_pair(true, x->nick);
52 return std::make_pair(false, parameter);
56 return std::make_pair(false, parameter);
60 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
62 userrec* theuser = Srv->FindNick(parameter);
64 log(DEBUG,"ChanFounder::OnModeChange");
66 // cant find the user given as the parameter, eat the mode change.
69 log(DEBUG,"No such user in ChanFounder");
71 return MODEACTION_DENY;
74 // given user isnt even on the channel, eat the mode change
75 if (!channel->HasUser(theuser))
77 log(DEBUG,"Channel doesn't have user in ChanFounder");
79 return MODEACTION_DENY;
82 std::string protect = "cm_protect_"+std::string(channel->name);
83 std::string founder = "cm_founder_"+std::string(channel->name);
85 // source is a server, or ulined, we'll let them +-q the user.
86 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
88 log(DEBUG,"Allowing remote mode change in ChanFounder");
91 if (!theuser->GetExt(founder,dummyptr))
93 log(DEBUG,"Does not have the ext item in ChanFounder");
94 if (!theuser->Extend(founder,fakevalue))
95 log(DEBUG,"COULD NOT EXTEND!!!");
96 // Tidy the nickname (make case match etc)
97 parameter = theuser->nick;
98 if (theuser->GetExt(founder, dummyptr))
99 log(DEBUG,"Extended!");
101 log(DEBUG,"Not extended :(");
102 return MODEACTION_ALLOW;
107 if (theuser->GetExt(founder, dummyptr))
109 theuser->Shrink(founder);
110 // Tidy the nickname (make case match etc)
111 parameter = theuser->nick;
112 return MODEACTION_ALLOW;
115 return MODEACTION_DENY;
119 // whoops, someones being naughty!
120 WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
122 return MODEACTION_DENY;
126 void DisplayList(userrec* user, chanrec* channel)
128 chanuserlist cl = Srv->GetUsers(channel);
129 std::string founder = "cm_founder_"+std::string(channel->name);
130 for (unsigned int i = 0; i < cl.size(); i++)
132 if (cl[i]->GetExt(founder, dummyptr))
134 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
137 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
142 class ChanProtect : public ModeHandler
147 ChanProtect(Server* s) : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
149 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
151 userrec* x = Find(parameter);
154 if (!channel->HasUser(x))
156 return std::make_pair(false, parameter);
160 std::string founder = "cm_protect_"+std::string(channel->name);
161 if (x->GetExt(founder,dummyptr))
163 return std::make_pair(true, x->nick);
167 return std::make_pair(false, parameter);
171 return std::make_pair(false, parameter);
174 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
176 userrec* theuser = Srv->FindNick(parameter);
178 // cant find the user given as the parameter, eat the mode change.
182 return MODEACTION_DENY;
185 // given user isnt even on the channel, eat the mode change
186 if (!channel->HasUser(theuser))
189 return MODEACTION_DENY;
192 std::string protect = "cm_protect_"+std::string(channel->name);
193 std::string founder = "cm_founder_"+std::string(channel->name);
195 // source has +q, is a server, or ulined, we'll let them +-a the user.
196 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
200 if (!theuser->GetExt(protect,dummyptr))
202 theuser->Extend(protect,fakevalue);
203 // Tidy the nickname (make case match etc)
204 parameter = theuser->nick;
205 return MODEACTION_ALLOW;
210 if (theuser->GetExt(protect,dummyptr))
212 theuser->Shrink(protect);
213 // Tidy the nickname (make case match etc)
214 parameter = theuser->nick;
215 return MODEACTION_ALLOW;
218 return MODEACTION_DENY;
222 // bzzzt, wrong answer!
223 WriteServ(source->fd,"482 %s %s :You are not a channel founder",source->nick, channel->name);
224 return MODEACTION_DENY;
228 virtual void DisplayList(userrec* user, chanrec* channel)
230 chanuserlist cl = Srv->GetUsers(channel);
231 std::string protect = "cm_protect_"+std::string(channel->name);
232 for (unsigned int i = 0; i < cl.size(); i++)
234 if (cl[i]->GetExt(protect,dummyptr))
236 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
239 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
244 class ModuleChanProtect : public Module
247 bool FirstInGetsFounder;
254 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
256 /* Initialise module variables */
258 cp = new ChanProtect(Me);
259 cf = new ChanFounder(Me);
261 Srv->AddMode(cp, 'a');
262 Srv->AddMode(cf, 'q');
264 /* Load config stuff */
268 void Implements(char* List)
270 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
273 virtual void On005Numeric(std::string &output)
275 InsertMode(output,"qa",1);
278 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
280 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
281 user->Shrink("cm_founder_"+std::string(chan->name));
282 user->Shrink("cm_protect_"+std::string(chan->name));
285 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
287 // FIX: when someone parts a channel we must remove their Extensibles!
288 user->Shrink("cm_founder_"+std::string(channel->name));
289 user->Shrink("cm_protect_"+std::string(channel->name));
292 virtual void OnRehash(const std::string ¶meter)
294 /* Create a configreader class and read our flag,
295 * in old versions this was heap-allocated and the
296 * object was kept between rehashes...now we just
297 * stack-allocate it locally.
301 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
304 virtual void OnUserJoin(userrec* user, chanrec* channel)
306 // if the user is the first user into the channel, mark them as the founder, but only if
307 // the config option for it is set
308 if (FirstInGetsFounder)
310 if (Srv->CountUsers(channel) == 1)
312 // we're using Extensible::Extend to add data into user objects.
313 // this way is best as it adds data thats accessible to other modules
314 // (so long as you document your code properly) without breaking anything
315 // because its encapsulated neatly in a map.
317 // Change requested by katsklaw... when the first in is set to get founder,
318 // to make it clearer that +q has been given, send that one user the +q notice
319 // so that their client's syncronization and their sanity are left intact.
320 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
321 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
323 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
329 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
331 // here we perform access checks, this is the important bit that actually stops kicking/deopping
332 // etc of protected users. There are many types of access check, we're going to handle
333 // a relatively small number of them relevent to our module using a switch statement.
335 log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
336 // don't allow action if:
337 // (A) Theyre founder (no matter what)
338 // (B) Theyre protected, and you're not
339 // always allow the action if:
340 // (A) The source is ulined
343 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
344 // without any access checks, we're not worthy :p
345 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
350 std::string founder = "cm_founder_"+std::string(channel->name);
351 std::string protect = "cm_protect_"+std::string(channel->name);
355 // a user has been deopped. Do we let them? hmmm...
357 log(DEBUG,"OnAccessCheck AC_DEOP");
358 if (dest->GetExt(founder,dummyptr))
360 log(DEBUG,"Has %s",founder.c_str());
361 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
366 log(DEBUG,"Doesnt have %s",founder.c_str());
368 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
370 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
375 // a user is being kicked. do we chop off the end of the army boot?
377 log(DEBUG,"OnAccessCheck AC_KICK");
378 if (dest->GetExt(founder,dummyptr))
380 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
383 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
385 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
390 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
392 if (dest->GetExt(founder,dummyptr))
394 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
397 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
399 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
404 // same with devoice.
406 if (dest->GetExt(founder,dummyptr))
408 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
411 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
413 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
419 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
423 virtual ~ModuleChanProtect()
429 virtual Version GetVersion()
431 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
434 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
436 // this is called when the server is linking into a net and wants to sync channel data.
437 // we should send our mode changes for the channel here to ensure that other servers
438 // know whos +q/+a on the channel.
439 chanuserlist cl = Srv->GetUsers(chan);
440 string_list commands;
441 std::string founder = "cm_founder_"+std::string(chan->name);
442 std::string protect = "cm_protect_"+std::string(chan->name);
443 for (unsigned int i = 0; i < cl.size(); i++)
445 if (cl[i]->GetExt(founder,dummyptr))
447 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
449 if (cl[i]->GetExt(protect,dummyptr))
451 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
459 class ModuleChanProtectFactory : public ModuleFactory
462 ModuleChanProtectFactory()
466 ~ModuleChanProtectFactory()
470 virtual Module * CreateModule(Server* Me)
472 return new ModuleChanProtect(Me);
478 extern "C" void * init_module( void )
480 return new ModuleChanProtectFactory;