]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
9c15cf5b2aa0d2ac7fe90fd5ffc77ebc460b0a4f
[user/henk/code/inspircd.git] / src / modules / m_operprefix.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 /*
22  * Originally by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru
23  */
24
25 /* $ModDesc: Gives opers cmode +y which provides a staff prefix. */
26
27 #include "inspircd.h"
28
29 #define OPERPREFIX_VALUE 1000000
30
31 class OperPrefixMode : public ModeHandler
32 {
33         public:
34                 OperPrefixMode(Module* Creator) : ModeHandler(Creator, "operprefix", 'y', PARAM_ALWAYS, MODETYPE_CHANNEL)
35                 {
36                         std::string pfx = ServerInstance->Config->ConfValue("operprefix")->getString("prefix", "!");
37                         list = true;
38                         prefix = pfx.empty() ? '!' : pfx[0];
39                         levelrequired = OPERPREFIX_VALUE;
40                         m_paramtype = TR_NICK;
41                         prefixrank = OPERPREFIX_VALUE;
42                 }
43
44                 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
45                 {
46                         if (IS_SERVER(source) || ServerInstance->ULine(source->server))
47                                 return MODEACTION_ALLOW;
48                         else
49                         {
50                                 if (channel)
51                                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only servers are permitted to change channel mode '%c'", source->nick.c_str(), channel->name.c_str(), 'y');
52                                 return MODEACTION_DENY;
53                         }
54                 }
55
56                 bool NeedsOper() { return true; }
57 };
58
59 class ModuleOperPrefixMode;
60 class HideOperWatcher : public ModeWatcher
61 {
62         ModuleOperPrefixMode* parentmod;
63
64  public:
65         HideOperWatcher(ModuleOperPrefixMode* parent);
66         void AfterMode(User* source, User* dest, Channel* channel, const std::string &parameter, bool adding);
67 };
68
69 class ModuleOperPrefixMode : public Module
70 {
71         OperPrefixMode opm;
72         HideOperWatcher hideoperwatcher;
73
74  public:
75         ModuleOperPrefixMode()
76                 : opm(this), hideoperwatcher(this)
77         {
78         }
79
80         void init() CXX11_OVERRIDE
81         {
82                 ServerInstance->Modules->AddService(opm);
83
84                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnPostOper };
85                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
86
87                 /* To give clients a chance to learn about the new prefix we don't give +y to opers
88                  * right now. That means if the module was loaded after opers have joined channels
89                  * they need to rejoin them in order to get the oper prefix.
90                  */
91
92                 ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
93         }
94
95         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
96         {
97                 if ((user->IsOper()) && (user->IsModeSet('H')))
98                         privs.push_back('y');
99                 return MOD_RES_PASSTHRU;
100         }
101
102         void SetOperPrefix(User* user, bool add)
103         {
104                 std::vector<std::string> modechange;
105                 modechange.push_back("");
106                 modechange.push_back(add ? "+y" : "-y");
107                 modechange.push_back(user->nick);
108                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
109                 {
110                         modechange[0] = (*v)->name;
111                         ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
112                 }
113         }
114
115         void OnPostOper(User* user, const std::string& opername, const std::string& opertype) CXX11_OVERRIDE
116         {
117                 if (IS_LOCAL(user) && (!user->IsModeSet('H')))
118                         SetOperPrefix(user, true);
119         }
120
121
122         ~ModuleOperPrefixMode()
123         {
124                 ServerInstance->Modes->DelModeWatcher(&hideoperwatcher);
125         }
126
127         Version GetVersion() CXX11_OVERRIDE
128         {
129                 return Version("Gives opers cmode +y which provides a staff prefix.", VF_VENDOR);
130         }
131
132         void Prioritize()
133         {
134                 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
135                 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
136                 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
137         }
138 };
139
140 HideOperWatcher::HideOperWatcher(ModuleOperPrefixMode* parent)
141         : ModeWatcher(parent, "hideoper", MODETYPE_USER)
142         , parentmod(parent)
143 {
144 }
145
146 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding)
147 {
148         if (IS_LOCAL(dest))
149                 parentmod->SetOperPrefix(dest, !adding);
150 }
151
152 MODULE_INIT(ModuleOperPrefixMode)