1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "m_override.h"
17 /* $ModDesc: Provides channel modes +a and +q */
19 #define PROTECT_VALUE 40000
20 #define FOUNDER_VALUE 50000
22 /** Handles basic operation of +qa channel modes
24 class FounderProtectBase
27 InspIRCd* const MyInstance;
28 const std::string type;
33 bool& remove_own_privs;
34 bool& remove_other_privs;
36 FounderProtectBase(InspIRCd* Instance, char Mode, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
37 MyInstance(Instance), type(mtype), mode(Mode), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
41 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
43 User* x = MyInstance->FindNick(parameter);
46 Membership* memb = channel->GetUser(x);
49 return std::make_pair(false, parameter);
53 if (memb->hasMode(mode))
55 return std::make_pair(true, x->nick);
59 return std::make_pair(false, parameter);
63 return std::make_pair(false, parameter);
66 void RemoveMode(Channel* channel, irc::modestacker* stack)
68 const UserMembList* cl = channel->GetUsers();
69 std::vector<std::string> mode_junk;
70 mode_junk.push_back(channel->name);
71 irc::modestacker modestack(MyInstance, false);
72 std::deque<std::string> stackresult;
74 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
76 if (i->second->hasMode(mode))
79 stack->Push(mode, i->first->nick);
81 modestack.Push(mode, i->first->nick);
88 while (modestack.GetStackedLine(stackresult))
90 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
91 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
92 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
96 void DisplayList(User* user, Channel* channel)
98 const UserMembList* cl = channel->GetUsers();
99 for (UserMembCIter i = cl->begin(); i != cl->end(); ++i)
101 if (i->second->hasMode(mode))
103 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
106 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
109 bool CanRemoveOthers(User* u1, Channel* c)
111 Membership* m1 = c->GetUser(u1);
112 return (remove_other_privs && m1 && m1->hasMode(mode));
116 /** Abstraction of FounderProtectBase for channel mode +q
118 class ChanFounder : public ModeHandler, public FounderProtectBase
121 ChanFounder(InspIRCd* Instance, Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
122 : ModeHandler(Instance, Creator, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
123 FounderProtectBase(Instance, 'q', "founder", 386, 387, depriv_self, depriv_others) { }
125 unsigned int GetPrefixRank()
127 return FOUNDER_VALUE;
130 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
132 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
135 void RemoveMode(Channel* channel, irc::modestacker* stack)
137 FounderProtectBase::RemoveMode(channel, stack);
140 void RemoveMode(User* user, irc::modestacker* stack)
144 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
146 User* theuser = ServerInstance->FindNick(parameter);
150 return MODEACTION_DENY;
153 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, channel))
155 return MODEACTION_ALLOW;
159 Module *Override = ServerInstance->Modules->FindFeature("Override");
162 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
163 const char * tmp = ovr.Send();
166 // source is a server, or ulined, we'll let them +-q the user.
167 if (!IS_LOCAL(source) ||
168 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
169 (ServerInstance->ULine(source->nick.c_str())) ||
170 (ServerInstance->ULine(source->server)) ||
171 (!*source->server) ||
174 return MODEACTION_ALLOW;
178 // whoops, someones being naughty!
179 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
181 return MODEACTION_DENY;
185 void DisplayList(User* user, Channel* channel)
187 FounderProtectBase::DisplayList(user,channel);
191 /** Abstraction of FounderProtectBase for channel mode +a
193 class ChanProtect : public ModeHandler, public FounderProtectBase
196 ChanProtect(InspIRCd* Instance, Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
197 : ModeHandler(Instance, Creator, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
198 FounderProtectBase(Instance,'a',"protected user", 388, 389, depriv_self, depriv_others) { }
200 unsigned int GetPrefixRank()
202 return PROTECT_VALUE;
205 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
207 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
210 void RemoveMode(Channel* channel, irc::modestacker* stack)
212 FounderProtectBase::RemoveMode(channel, stack);
215 void RemoveMode(User* user, irc::modestacker* stack)
219 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
221 User* theuser = ServerInstance->FindNick(parameter);
224 return MODEACTION_DENY;
226 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, channel))
228 return MODEACTION_ALLOW;
232 Module *Override = ServerInstance->Modules->FindFeature("Override");
235 OVRrequest ovr(NULL,Override,source,"OTHERMODE");
236 const char * tmp = ovr.Send();
239 // source has +q, is a server, or ulined, we'll let them +-a the user.
240 if (!IS_LOCAL(source) ||
241 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
242 (ServerInstance->ULine(source->nick.c_str())) ||
243 (ServerInstance->ULine(source->server)) ||
244 (!*source->server) ||
245 (channel->GetPrefixValue(source) > PROTECT_VALUE) ||
249 return MODEACTION_ALLOW;
253 // bzzzt, wrong answer!
254 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
255 return MODEACTION_DENY;
259 void DisplayList(User* user, Channel* channel)
261 FounderProtectBase::DisplayList(user, channel);
266 class ModuleChanProtect : public Module
269 bool FirstInGetsFounder;
280 ModuleChanProtect(InspIRCd* Me)
281 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
283 /* Load config stuff */
287 /* Initialise module variables */
289 cp = new ChanProtect(ServerInstance, this, APrefix, DeprivSelf, DeprivOthers);
290 cf = new ChanFounder(ServerInstance, this, QPrefix, DeprivSelf, DeprivOthers);
292 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
296 throw ModuleException("Could not add new modes!");
299 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin };
300 ServerInstance->Modules->Attach(eventlist, this, 3);
305 ConfigReader Conf(ServerInstance);
307 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
309 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
310 QPrefix = qpre.empty() ? 0 : qpre[0];
312 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
313 APrefix = apre.empty() ? 0 : apre[0];
315 if ((APrefix && QPrefix) && APrefix == QPrefix)
316 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
318 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
319 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
321 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
322 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
324 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
325 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
328 ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
330 // if the user is the first user into the channel, mark them as the founder, but only if
331 // the config option for it is set
333 if (FirstInGetsFounder && !chan)
334 privs += std::string(1, QPrefix);
336 return MOD_RES_PASSTHRU;
341 ServerInstance->Modes->DelMode(cp);
342 ServerInstance->Modes->DelMode(cf);
349 return Version("Founder and Protect modes (+qa)", VF_COMMON | VF_VENDOR);
353 MODULE_INIT(ModuleChanProtect)