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 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
38 virtual void OnRehash()
40 // on a rehash we delete our classes for good measure and create them again.
42 Conf = new ConfigReader;
43 // re-read our config options on a rehash
44 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
47 virtual void OnUserJoin(userrec* user, chanrec* channel)
49 // if the user is the first user into the channel, mark them as the founder, but only if
50 // the config option for it is set
51 if (FirstInGetsFounder)
53 if (Srv->CountUsers(channel) == 1)
55 // we're using Extensible::Extend to add data into user objects.
56 // this way is best as it adds data thats accessible to other modules
57 // (so long as you document your code properly) without breaking anything
58 // because its encapsulated neatly in a map.
59 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
61 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
67 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
69 // here we perform access checks, this is the important bit that actually stops kicking/deopping
70 // etc of protected users. There are many types of access check, we're going to handle
71 // a relatively small number of them relevent to our module using a switch statement.
73 // don't allow action if:
74 // (A) Theyre founder (no matter what)
75 // (B) Theyre protected, and you're not
76 // always allow the action if:
77 // (A) The source is ulined
80 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
81 // without any access checks, we're not worthy :p
82 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
89 // a user has been deopped. Do we let them? hmmm...
91 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
93 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");
96 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
98 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)");
103 // a user is being kicked. do we chop off the end of the army boot?
105 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
107 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");
110 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
112 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)");
117 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
119 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
121 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");
124 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
126 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)");
131 // same with devoice.
133 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
135 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");
138 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
140 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)");
146 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
150 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
152 // not out mode, bail
153 if ((modechar == 'q') && (type == MT_CHANNEL))
156 chanrec* chan = (chanrec*)target;
157 userrec* theuser = Srv->FindNick(params[0]);
159 // cant find the user given as the parameter, eat the mode change.
163 // given user isnt even on the channel, eat the mode change
164 if (!Srv->IsOnChannel(theuser,chan))
167 // source is a server, or ulined, we'll let them +-q the user.
168 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
172 if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
174 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
180 if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
182 theuser->Shrink("cm_founder_"+std::string(chan->name));
191 // whoops, someones being naughty!
192 WriteServ(user->fd,"482 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
196 if ((modechar == 'a') && (type == MT_CHANNEL))
199 chanrec* chan = (chanrec*)target;
200 userrec* theuser = Srv->FindNick(params[0]);
202 // cant find the user given as the parameter, eat the mode change.
206 // given user isnt even on the channel, eat the mode change
207 if (!Srv->IsOnChannel(theuser,chan))
210 // source has +q, is a server, or ulined, we'll let them +-a the user.
211 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
215 if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
217 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
223 if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
225 theuser->Shrink("cm_protect_"+std::string(chan->name));
234 // bzzzt, wrong answer!
235 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
242 virtual ~ModuleChanProtect()
248 virtual Version GetVersion()
250 return Version(1,0,0,0);
253 virtual string_list OnChannelSync(chanrec* chan)
255 // this is called when the server is linking into a net and wants to sync channel data.
256 // we should send our mode changes for the channel here to ensure that other servers
257 // know whos +q/+a on the channel.
258 chanuserlist cl = Srv->GetUsers(chan);
259 string_list commands;
260 for (int i = 0; i < cl.size(); i++)
262 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
264 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
266 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
268 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
277 class ModuleChanProtectFactory : public ModuleFactory
280 ModuleChanProtectFactory()
284 ~ModuleChanProtectFactory()
288 virtual Module * CreateModule()
290 return new ModuleChanProtect;
296 extern "C" void * init_module( void )
298 return new ModuleChanProtectFactory;