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 * ---------------------------------------------------
16 /* $ModDesc: Provides channel modes +a and +q */
18 #define PROTECT_VALUE 40000
19 #define FOUNDER_VALUE 50000
21 struct ChanProtectSettings
25 bool FirstInGetsFounder;
27 ChanProtectSettings() : booting(true) {}
30 static ChanProtectSettings settings;
32 /** Handles basic operation of +qa channel modes
34 class FounderProtectBase
37 const std::string type;
42 FounderProtectBase(char Mode, const std::string &mtype, int l, int e) :
43 type(mtype), mode(Mode), list(l), end(e)
47 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
49 User* x = ServerInstance->FindNick(parameter);
52 Membership* memb = channel->GetUser(x);
55 return std::make_pair(false, parameter);
59 if (memb->hasMode(mode))
61 return std::make_pair(true, x->nick);
65 return std::make_pair(false, parameter);
69 return std::make_pair(false, parameter);
72 void RemoveMode(Channel* channel, irc::modestacker* stack)
74 const UserMembList* cl = channel->GetUsers();
75 std::vector<std::string> mode_junk;
76 mode_junk.push_back(channel->name);
77 irc::modestacker modestack(false);
78 std::deque<std::string> stackresult;
80 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
82 if (i->second->hasMode(mode))
85 stack->Push(mode, i->first->nick);
87 modestack.Push(mode, i->first->nick);
94 while (modestack.GetStackedLine(stackresult))
96 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
97 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
98 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
102 void DisplayList(User* user, Channel* channel)
104 const UserMembList* cl = channel->GetUsers();
105 for (UserMembCIter i = cl->begin(); i != cl->end(); ++i)
107 if (i->second->hasMode(mode))
109 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
112 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
115 bool CanRemoveOthers(User* u1, Channel* c)
117 Membership* m1 = c->GetUser(u1);
118 return (settings.DeprivOthers && m1 && m1->hasMode(mode));
122 /** Abstraction of FounderProtectBase for channel mode +q
124 class ChanFounder : public ModeHandler, public FounderProtectBase
127 ChanFounder(Module* Creator)
128 : ModeHandler(Creator, "founder", 'q', PARAM_ALWAYS, MODETYPE_CHANNEL),
129 FounderProtectBase('q', "founder", 386, 387)
131 ModeHandler::list = true;
132 levelrequired = FOUNDER_VALUE;
133 m_paramtype = TR_NICK;
136 void setPrefix(int pfx)
141 unsigned int GetPrefixRank()
143 return FOUNDER_VALUE;
146 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
148 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
151 void RemoveMode(Channel* channel, irc::modestacker* stack)
153 FounderProtectBase::RemoveMode(channel, stack);
156 void RemoveMode(User* user, irc::modestacker* stack)
160 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
162 User* theuser = ServerInstance->FindNick(parameter);
164 if (source == theuser && !adding && settings.DeprivSelf)
165 return MOD_RES_ALLOW;
167 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
169 return MOD_RES_PASSTHRU;
173 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
178 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
180 User* theuser = ServerInstance->FindNick(parameter);
183 return MODEACTION_DENY;
184 return MODEACTION_ALLOW;
187 void DisplayList(User* user, Channel* channel)
189 FounderProtectBase::DisplayList(user,channel);
193 /** Abstraction of FounderProtectBase for channel mode +a
195 class ChanProtect : public ModeHandler, public FounderProtectBase
198 ChanProtect(Module* Creator)
199 : ModeHandler(Creator, "protected", 'a', PARAM_ALWAYS, MODETYPE_CHANNEL),
200 FounderProtectBase('a',"protected user", 388, 389)
202 ModeHandler::list = true;
203 levelrequired = PROTECT_VALUE;
204 m_paramtype = TR_NICK;
207 void setPrefix(int pfx)
213 unsigned int GetPrefixRank()
215 return PROTECT_VALUE;
218 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
220 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
223 void RemoveMode(Channel* channel, irc::modestacker* stack)
225 FounderProtectBase::RemoveMode(channel, stack);
228 void RemoveMode(User* user, irc::modestacker* stack)
232 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
234 User* theuser = ServerInstance->FindNick(parameter);
236 if (channel->GetPrefixValue(source) > PROTECT_VALUE)
237 return MOD_RES_ALLOW;
239 // removing own privs?
240 if (source == theuser && !adding && settings.DeprivSelf)
241 return MOD_RES_ALLOW;
243 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
245 return MOD_RES_PASSTHRU;
249 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
254 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
256 User* theuser = ServerInstance->FindNick(parameter);
259 return MODEACTION_DENY;
261 return MODEACTION_ALLOW;
264 void DisplayList(User* user, Channel* channel)
266 FounderProtectBase::DisplayList(user, channel);
271 class ModuleChanProtect : public Module
276 ModuleChanProtect() : cp(this), cf(this)
278 /* Load config stuff */
280 settings.booting = false;
282 if (!ServerInstance->Modes->AddMode(&cp) || !ServerInstance->Modes->AddMode(&cf))
284 throw ModuleException("Could not add new modes!");
287 Implementation eventlist[] = { I_OnUserPreJoin };
288 ServerInstance->Modules->Attach(eventlist, this, 1);
295 settings.FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
297 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
298 char QPrefix = qpre.empty() ? 0 : qpre[0];
300 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
301 char APrefix = apre.empty() ? 0 : apre[0];
303 if ((APrefix && QPrefix) && APrefix == QPrefix)
304 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
306 if (ServerInstance->Modes->FindPrefix(APrefix) && ServerInstance->Modes->FindPrefix(APrefix) != &cp)
307 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
309 if (ServerInstance->Modes->FindPrefix(QPrefix) && ServerInstance->Modes->FindPrefix(QPrefix) != &cf)
310 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
312 if (settings.booting)
314 cp.setPrefix(APrefix);
315 cf.setPrefix(QPrefix);
317 settings.DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
318 settings.DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
321 ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
323 // if the user is the first user into the channel, mark them as the founder, but only if
324 // the config option for it is set
326 if (settings.FirstInGetsFounder && !chan)
329 return MOD_RES_PASSTHRU;
334 return Version("Founder and Protect modes (+qa)", VF_COMMON | VF_VENDOR);
338 MODULE_INIT(ModuleChanProtect)