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;
180 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
182 /* Initialise module variables */
184 cp = new ChanProtect(Me);
185 cf = new ChanFounder(Me);
187 Srv->AddMode(cp, 'a');
188 Srv->AddMode(cf, 'q');
190 /* Load config stuff */
194 void Implements(char* List)
196 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
199 virtual void On005Numeric(std::string &output)
201 InsertMode(output,"qa",1);
204 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
206 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
207 user->Shrink("cm_founder_"+std::string(chan->name));
208 user->Shrink("cm_protect_"+std::string(chan->name));
211 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
213 // FIX: when someone parts a channel we must remove their Extensibles!
214 user->Shrink("cm_founder_"+std::string(channel->name));
215 user->Shrink("cm_protect_"+std::string(channel->name));
218 virtual void OnRehash(const std::string ¶meter)
220 /* Create a configreader class and read our flag,
221 * in old versions this was heap-allocated and the
222 * object was kept between rehashes...now we just
223 * stack-allocate it locally.
227 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
230 virtual void OnUserJoin(userrec* user, chanrec* channel)
232 // if the user is the first user into the channel, mark them as the founder, but only if
233 // the config option for it is set
234 if (FirstInGetsFounder)
236 if (Srv->CountUsers(channel) == 1)
238 // we're using Extensible::Extend to add data into user objects.
239 // this way is best as it adds data thats accessible to other modules
240 // (so long as you document your code properly) without breaking anything
241 // because its encapsulated neatly in a map.
243 // Change requested by katsklaw... when the first in is set to get founder,
244 // to make it clearer that +q has been given, send that one user the +q notice
245 // so that their client's syncronization and their sanity are left intact.
246 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
247 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
249 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
255 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
257 // here we perform access checks, this is the important bit that actually stops kicking/deopping
258 // etc of protected users. There are many types of access check, we're going to handle
259 // a relatively small number of them relevent to our module using a switch statement.
261 // don't allow action if:
262 // (A) Theyre founder (no matter what)
263 // (B) Theyre protected, and you're not
264 // always allow the action if:
265 // (A) The source is ulined
268 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
269 // without any access checks, we're not worthy :p
270 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
277 // a user has been deopped. Do we let them? hmmm...
279 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
281 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");
284 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
286 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)");
291 // a user is being kicked. do we chop off the end of the army boot?
293 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
295 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");
298 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
300 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)");
305 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
307 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
309 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");
312 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
314 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)");
319 // same with devoice.
321 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
323 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");
326 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
328 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)");
334 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
338 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;