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 dummyvalue[] = "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()
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.
110 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
112 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
118 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
120 // here we perform access checks, this is the important bit that actually stops kicking/deopping
121 // etc of protected users. There are many types of access check, we're going to handle
122 // a relatively small number of them relevent to our module using a switch statement.
124 // don't allow action if:
125 // (A) Theyre founder (no matter what)
126 // (B) Theyre protected, and you're not
127 // always allow the action if:
128 // (A) The source is ulined
131 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
132 // without any access checks, we're not worthy :p
133 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
140 // a user has been deopped. Do we let them? hmmm...
142 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
144 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");
147 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+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 the're protected (+a)");
154 // a user is being kicked. do we chop off the end of the army boot?
156 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
158 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");
161 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+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 the're protected (+a)");
168 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
170 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
172 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");
175 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+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 the're protected (+a)");
182 // same with devoice.
184 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
186 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");
189 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+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 the're protected (+a)");
197 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
201 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
203 // not out mode, bail
204 if ((modechar == 'q') && (type == MT_CHANNEL))
207 chanrec* chan = (chanrec*)target;
208 userrec* theuser = Srv->FindNick(params[0]);
210 // cant find the user given as the parameter, eat the mode change.
214 // given user isnt even on the channel, eat the mode change
215 if (!Srv->IsOnChannel(theuser,chan))
218 // source is a server, or ulined, we'll let them +-q the user.
219 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
223 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
225 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
231 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
233 theuser->Shrink("cm_founder_"+std::string(chan->name));
242 // whoops, someones being naughty!
243 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
247 if ((modechar == 'a') && (type == MT_CHANNEL))
250 chanrec* chan = (chanrec*)target;
251 userrec* theuser = Srv->FindNick(params[0]);
253 // cant find the user given as the parameter, eat the mode change.
257 // given user isnt even on the channel, eat the mode change
258 if (!Srv->IsOnChannel(theuser,chan))
261 // source has +q, is a server, or ulined, we'll let them +-a the user.
262 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
266 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
268 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
274 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
276 theuser->Shrink("cm_protect_"+std::string(chan->name));
285 // bzzzt, wrong answer!
286 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
293 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
297 chanuserlist cl = Srv->GetUsers(channel);
298 for (int i = 0; i < cl.size(); i++)
300 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
302 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
305 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
309 chanuserlist cl = Srv->GetUsers(channel);
310 for (int i = 0; i < cl.size(); i++)
312 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
314 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
317 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
322 virtual ~ModuleChanProtect()
328 virtual Version GetVersion()
330 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
333 virtual string_list OnChannelSync(chanrec* chan)
335 // this is called when the server is linking into a net and wants to sync channel data.
336 // we should send our mode changes for the channel here to ensure that other servers
337 // know whos +q/+a on the channel.
338 chanuserlist cl = Srv->GetUsers(chan);
339 string_list commands;
340 for (int i = 0; i < cl.size(); i++)
342 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
344 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
346 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
348 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
357 class ModuleChanProtectFactory : public ModuleFactory
360 ModuleChanProtectFactory()
364 ~ModuleChanProtectFactory()
368 virtual Module * CreateModule()
370 return new ModuleChanProtect;
376 extern "C" void * init_module( void )
378 return new ModuleChanProtectFactory;