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