2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2009 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8 * Copyright (C) 2004-2008 Craig Edwards <craigedwards@brainbox.cc>
9 * Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
10 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /* $ModDesc: Provides channel modes +a and +q */
30 #define PROTECT_VALUE 40000
31 #define FOUNDER_VALUE 50000
33 struct ChanProtectSettings
37 bool FirstInGetsFounder;
39 ChanProtectSettings() : booting(true) {}
42 static ChanProtectSettings settings;
44 /** Handles basic operation of +qa channel modes
46 class FounderProtectBase
49 const std::string type;
54 FounderProtectBase(char Mode, const std::string &mtype, int l, int e) :
55 type(mtype), mode(Mode), list(l), end(e)
59 void RemoveMode(Channel* channel, irc::modestacker* stack)
61 const UserMembList* cl = channel->GetUsers();
62 std::vector<std::string> mode_junk;
63 mode_junk.push_back(channel->name);
64 irc::modestacker modestack(false);
65 std::deque<std::string> stackresult;
67 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
69 if (i->second->hasMode(mode))
72 stack->Push(mode, i->first->nick);
74 modestack.Push(mode, i->first->nick);
81 while (modestack.GetStackedLine(stackresult))
83 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
84 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
85 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
89 void DisplayList(User* user, Channel* channel)
91 const UserMembList* cl = channel->GetUsers();
92 for (UserMembCIter i = cl->begin(); i != cl->end(); ++i)
94 if (i->second->hasMode(mode))
96 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
99 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
102 bool CanRemoveOthers(User* u1, Channel* c)
104 Membership* m1 = c->GetUser(u1);
105 return (settings.DeprivOthers && m1 && m1->hasMode(mode));
109 /** Abstraction of FounderProtectBase for channel mode +q
111 class ChanFounder : public ModeHandler, public FounderProtectBase
114 ChanFounder(Module* Creator)
115 : ModeHandler(Creator, "founder", 'q', PARAM_ALWAYS, MODETYPE_CHANNEL),
116 FounderProtectBase('q', "founder", 386, 387)
118 ModeHandler::list = true;
119 levelrequired = FOUNDER_VALUE;
120 m_paramtype = TR_NICK;
123 void setPrefix(int pfx)
128 unsigned int GetPrefixRank()
130 return FOUNDER_VALUE;
133 void RemoveMode(Channel* channel, irc::modestacker* stack)
135 FounderProtectBase::RemoveMode(channel, stack);
138 void RemoveMode(User* user, irc::modestacker* stack)
142 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
144 User* theuser = ServerInstance->FindNick(parameter);
146 if (source == theuser && !adding && settings.DeprivSelf)
147 return MOD_RES_ALLOW;
149 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
151 return MOD_RES_PASSTHRU;
155 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
160 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
162 return MODEACTION_ALLOW;
165 void DisplayList(User* user, Channel* channel)
167 FounderProtectBase::DisplayList(user,channel);
171 /** Abstraction of FounderProtectBase for channel mode +a
173 class ChanProtect : public ModeHandler, public FounderProtectBase
176 ChanProtect(Module* Creator)
177 : ModeHandler(Creator, "admin", 'a', PARAM_ALWAYS, MODETYPE_CHANNEL),
178 FounderProtectBase('a',"protected user", 388, 389)
180 ModeHandler::list = true;
181 levelrequired = PROTECT_VALUE;
182 m_paramtype = TR_NICK;
185 void setPrefix(int pfx)
191 unsigned int GetPrefixRank()
193 return PROTECT_VALUE;
196 void RemoveMode(Channel* channel, irc::modestacker* stack)
198 FounderProtectBase::RemoveMode(channel, stack);
201 void RemoveMode(User* user, irc::modestacker* stack)
205 ModResult AccessCheck(User* source, Channel* channel, std::string ¶meter, bool adding)
207 User* theuser = ServerInstance->FindNick(parameter);
209 if (channel->GetPrefixValue(source) > PROTECT_VALUE)
210 return MOD_RES_ALLOW;
212 // removing own privs?
213 if (source == theuser && !adding && settings.DeprivSelf)
214 return MOD_RES_ALLOW;
216 if (!adding && FounderProtectBase::CanRemoveOthers(source, channel))
218 return MOD_RES_PASSTHRU;
222 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
227 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
229 return MODEACTION_ALLOW;
232 void DisplayList(User* user, Channel* channel)
234 FounderProtectBase::DisplayList(user, channel);
239 class ModuleChanProtect : public Module
244 ModuleChanProtect() : cp(this), cf(this)
250 /* Load config stuff */
252 settings.booting = false;
254 ServerInstance->Modules->AddService(cf);
255 ServerInstance->Modules->AddService(cp);
257 Implementation eventlist[] = { I_OnUserPreJoin };
258 ServerInstance->Modules->Attach(eventlist, this, 1);
265 settings.FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
267 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
268 char QPrefix = qpre.empty() ? 0 : qpre[0];
270 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
271 char APrefix = apre.empty() ? 0 : apre[0];
273 if ((APrefix && QPrefix) && APrefix == QPrefix)
274 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
276 if (settings.booting)
278 if (APrefix && ServerInstance->Modes->FindPrefix(APrefix) && ServerInstance->Modes->FindPrefix(APrefix) != &cp)
279 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
281 if (QPrefix && ServerInstance->Modes->FindPrefix(QPrefix) && ServerInstance->Modes->FindPrefix(QPrefix) != &cf)
282 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
284 cp.setPrefix(APrefix);
285 cf.setPrefix(QPrefix);
287 settings.DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
288 settings.DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
291 ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
293 // if the user is the first user into the channel, mark them as the founder, but only if
294 // the config option for it is set
296 if (settings.FirstInGetsFounder && !chan)
299 return MOD_RES_PASSTHRU;
304 return Version("Founder and Protect modes (+qa)", VF_VENDOR);
308 MODULE_INIT(ModuleChanProtect)