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 * ---------------------------------------------------
22 /* $ModDesc: Provides channel modes +a and +q */
24 char dummyvalue[] = "on";
26 class ModuleChanProtect : public Module
29 bool FirstInGetsFounder;
37 // here we initialise our module. Use new to create new instances of the required
41 Conf = new ConfigReader;
43 // set up our modes. We're using listmodes and not normal extmodes here.
44 // listmodes only need one parameter as everything else is assumed by the
45 // nature of the mode thats being created.
46 Srv->AddExtendedListMode('a');
47 Srv->AddExtendedListMode('q');
49 // read our config options (main config file)
50 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
53 virtual void On005Numeric(std::string &output)
55 std::stringstream line(output);
56 std::string temp1, temp2;
60 if (temp1.substr(0,10) == "CHANMODES=")
62 // append the chanmode to the end
63 temp1 = temp1.substr(10,temp1.length());
64 temp1 = "CHANMODES=qa" + temp1;
66 temp2 = temp2 + temp1 + " ";
69 output = temp2.substr(0,temp2.length()-1);
72 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
74 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
75 user->Shrink("cm_founder_"+std::string(chan->name));
76 user->Shrink("cm_protect_"+std::string(chan->name));
79 virtual void OnUserPart(userrec* user, chanrec* channel)
81 // FIX: when someone parts a channel we must remove their Extensibles!
82 user->Shrink("cm_founder_"+std::string(channel->name));
83 user->Shrink("cm_protect_"+std::string(channel->name));
86 virtual void OnRehash()
88 // on a rehash we delete our classes for good measure and create them again.
90 Conf = new ConfigReader;
91 // re-read our config options on a rehash
92 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
95 virtual void OnUserJoin(userrec* user, chanrec* channel)
97 // if the user is the first user into the channel, mark them as the founder, but only if
98 // the config option for it is set
99 if (FirstInGetsFounder)
101 if (Srv->CountUsers(channel) == 1)
103 // we're using Extensible::Extend to add data into user objects.
104 // this way is best as it adds data thats accessible to other modules
105 // (so long as you document your code properly) without breaking anything
106 // because its encapsulated neatly in a map.
107 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
109 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
115 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
117 // here we perform access checks, this is the important bit that actually stops kicking/deopping
118 // etc of protected users. There are many types of access check, we're going to handle
119 // a relatively small number of them relevent to our module using a switch statement.
121 // don't allow action if:
122 // (A) Theyre founder (no matter what)
123 // (B) Theyre protected, and you're not
124 // always allow the action if:
125 // (A) The source is ulined
128 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
129 // without any access checks, we're not worthy :p
130 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
137 // a user has been deopped. Do we let them? hmmm...
139 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
141 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");
144 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
146 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)");
151 // a user is being kicked. do we chop off the end of the army boot?
153 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
155 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");
158 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
160 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)");
165 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
167 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
169 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");
172 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
174 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)");
179 // same with devoice.
181 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
183 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");
186 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
188 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)");
194 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
198 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
200 // not out mode, bail
201 if ((modechar == 'q') && (type == MT_CHANNEL))
204 chanrec* chan = (chanrec*)target;
205 userrec* theuser = Srv->FindNick(params[0]);
207 // cant find the user given as the parameter, eat the mode change.
211 // given user isnt even on the channel, eat the mode change
212 if (!Srv->IsOnChannel(theuser,chan))
215 // source is a server, or ulined, we'll let them +-q the user.
216 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
220 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
222 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
228 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
230 theuser->Shrink("cm_founder_"+std::string(chan->name));
239 // whoops, someones being naughty!
240 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
244 if ((modechar == 'a') && (type == MT_CHANNEL))
247 chanrec* chan = (chanrec*)target;
248 userrec* theuser = Srv->FindNick(params[0]);
250 // cant find the user given as the parameter, eat the mode change.
254 // given user isnt even on the channel, eat the mode change
255 if (!Srv->IsOnChannel(theuser,chan))
258 // source has +q, is a server, or ulined, we'll let them +-a the user.
259 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
263 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
265 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
271 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
273 theuser->Shrink("cm_protect_"+std::string(chan->name));
282 // bzzzt, wrong answer!
283 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
290 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
294 chanuserlist cl = Srv->GetUsers(channel);
295 for (int i = 0; i < cl.size(); i++)
297 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
299 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
302 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
306 chanuserlist cl = Srv->GetUsers(channel);
307 for (int i = 0; i < cl.size(); i++)
309 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
311 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
314 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
319 virtual ~ModuleChanProtect()
325 virtual Version GetVersion()
327 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
330 virtual string_list OnChannelSync(chanrec* chan)
332 // this is called when the server is linking into a net and wants to sync channel data.
333 // we should send our mode changes for the channel here to ensure that other servers
334 // know whos +q/+a on the channel.
335 chanuserlist cl = Srv->GetUsers(chan);
336 string_list commands;
337 for (int i = 0; i < cl.size(); i++)
339 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
341 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
343 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
345 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
354 class ModuleChanProtectFactory : public ModuleFactory
357 ModuleChanProtectFactory()
361 ~ModuleChanProtectFactory()
365 virtual Module * CreateModule()
367 return new ModuleChanProtect;
373 extern "C" void * init_module( void )
375 return new ModuleChanProtectFactory;