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 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
36 userrec* theuser = Srv->FindNick(parameter);
38 // cant find the user given as the parameter, eat the mode change.
42 return MODEACTION_DENY;
45 // given user isnt even on the channel, eat the mode change
46 if (!channel->HasUser(theuser))
49 return MODEACTION_DENY;
52 std::string protect = "cm_protect_"+std::string(channel->name);
53 std::string founder = "cm_founder_"+std::string(channel->name);
55 // source is a server, or ulined, we'll let them +-q the user.
56 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server))
60 if (!theuser->GetExt(founder,dummyptr))
62 theuser->Extend(founder,fakevalue);
63 // Tidy the nickname (make case match etc)
64 parameter = theuser->nick;
65 return MODEACTION_ALLOW;
70 if (theuser->GetExt(founder, dummyptr))
72 theuser->Shrink(founder);
73 // Tidy the nickname (make case match etc)
74 parameter = theuser->nick;
75 return MODEACTION_ALLOW;
78 return MODEACTION_DENY;
82 // whoops, someones being naughty!
83 WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
85 return MODEACTION_DENY;
89 void DisplayList(userrec* user, chanrec* channel)
91 chanuserlist cl = Srv->GetUsers(channel);
92 std::string founder = "cm_founder_"+std::string(channel->name);
93 for (unsigned int i = 0; i < cl.size(); i++)
95 if (cl[i]->GetExt(founder, dummyptr))
97 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
100 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
105 class ChanProtect : public ModeHandler
110 ChanProtect(Server* s) : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
112 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
114 userrec* theuser = Srv->FindNick(parameter);
116 // cant find the user given as the parameter, eat the mode change.
120 return MODEACTION_DENY;
123 // given user isnt even on the channel, eat the mode change
124 if (!channel->HasUser(theuser))
127 return MODEACTION_DENY;
130 std::string protect = "cm_protect_"+std::string(channel->name);
131 std::string founder = "cm_founder_"+std::string(channel->name);
133 // source has +q, is a server, or ulined, we'll let them +-a the user.
134 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)))
138 if (!theuser->GetExt(protect,dummyptr))
140 theuser->Extend(protect,fakevalue);
141 // Tidy the nickname (make case match etc)
142 parameter = theuser->nick;
143 return MODEACTION_ALLOW;
148 if (theuser->GetExt(protect,dummyptr))
150 theuser->Shrink(protect);
151 // Tidy the nickname (make case match etc)
152 parameter = theuser->nick;
153 return MODEACTION_ALLOW;
156 return MODEACTION_DENY;
160 // bzzzt, wrong answer!
161 WriteServ(source->fd,"482 %s %s :You are not a channel founder",source->nick, channel->name);
162 return MODEACTION_DENY;
166 virtual void DisplayList(userrec* user, chanrec* channel)
168 chanuserlist cl = Srv->GetUsers(channel);
169 std::string protect = "cm_protect_"+std::string(channel->name);
170 for (unsigned int i = 0; i < cl.size(); i++)
172 if (cl[i]->GetExt(protect,dummyptr))
174 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
177 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
182 class ModuleChanProtect : public Module
185 bool FirstInGetsFounder;
192 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
194 /* Initialise module variables */
196 cp = new ChanProtect(Me);
197 cf = new ChanFounder(Me);
199 Srv->AddMode(cp, 'a');
200 Srv->AddMode(cf, 'q');
202 /* Load config stuff */
206 void Implements(char* List)
208 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
211 virtual void On005Numeric(std::string &output)
213 InsertMode(output,"qa",1);
216 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
218 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
219 user->Shrink("cm_founder_"+std::string(chan->name));
220 user->Shrink("cm_protect_"+std::string(chan->name));
223 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
225 // FIX: when someone parts a channel we must remove their Extensibles!
226 user->Shrink("cm_founder_"+std::string(channel->name));
227 user->Shrink("cm_protect_"+std::string(channel->name));
230 virtual void OnRehash(const std::string ¶meter)
232 /* Create a configreader class and read our flag,
233 * in old versions this was heap-allocated and the
234 * object was kept between rehashes...now we just
235 * stack-allocate it locally.
239 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
242 virtual void OnUserJoin(userrec* user, chanrec* channel)
244 // if the user is the first user into the channel, mark them as the founder, but only if
245 // the config option for it is set
246 if (FirstInGetsFounder)
248 if (Srv->CountUsers(channel) == 1)
250 // we're using Extensible::Extend to add data into user objects.
251 // this way is best as it adds data thats accessible to other modules
252 // (so long as you document your code properly) without breaking anything
253 // because its encapsulated neatly in a map.
255 // Change requested by katsklaw... when the first in is set to get founder,
256 // to make it clearer that +q has been given, send that one user the +q notice
257 // so that their client's syncronization and their sanity are left intact.
258 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
259 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
261 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
267 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
269 // here we perform access checks, this is the important bit that actually stops kicking/deopping
270 // etc of protected users. There are many types of access check, we're going to handle
271 // a relatively small number of them relevent to our module using a switch statement.
273 // don't allow action if:
274 // (A) Theyre founder (no matter what)
275 // (B) Theyre protected, and you're not
276 // always allow the action if:
277 // (A) The source is ulined
280 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
281 // without any access checks, we're not worthy :p
282 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
287 std::string founder = "cm_founder_"+std::string(channel->name);
288 std::string protect = "cm_protect_"+std::string(channel->name);
292 // a user has been deopped. Do we let them? hmmm...
294 if (dest->GetExt(founder,dummyptr))
296 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");
299 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
301 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)");
306 // a user is being kicked. do we chop off the end of the army boot?
308 if (dest->GetExt(founder,dummyptr))
310 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");
313 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
315 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)");
320 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
322 if (dest->GetExt(founder,dummyptr))
324 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");
327 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
329 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)");
334 // same with devoice.
336 if (dest->GetExt(founder,dummyptr))
338 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");
341 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
343 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)");
349 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
353 virtual ~ModuleChanProtect()
359 virtual Version GetVersion()
361 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
364 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
366 // this is called when the server is linking into a net and wants to sync channel data.
367 // we should send our mode changes for the channel here to ensure that other servers
368 // know whos +q/+a on the channel.
369 chanuserlist cl = Srv->GetUsers(chan);
370 string_list commands;
371 std::string founder = "cm_founder_"+std::string(chan->name);
372 std::string protect = "cm_protect_"+std::string(chan->name);
373 for (unsigned int i = 0; i < cl.size(); i++)
375 if (cl[i]->GetExt(founder,dummyptr))
377 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
379 if (cl[i]->GetExt(protect,dummyptr))
381 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
389 class ModuleChanProtectFactory : public ModuleFactory
392 ModuleChanProtectFactory()
396 ~ModuleChanProtectFactory()
400 virtual Module * CreateModule(Server* Me)
402 return new ModuleChanProtect(Me);
408 extern "C" void * init_module( void )
410 return new ModuleChanProtectFactory;