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 OnUserPart(userrec* user, chanrec* channel)
74 // FIX: when someone parts a channel we must remove their Extensibles!
75 user->Shrink("cm_founder_"+std::string(channel->name));
76 user->Shrink("cm_protect_"+std::string(channel->name));
79 virtual void OnRehash()
81 // on a rehash we delete our classes for good measure and create them again.
83 Conf = new ConfigReader;
84 // re-read our config options on a rehash
85 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
88 virtual void OnUserJoin(userrec* user, chanrec* channel)
90 // if the user is the first user into the channel, mark them as the founder, but only if
91 // the config option for it is set
92 if (FirstInGetsFounder)
94 if (Srv->CountUsers(channel) == 1)
96 // we're using Extensible::Extend to add data into user objects.
97 // this way is best as it adds data thats accessible to other modules
98 // (so long as you document your code properly) without breaking anything
99 // because its encapsulated neatly in a map.
100 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
102 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
108 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
110 // here we perform access checks, this is the important bit that actually stops kicking/deopping
111 // etc of protected users. There are many types of access check, we're going to handle
112 // a relatively small number of them relevent to our module using a switch statement.
114 // don't allow action if:
115 // (A) Theyre founder (no matter what)
116 // (B) Theyre protected, and you're not
117 // always allow the action if:
118 // (A) The source is ulined
121 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
122 // without any access checks, we're not worthy :p
123 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
130 // a user has been deopped. Do we let them? hmmm...
132 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
134 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");
137 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
139 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)");
144 // a user is being kicked. do we chop off the end of the army boot?
146 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
148 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");
151 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
153 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)");
158 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
160 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
162 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");
165 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
167 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)");
172 // same with devoice.
174 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
176 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");
179 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
181 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)");
187 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
191 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
193 // not out mode, bail
194 if ((modechar == 'q') && (type == MT_CHANNEL))
197 chanrec* chan = (chanrec*)target;
198 userrec* theuser = Srv->FindNick(params[0]);
200 // cant find the user given as the parameter, eat the mode change.
204 // given user isnt even on the channel, eat the mode change
205 if (!Srv->IsOnChannel(theuser,chan))
208 // source is a server, or ulined, we'll let them +-q the user.
209 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
213 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
215 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
221 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
223 theuser->Shrink("cm_founder_"+std::string(chan->name));
232 // whoops, someones being naughty!
233 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
237 if ((modechar == 'a') && (type == MT_CHANNEL))
240 chanrec* chan = (chanrec*)target;
241 userrec* theuser = Srv->FindNick(params[0]);
243 // cant find the user given as the parameter, eat the mode change.
247 // given user isnt even on the channel, eat the mode change
248 if (!Srv->IsOnChannel(theuser,chan))
251 // source has +q, is a server, or ulined, we'll let them +-a the user.
252 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
256 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
258 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
264 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
266 theuser->Shrink("cm_protect_"+std::string(chan->name));
275 // bzzzt, wrong answer!
276 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
283 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
287 chanuserlist cl = Srv->GetUsers(channel);
288 for (int i = 0; i < cl.size(); i++)
290 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
292 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
295 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
299 chanuserlist cl = Srv->GetUsers(channel);
300 for (int i = 0; i < cl.size(); i++)
302 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
304 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
307 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
312 virtual ~ModuleChanProtect()
318 virtual Version GetVersion()
320 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
323 virtual string_list OnChannelSync(chanrec* chan)
325 // this is called when the server is linking into a net and wants to sync channel data.
326 // we should send our mode changes for the channel here to ensure that other servers
327 // know whos +q/+a on the channel.
328 chanuserlist cl = Srv->GetUsers(chan);
329 string_list commands;
330 for (int i = 0; i < cl.size(); i++)
332 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
334 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
336 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
338 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
347 class ModuleChanProtectFactory : public ModuleFactory
350 ModuleChanProtectFactory()
354 ~ModuleChanProtectFactory()
358 virtual Module * CreateModule()
360 return new ModuleChanProtect;
366 extern "C" void * init_module( void )
368 return new ModuleChanProtectFactory;