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;
54 if (source && channel)
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; }
63 class ModuleOperPrefixMode;
64 class HideOperWatcher : public ModeWatcher
66 ModuleOperPrefixMode* parentmod;
68 HideOperWatcher(ModuleOperPrefixMode* parent) : ModeWatcher((Module*) parent, 'H', MODETYPE_USER), parentmod(parent) {}
69 void AfterMode(User* source, User* dest, Channel* channel, const std::string ¶meter, bool adding, ModeType type);
72 class ModuleOperPrefixMode : public Module
77 HideOperWatcher hideoperwatcher;
79 ModuleOperPrefixMode()
80 : opm(this), mw_added(false), hideoperwatcher(this)
86 ServerInstance->Modules->AddService(opm);
88 Implementation eventlist[] = { I_OnUserPreJoin, I_OnPostOper, I_OnLoadModule, I_OnUnloadModule };
89 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
91 /* To give clients a chance to learn about the new prefix we don't give +y to opers
92 * right now. That means if the module was loaded after opers have joined channels
93 * they need to rejoin them in order to get the oper prefix.
96 if (ServerInstance->Modules->Find("m_hideoper.so"))
97 mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
100 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string& privs, const std::string& keygiven)
102 /* The user may have the +H umode on himself, but +H does not necessarily correspond
103 * to the +H of m_hideoper.
104 * However we only add the modewatcher when m_hideoper is loaded, so these
105 * conditions (mw_added and the user being +H) together mean the user is a hidden oper.
108 if (IS_OPER(user) && (!mw_added || !user->IsModeSet('H')))
109 privs.push_back('y');
110 return MOD_RES_PASSTHRU;
113 void SetOperPrefix(User* user, bool add)
115 std::vector<std::string> modechange;
116 modechange.push_back("");
117 modechange.push_back(add ? "+y" : "-y");
118 modechange.push_back(user->nick);
119 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
121 modechange[0] = (*v)->name;
122 ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
126 void OnPostOper(User* user, const std::string& opername, const std::string& opertype)
128 if (IS_LOCAL(user) && (!mw_added || !user->IsModeSet('H')))
129 SetOperPrefix(user, true);
132 void OnLoadModule(Module* mod)
134 if ((!mw_added) && (mod->ModuleSourceFile == "m_hideoper.so"))
135 mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
138 void OnUnloadModule(Module* mod)
140 if ((mw_added) && (mod->ModuleSourceFile == "m_hideoper.so") && (ServerInstance->Modes->DelModeWatcher(&hideoperwatcher)))
144 ~ModuleOperPrefixMode()
147 ServerInstance->Modes->DelModeWatcher(&hideoperwatcher);
152 return Version("Gives opers cmode +y which provides a staff prefix.", VF_VENDOR);
157 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
158 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
159 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
163 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding, ModeType type)
166 parentmod->SetOperPrefix(dest, !adding);
169 MODULE_INIT(ModuleOperPrefixMode)