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 * ---------------------------------------------------
24 /* $ModDesc: Provides channel modes +a and +q */
28 const char* fakevalue = "on";
30 class ChanFounder : public ModeHandler
34 ChanFounder(InspIRCd* Instance) : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false) { }
36 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
38 userrec* x = ServerInstance->FindNick(parameter);
41 if (!channel->HasUser(x))
43 return std::make_pair(false, parameter);
47 std::string founder = "cm_founder_"+std::string(channel->name);
48 if (x->GetExt(founder,dummyptr))
50 return std::make_pair(true, x->nick);
54 return std::make_pair(false, parameter);
58 return std::make_pair(false, parameter);
62 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
64 userrec* theuser = ServerInstance->FindNick(parameter);
66 ServerInstance->Log(DEBUG,"ChanFounder::OnModeChange");
68 // cant find the user given as the parameter, eat the mode change.
71 ServerInstance->Log(DEBUG,"No such user in ChanFounder");
73 return MODEACTION_DENY;
76 // given user isnt even on the channel, eat the mode change
77 if (!channel->HasUser(theuser))
79 ServerInstance->Log(DEBUG,"Channel doesn't have user in ChanFounder");
81 return MODEACTION_DENY;
84 std::string protect = "cm_protect_"+std::string(channel->name);
85 std::string founder = "cm_founder_"+std::string(channel->name);
87 // source is a server, or ulined, we'll let them +-q the user.
88 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
90 ServerInstance->Log(DEBUG,"Allowing remote mode change in ChanFounder");
93 if (!theuser->GetExt(founder,dummyptr))
95 ServerInstance->Log(DEBUG,"Does not have the ext item in ChanFounder");
96 if (!theuser->Extend(founder,fakevalue))
97 ServerInstance->Log(DEBUG,"COULD NOT EXTEND!!!");
98 // Tidy the nickname (make case match etc)
99 parameter = theuser->nick;
100 if (theuser->GetExt(founder, dummyptr))
101 ServerInstance->Log(DEBUG,"Extended!");
103 ServerInstance->Log(DEBUG,"Not extended :(");
104 return MODEACTION_ALLOW;
109 if (theuser->GetExt(founder, dummyptr))
111 theuser->Shrink(founder);
112 // Tidy the nickname (make case match etc)
113 parameter = theuser->nick;
114 return MODEACTION_ALLOW;
117 return MODEACTION_DENY;
121 // whoops, someones being naughty!
122 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
124 return MODEACTION_DENY;
128 void DisplayList(userrec* user, chanrec* channel)
130 CUList* cl = channel->GetUsers();
131 std::string founder = "cm_founder_"+std::string(channel->name);
132 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
134 if (i->second->GetExt(founder, dummyptr))
136 user->WriteServ("386 %s %s %s",user->nick, channel->name,i->second->nick);
139 user->WriteServ("387 %s %s :End of channel founder list",user->nick, channel->name);
144 class ChanProtect : public ModeHandler
148 ChanProtect(InspIRCd* Instance) : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false) { }
150 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
152 userrec* x = ServerInstance->FindNick(parameter);
155 if (!channel->HasUser(x))
157 return std::make_pair(false, parameter);
161 std::string founder = "cm_protect_"+std::string(channel->name);
162 if (x->GetExt(founder,dummyptr))
164 return std::make_pair(true, x->nick);
168 return std::make_pair(false, parameter);
172 return std::make_pair(false, parameter);
175 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
177 userrec* theuser = ServerInstance->FindNick(parameter);
179 // cant find the user given as the parameter, eat the mode change.
183 return MODEACTION_DENY;
186 // given user isnt even on the channel, eat the mode change
187 if (!channel->HasUser(theuser))
190 return MODEACTION_DENY;
193 std::string protect = "cm_protect_"+std::string(channel->name);
194 std::string founder = "cm_founder_"+std::string(channel->name);
196 // source has +q, is a server, or ulined, we'll let them +-a the user.
197 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
201 if (!theuser->GetExt(protect,dummyptr))
203 theuser->Extend(protect,fakevalue);
204 // Tidy the nickname (make case match etc)
205 parameter = theuser->nick;
206 return MODEACTION_ALLOW;
211 if (theuser->GetExt(protect,dummyptr))
213 theuser->Shrink(protect);
214 // Tidy the nickname (make case match etc)
215 parameter = theuser->nick;
216 return MODEACTION_ALLOW;
219 return MODEACTION_DENY;
223 // bzzzt, wrong answer!
224 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
225 return MODEACTION_DENY;
229 virtual void DisplayList(userrec* user, chanrec* channel)
231 CUList* cl = channel->GetUsers();
232 std::string protect = "cm_protect_"+std::string(channel->name);
233 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
235 if (i->second->GetExt(protect,dummyptr))
237 user->WriteServ("388 %s %s %s",user->nick, channel->name,i->second->nick);
240 user->WriteServ("389 %s %s :End of channel protected user list",user->nick, channel->name);
245 class ModuleChanProtect : public Module
248 bool FirstInGetsFounder;
255 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
257 /* Initialise module variables */
259 cp = new ChanProtect(ServerInstance);
260 cf = new ChanFounder(ServerInstance);
262 ServerInstance->AddMode(cp, 'a');
263 ServerInstance->AddMode(cf, 'q');
265 /* Load config stuff */
269 void Implements(char* List)
271 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
274 virtual void On005Numeric(std::string &output)
276 ServerInstance->Modes->InsertMode(output,"qa",1);
279 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
281 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
282 user->Shrink("cm_founder_"+std::string(chan->name));
283 user->Shrink("cm_protect_"+std::string(chan->name));
286 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
288 // FIX: when someone parts a channel we must remove their Extensibles!
289 user->Shrink("cm_founder_"+std::string(channel->name));
290 user->Shrink("cm_protect_"+std::string(channel->name));
293 virtual void OnRehash(const std::string ¶meter)
295 /* Create a configreader class and read our flag,
296 * in old versions this was heap-allocated and the
297 * object was kept between rehashes...now we just
298 * stack-allocate it locally.
300 ConfigReader Conf(ServerInstance);
302 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
305 virtual void OnUserJoin(userrec* user, chanrec* channel)
307 // if the user is the first user into the channel, mark them as the founder, but only if
308 // the config option for it is set
309 if (FirstInGetsFounder)
311 if (channel->GetUserCounter() == 1)
313 // we're using Extensible::Extend to add data into user objects.
314 // this way is best as it adds data thats accessible to other modules
315 // (so long as you document your code properly) without breaking anything
316 // because its encapsulated neatly in a map.
318 // Change requested by katsklaw... when the first in is set to get founder,
319 // to make it clearer that +q has been given, send that one user the +q notice
320 // so that their client's syncronization and their sanity are left intact.
321 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
322 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
324 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
330 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
332 // here we perform access checks, this is the important bit that actually stops kicking/deopping
333 // etc of protected users. There are many types of access check, we're going to handle
334 // a relatively small number of them relevent to our module using a switch statement.
336 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
337 // don't allow action if:
338 // (A) Theyre founder (no matter what)
339 // (B) Theyre protected, and you're not
340 // always allow the action if:
341 // (A) The source is ulined
344 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
345 // without any access checks, we're not worthy :p
346 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
351 std::string founder = "cm_founder_"+std::string(channel->name);
352 std::string protect = "cm_protect_"+std::string(channel->name);
356 // a user has been deopped. Do we let them? hmmm...
358 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
359 if (dest->GetExt(founder,dummyptr))
361 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
362 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
367 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
369 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
371 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
376 // a user is being kicked. do we chop off the end of the army boot?
378 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
379 if (dest->GetExt(founder,dummyptr))
381 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
384 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
386 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
391 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
393 if (dest->GetExt(founder,dummyptr))
395 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");
398 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
400 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
405 // same with devoice.
407 if (dest->GetExt(founder,dummyptr))
409 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
412 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
414 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
420 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
424 virtual ~ModuleChanProtect()
430 virtual Version GetVersion()
432 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
435 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
437 // this is called when the server is linking into a net and wants to sync channel data.
438 // we should send our mode changes for the channel here to ensure that other servers
439 // know whos +q/+a on the channel.
440 CUList* cl = chan->GetUsers();
441 string_list commands;
442 std::string founder = "cm_founder_"+std::string(chan->name);
443 std::string protect = "cm_protect_"+std::string(chan->name);
444 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
446 if (i->second->GetExt(founder,dummyptr))
448 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
450 if (i->second->GetExt(protect,dummyptr))
452 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
460 class ModuleChanProtectFactory : public ModuleFactory
463 ModuleChanProtectFactory()
467 ~ModuleChanProtectFactory()
471 virtual Module * CreateModule(InspIRCd* Me)
473 return new ModuleChanProtect(Me);
479 extern "C" void * init_module( void )
481 return new ModuleChanProtectFactory;