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
30 ChanFounder(Server* s) : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
32 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
34 userrec* theuser = Srv->FindNick(parameter);
36 // cant find the user given as the parameter, eat the mode change.
40 return MODEACTION_DENY;
43 // given user isnt even on the channel, eat the mode change
44 if (!channel->HasUser(theuser))
47 return MODEACTION_DENY;
50 // source is a server, or ulined, we'll let them +-q the user.
51 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server))
55 if (!theuser->GetExt("cm_founder_"+std::string(channel->name)))
57 theuser->Extend("cm_founder_"+std::string(channel->name),fakevalue);
58 // Tidy the nickname (make case match etc)
59 parameter = theuser->nick;
60 return MODEACTION_ALLOW;
65 if (theuser->GetExt("cm_founder_"+std::string(channel->name)))
67 theuser->Shrink("cm_founder_"+std::string(channel->name));
68 // Tidy the nickname (make case match etc)
69 parameter = theuser->nick;
70 return MODEACTION_ALLOW;
73 return MODEACTION_DENY;
77 // whoops, someones being naughty!
78 WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
80 return MODEACTION_DENY;
84 void DisplayList(userrec* user, chanrec* channel)
86 chanuserlist cl = Srv->GetUsers(channel);
87 for (unsigned int i = 0; i < cl.size(); i++)
89 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
91 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
94 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
99 class ChanProtect : public ModeHandler
103 ChanProtect(Server* s) : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
105 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
107 userrec* theuser = Srv->FindNick(parameter);
109 // cant find the user given as the parameter, eat the mode change.
113 return MODEACTION_DENY;
116 // given user isnt even on the channel, eat the mode change
117 if (!channel->HasUser(theuser))
120 return MODEACTION_DENY;
123 // source has +q, is a server, or ulined, we'll let them +-a the user.
124 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt("cm_founder_"+std::string(channel->name))))
128 if (!theuser->GetExt("cm_protect_"+std::string(channel->name)))
130 theuser->Extend("cm_protect_"+std::string(channel->name),fakevalue);
131 // Tidy the nickname (make case match etc)
132 parameter = theuser->nick;
133 return MODEACTION_ALLOW;
138 if (theuser->GetExt("cm_protect_"+std::string(channel->name)))
140 theuser->Shrink("cm_protect_"+std::string(channel->name));
141 // Tidy the nickname (make case match etc)
142 parameter = theuser->nick;
143 return MODEACTION_ALLOW;
146 return MODEACTION_DENY;
150 // bzzzt, wrong answer!
151 WriteServ(source->fd,"482 %s %s :You are not a channel founder",source->nick, channel->name);
152 return MODEACTION_DENY;
156 virtual void DisplayList(userrec* user, chanrec* channel)
158 chanuserlist cl = Srv->GetUsers(channel);
159 for (unsigned int i = 0; i < cl.size(); i++)
161 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
163 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
166 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
171 class ModuleChanProtect : public Module
174 bool FirstInGetsFounder;
181 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
183 /* Initialise module variables */
184 Conf = new ConfigReader;
186 cp = new ChanProtect(Me);
187 cf = new ChanFounder(Me);
189 Srv->AddMode(cp, 'a');
190 Srv->AddMode(cf, 'q');
192 // read our config options (main config file)
193 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
196 void Implements(char* List)
198 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
201 virtual void On005Numeric(std::string &output)
203 InsertMode(output,"qa",1);
206 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
208 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
209 user->Shrink("cm_founder_"+std::string(chan->name));
210 user->Shrink("cm_protect_"+std::string(chan->name));
213 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
215 // FIX: when someone parts a channel we must remove their Extensibles!
216 user->Shrink("cm_founder_"+std::string(channel->name));
217 user->Shrink("cm_protect_"+std::string(channel->name));
220 virtual void OnRehash(const std::string ¶meter)
222 // on a rehash we delete our classes for good measure and create them again.
224 Conf = new ConfigReader;
225 // re-read our config options on a rehash
226 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
229 virtual void OnUserJoin(userrec* user, chanrec* channel)
231 // if the user is the first user into the channel, mark them as the founder, but only if
232 // the config option for it is set
233 if (FirstInGetsFounder)
235 if (Srv->CountUsers(channel) == 1)
237 // we're using Extensible::Extend to add data into user objects.
238 // this way is best as it adds data thats accessible to other modules
239 // (so long as you document your code properly) without breaking anything
240 // because its encapsulated neatly in a map.
242 // Change requested by katsklaw... when the first in is set to get founder,
243 // to make it clearer that +q has been given, send that one user the +q notice
244 // so that their client's syncronization and their sanity are left intact.
245 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
246 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
248 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
254 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
256 // here we perform access checks, this is the important bit that actually stops kicking/deopping
257 // etc of protected users. There are many types of access check, we're going to handle
258 // a relatively small number of them relevent to our module using a switch statement.
260 // don't allow action if:
261 // (A) Theyre founder (no matter what)
262 // (B) Theyre protected, and you're not
263 // always allow the action if:
264 // (A) The source is ulined
267 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
268 // without any access checks, we're not worthy :p
269 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
276 // a user has been deopped. Do we let them? hmmm...
278 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
280 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");
283 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
285 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)");
290 // a user is being kicked. do we chop off the end of the army boot?
292 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
294 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");
297 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
299 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)");
304 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
306 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
308 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");
311 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
313 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)");
318 // same with devoice.
320 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
322 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");
325 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
327 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)");
333 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
337 virtual ~ModuleChanProtect()
344 virtual Version GetVersion()
346 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
349 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
351 // this is called when the server is linking into a net and wants to sync channel data.
352 // we should send our mode changes for the channel here to ensure that other servers
353 // know whos +q/+a on the channel.
354 chanuserlist cl = Srv->GetUsers(chan);
355 string_list commands;
356 for (unsigned int i = 0; i < cl.size(); i++)
358 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
360 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
362 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
364 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
372 class ModuleChanProtectFactory : public ModuleFactory
375 ModuleChanProtectFactory()
379 ~ModuleChanProtectFactory()
383 virtual Module * CreateModule(Server* Me)
385 return new ModuleChanProtect(Me);
391 extern "C" void * init_module( void )
393 return new ModuleChanProtectFactory;