7 /* $ModDesc: Provides channel modes +a and +q */
9 char dummyvalue[] = "on";
11 class ModuleChanProtect : public Module
14 bool FirstInGetsFounder;
22 // here we initialise our module. Use new to create new instances of the required
26 Conf = new ConfigReader;
28 // set up our modes. We're using listmodes and not normal extmodes here.
29 // listmodes only need one parameter as everything else is assumed by the
30 // nature of the mode thats being created.
31 Srv->AddExtendedListMode('a');
32 Srv->AddExtendedListMode('q');
34 // read our config options (main config file)
35 std::string val = Conf->ReadValue("options","noservices",0);
36 FirstInGetsFounder = ((val == "yes") || (val == "1") || (val == "true"));
39 virtual void OnRehash()
41 // on a rehash we delete our classes for good measure and create them again.
43 Conf = new ConfigReader;
44 // re-read our config options on a rehash
45 std::string val = Conf->ReadValue("options","noservices",0);
46 FirstInGetsFounder = ((val == "yes") || (val == "1") || (val == "true"));
49 virtual void OnUserJoin(userrec* user, chanrec* channel)
51 // if the user is the first user into the channel, mark them as the founder, but only if
52 // the config option for it is set
53 if (FirstInGetsFounder)
55 if (Srv->CountUsers(channel) == 1)
57 // we're using Extensible::Extend to add data into user objects.
58 // this way is best as it adds data thats accessible to other modules
59 // (so long as you document your code properly) without breaking anything
60 // because its encapsulated neatly in a map.
61 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
63 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
69 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
71 // here we perform access checks, this is the important bit that actually stops kicking/deopping
72 // etc of protected users. There are many types of access check, we're going to handle
73 // a relatively small number of them relevent to our module using a switch statement.
75 // don't allow action if:
76 // (A) Theyre founder (no matter what)
77 // (B) Theyre protected, and you're not
78 // always allow the action if:
79 // (A) The source is ulined
82 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
83 // without any access checks, we're not worthy :p
84 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
91 // a user has been deopped. Do we let them? hmmm...
93 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
95 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're a channel founder");
98 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
100 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're protected (+a)");
105 // a user is being kicked. do we chop off the end of the army boot?
107 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
109 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're a channel founder");
112 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
114 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're protected (+a)");
119 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
121 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
123 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're a channel founder");
126 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
128 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're protected (+a)");
133 // same with devoice.
135 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
137 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're a channel founder");
140 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
142 Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're protected (+a)");
148 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
152 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
154 // not out mode, bail
155 if ((modechar == 'q') && (type == MT_CHANNEL))
158 chanrec* chan = (chanrec*)target;
159 userrec* theuser = Srv->FindNick(params[0]);
161 // cant find the user given as the parameter, eat the mode change.
165 // given user isnt even on the channel, eat the mode change
166 if (!Srv->IsOnChannel(theuser,chan))
169 // source is a server, or ulined, we'll let them +-q the user.
170 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
174 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
176 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
182 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
184 theuser->Shrink("cm_founder_"+std::string(chan->name));
193 // whoops, someones being naughty!
194 WriteServ(user->fd,"482 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
198 if ((modechar == 'a') && (type == MT_CHANNEL))
201 chanrec* chan = (chanrec*)target;
202 userrec* theuser = Srv->FindNick(params[0]);
204 // cant find the user given as the parameter, eat the mode change.
208 // given user isnt even on the channel, eat the mode change
209 if (!Srv->IsOnChannel(theuser,chan))
212 // source has +q, is a server, or ulined, we'll let them +-a the user.
213 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
217 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
219 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
225 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
227 theuser->Shrink("cm_protect_"+std::string(chan->name));
236 // bzzzt, wrong answer!
237 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
244 virtual ~ModuleChanProtect()
250 virtual Version GetVersion()
252 return Version(1,0,0,0);
255 virtual string_list OnChannelSync(chanrec* chan)
257 // this is called when the server is linking into a net and wants to sync channel data.
258 // we should send our mode changes for the channel here to ensure that other servers
259 // know whos +q/+a on the channel.
260 chanuserlist cl = Srv->GetUsers(chan);
261 string_list commands;
262 for (int i = 0; i < cl.size(); i++)
264 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
266 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
268 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
270 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
279 class ModuleChanProtectFactory : public ModuleFactory
282 ModuleChanProtectFactory()
286 ~ModuleChanProtectFactory()
290 virtual Module * CreateModule()
292 return new ModuleChanProtect;
298 extern "C" void * init_module( void )
300 return new ModuleChanProtectFactory;