1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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 * ---------------------------------------------------
23 #include "helperfuncs.h"
25 /* $ModDesc: Provides channel modes +a and +q */
27 char fakevalue[] = "on";
29 class ModuleChanProtect : public Module
32 bool FirstInGetsFounder;
40 // here we initialise our module. Use new to create new instances of the required
44 Conf = new ConfigReader;
46 // set up our modes. We're using listmodes and not normal extmodes here.
47 // listmodes only need one parameter as everything else is assumed by the
48 // nature of the mode thats being created.
49 Srv->AddExtendedListMode('a');
50 Srv->AddExtendedListMode('q');
52 // read our config options (main config file)
53 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
56 virtual void On005Numeric(std::string &output)
58 std::stringstream line(output);
59 std::string temp1, temp2;
63 if (temp1.substr(0,10) == "CHANMODES=")
65 // append the chanmode to the end
66 temp1 = temp1.substr(10,temp1.length());
67 temp1 = "CHANMODES=qa" + temp1;
69 temp2 = temp2 + temp1 + " ";
72 output = temp2.substr(0,temp2.length()-1);
75 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
77 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
78 user->Shrink("cm_founder_"+std::string(chan->name));
79 user->Shrink("cm_protect_"+std::string(chan->name));
82 virtual void OnUserPart(userrec* user, chanrec* channel)
84 // FIX: when someone parts a channel we must remove their Extensibles!
85 user->Shrink("cm_founder_"+std::string(channel->name));
86 user->Shrink("cm_protect_"+std::string(channel->name));
89 virtual void OnRehash(std::string parameter)
91 // on a rehash we delete our classes for good measure and create them again.
93 Conf = new ConfigReader;
94 // re-read our config options on a rehash
95 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
98 virtual void OnUserJoin(userrec* user, chanrec* channel)
100 // if the user is the first user into the channel, mark them as the founder, but only if
101 // the config option for it is set
102 if (FirstInGetsFounder)
104 if (Srv->CountUsers(channel) == 1)
106 // we're using Extensible::Extend to add data into user objects.
107 // this way is best as it adds data thats accessible to other modules
108 // (so long as you document your code properly) without breaking anything
109 // because its encapsulated neatly in a map.
111 // Change requested by katsklaw... when the first in is set to get founder,
112 // to make it clearer that +q has been given, send that one user the +q notice
113 // so that their client's syncronization and their sanity are left intact.
114 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
115 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
117 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
123 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
125 // here we perform access checks, this is the important bit that actually stops kicking/deopping
126 // etc of protected users. There are many types of access check, we're going to handle
127 // a relatively small number of them relevent to our module using a switch statement.
129 // don't allow action if:
130 // (A) Theyre founder (no matter what)
131 // (B) Theyre protected, and you're not
132 // always allow the action if:
133 // (A) The source is ulined
136 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
137 // without any access checks, we're not worthy :p
138 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
145 // a user has been deopped. Do we let them? hmmm...
147 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
149 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");
152 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
154 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)");
159 // a user is being kicked. do we chop off the end of the army boot?
161 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
163 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");
166 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
168 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)");
173 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
175 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
177 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");
180 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
182 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)");
187 // same with devoice.
189 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
191 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");
194 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
196 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)");
202 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
206 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
208 // not out mode, bail
209 if ((modechar == 'q') && (type == MT_CHANNEL))
212 chanrec* chan = (chanrec*)target;
213 userrec* theuser = Srv->FindNick(params[0]);
215 // cant find the user given as the parameter, eat the mode change.
219 // given user isnt even on the channel, eat the mode change
220 if (!Srv->IsOnChannel(theuser,chan))
223 // source is a server, or ulined, we'll let them +-q the user.
224 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
228 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
230 theuser->Extend("cm_founder_"+std::string(chan->name),fakevalue);
236 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
238 theuser->Shrink("cm_founder_"+std::string(chan->name));
247 // whoops, someones being naughty!
248 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
252 if ((modechar == 'a') && (type == MT_CHANNEL))
255 chanrec* chan = (chanrec*)target;
256 userrec* theuser = Srv->FindNick(params[0]);
258 // cant find the user given as the parameter, eat the mode change.
262 // given user isnt even on the channel, eat the mode change
263 if (!Srv->IsOnChannel(theuser,chan))
266 // source has +q, is a server, or ulined, we'll let them +-a the user.
267 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
271 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
273 theuser->Extend("cm_protect_"+std::string(chan->name),fakevalue);
279 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
281 theuser->Shrink("cm_protect_"+std::string(chan->name));
290 // bzzzt, wrong answer!
291 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
298 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
302 chanuserlist cl = Srv->GetUsers(channel);
303 for (unsigned int i = 0; i < cl.size(); i++)
305 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
307 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
310 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
314 chanuserlist cl = Srv->GetUsers(channel);
315 for (unsigned int i = 0; i < cl.size(); i++)
317 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
319 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
322 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
327 virtual ~ModuleChanProtect()
333 virtual Version GetVersion()
335 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
338 virtual string_list OnChannelSync(chanrec* chan)
340 // this is called when the server is linking into a net and wants to sync channel data.
341 // we should send our mode changes for the channel here to ensure that other servers
342 // know whos +q/+a on the channel.
343 chanuserlist cl = Srv->GetUsers(chan);
344 string_list commands;
345 for (unsigned int i = 0; i < cl.size(); i++)
347 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
349 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
351 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
353 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
362 class ModuleChanProtectFactory : public ModuleFactory
365 ModuleChanProtectFactory()
369 ~ModuleChanProtectFactory()
373 virtual Module * CreateModule()
375 return new ModuleChanProtect;
381 extern "C" void * init_module( void )
383 return new ModuleChanProtectFactory;