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"
23 /* $ModDesc: Provides channel modes +a and +q */
25 extern InspIRCd* ServerInstance;
27 const char* fakevalue = "on";
29 class ChanFounder : public ModeHandler
35 ChanFounder(InspIRCd* Instance, Server* s) : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
37 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
39 userrec* x = ServerInstance->FindNick(parameter);
42 if (!channel->HasUser(x))
44 return std::make_pair(false, parameter);
48 std::string founder = "cm_founder_"+std::string(channel->name);
49 if (x->GetExt(founder,dummyptr))
51 return std::make_pair(true, x->nick);
55 return std::make_pair(false, parameter);
59 return std::make_pair(false, parameter);
63 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
65 userrec* theuser = ServerInstance->FindNick(parameter);
67 log(DEBUG,"ChanFounder::OnModeChange");
69 // cant find the user given as the parameter, eat the mode change.
72 log(DEBUG,"No such user in ChanFounder");
74 return MODEACTION_DENY;
77 // given user isnt even on the channel, eat the mode change
78 if (!channel->HasUser(theuser))
80 log(DEBUG,"Channel doesn't have user in ChanFounder");
82 return MODEACTION_DENY;
85 std::string protect = "cm_protect_"+std::string(channel->name);
86 std::string founder = "cm_founder_"+std::string(channel->name);
88 // source is a server, or ulined, we'll let them +-q the user.
89 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
91 log(DEBUG,"Allowing remote mode change in ChanFounder");
94 if (!theuser->GetExt(founder,dummyptr))
96 log(DEBUG,"Does not have the ext item in ChanFounder");
97 if (!theuser->Extend(founder,fakevalue))
98 log(DEBUG,"COULD NOT EXTEND!!!");
99 // Tidy the nickname (make case match etc)
100 parameter = theuser->nick;
101 if (theuser->GetExt(founder, dummyptr))
102 log(DEBUG,"Extended!");
104 log(DEBUG,"Not extended :(");
105 return MODEACTION_ALLOW;
110 if (theuser->GetExt(founder, dummyptr))
112 theuser->Shrink(founder);
113 // Tidy the nickname (make case match etc)
114 parameter = theuser->nick;
115 return MODEACTION_ALLOW;
118 return MODEACTION_DENY;
122 // whoops, someones being naughty!
123 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
125 return MODEACTION_DENY;
129 void DisplayList(userrec* user, chanrec* channel)
131 CUList* cl = channel->GetUsers();
132 std::string founder = "cm_founder_"+std::string(channel->name);
133 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
135 if (i->second->GetExt(founder, dummyptr))
137 user->WriteServ("386 %s %s %s",user->nick, channel->name,i->second->nick);
140 user->WriteServ("387 %s %s :End of channel founder list",user->nick, channel->name);
145 class ChanProtect : public ModeHandler
150 ChanProtect(InspIRCd* Instance, Server* s) : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
152 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
154 userrec* x = ServerInstance->FindNick(parameter);
157 if (!channel->HasUser(x))
159 return std::make_pair(false, parameter);
163 std::string founder = "cm_protect_"+std::string(channel->name);
164 if (x->GetExt(founder,dummyptr))
166 return std::make_pair(true, x->nick);
170 return std::make_pair(false, parameter);
174 return std::make_pair(false, parameter);
177 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
179 userrec* theuser = ServerInstance->FindNick(parameter);
181 // cant find the user given as the parameter, eat the mode change.
185 return MODEACTION_DENY;
188 // given user isnt even on the channel, eat the mode change
189 if (!channel->HasUser(theuser))
192 return MODEACTION_DENY;
195 std::string protect = "cm_protect_"+std::string(channel->name);
196 std::string founder = "cm_founder_"+std::string(channel->name);
198 // source has +q, is a server, or ulined, we'll let them +-a the user.
199 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
203 if (!theuser->GetExt(protect,dummyptr))
205 theuser->Extend(protect,fakevalue);
206 // Tidy the nickname (make case match etc)
207 parameter = theuser->nick;
208 return MODEACTION_ALLOW;
213 if (theuser->GetExt(protect,dummyptr))
215 theuser->Shrink(protect);
216 // Tidy the nickname (make case match etc)
217 parameter = theuser->nick;
218 return MODEACTION_ALLOW;
221 return MODEACTION_DENY;
225 // bzzzt, wrong answer!
226 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
227 return MODEACTION_DENY;
231 virtual void DisplayList(userrec* user, chanrec* channel)
233 CUList* cl = channel->GetUsers();
234 std::string protect = "cm_protect_"+std::string(channel->name);
235 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
237 if (i->second->GetExt(protect,dummyptr))
239 user->WriteServ("388 %s %s %s",user->nick, channel->name,i->second->nick);
242 user->WriteServ("389 %s %s :End of channel protected user list",user->nick, channel->name);
247 class ModuleChanProtect : public Module
250 bool FirstInGetsFounder;
257 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
259 /* Initialise module variables */
261 cp = new ChanProtect(ServerInstance, Me);
262 cf = new ChanFounder(ServerInstance, Me);
264 Srv->AddMode(cp, 'a');
265 Srv->AddMode(cf, 'q');
267 /* Load config stuff */
271 void Implements(char* List)
273 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
276 virtual void On005Numeric(std::string &output)
278 ServerInstance->ModeGrok->InsertMode(output,"qa",1);
281 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
283 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
284 user->Shrink("cm_founder_"+std::string(chan->name));
285 user->Shrink("cm_protect_"+std::string(chan->name));
288 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
290 // FIX: when someone parts a channel we must remove their Extensibles!
291 user->Shrink("cm_founder_"+std::string(channel->name));
292 user->Shrink("cm_protect_"+std::string(channel->name));
295 virtual void OnRehash(const std::string ¶meter)
297 /* Create a configreader class and read our flag,
298 * in old versions this was heap-allocated and the
299 * object was kept between rehashes...now we just
300 * stack-allocate it locally.
304 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
307 virtual void OnUserJoin(userrec* user, chanrec* channel)
309 // if the user is the first user into the channel, mark them as the founder, but only if
310 // the config option for it is set
311 if (FirstInGetsFounder)
313 if (channel->GetUserCounter() == 1)
315 // we're using Extensible::Extend to add data into user objects.
316 // this way is best as it adds data thats accessible to other modules
317 // (so long as you document your code properly) without breaking anything
318 // because its encapsulated neatly in a map.
320 // Change requested by katsklaw... when the first in is set to get founder,
321 // to make it clearer that +q has been given, send that one user the +q notice
322 // so that their client's syncronization and their sanity are left intact.
323 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
324 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
326 log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
332 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
334 // here we perform access checks, this is the important bit that actually stops kicking/deopping
335 // etc of protected users. There are many types of access check, we're going to handle
336 // a relatively small number of them relevent to our module using a switch statement.
338 log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
339 // don't allow action if:
340 // (A) Theyre founder (no matter what)
341 // (B) Theyre protected, and you're not
342 // always allow the action if:
343 // (A) The source is ulined
346 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
347 // without any access checks, we're not worthy :p
348 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
353 std::string founder = "cm_founder_"+std::string(channel->name);
354 std::string protect = "cm_protect_"+std::string(channel->name);
358 // a user has been deopped. Do we let them? hmmm...
360 log(DEBUG,"OnAccessCheck AC_DEOP");
361 if (dest->GetExt(founder,dummyptr))
363 log(DEBUG,"Has %s",founder.c_str());
364 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
369 log(DEBUG,"Doesnt have %s",founder.c_str());
371 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
373 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
378 // a user is being kicked. do we chop off the end of the army boot?
380 log(DEBUG,"OnAccessCheck AC_KICK");
381 if (dest->GetExt(founder,dummyptr))
383 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
386 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
388 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
393 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
395 if (dest->GetExt(founder,dummyptr))
397 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");
400 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
402 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
407 // same with devoice.
409 if (dest->GetExt(founder,dummyptr))
411 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
414 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
416 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
422 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
426 virtual ~ModuleChanProtect()
432 virtual Version GetVersion()
434 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
437 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
439 // this is called when the server is linking into a net and wants to sync channel data.
440 // we should send our mode changes for the channel here to ensure that other servers
441 // know whos +q/+a on the channel.
442 CUList* cl = chan->GetUsers();
443 string_list commands;
444 std::string founder = "cm_founder_"+std::string(chan->name);
445 std::string protect = "cm_protect_"+std::string(chan->name);
446 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
448 if (i->second->GetExt(founder,dummyptr))
450 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
452 if (i->second->GetExt(protect,dummyptr))
454 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
462 class ModuleChanProtectFactory : public ModuleFactory
465 ModuleChanProtectFactory()
469 ~ModuleChanProtectFactory()
473 virtual Module * CreateModule(Server* Me)
475 return new ModuleChanProtect(Me);
481 extern "C" void * init_module( void )
483 return new ModuleChanProtectFactory;