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 * ---------------------------------------------------
21 #include "helperfuncs.h"
23 /* $ModDesc: Provides channel modes +a and +q */
25 char dummyvalue[] = "on";
27 class ModuleChanProtect : public Module
30 bool FirstInGetsFounder;
38 // here we initialise our module. Use new to create new instances of the required
42 Conf = new ConfigReader;
44 // set up our modes. We're using listmodes and not normal extmodes here.
45 // listmodes only need one parameter as everything else is assumed by the
46 // nature of the mode thats being created.
47 Srv->AddExtendedListMode('a');
48 Srv->AddExtendedListMode('q');
50 // read our config options (main config file)
51 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
54 virtual void On005Numeric(std::string &output)
56 std::stringstream line(output);
57 std::string temp1, temp2;
61 if (temp1.substr(0,10) == "CHANMODES=")
63 // append the chanmode to the end
64 temp1 = temp1.substr(10,temp1.length());
65 temp1 = "CHANMODES=qa" + temp1;
67 temp2 = temp2 + temp1 + " ";
70 output = temp2.substr(0,temp2.length()-1);
73 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
75 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
76 user->Shrink("cm_founder_"+std::string(chan->name));
77 user->Shrink("cm_protect_"+std::string(chan->name));
80 virtual void OnUserPart(userrec* user, chanrec* channel)
82 // FIX: when someone parts a channel we must remove their Extensibles!
83 user->Shrink("cm_founder_"+std::string(channel->name));
84 user->Shrink("cm_protect_"+std::string(channel->name));
87 virtual void OnRehash()
89 // on a rehash we delete our classes for good measure and create them again.
91 Conf = new ConfigReader;
92 // re-read our config options on a rehash
93 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
96 virtual void OnUserJoin(userrec* user, chanrec* channel)
98 // if the user is the first user into the channel, mark them as the founder, but only if
99 // the config option for it is set
100 if (FirstInGetsFounder)
102 if (Srv->CountUsers(channel) == 1)
104 // we're using Extensible::Extend to add data into user objects.
105 // this way is best as it adds data thats accessible to other modules
106 // (so long as you document your code properly) without breaking anything
107 // because its encapsulated neatly in a map.
108 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
110 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
116 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
118 // here we perform access checks, this is the important bit that actually stops kicking/deopping
119 // etc of protected users. There are many types of access check, we're going to handle
120 // a relatively small number of them relevent to our module using a switch statement.
122 // don't allow action if:
123 // (A) Theyre founder (no matter what)
124 // (B) Theyre protected, and you're not
125 // always allow the action if:
126 // (A) The source is ulined
129 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
130 // without any access checks, we're not worthy :p
131 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
138 // a user has been deopped. Do we let them? hmmm...
140 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
142 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're a channel founder");
145 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
147 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're protected (+a)");
152 // a user is being kicked. do we chop off the end of the army boot?
154 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
156 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're a channel founder");
159 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
161 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're protected (+a)");
166 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
168 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
170 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're a channel founder");
173 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
175 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're protected (+a)");
180 // same with devoice.
182 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
184 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're a channel founder");
187 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
189 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're protected (+a)");
195 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
199 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
201 // not out mode, bail
202 if ((modechar == 'q') && (type == MT_CHANNEL))
205 chanrec* chan = (chanrec*)target;
206 userrec* theuser = Srv->FindNick(params[0]);
208 // cant find the user given as the parameter, eat the mode change.
212 // given user isnt even on the channel, eat the mode change
213 if (!Srv->IsOnChannel(theuser,chan))
216 // source is a server, or ulined, we'll let them +-q the user.
217 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
221 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
223 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
229 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
231 theuser->Shrink("cm_founder_"+std::string(chan->name));
240 // whoops, someones being naughty!
241 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
245 if ((modechar == 'a') && (type == MT_CHANNEL))
248 chanrec* chan = (chanrec*)target;
249 userrec* theuser = Srv->FindNick(params[0]);
251 // cant find the user given as the parameter, eat the mode change.
255 // given user isnt even on the channel, eat the mode change
256 if (!Srv->IsOnChannel(theuser,chan))
259 // source has +q, is a server, or ulined, we'll let them +-a the user.
260 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
264 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
266 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
272 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
274 theuser->Shrink("cm_protect_"+std::string(chan->name));
283 // bzzzt, wrong answer!
284 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
291 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
295 chanuserlist cl = Srv->GetUsers(channel);
296 for (int i = 0; i < cl.size(); i++)
298 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
300 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
303 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
307 chanuserlist cl = Srv->GetUsers(channel);
308 for (int i = 0; i < cl.size(); i++)
310 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
312 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
315 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
320 virtual ~ModuleChanProtect()
326 virtual Version GetVersion()
328 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
331 virtual string_list OnChannelSync(chanrec* chan)
333 // this is called when the server is linking into a net and wants to sync channel data.
334 // we should send our mode changes for the channel here to ensure that other servers
335 // know whos +q/+a on the channel.
336 chanuserlist cl = Srv->GetUsers(chan);
337 string_list commands;
338 for (int i = 0; i < cl.size(); i++)
340 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
342 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
344 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
346 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
355 class ModuleChanProtectFactory : public ModuleFactory
358 ModuleChanProtectFactory()
362 ~ModuleChanProtectFactory()
366 virtual Module * CreateModule()
368 return new ModuleChanProtect;
374 extern "C" void * init_module( void )
376 return new ModuleChanProtectFactory;