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
29 ChanFounder() : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false) { }
31 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
33 userrec* theuser = Srv->FindNick(parameter);
35 // cant find the user given as the parameter, eat the mode change.
39 return MODEACTION_DENY;
42 // given user isnt even on the channel, eat the mode change
43 if (!channel->HasUser(theuser))
46 return MODEACTION_DENY;
49 // source is a server, or ulined, we'll let them +-q the user.
50 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server))
54 if (!theuser->GetExt("cm_founder_"+std::string(channel->name)))
56 theuser->Extend("cm_founder_"+std::string(channel->name),fakevalue);
57 // Tidy the nickname (make case match etc)
58 parameter = theuser->nick;
59 return MODEACTION_ALLOW;
64 if (theuser->GetExt("cm_founder_"+std::string(channel->name)))
66 theuser->Shrink("cm_founder_"+std::string(channel->name));
67 // Tidy the nickname (make case match etc)
68 parameter = theuser->nick;
69 return MODEACTION_ALLOW;
72 return MODEACTION_DENY;
76 // whoops, someones being naughty!
77 WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
79 return MODEACTION_DENY;
83 void DisplayList(userrec* user, chanrec* channel)
85 chanuserlist cl = Srv->GetUsers(channel);
86 for (unsigned int i = 0; i < cl.size(); i++)
88 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
90 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
93 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
98 class ChanProtect : public ModeHandler
101 ChanProtect() : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false) { }
103 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
105 userrec* theuser = Srv->FindNick(parameter);
107 // cant find the user given as the parameter, eat the mode change.
111 return MODEACTION_DENY;
114 // given user isnt even on the channel, eat the mode change
115 if (!chan->HasUser(theuser))
118 return MODEACTION_DENY;
121 // source has +q, is a server, or ulined, we'll let them +-a the user.
122 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt("cm_founder_"+std::string(channel->name))))
126 if (!theuser->GetExt("cm_protect_"+std::string(channel->name)))
128 theuser->Extend("cm_protect_"+std::string(channel->name),fakevalue);
129 // Tidy the nickname (make case match etc)
130 parameter = theuser->nick;
131 return MODEACTION_ALLOW;
136 if (theuser->GetExt("cm_protect_"+std::string(channel->name)))
138 theuser->Shrink("cm_protect_"+std::string(channel->name));
139 // Tidy the nickname (make case match etc)
140 parameter = theuser->nick;
141 return MODEACTION_ALLOW;
144 return MODEACTION_DENY;
148 // bzzzt, wrong answer!
149 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
150 return MODEACTION_DENY;
154 virtual void DisplayList(userrec* user, chanrec* channel)
156 chanuserlist cl = Srv->GetUsers(channel);
157 for (unsigned int i = 0; i < cl.size(); i++)
159 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
161 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
164 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
169 class ModuleChanProtect : public Module
172 bool FirstInGetsFounder;
177 ModuleChanProtect(Server* Me)
178 : Module::Module(Me), Srv(Me)
180 /* Initialise module variables */
181 Conf = new ConfigReader;
183 // set up our modes. We're using listmodes and not normal extmodes here.
184 // listmodes only need one parameter as everything else is assumed by the
185 // nature of the mode thats being created.
186 Srv->AddExtendedListMode('a');
187 Srv->AddExtendedListMode('q');
189 // read our config options (main config file)
190 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
193 void Implements(char* List)
195 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnExtendedMode] = List[I_OnSendList] = List[I_OnSyncChannel] = 1;
198 virtual void On005Numeric(std::string &output)
200 InsertMode(output,"qa",1);
203 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
205 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
206 user->Shrink("cm_founder_"+std::string(chan->name));
207 user->Shrink("cm_protect_"+std::string(chan->name));
210 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
212 // FIX: when someone parts a channel we must remove their Extensibles!
213 user->Shrink("cm_founder_"+std::string(channel->name));
214 user->Shrink("cm_protect_"+std::string(channel->name));
217 virtual void OnRehash(const std::string ¶meter)
219 // on a rehash we delete our classes for good measure and create them again.
221 Conf = new ConfigReader;
222 // re-read our config options on a rehash
223 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
226 virtual void OnUserJoin(userrec* user, chanrec* channel)
228 // if the user is the first user into the channel, mark them as the founder, but only if
229 // the config option for it is set
230 if (FirstInGetsFounder)
232 if (Srv->CountUsers(channel) == 1)
234 // we're using Extensible::Extend to add data into user objects.
235 // this way is best as it adds data thats accessible to other modules
236 // (so long as you document your code properly) without breaking anything
237 // because its encapsulated neatly in a map.
239 // Change requested by katsklaw... when the first in is set to get founder,
240 // to make it clearer that +q has been given, send that one user the +q notice
241 // so that their client's syncronization and their sanity are left intact.
242 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
243 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
245 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
251 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
253 // here we perform access checks, this is the important bit that actually stops kicking/deopping
254 // etc of protected users. There are many types of access check, we're going to handle
255 // a relatively small number of them relevent to our module using a switch statement.
257 // don't allow action if:
258 // (A) Theyre founder (no matter what)
259 // (B) Theyre protected, and you're not
260 // always allow the action if:
261 // (A) The source is ulined
264 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
265 // without any access checks, we're not worthy :p
266 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
273 // a user has been deopped. Do we let them? hmmm...
275 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
277 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");
280 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
282 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)");
287 // a user is being kicked. do we chop off the end of the army boot?
289 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
291 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");
294 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
296 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)");
301 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
303 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
305 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");
308 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
310 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)");
315 // same with devoice.
317 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
319 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");
322 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
324 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)");
330 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
334 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
336 // not out mode, bail
337 if ((modechar == 'q') && (type == MT_CHANNEL))
340 chanrec* chan = (chanrec*)target;
341 userrec* theuser = Srv->FindNick(params[0]);
343 // cant find the user given as the parameter, eat the mode change.
347 // given user isnt even on the channel, eat the mode change
348 if (!chan->HasUser(theuser))
351 // source is a server, or ulined, we'll let them +-q the user.
352 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
356 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
358 theuser->Extend("cm_founder_"+std::string(chan->name),fakevalue);
364 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
366 theuser->Shrink("cm_founder_"+std::string(chan->name));
375 // whoops, someones being naughty!
376 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
380 if ((modechar == 'a') && (type == MT_CHANNEL))
383 chanrec* chan = (chanrec*)target;
384 userrec* theuser = Srv->FindNick(params[0]);
386 // cant find the user given as the parameter, eat the mode change.
390 // given user isnt even on the channel, eat the mode change
391 if (!chan->HasUser(theuser))
394 // source has +q, is a server, or ulined, we'll let them +-a the user.
395 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
399 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
401 theuser->Extend("cm_protect_"+std::string(chan->name),fakevalue);
407 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
409 theuser->Shrink("cm_protect_"+std::string(chan->name));
418 // bzzzt, wrong answer!
419 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
426 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
430 chanuserlist cl = Srv->GetUsers(channel);
431 for (unsigned int i = 0; i < cl.size(); i++)
433 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
435 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
438 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
442 chanuserlist cl = Srv->GetUsers(channel);
443 for (unsigned int i = 0; i < cl.size(); i++)
445 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
447 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
450 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
455 virtual ~ModuleChanProtect()
460 virtual Version GetVersion()
462 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
465 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
467 // this is called when the server is linking into a net and wants to sync channel data.
468 // we should send our mode changes for the channel here to ensure that other servers
469 // know whos +q/+a on the channel.
470 chanuserlist cl = Srv->GetUsers(chan);
471 string_list commands;
472 for (unsigned int i = 0; i < cl.size(); i++)
474 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
476 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
478 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
480 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
488 class ModuleChanProtectFactory : public ModuleFactory
491 ModuleChanProtectFactory()
495 ~ModuleChanProtectFactory()
499 virtual Module * CreateModule(Server* Me)
501 return new ModuleChanProtect(Me);
507 extern "C" void * init_module( void )
509 return new ModuleChanProtectFactory;