2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Originally by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru
25 /* $ModDesc: Gives opers cmode +y which provides a staff prefix. */
29 #define OPERPREFIX_VALUE 1000000
31 class OperPrefixMode : public ModeHandler
34 OperPrefixMode(Module* Creator) : ModeHandler(Creator, "operprefix", 'y', PARAM_ALWAYS, MODETYPE_CHANNEL)
36 std::string pfx = ServerInstance->Config->ConfValue("operprefix")->getString("prefix", "!");
38 prefix = pfx.empty() ? '!' : pfx[0];
39 levelrequired = OPERPREFIX_VALUE;
40 m_paramtype = TR_NICK;
43 unsigned int GetPrefixRank()
45 return OPERPREFIX_VALUE;
48 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
50 if (IS_SERVER(source) || ServerInstance->ULine(source->server))
51 return MODEACTION_ALLOW;
55 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only servers are permitted to change channel mode '%c'", source->nick.c_str(), channel->name.c_str(), 'y');
56 return MODEACTION_DENY;
60 bool NeedsOper() { return true; }
62 void RemoveMode(Channel* chan, irc::modestacker* stack)
64 irc::modestacker modestack(false);
65 const UserMembList* users = chan->GetUsers();
66 for (UserMembCIter i = users->begin(); i != users->end(); ++i)
68 if (i->second->hasMode(mode))
71 stack->Push(this->GetModeChar(), i->first->nick);
73 modestack.Push(this->GetModeChar(), i->first->nick);
80 std::deque<std::string> stackresult;
81 std::vector<std::string> mode_junk;
82 mode_junk.push_back(chan->name);
83 while (modestack.GetStackedLine(stackresult))
85 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
86 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
87 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
91 void RemoveMode(User* user, irc::modestacker* stack)
96 class ModuleOperPrefixMode;
97 class HideOperWatcher : public ModeWatcher
99 ModuleOperPrefixMode* parentmod;
101 HideOperWatcher(ModuleOperPrefixMode* parent) : ModeWatcher((Module*) parent, 'H', MODETYPE_USER), parentmod(parent) {}
102 void AfterMode(User* source, User* dest, Channel* channel, const std::string ¶meter, bool adding, ModeType type);
105 class ModuleOperPrefixMode : public Module
110 HideOperWatcher hideoperwatcher;
112 ModuleOperPrefixMode()
113 : opm(this), mw_added(false), hideoperwatcher(this)
119 ServerInstance->Modules->AddService(opm);
121 Implementation eventlist[] = { I_OnUserPreJoin, I_OnPostOper, I_OnLoadModule, I_OnUnloadModule, I_OnPostJoin };
122 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
124 /* To give clients a chance to learn about the new prefix we don't give +y to opers
125 * right now. That means if the module was loaded after opers have joined channels
126 * they need to rejoin them in order to get the oper prefix.
129 if (ServerInstance->Modules->Find("m_hideoper.so"))
130 mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
133 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string& privs, const std::string& keygiven)
135 /* The user may have the +H umode on himself, but +H does not necessarily correspond
136 * to the +H of m_hideoper.
137 * However we only add the modewatcher when m_hideoper is loaded, so these
138 * conditions (mw_added and the user being +H) together mean the user is a hidden oper.
141 if (IS_OPER(user) && (!mw_added || !user->IsModeSet('H')))
142 privs.push_back('y');
143 return MOD_RES_PASSTHRU;
146 void OnPostJoin(Membership* memb)
148 if ((!IS_LOCAL(memb->user)) || (!IS_OPER(memb->user)) || (((mw_added) && (memb->user->IsModeSet('H')))))
151 if (memb->hasMode(opm.GetModeChar()))
154 // The user was force joined and OnUserPreJoin() did not run. Set the operprefix now.
155 std::vector<std::string> modechange;
156 modechange.push_back(memb->chan->name);
157 modechange.push_back("+y");
158 modechange.push_back(memb->user->nick);
159 ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
162 void SetOperPrefix(User* user, bool add)
164 std::vector<std::string> modechange;
165 modechange.push_back("");
166 modechange.push_back(add ? "+y" : "-y");
167 modechange.push_back(user->nick);
168 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
170 modechange[0] = (*v)->name;
171 ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
175 void OnPostOper(User* user, const std::string& opername, const std::string& opertype)
177 if (IS_LOCAL(user) && (!mw_added || !user->IsModeSet('H')))
178 SetOperPrefix(user, true);
181 void OnLoadModule(Module* mod)
183 if ((!mw_added) && (mod->ModuleSourceFile == "m_hideoper.so"))
184 mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
187 void OnUnloadModule(Module* mod)
189 if ((mw_added) && (mod->ModuleSourceFile == "m_hideoper.so") && (ServerInstance->Modes->DelModeWatcher(&hideoperwatcher)))
193 ~ModuleOperPrefixMode()
196 ServerInstance->Modes->DelModeWatcher(&hideoperwatcher);
201 return Version("Gives opers cmode +y which provides a staff prefix.", VF_VENDOR);
206 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
207 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
208 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
212 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding, ModeType type)
214 // If hideoper is being unset because the user is deopering, don't set +y
215 if (IS_LOCAL(dest) && IS_OPER(dest))
216 parentmod->SetOperPrefix(dest, !adding);
219 MODULE_INIT(ModuleOperPrefixMode)