1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 void RemoveMode(Channel* channel, irc::modestacker* stack)
49 const UserMembList* cl = channel->GetUsers();
50 std::vector<std::string> mode_junk;
51 mode_junk.push_back(channel->name);
52 irc::modestacker modestack(false);
53 std::deque<std::string> stackresult;
55 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
57 if (i->second->hasMode(mode))
60 stack->Push(mode, i->first->nick);
62 modestack.Push(mode, i->first->nick);
69 while (modestack.GetStackedLine(stackresult))
71 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
72 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
73 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
77 void DisplayList(User* user, Channel* channel)
79 const UserMembList* cl = channel->GetUsers();
80 for (UserMembCIter i = cl->begin(); i != cl->end(); ++i)
82 if (i->second->hasMode(mode))
84 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
87 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
90 bool CanRemoveOthers(User* u1, Channel* c)
92 Membership* m1 = c->GetUser(u1);
93 return (settings.DeprivOthers && m1 && m1->hasMode(mode));
97 /** Abstraction of FounderProtectBase for channel mode +q
99 class ChanFounder : public ModeHandler, public FounderProtectBase
102 ChanFounder(Module* Creator)
103 : ModeHandler(Creator, "founder", 'q', PARAM_ALWAYS, MODETYPE_CHANNEL),
104 FounderProtectBase('q', "founder", 386, 387)
106 ModeHandler::list = true;
107 levelrequired = FOUNDER_VALUE;
108 m_paramtype = TR_NICK;
111 void setPrefix(int pfx)
116 unsigned int GetPrefixRank()
118 return FOUNDER_VALUE;
121 void RemoveMode(Channel* channel, irc::modestacker* stack)
123 FounderProtectBase::RemoveMode(channel, stack);
126 void RemoveMode(User* user, irc::modestacker* stack)
130 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
132 User* theuser = ServerInstance->FindNick(parameter);
134 if (source == theuser && !adding && settings.DeprivSelf)
135 return MOD_RES_ALLOW;
137 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
139 return MOD_RES_PASSTHRU;
143 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
148 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
150 return MODEACTION_ALLOW;
153 void DisplayList(User* user, Channel* channel)
155 FounderProtectBase::DisplayList(user,channel);
159 /** Abstraction of FounderProtectBase for channel mode +a
161 class ChanProtect : public ModeHandler, public FounderProtectBase
164 ChanProtect(Module* Creator)
165 : ModeHandler(Creator, "protected", 'a', PARAM_ALWAYS, MODETYPE_CHANNEL),
166 FounderProtectBase('a',"protected user", 388, 389)
168 ModeHandler::list = true;
169 levelrequired = PROTECT_VALUE;
170 m_paramtype = TR_NICK;
173 void setPrefix(int pfx)
179 unsigned int GetPrefixRank()
181 return PROTECT_VALUE;
184 void RemoveMode(Channel* channel, irc::modestacker* stack)
186 FounderProtectBase::RemoveMode(channel, stack);
189 void RemoveMode(User* user, irc::modestacker* stack)
193 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
195 User* theuser = ServerInstance->FindNick(parameter);
197 if (channel->GetPrefixValue(source) > PROTECT_VALUE)
198 return MOD_RES_ALLOW;
200 // removing own privs?
201 if (source == theuser && !adding && settings.DeprivSelf)
202 return MOD_RES_ALLOW;
204 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
206 return MOD_RES_PASSTHRU;
210 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
215 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
217 return MODEACTION_ALLOW;
220 void DisplayList(User* user, Channel* channel)
222 FounderProtectBase::DisplayList(user, channel);
227 class ModuleChanProtect : public Module
232 ModuleChanProtect() : cp(this), cf(this)
238 /* Load config stuff */
240 settings.booting = false;
242 ServerInstance->Modules->AddService(cf);
243 ServerInstance->Modules->AddService(cp);
245 Implementation eventlist[] = { I_OnUserPreJoin };
246 ServerInstance->Modules->Attach(eventlist, this, 1);
253 settings.FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
255 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
256 char QPrefix = qpre.empty() ? 0 : qpre[0];
258 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
259 char APrefix = apre.empty() ? 0 : apre[0];
261 if ((APrefix && QPrefix) && APrefix == QPrefix)
262 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
264 if (ServerInstance->Modes->FindPrefix(APrefix) && ServerInstance->Modes->FindPrefix(APrefix) != &cp)
265 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
267 if (ServerInstance->Modes->FindPrefix(QPrefix) && ServerInstance->Modes->FindPrefix(QPrefix) != &cf)
268 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
270 if (settings.booting)
272 cp.setPrefix(APrefix);
273 cf.setPrefix(QPrefix);
275 settings.DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
276 settings.DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
279 ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
281 // if the user is the first user into the channel, mark them as the founder, but only if
282 // the config option for it is set
284 if (settings.FirstInGetsFounder && !chan)
287 return MOD_RES_PASSTHRU;
292 return Version("Founder and Protect modes (+qa)", VF_VENDOR);
296 MODULE_INIT(ModuleChanProtect)